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

Revision 4506, 11.2 kB (checked in by robert, 9 years ago)

Added osgSim::OverlayNode? to osganimate and osgspheresegment examples, and
added support for setting texture size hint and coping with scene graphs
with CoordinateSystemNode? in them.

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