root/OpenSceneGraph/trunk/examples/osggameoflife/osggameoflife.cpp @ 13797

Revision 13574, 5.4 kB (checked in by robert, 6 hours ago)

From Mattias Helsing, "Seems I was only half right given what you asked for. CMP0017 only
says that modules that are found and ran from cmake modules dir should
prefer cmake-provided modules. find_package() and include() still look
in CMAKE_MODULE_PATH first.

After some investigating I've come up with a proposal examplified in
the attached FindGDAL.cmake script. It simply calls the cmake provided
FindGDAL.cmake if it exists and returns if it succeeds in finding GDAL
using that, otherwise continue with our local cmake code.
Pro: Wont clutter our root CMakeLists.txt
Con: If we begin to write more advanced Findxxx modules (using
COMPONENTS, REQUIRED etc.) we may have to revise this scheme.
"

  • Property svn:eol-style set to native
Line 
1/* OpenSceneGraph example, osggameoflife.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osg/Vec3>
20#include <osg/Vec4>
21#include <osg/Geometry>
22#include <osg/Geode>
23#include <osg/TextureRectangle>
24
25#include <osgDB/FileUtils>
26#include <osgDB/ReadFile>
27
28#include <osgViewer/Viewer>
29#include <osgViewer/ViewerEventHandlers>
30
31#include <osgGA/TrackballManipulator>
32
33#include <iostream>
34
35#include "GameOfLifePass.h"
36
37GameOfLifePass *golpass;
38osg::ref_ptr<osg::StateSet> geomss; // stateset where we can attach textures
39
40osg::Node* createScene(osg::Image *start_im)
41{
42    int width = start_im->s();
43    int height = start_im->t();
44
45    osg::Group* topnode = new osg::Group;
46
47    // create quad to display image on
48    osg::ref_ptr<osg::Geode> geode = new osg::Geode();
49
50    // each geom will contain a quad
51    osg::ref_ptr<osg::DrawArrays> da = new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4);
52
53    osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
54    colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
55
56    osg::ref_ptr<osg::Vec2Array> tcoords = new osg::Vec2Array; // texture coords
57    tcoords->push_back(osg::Vec2(0, 0));
58    tcoords->push_back(osg::Vec2(width, 0));
59    tcoords->push_back(osg::Vec2(width, height));
60    tcoords->push_back(osg::Vec2(0, height));
61
62    osg::ref_ptr<osg::Vec3Array> vcoords = new osg::Vec3Array; // vertex coords
63    osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
64
65    // initial viewer camera looks along y
66    vcoords->push_back(osg::Vec3d(0, 0, 0));
67    vcoords->push_back(osg::Vec3d(width, 0, 0));
68    vcoords->push_back(osg::Vec3d(width, 0, height));
69    vcoords->push_back(osg::Vec3d(0, 0, height));
70
71    geom->setVertexArray(vcoords.get());
72    geom->setTexCoordArray(0,tcoords.get());
73    geom->addPrimitiveSet(da.get());
74    geom->setColorArray(colors.get(), osg::Array::BIND_OVERALL);
75    geomss = geom->getOrCreateStateSet();
76    geomss->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
77
78    geode->addDrawable(geom.get());
79
80    topnode->addChild(geode.get());
81
82    // create the ping pong processing passes
83    golpass = new GameOfLifePass(start_im);
84    topnode->addChild(golpass->getRoot().get());
85
86    // attach the output of the processing to the geom
87    geomss->setTextureAttributeAndModes(0,
88                                        golpass->getOutputTexture().get(),
89                                        osg::StateAttribute::ON);
90    return topnode;
91}
92
93int main(int argc, char *argv[])
94{
95    // use an ArgumentParser object to manage the program arguments.
96    osg::ArgumentParser arguments(&argc,argv);
97
98    // set up the usage document, in case we need to print out how to use this program.
99    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates ping pong rendering with FBOs and mutliple rendering branches. It uses Conway's Game of Life to illustrate the concept.");
100    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] --startim start_image");
101    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
102    arguments.getApplicationUsage()->addCommandLineOption("--startim","The initial image to seed the game of life with.");
103
104    // if user request help write it out to cout.
105    if (arguments.read("-h") || arguments.read("--help"))
106    {
107        arguments.getApplicationUsage()->write(std::cout);
108        return 1;
109    }
110
111    std::string startName("");
112    while(arguments.read("--startim", startName)) {}
113
114    if (startName == "") {
115        arguments.getApplicationUsage()->write(std::cout);
116        return 1;
117    }
118
119    // load the image
120    osg::ref_ptr<osg::Image> startIm = osgDB::readImageFile(startName);
121
122    if (!startIm) {
123        std::cout << "Could not load start image.\n";
124        return(1);
125    }
126
127    osg::Node* scene = createScene(startIm.get());
128
129    // construct the viewer.
130    osgViewer::Viewer viewer;
131    viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
132
133    // add the stats handler
134    viewer.addEventHandler(new osgViewer::StatsHandler);
135
136    viewer.setSceneData(scene);
137
138    viewer.realize();
139    viewer.setCameraManipulator( new osgGA::TrackballManipulator );
140
141    while(!viewer.done())
142    {
143        viewer.frame();
144        // flip the textures after we've completed a frame
145        golpass->flip();
146        // attach the proper output to view
147        geomss->setTextureAttributeAndModes(0,
148                                            golpass->getOutputTexture().get(),
149                                            osg::StateAttribute::ON);
150    }
151
152    return 0;
153}
Note: See TracBrowser for help on using the browser.