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

Revision 3039, 6.5 kB (checked in by robert, 11 years ago)

Added sphere segment to osgsimulator.

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