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

Revision 5910, 7.1 kB (checked in by robert, 7 years ago)

Simplified the osganimate and osgautotransform mains, removing most or all of the argument parser code.

  • 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 <osgViewer/Viewer>
19#include <iostream>
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   
210    bool overlay = false;
211    osg::ArgumentParser arguments(&argc,argv);
212    while (arguments.read("--overlay")) overlay = true;
213   
214    // initialize the viewer.
215    osgViewer::Viewer viewer;
216
217    // load the nodes from the commandline arguments.
218    osg::Node* model = createModel(overlay);
219    if (!model)
220    {
221        return 1;
222    }
223   
224    // tilt the scene so the default eye position is looking down on the model.
225    osg::MatrixTransform* rootnode = new osg::MatrixTransform;
226    rootnode->setMatrix(osg::Matrix::rotate(osg::inDegrees(30.0f),1.0f,0.0f,0.0f));
227    rootnode->addChild(model);
228
229    // run optimization over the scene graph
230    osgUtil::Optimizer optimzer;
231    optimzer.optimize(rootnode);
232     
233    // set the scene to render
234    viewer.setSceneData(rootnode);
235
236    return viewer.run();
237}
Note: See TracBrowser for help on using the browser.