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

Revision 3522, 10.9 kB (checked in by robert, 9 years ago)

From Rainer Oder, updates to osgplanets

  • 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#include <osg/ShapeDrawable>
9
10#include <osgUtil/Optimizer>
11
12#include <osgDB/Registry>
13#include <osgDB/ReadFile>
14
15#include <osgGA/TrackballManipulator>
16#include <osgGA/FlightManipulator>
17#include <osgGA/DriveManipulator>
18
19#include <osgProducer/Viewer>
20
21
22osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime)
23{
24    // set up the animation path
25    osg::AnimationPath* animationPath = new osg::AnimationPath;
26    animationPath->setLoopMode(osg::AnimationPath::LOOP);
27   
28    int numSamples = 40;
29    float yaw = 0.0f;
30    float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f);
31    float roll = osg::inDegrees(30.0f);
32   
33    double time=0.0f;
34    double time_delta = looptime/(double)numSamples;
35    for(int i=0;i<numSamples;++i)
36    {
37        osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f));
38        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)));
39       
40        animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation));
41
42        yaw += yaw_delta;
43        time += time_delta;
44
45    }
46    return animationPath;   
47}// end createAnimationPath
48
49
50osg::MatrixTransform* createEarthTranslationAndTilt( double RorbitEarth, double tiltEarth )
51{
52        osg::MatrixTransform* earthPositioned = new osg::MatrixTransform;
53        //earthPositioned->setDataVariance(osg::Object::STATIC);
54        earthPositioned->setMatrix(osg::Matrix::translate(osg::Vec3( 0.0, RorbitEarth, 0.0 ) )*
55                                     osg::Matrix::scale(1.0, 1.0, 1.0)*
56                                     osg::Matrix::rotate(osg::inDegrees( tiltEarth ),0.0f,0.0f,1.0f));
57                                     
58        return earthPositioned;
59}// end createEarthTranslationAndTilt
60
61
62osg::MatrixTransform* createRotation( double orbit, double speed )
63{
64    osg::Vec3 center( 0.0, 0.0, 0.0 );
65    float animationLength = 10.0f;
66    osg::AnimationPath* animationPath = createAnimationPath( center, orbit, animationLength );
67   
68    osg::MatrixTransform* rotation = new osg::MatrixTransform;
69    rotation->setUpdateCallback( new osg::AnimationPathCallback( animationPath, 0.0f, speed ) );
70   
71    return rotation;
72}// end createEarthRotation
73
74
75osg::MatrixTransform* createMoonTranslation( double RorbitMoon )
76{
77    osg::MatrixTransform* moonPositioned = new osg::MatrixTransform;
78    //earthPositioned->setDataVariance(osg::Object::STATIC);
79    //earthPositioned->setMatrix(osg::Matrix::translate(osg::Vec3( RorbitEarth, 0.0, 0.0 ) )*
80    moonPositioned->setMatrix(osg::Matrix::translate(osg::Vec3( 0.0, RorbitMoon, 0.0 ) )*
81    //earthPositioned->setMatrix(osg::Matrix::translate(osg::Vec3( 0.0, 0.0, RorbitEarth ) )*
82                                 osg::Matrix::scale(1.0, 1.0, 1.0)*
83                                 osg::Matrix::rotate(osg::inDegrees(0.0f),0.0f,0.0f,1.0f));
84
85    return moonPositioned;
86}// end createMoonTranslation
87
88
89osg::Geode* createPlanet( double radius, std::string name, osg::Vec4 color )
90{
91    // create a cube shape
92    osg::Sphere *planetSphere = new osg::Sphere( osg::Vec3( 0.0, 0.0, 0.0 ), radius );
93
94    // create a container that makes the sphere drawable
95    osg::ShapeDrawable *sPlanetSphere = new osg::ShapeDrawable( planetSphere );
96 
97    // set the object color
98    sPlanetSphere->setColor( color );
99   
100    // create a geode object to as a container for our drawable sphere object
101    osg::Geode* geodePlanet = new osg::Geode();
102    geodePlanet->setName( name );
103   
104    // add our drawable sphere to the geode container
105    geodePlanet->addDrawable( sPlanetSphere );
106
107    return( geodePlanet );
108}// end createPlanet
109
110
111class SolarSystem
112{
113
114public:
115    double _radiusSun;
116    double _radiusEarth;
117    double _RorbitEarth;
118    double _tiltEarth;
119    double _rotateSpeedEarthAndMoon;
120    double _rotateSpeedEarth;
121    double _radiusMoon;
122    double _RorbitMoon;
123    double _rotateSpeedMoon;
124   
125    SolarSystem(
126        double _radiusSun = 20.0,
127        double _radiusEarth = 10.0,
128        double _RorbitEarth = 100.0,
129        double _tiltEarth = 5.0,
130        double _rotateSpeedEarthAndMoon = 5.0,
131        double _rotateSpeedEarth = 5.0,
132        double _radiusMoon = 2.0,
133        double _RorbitMoon = 20.0,
134        double _rotateSpeedMoon = 5.0 )
135    {}
136   
137    osg::Group* built()
138    {
139        osg::Group* thisSystem = new osg::Group;
140       
141       
142        // create the sun
143        osg::Node* sun = createPlanet( _radiusSun, "Sun", osg::Vec4( 1.0f, 1.0f, 0.5f, 1.0f) );
144       
145        // stick sun right under root, no transformations for the sun
146        thisSystem->addChild( sun );
147       
148       
149       
150        //creating right side of the graph with earth and moon and the rotations above it
151       
152        // create earth and moon
153        osg::Node* earth = createPlanet( _radiusEarth, "Earth", osg::Vec4( 0.0f, 0.0f, 1.0f, 1.0f) );
154        osg::Node* moon = createPlanet( _radiusMoon, "Moon", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f) );
155       
156        // create transformations for the earthMoonGroup
157        osg::MatrixTransform* aroundSunRotation = createRotation( _RorbitEarth, _rotateSpeedEarthAndMoon );
158        osg::MatrixTransform* earthPosition = createEarthTranslationAndTilt( _RorbitEarth, _tiltEarth );
159       
160        //Group with earth and moon under it
161        osg::Group* earthMoonGroup = new osg::Group;
162       
163        //transformation to rotate the earth around itself
164        osg::MatrixTransform* earthRotationAroundItself = createRotation ( 0.0, _rotateSpeedEarth );
165       
166        //transformations for the moon
167        osg::MatrixTransform* moonAroundEarthXform = createRotation( _RorbitMoon, _rotateSpeedMoon );
168        osg::MatrixTransform* moonTranslation = createMoonTranslation( _RorbitMoon );
169       
170       
171        moonTranslation->addChild( moon );
172        moonAroundEarthXform->addChild( moonTranslation );
173        earthMoonGroup->addChild( moonAroundEarthXform );
174       
175        earthRotationAroundItself->addChild( earth );
176        earthMoonGroup->addChild( earthRotationAroundItself );
177       
178       
179        earthPosition->addChild( earthMoonGroup );
180        aroundSunRotation->addChild( earthPosition );
181       
182       
183        thisSystem->addChild( aroundSunRotation );
184               
185        return( thisSystem );
186    }
187   
188    void printParameters()
189    {
190        std::cout << "radiusSun\t= " << _radiusSun << std::endl;
191        std::cout << "radiusEarth\t= " << _radiusEarth << std::endl;
192        std::cout << "RorbitEarth\t= " << _RorbitEarth << std::endl;
193        std::cout << "tiltEarth\t= " << _tiltEarth << std::endl;
194        std::cout << "rotateSpeedEarthAndMoon= " << _rotateSpeedEarthAndMoon     << std::endl;
195        std::cout << "rotateSpeedEarth= " << _rotateSpeedEarth << std::endl;
196        std::cout << "radiusMoon\t= " << _radiusMoon << std::endl;
197        std::cout << "RorbitMoon\t= " << _RorbitMoon << std::endl;
198        std::cout << "rotateSpeedMoon\t= " << _rotateSpeedMoon << std::endl;
199    }
200   
201};  // end SolarSystem
202
203
204int main( int argc, char **argv )
205{
206    // use an ArgumentParser object to manage the program arguments.
207    osg::ArgumentParser arguments(&argc,argv);
208
209    // set up the usage document, in case we need to print out how to use this program.
210    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of osg::AnimationPath and UpdateCallbacks for adding animation to your scenes.");
211    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
212    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
213
214    // initialize the viewer.
215    osgProducer::Viewer viewer(arguments);
216
217    // set up the value with sensible default event handlers.
218    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
219
220    // get details on keyboard and mouse bindings used by the viewer.
221    viewer.getUsage(*arguments.getApplicationUsage());
222
223    SolarSystem solarSystem;
224
225    while (arguments.read("--radiusSun",solarSystem._radiusSun)) { }
226    while (arguments.read("--radiusEarth",solarSystem._radiusEarth)) { }
227    while (arguments.read("--RorbitEarth",solarSystem._RorbitEarth)) { }
228    while (arguments.read("--tiltEarth",solarSystem._tiltEarth)) { }
229    while (arguments.read("--rotateSpeedEarthAndMoon",solarSystem._rotateSpeedEarthAndMoon)) { }
230    while (arguments.read("--rotateSpeedEarth",solarSystem._rotateSpeedEarth)) { }
231    while (arguments.read("--radiusMoon",solarSystem._radiusMoon)) { }
232    while (arguments.read("--RorbitMoon",solarSystem._RorbitMoon)) { }
233    while (arguments.read("--rotateSpeedMoon",solarSystem._rotateSpeedMoon)) { }
234
235
236    // solarSystem.printParameters();
237
238    // if user request help write it out to cout.
239    if (arguments.read("-h") || arguments.read("--help"))
240    {
241        std::cout << "setup the following arguments: " << std::endl;
242        std::cout << "--radiusSun: double" << std::endl;
243        std::cout << "--radiusEarth: double" << std::endl;
244        std::cout << "--RorbitEarth: double" << std::endl;
245        std::cout << "--tiltEarth: double" << std::endl;
246        std::cout << "--rotateSpeedEarthAndMoon: double" << std::endl;
247        std::cout << "--rotateSpeedEarth: double" << std::endl;
248        std::cout << "--radiusMoon: double" << std::endl;
249        std::cout << "--RorbitMoon: double" << std::endl;
250        std::cout << "--rotateSpeedMoon: double" << std::endl;
251               
252        return 1;
253    }
254
255    // any option left unread are converted into errors to write out later.
256    arguments.reportRemainingOptionsAsUnrecognized();
257
258    // report any errors if they have occured when parsing the program aguments.
259    if (arguments.errors())
260    {
261        arguments.writeErrorMessages(std::cout);
262        return 1;
263    }
264   
265   
266    osg::Group* root = solarSystem.built();
267       
268    /*
269    // tilt the scene so the default eye position is looking down on the model.
270    osg::MatrixTransform* rootnode = new osg::MatrixTransform;
271    rootnode->setMatrix(osg::Matrix::rotate(osg::inDegrees(30.0f),1.0f,0.0f,0.0f));
272    rootnode->addChild(model);
273    */
274
275    // run optimization over the scene graph
276    osgUtil::Optimizer optimzer;
277    optimzer.optimize( root );
278     
279    // set the scene to render
280    viewer.setSceneData( root );
281
282    // create the windows and run the threads.
283    viewer.realize();
284
285    while( !viewer.done() )
286    {
287        // wait for all cull and draw threads to complete.
288        viewer.sync();
289
290        // update the scene by traversing it with the the update visitor which will
291        // call all node update callbacks and animations.
292        viewer.update();
293         
294        // fire off the cull and draw traversals of the scene.
295        viewer.frame();
296       
297    }
298   
299    // wait for all cull and draw threads to complete before exit.
300    viewer.sync();
301
302    return 0;
303}
Note: See TracBrowser for help on using the browser.