| [9026] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 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 | |
|---|
| 37 | GameOfLifePass *golpass; |
|---|
| 38 | osg::ref_ptr<osg::StateSet> geomss; |
|---|
| 39 | |
|---|
| 40 | osg::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 | |
|---|
| 48 | osg::ref_ptr<osg::Geode> geode = new osg::Geode(); |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 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; |
|---|
| 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; |
|---|
| 63 | osg::ref_ptr<osg::Geometry> geom = new osg::Geometry; |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 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 | |
|---|
| 84 | golpass = new GameOfLifePass(start_im); |
|---|
| 85 | topnode->addChild(golpass->getRoot().get()); |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | geomss->setTextureAttributeAndModes(0, |
|---|
| 89 | golpass->getOutputTexture().get(), |
|---|
| 90 | osg::StateAttribute::ON); |
|---|
| 91 | return topnode; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | int main(int argc, char *argv[]) |
|---|
| 95 | { |
|---|
| 96 | |
|---|
| 97 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 131 | osgViewer::Viewer viewer; |
|---|
| 132 | viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 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 | |
|---|
| 146 | golpass->flip(); |
|---|
| 147 | |
|---|
| 148 | geomss->setTextureAttributeAndModes(0, |
|---|
| 149 | golpass->getOutputTexture().get(), |
|---|
| 150 | osg::StateAttribute::ON); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | return 0; |
|---|
| 154 | } |
|---|