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

Revision 4506, 8.7 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 <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()
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 1
188    osgSim::OverlayNode* overlayNode = new osgSim::OverlayNode;
189    overlayNode->setOverlaySubgraph(movingModel);
190    overlayNode->addChild(baseModel);
191    root->addChild(overlayNode);
192#else
193    root->addChild(baseModel);
194#endif
195
196    root->addChild(movingModel);
197
198    return root;
199}
200
201
202int main( int argc, char **argv )
203{
204    // use an ArgumentParser object to manage the program arguments.
205    osg::ArgumentParser arguments(&argc,argv);
206
207    // set up the usage document, in case we need to print out how to use this program.
208    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of osg::AnimationPath and UpdateCallbacks for adding animation to your scenes.");
209    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
210    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
211
212    // initialize the viewer.
213    osgProducer::Viewer viewer(arguments);
214
215    // set up the value with sensible default event handlers.
216    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
217
218    // get details on keyboard and mouse bindings used by the viewer.
219    viewer.getUsage(*arguments.getApplicationUsage());
220
221    // if user request help write it out to cout.
222    if (arguments.read("-h") || arguments.read("--help"))
223    {
224        arguments.getApplicationUsage()->write(std::cout);
225        return 1;
226    }
227
228    // any option left unread are converted into errors to write out later.
229    arguments.reportRemainingOptionsAsUnrecognized();
230
231    // report any errors if they have occured when parsing the program aguments.
232    if (arguments.errors())
233    {
234        arguments.writeErrorMessages(std::cout);
235        return 1;
236    }
237   
238    // load the nodes from the commandline arguments.
239    osg::Node* model = createModel();
240    if (!model)
241    {
242        return 1;
243    }
244   
245    // tilt the scene so the default eye position is looking down on the model.
246    osg::MatrixTransform* rootnode = new osg::MatrixTransform;
247    rootnode->setMatrix(osg::Matrix::rotate(osg::inDegrees(30.0f),1.0f,0.0f,0.0f));
248    rootnode->addChild(model);
249
250    // run optimization over the scene graph
251    osgUtil::Optimizer optimzer;
252    optimzer.optimize(rootnode);
253     
254    // set the scene to render
255    viewer.setSceneData(rootnode);
256
257    // create the windows and run the threads.
258    viewer.realize();
259
260    while( !viewer.done() )
261    {
262        // wait for all cull and draw threads to complete.
263        viewer.sync();
264
265        // update the scene by traversing it with the the update visitor which will
266        // call all node update callbacks and animations.
267        viewer.update();
268         
269        // fire off the cull and draw traversals of the scene.
270        viewer.frame();
271       
272    }
273   
274    // wait for all cull and draw threads to complete before exit.
275    viewer.sync();
276
277    return 0;
278}
Note: See TracBrowser for help on using the browser.