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

Revision 1815, 8.2 kB (checked in by robert, 12 years ago)

Added the usage report to the examples for when no arguments are passed
to the examples.

  • 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()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
197    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
198
199    // initialize the viewer.
200    osgProducer::Viewer viewer(arguments);
201
202    // set up the value with sensible default event handlers.
203    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
204
205    // get details on keyboard and mouse bindings used by the viewer.
206    viewer.getUsage(*arguments.getApplicationUsage());
207
208    // if user request help write it out to cout.
209    if (arguments.read("-h") || arguments.read("--help"))
210    {
211        arguments.getApplicationUsage()->write(std::cout);
212        return 1;
213    }
214
215    // any option left unread are converted into errors to write out later.
216    arguments.reportRemainingOptionsAsUnrecognized();
217
218    // report any errors if they have occured when parsing the program aguments.
219    if (arguments.errors())
220    {
221        arguments.writeErrorMessages(std::cout);
222        return 1;
223    }
224   
225    // load the nodes from the commandline arguments.
226    osg::Node* model = createModel();
227    if (!model)
228    {
229        return 1;
230    }
231   
232    // tilt the scene so the default eye position is looking down on the model.
233    osg::MatrixTransform* rootnode = new osg::MatrixTransform;
234    rootnode->setMatrix(osg::Matrix::rotate(osg::inDegrees(30.0f),1.0f,0.0f,0.0f));
235    rootnode->addChild(model);
236
237    // run optimization over the scene graph
238    osgUtil::Optimizer optimzer;
239    optimzer.optimize(rootnode);
240     
241    // set the scene to render
242    viewer.setSceneData(rootnode);
243
244    // create the windows and run the threads.
245    viewer.realize(Producer::CameraGroup::ThreadPerCamera);
246
247    while( !viewer.done() )
248    {
249        // wait for all cull and draw threads to complete.
250        viewer.sync();
251
252        // update the scene by traversing it with the the update visitor which will
253        // call all node update callbacks and animations.
254        viewer.update();
255         
256        // fire off the cull and draw traversals of the scene.
257        viewer.frame();
258       
259    }
260   
261    // wait for all cull and draw threads to complete before exit.
262    viewer.sync();
263
264    return 0;
265}
Note: See TracBrowser for help on using the browser.