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

Revision 12292, 5.5 kB (checked in by robert, 4 years ago)

Ran svn propset -R svn:eol-style native . on the OpenSceneGraph

  • 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());
75    geom->setColorBinding(osg::Geometry::BIND_OVERALL);
76    geomss = geom->getOrCreateStateSet();
77    geomss->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
78
79    geode->addDrawable(geom.get());
80 
81    topnode->addChild(geode.get());
82   
83    // create the ping pong processing passes
84    golpass = new GameOfLifePass(start_im);
85    topnode->addChild(golpass->getRoot().get());
86
87    // attach the output of the processing to the geom
88    geomss->setTextureAttributeAndModes(0,
89                                        golpass->getOutputTexture().get(),
90                                        osg::StateAttribute::ON);
91    return topnode;
92}
93
94int main(int argc, char *argv[])
95{
96    // use an ArgumentParser object to manage the program arguments.
97    osg::ArgumentParser arguments(&argc,argv);
98
99    // set up the usage document, in case we need to print out how to use this program.
100    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.");
101    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] --startim start_image");
102    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
103    arguments.getApplicationUsage()->addCommandLineOption("--startim","The initial image to seed the game of life with.");
104
105    // if user request help write it out to cout.
106    if (arguments.read("-h") || arguments.read("--help"))
107    {
108        arguments.getApplicationUsage()->write(std::cout);
109        return 1;
110    }
111
112    std::string startName("");
113    while(arguments.read("--startim", startName)) {}
114
115    if (startName == "") {
116        arguments.getApplicationUsage()->write(std::cout);
117        return 1;
118    }
119
120    // load the image
121    osg::ref_ptr<osg::Image> startIm = osgDB::readImageFile(startName);
122   
123    if (!startIm) {
124        std::cout << "Could not load start image.\n";
125        return(1);
126    }
127
128    osg::Node* scene = createScene(startIm.get());
129   
130    // construct the viewer.
131    osgViewer::Viewer viewer;
132    viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
133
134    // add the stats handler
135    viewer.addEventHandler(new osgViewer::StatsHandler);
136
137    viewer.setSceneData(scene);
138
139    viewer.realize();
140    viewer.setCameraManipulator( new osgGA::TrackballManipulator );
141
142    while(!viewer.done())
143    {
144        viewer.frame();
145        // flip the textures after we've completed a frame
146        golpass->flip();
147        // attach the proper output to view
148        geomss->setTextureAttributeAndModes(0,
149                                            golpass->getOutputTexture().get(),
150                                            osg::StateAttribute::ON);
151    }
152
153    return 0;
154}
Note: See TracBrowser for help on using the browser.