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

Revision 6803, 8.2 kB (checked in by robert, 7 years ago)

Removed setViewUpOnScreen settings

  • 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, osgSim::OverlayNode::OverlayTechnique technique)
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    float baseHeight = center.z()-radius*0.5;
185    osg::Node* baseModel = createBase(osg::Vec3(center.x(), center.y(), baseHeight),radius);
186    osg::Node* movingModel = createMovingModel(center,radius*0.8f);
187
188    if (overlay)
189    {
190        osgSim::OverlayNode* overlayNode = new osgSim::OverlayNode(technique);
191        overlayNode->setContinuousUpdate(true);
192        overlayNode->setOverlaySubgraph(movingModel);
193        overlayNode->setOverlayBaseHeight(baseHeight-0.01);
194        overlayNode->addChild(baseModel);
195        root->addChild(overlayNode);
196    }
197    else
198    {
199   
200        root->addChild(baseModel);
201    }
202   
203    root->addChild(movingModel);
204
205    return root;
206}
207
208
209int main( int argc, char **argv )
210{
211   
212    bool overlay = false;
213    osg::ArgumentParser arguments(&argc,argv);
214    while (arguments.read("--overlay")) overlay = true;
215   
216    osgSim::OverlayNode::OverlayTechnique technique = osgSim::OverlayNode::OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY;
217    while (arguments.read("--object")) { technique = osgSim::OverlayNode::OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY; overlay=true; }
218    while (arguments.read("--ortho") || arguments.read("--orthographic")) { technique = osgSim::OverlayNode::VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY; overlay=true; }
219    while (arguments.read("--persp") || arguments.read("--perspective")) { technique = osgSim::OverlayNode::VIEW_DEPENDENT_WITH_PERSPECTIVE_OVERLAY; overlay=true; }
220   
221
222    // initialize the viewer.
223    osgViewer::Viewer viewer;
224
225    // load the nodes from the commandline arguments.
226    osg::Node* model = createModel(overlay, technique);
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    viewer.setCameraManipulator(new osgGA::TrackballManipulator());
245
246    // viewer.setUpViewOnSingleScreen(1);
247
248#if 0
249
250    // use of custom simulation time.
251   
252    viewer.realize();
253   
254    double simulationTime = 0.0;
255   
256    while (!viewer.done())
257    {
258        viewer.frame(simulationTime);
259        simulationTime += 0.001;
260    }
261   
262    return 0;
263#else
264
265    // normal viewer usage.
266    return viewer.run();
267
268#endif
269}
Note: See TracBrowser for help on using the browser.