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

Revision 4508, 12.8 kB (checked in by robert, 9 years ago)

Added a grid of lines into the OverlaySubgraph? to demonstate use the
OverlayNode? for applying general drawing onto terrain.

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