root/OpenSceneGraph/trunk/examples/osgspheresegment/osgspheresegment.cpp @ 3934

Revision 3934, 10.3 kB (checked in by robert, 9 years ago)

Updated sphere segment example to use original osgsimulation source, adding
in terrain, moving models and particle effects.

  • 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#include <osg/MatrixTransform>
9
10#include <osgDB/ReadFile>
11
12#include <osgText/Text>
13
14#include <osgSim/SphereSegment>
15
16#include <osgParticle/ExplosionEffect>
17#include <osgParticle/SmokeEffect>
18#include <osgParticle/FireEffect>
19#include <osgParticle/ParticleSystemUpdater>
20
21// for the grid data..
22#include "../osghangglide/terrain_coords.h"
23
24osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime)
25{
26    // set up the animation path
27    osg::AnimationPath* animationPath = new osg::AnimationPath;
28    animationPath->setLoopMode(osg::AnimationPath::LOOP);
29   
30    int numSamples = 40;
31    float yaw = 0.0f;
32    float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f);
33    float roll = osg::inDegrees(30.0f);
34   
35    double time=0.0f;
36    double time_delta = looptime/(double)numSamples;
37    for(int i=0;i<numSamples;++i)
38    {
39        osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f));
40        osg::Quat rotation(osg::Quat(roll,osg::Vec3(0.0,1.0,0.0))*osg::Quat(-(yaw+osg::inDegrees(90.0f)),osg::Vec3(0.0,0.0,1.0)));
41       
42        animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation));
43
44        yaw += yaw_delta;
45        time += time_delta;
46
47    }
48    return animationPath;   
49}
50
51osg::Node* createMovingModel(const osg::Vec3& center, float radius)
52{
53    float animationLength = 10.0f;
54
55    osg::AnimationPath* animationPath = createAnimationPath(center,radius,animationLength);
56
57    osg::Group* model = new osg::Group;
58
59    osg::Node* glider = osgDB::readNodeFile("glider.osg");
60    if (glider)
61    {
62        const osg::BoundingSphere& bs = glider->getBound();
63
64        float size = radius/bs.radius()*0.3f;
65        osg::MatrixTransform* positioned = new osg::MatrixTransform;
66        positioned->setDataVariance(osg::Object::STATIC);
67        positioned->setMatrix(osg::Matrix::translate(-bs.center())*
68                                     osg::Matrix::scale(size,size,size)*
69                                     osg::Matrix::rotate(osg::inDegrees(-90.0f),0.0f,0.0f,1.0f));
70   
71        positioned->addChild(glider);
72   
73        osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform;   
74        xform->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
75        xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,1.0));
76        xform->addChild(positioned);
77
78        model->addChild(xform);
79    }
80 
81    osg::Node* cessna = osgDB::readNodeFile("cessna.osg");
82    if (cessna)
83    {
84        const osg::BoundingSphere& bs = cessna->getBound();
85
86        osgText::Text* text = new osgText::Text;
87        float size = radius/bs.radius()*0.3f;
88
89        text->setPosition(bs.center());
90        text->setText("Cessna");
91        text->setAlignment(osgText::Text::CENTER_CENTER);
92        text->setAxisAlignment(osgText::Text::SCREEN);
93        text->setCharacterSize(40.0f);
94        text->setCharacterSizeMode(osgText::Text::SCREEN_COORDS);
95       
96        osg::Geode* geode = new osg::Geode;
97        geode->addDrawable(text);
98   
99        osg::LOD* lod = new osg::LOD;
100        lod->setRangeMode(osg::LOD::PIXEL_SIZE_ON_SCREEN);
101        lod->addChild(geode,0.0f,100.0f);
102        lod->addChild(cessna,100.0f,10000.0f);
103
104
105        osg::MatrixTransform* positioned = new osg::MatrixTransform;
106        positioned->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
107        positioned->setDataVariance(osg::Object::STATIC);
108        positioned->setMatrix(osg::Matrix::translate(-bs.center())*
109                                     osg::Matrix::scale(size,size,size)*
110                                     osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,1.0f));
111   
112        //positioned->addChild(cessna);
113        positioned->addChild(lod);
114   
115        osg::MatrixTransform* xform = new osg::MatrixTransform;
116        xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,2.0));
117        xform->addChild(positioned);
118
119        model->addChild(xform);
120    }
121   
122    return model;
123}
124
125
126osg::Vec3 computeTerrainIntersection(osg::Node* subgraph,float x,float y)
127{
128    osgUtil::IntersectVisitor iv;
129    osg::ref_ptr<osg::LineSegment> segDown = new osg::LineSegment;
130
131    const osg::BoundingSphere& bs = subgraph->getBound();
132    float zMax = bs.center().z()+bs.radius();
133    float zMin = bs.center().z()-bs.radius();
134   
135    segDown->set(osg::Vec3(x,y,zMin),osg::Vec3(x,y,zMax));
136    iv.addLineSegment(segDown.get());
137
138    subgraph->accept(iv);
139
140    if (iv.hits())
141    {
142        osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(segDown.get());
143        if (!hitList.empty())
144        {
145            osg::Vec3 ip = hitList.front().getWorldIntersectPoint();
146            return  ip;
147        }
148    }
149
150    return osg::Vec3(x,y,0.0f);
151}
152
153
154//////////////////////////////////////////////////////////////////////////////
155// MAIN SCENE GRAPH BUILDING FUNCTION
156//////////////////////////////////////////////////////////////////////////////
157
158void build_world(osg::Group *root)
159{
160
161    osg::Geode* terrainGeode = new osg::Geode;
162    // create terrain
163    {
164        osg::StateSet* stateset = new osg::StateSet();
165        osg::Image* image = osgDB::readImageFile("Images/lz.rgb");
166        if (image)
167        {
168            osg::Texture2D* texture = new osg::Texture2D;
169            texture->setImage(image);
170            stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
171        }
172
173        terrainGeode->setStateSet( stateset );
174
175        float size = 1000; // 10km;
176        float scale = size/39.0f; // 10km;
177        float z_scale = scale*3.0f;
178
179        osg::HeightField* grid = new osg::HeightField;
180        grid->allocateGrid(38,39);
181        grid->setXInterval(scale);
182        grid->setYInterval(scale);
183
184        for(unsigned int r=0;r<39;++r)
185        {
186            for(unsigned int c=0;c<38;++c)
187            {
188                grid->setHeight(c,r,z_scale*vertex[r+c*39][2]);
189            }
190        }
191        terrainGeode->addDrawable(new osg::ShapeDrawable(grid));
192       
193        root->addChild(terrainGeode);
194    }   
195
196    // create sphere segment
197    {
198        osgSim::SphereSegment* ss = new osgSim::SphereSegment(
199                        computeTerrainIntersection(terrainGeode,550.0f,780.0f), // center
200                        500.0f, // radius
201                        osg::DegreesToRadians(135.0f),
202                        osg::DegreesToRadians(245.0f),
203                        osg::DegreesToRadians(-10.0f),
204                        osg::DegreesToRadians(30.0f),
205                        60);
206        ss->setAllColors(osg::Vec4(1.0f,1.0f,1.0f,0.5f));
207        ss->setSideColor(osg::Vec4(0.0f,1.0f,1.0f,0.1f));
208
209        root->addChild(ss);       
210    }
211
212
213    // create particle effects
214    {   
215        osg::PositionAttitudeTransform* positionEffects = new osg::PositionAttitudeTransform;
216        positionEffects->setPosition(computeTerrainIntersection(terrainGeode,100.0f,100.0f));
217        root->addChild(positionEffects);
218
219        osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect;
220        osgParticle::SmokeEffect* smoke = new osgParticle::SmokeEffect;
221        osgParticle::FireEffect* fire = new osgParticle::FireEffect;
222
223        osg::Geode* geode = new osg::Geode;
224        geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),10.0f)));
225        positionEffects->addChild(geode);
226
227        positionEffects->addChild(explosion);
228        positionEffects->addChild(smoke);
229        positionEffects->addChild(fire);
230
231        osgParticle::ParticleSystemUpdater *psu = new osgParticle::ParticleSystemUpdater;
232
233        psu->addParticleSystem(explosion->getParticleSystem());
234        psu->addParticleSystem(smoke->getParticleSystem());
235        psu->addParticleSystem(fire->getParticleSystem());
236
237        // add the updater node to the scene graph
238        root->addChild(psu);
239    }
240   
241   
242    // create the moving models.
243    {
244        root->addChild(createMovingModel(osg::Vec3(500.0f,500.0f,500.0f),100.0f));
245    }
246}
247
248
249//////////////////////////////////////////////////////////////////////////////
250// main()
251//////////////////////////////////////////////////////////////////////////////
252
253
254int main(int argc, char **argv)
255{
256    // use an ArgumentParser object to manage the program arguments.
257    osg::ArgumentParser arguments(&argc,argv);
258   
259    // set up the usage document, in case we need to print out how to use this program.
260    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of particle systems.");
261    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] image_file_left_eye image_file_right_eye");
262    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
263   
264
265    // construct the viewer.
266    osgProducer::Viewer viewer(arguments);
267
268    // set up the value with sensible default event handlers.
269    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
270
271    // get details on keyboard and mouse bindings used by the viewer.
272    viewer.getUsage(*arguments.getApplicationUsage());
273
274    // if user request help write it out to cout.
275    if (arguments.read("-h") || arguments.read("--help"))
276    {
277        arguments.getApplicationUsage()->write(std::cout);
278        return 1;
279    }
280
281    // any option left unread are converted into errors to write out later.
282    arguments.reportRemainingOptionsAsUnrecognized();
283
284    // report any errors if they have occured when parsing the program aguments.
285    if (arguments.errors())
286    {
287        arguments.writeErrorMessages(std::cout);
288        return 1;
289    }
290   
291    osg::Group *root = new osg::Group;
292    build_world(root);
293   
294    // add a viewport to the viewer and attach the scene graph.
295    viewer.setSceneData(root);
296       
297    // create the windows and run the threads.
298    viewer.realize();
299
300    while( !viewer.done() )
301    {
302        // wait for all cull and draw threads to complete.
303        viewer.sync();
304
305        // update the scene by traversing it with the the update visitor which will
306        // call all node update callbacks and animations.
307        viewer.update();
308         
309        // fire off the cull and draw traversals of the scene.
310        viewer.frame();
311       
312    }
313   
314    // wait for all cull and draw threads to complete before exit.
315    viewer.sync();
316
317    return 0;
318}
Note: See TracBrowser for help on using the browser.