root/OpenSceneGraph/trunk/examples/osganimate/osganimate.cpp @ 4994

Revision 4994, 8.9 kB (checked in by robert, 8 years ago)

From Ulrich Hertlein, spelling corrections and a few Doxgen comments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/Notify>
2#include <osg/MatrixTransform>
3#include <osg/PositionAttitudeTransform>
4#include <osg/Geometry>
5#include <osg/Geode>
6
7#include <osgUtil/Optimizer>
8
9#include <osgDB/Registry>
10#include <osgDB/ReadFile>
11
12#include <osgGA/TrackballManipulator>
13#include <osgGA/FlightManipulator>
14#include <osgGA/DriveManipulator>
15
16#include <osgSim/OverlayNode>
17
18#include <osgProducer/Viewer>
19
20
21osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime)
22{
23    // set up the animation path
24    osg::AnimationPath* animationPath = new osg::AnimationPath;
25    animationPath->setLoopMode(osg::AnimationPath::LOOP);
26   
27    int numSamples = 40;
28    float yaw = 0.0f;
29    float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f);
30    float roll = osg::inDegrees(30.0f);
31   
32    double time=0.0f;
33    double time_delta = looptime/(double)numSamples;
34    for(int i=0;i<numSamples;++i)
35    {
36        osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f));
37        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)));
38       
39        animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation));
40
41        yaw += yaw_delta;
42        time += time_delta;
43
44    }
45    return animationPath;   
46}
47
48osg::Node* createBase(const osg::Vec3& center,float radius)
49{
50
51   
52
53    int numTilesX = 10;
54    int numTilesY = 10;
55   
56    float width = 2*radius;
57    float height = 2*radius;
58   
59    osg::Vec3 v000(center - osg::Vec3(width*0.5f,height*0.5f,0.0f));
60    osg::Vec3 dx(osg::Vec3(width/((float)numTilesX),0.0,0.0f));
61    osg::Vec3 dy(osg::Vec3(0.0f,height/((float)numTilesY),0.0f));
62   
63    // fill in vertices for grid, note numTilesX+1 * numTilesY+1...
64    osg::Vec3Array* coords = new osg::Vec3Array;
65    int iy;
66    for(iy=0;iy<=numTilesY;++iy)
67    {
68        for(int ix=0;ix<=numTilesX;++ix)
69        {
70            coords->push_back(v000+dx*(float)ix+dy*(float)iy);
71        }
72    }
73   
74    //Just two colours - black and white.
75    osg::Vec4Array* colors = new osg::Vec4Array;
76    colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); // white
77    colors->push_back(osg::Vec4(0.0f,0.0f,0.0f,1.0f)); // black
78    int numColors=colors->size();
79   
80   
81    int numIndicesPerRow=numTilesX+1;
82    osg::UByteArray* coordIndices = new osg::UByteArray; // assumes we are using less than 256 points...
83    osg::UByteArray* colorIndices = new osg::UByteArray;
84    for(iy=0;iy<numTilesY;++iy)
85    {
86        for(int ix=0;ix<numTilesX;++ix)
87        {
88            // four vertices per quad.
89            coordIndices->push_back(ix    +(iy+1)*numIndicesPerRow);
90            coordIndices->push_back(ix    +iy*numIndicesPerRow);
91            coordIndices->push_back((ix+1)+iy*numIndicesPerRow);
92            coordIndices->push_back((ix+1)+(iy+1)*numIndicesPerRow);
93           
94            // one color per quad
95            colorIndices->push_back((ix+iy)%numColors);
96        }
97    }
98   
99
100    // set up a single normal
101    osg::Vec3Array* normals = new osg::Vec3Array;
102    normals->push_back(osg::Vec3(0.0f,0.0f,1.0f));
103   
104
105    osg::Geometry* geom = new osg::Geometry;
106    geom->setVertexArray(coords);
107    geom->setVertexIndices(coordIndices);
108   
109    geom->setColorArray(colors);
110    geom->setColorIndices(colorIndices);
111    geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE);
112   
113    geom->setNormalArray(normals);
114    geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
115   
116    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,coordIndices->size()));
117   
118    osg::Geode* geode = new osg::Geode;
119    geode->addDrawable(geom);
120   
121    return geode;
122}
123
124osg::Node* createMovingModel(const osg::Vec3& center, float radius)
125{
126    float animationLength = 10.0f;
127
128    osg::AnimationPath* animationPath = createAnimationPath(center,radius,animationLength);
129
130    osg::Group* model = new osg::Group;
131
132    osg::Node* glider = osgDB::readNodeFile("glider.osg");
133    if (glider)
134    {
135        const osg::BoundingSphere& bs = glider->getBound();
136
137        float size = radius/bs.radius()*0.3f;
138        osg::MatrixTransform* positioned = new osg::MatrixTransform;
139        positioned->setDataVariance(osg::Object::STATIC);
140        positioned->setMatrix(osg::Matrix::translate(-bs.center())*
141                                     osg::Matrix::scale(size,size,size)*
142                                     osg::Matrix::rotate(osg::inDegrees(-90.0f),0.0f,0.0f,1.0f));
143   
144        positioned->addChild(glider);
145   
146        osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform;   
147        xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,1.0));
148        xform->addChild(positioned);
149
150        model->addChild(xform);
151    }
152 
153    osg::Node* cessna = osgDB::readNodeFile("cessna.osg");
154    if (cessna)
155    {
156        const osg::BoundingSphere& bs = cessna->getBound();
157
158        float size = radius/bs.radius()*0.3f;
159        osg::MatrixTransform* positioned = new osg::MatrixTransform;
160        positioned->setDataVariance(osg::Object::STATIC);
161        positioned->setMatrix(osg::Matrix::translate(-bs.center())*
162                                     osg::Matrix::scale(size,size,size)*
163                                     osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,1.0f));
164   
165        positioned->addChild(cessna);
166   
167        osg::MatrixTransform* xform = new osg::MatrixTransform;
168        xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,2.0));
169        xform->addChild(positioned);
170
171        model->addChild(xform);
172    }
173   
174    return model;
175}
176
177osg::Node* createModel(bool overlay)
178{
179    osg::Vec3 center(0.0f,0.0f,0.0f);
180    float radius = 100.0f;
181
182    osg::Group* root = new osg::Group;
183
184    osg::Node* baseModel = createBase(center-osg::Vec3(0.0f,0.0f,radius*0.5),radius);
185    osg::Node* movingModel = createMovingModel(center,radius*0.8f);
186
187    if (overlay)
188    {
189        osgSim::OverlayNode* overlayNode = new osgSim::OverlayNode;
190        overlayNode->setContinuousUpdate(true);
191        overlayNode->setOverlaySubgraph(movingModel);
192        overlayNode->addChild(baseModel);
193        root->addChild(overlayNode);
194    }
195    else
196    {
197   
198        root->addChild(baseModel);
199    }
200   
201    root->addChild(movingModel);
202
203    return root;
204}
205
206
207int main( int argc, char **argv )
208{
209    // use an ArgumentParser object to manage the program arguments.
210    osg::ArgumentParser arguments(&argc,argv);
211
212    // set up the usage document, in case we need to print out how to use this program.
213    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of osg::AnimationPath and UpdateCallbacks for adding animation to your scenes.");
214    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
215    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
216
217    // initialize the viewer.
218    osgProducer::Viewer viewer(arguments);
219
220    // set up the value with sensible default event handlers.
221    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
222
223    // get details on keyboard and mouse bindings used by the viewer.
224    viewer.getUsage(*arguments.getApplicationUsage());
225
226    // if user request help write it out to cout.
227    if (arguments.read("-h") || arguments.read("--help"))
228    {
229        arguments.getApplicationUsage()->write(std::cout);
230        return 1;
231    }
232   
233    bool overlay = false;
234    while (arguments.read("--overlay")) overlay = true;
235
236    // any option left unread are converted into errors to write out later.
237    arguments.reportRemainingOptionsAsUnrecognized();
238
239    // report any errors if they have occured when parsing the program aguments.
240    if (arguments.errors())
241    {
242        arguments.writeErrorMessages(std::cout);
243        return 1;
244    }
245   
246    // load the nodes from the commandline arguments.
247    osg::Node* model = createModel(overlay);
248    if (!model)
249    {
250        return 1;
251    }
252   
253    // tilt the scene so the default eye position is looking down on the model.
254    osg::MatrixTransform* rootnode = new osg::MatrixTransform;
255    rootnode->setMatrix(osg::Matrix::rotate(osg::inDegrees(30.0f),1.0f,0.0f,0.0f));
256    rootnode->addChild(model);
257
258    // run optimization over the scene graph
259    osgUtil::Optimizer optimzer;
260    optimzer.optimize(rootnode);
261     
262    // set the scene to render
263    viewer.setSceneData(rootnode);
264
265    // create the windows and run the threads.
266    viewer.realize();
267
268    while( !viewer.done() )
269    {
270        // wait for all cull and draw threads to complete.
271        viewer.sync();
272
273        // update the scene by traversing it with the the update visitor which will
274        // call all node update callbacks and animations.
275        viewer.update();
276         
277        // fire off the cull and draw traversals of the scene.
278        viewer.frame();
279       
280    }
281   
282    // wait for all cull and draw threads to complete before exit.
283    viewer.sync();
284
285    return 0;
286}
Note: See TracBrowser for help on using the browser.