root/OpenSceneGraph/trunk/examples/osgplanets/osgplanets.cpp @ 3515

Revision 3515, 9.7 kB (checked in by robert, 9 years ago)

Compile fixes

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