root/OpenSceneGraph/trunk/examples/osgsimulation/osgsimulation.cpp @ 3008

Revision 3008, 5.7 kB (checked in by robert, 10 years ago)

Added Vec2d, Vec3d and Vec4d classes, and remapped Vec2, Vec3 and Vec4 to
Vec2f, Vec3f an Vec4f respectively, with typedef's to the from Vec* to Vec*f.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osgProducer/Viewer>
2
3#include <osg/Group>
4#include <osg/Geode>
5#include <osg/ShapeDrawable>
6#include <osg/Texture2D>
7#include <osg/PositionAttitudeTransform>
8
9#include <osgDB/ReadFile>
10
11#include <osgParticle/ExplosionEffect>
12#include <osgParticle/SmokeEffect>
13#include <osgParticle/FireEffect>
14#include <osgParticle/ParticleSystemUpdater>
15
16// for the grid data..
17#include "../osghangglide/terrain_coords.h"
18
19osg::Vec3 computeTerrainIntersection(osg::Node* subgraph,float x,float y)
20{
21    osgUtil::IntersectVisitor iv;
22    osg::ref_ptr<osg::LineSegment> segDown = new osg::LineSegment;
23
24    const osg::BoundingSphere& bs = subgraph->getBound();
25    float zMax = bs.center().z()+bs.radius();
26    float zMin = bs.center().z()-bs.radius();
27   
28    segDown->set(osg::Vec3(x,y,zMin),osg::Vec3(x,y,zMax));
29    iv.addLineSegment(segDown.get());
30
31    subgraph->accept(iv);
32
33    if (iv.hits())
34    {
35        osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(segDown.get());
36        if (!hitList.empty())
37        {
38            osg::Vec3 ip = hitList.front().getWorldIntersectPoint();
39            return  ip;
40        }
41    }
42
43    return osg::Vec3(x,y,0.0f);
44}
45
46
47//////////////////////////////////////////////////////////////////////////////
48// MAIN SCENE GRAPH BUILDING FUNCTION
49//////////////////////////////////////////////////////////////////////////////
50
51void build_world(osg::Group *root)
52{
53
54    osg::Geode* terrainGeode = new osg::Geode;
55    {
56        osg::StateSet* stateset = new osg::StateSet();
57        osg::Image* image = osgDB::readImageFile("Images/lz.rgb");
58        if (image)
59        {
60            osg::Texture2D* texture = new osg::Texture2D;
61            texture->setImage(image);
62            stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
63        }
64
65        terrainGeode->setStateSet( stateset );
66
67        float size = 1000; // 10km;
68        float scale = size/39.0f; // 10km;
69        float z_scale = scale*3.0f;
70
71        osg::HeightField* grid = new osg::HeightField;
72        grid->allocateGrid(38,39);
73        grid->setXInterval(scale);
74        grid->setYInterval(scale);
75
76        for(unsigned int r=0;r<39;++r)
77        {
78            for(unsigned int c=0;c<38;++c)
79            {
80                grid->setHeight(c,r,z_scale*vertex[r+c*39][2]);
81            }
82        }
83        terrainGeode->addDrawable(new osg::ShapeDrawable(grid));
84       
85        root->addChild(terrainGeode);
86    }   
87
88
89   
90    osg::PositionAttitudeTransform* positionEffects = new osg::PositionAttitudeTransform;
91    positionEffects->setPosition(computeTerrainIntersection(terrainGeode,100.0f,100.0f));
92    root->addChild(positionEffects);
93
94    osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect;
95    osgParticle::SmokeEffect* smoke = new osgParticle::SmokeEffect;
96    osgParticle::FireEffect* fire = new osgParticle::FireEffect;
97
98    osg::Geode* geode = new osg::Geode;
99    geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),10.0f)));
100    positionEffects->addChild(geode);
101
102    positionEffects->addChild(explosion);
103    positionEffects->addChild(smoke);
104    positionEffects->addChild(fire);
105
106    osgParticle::ParticleSystemUpdater *psu = new osgParticle::ParticleSystemUpdater;
107
108    psu->addParticleSystem(explosion->getParticleSystem());
109    psu->addParticleSystem(smoke->getParticleSystem());
110    psu->addParticleSystem(fire->getParticleSystem());
111
112    // add the updater node to the scene graph
113    root->addChild(psu);
114
115}
116
117
118//////////////////////////////////////////////////////////////////////////////
119// main()
120//////////////////////////////////////////////////////////////////////////////
121
122
123int main(int argc, char **argv)
124{
125    // use an ArgumentParser object to manage the program arguments.
126    osg::ArgumentParser arguments(&argc,argv);
127   
128    // set up the usage document, in case we need to print out how to use this program.
129    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of particle systems.");
130    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] image_file_left_eye image_file_right_eye");
131    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
132   
133
134    // construct the viewer.
135    osgProducer::Viewer viewer(arguments);
136
137    // set up the value with sensible default event handlers.
138    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
139
140    // get details on keyboard and mouse bindings used by the viewer.
141    viewer.getUsage(*arguments.getApplicationUsage());
142
143    // if user request help write it out to cout.
144    if (arguments.read("-h") || arguments.read("--help"))
145    {
146        arguments.getApplicationUsage()->write(std::cout);
147        return 1;
148    }
149
150    // any option left unread are converted into errors to write out later.
151    arguments.reportRemainingOptionsAsUnrecognized();
152
153    // report any errors if they have occured when parsing the program aguments.
154    if (arguments.errors())
155    {
156        arguments.writeErrorMessages(std::cout);
157        return 1;
158    }
159   
160    osg::Group *root = new osg::Group;
161    build_world(root);
162   
163    // add a viewport to the viewer and attach the scene graph.
164    viewer.setSceneData(root);
165       
166    // create the windows and run the threads.
167    viewer.realize();
168
169    while( !viewer.done() )
170    {
171        // wait for all cull and draw threads to complete.
172        viewer.sync();
173
174        // update the scene by traversing it with the the update visitor which will
175        // call all node update callbacks and animations.
176        viewer.update();
177         
178        // fire off the cull and draw traversals of the scene.
179        viewer.frame();
180       
181    }
182   
183    // wait for all cull and draw threads to complete before exit.
184    viewer.sync();
185
186    return 0;
187}
Note: See TracBrowser for help on using the browser.