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

Revision 1844, 8.4 kB (checked in by robert, 12 years ago)

Added desciptions to all the demos.

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