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

Revision 3666, 30.6 kB (checked in by robert, 10 years ago)

Increased accuracy of animation path, moved main to end.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* details about distances and rotation on http://www.solarviews.com/eng/solarsys.htm */
2
3#include <iostream>
4
5#include <osg/Notify>
6#include <osg/MatrixTransform>
7#include <osg/PositionAttitudeTransform>
8#include <osg/Geometry>
9#include <osg/Geode>
10#include <osg/ShapeDrawable>
11#include <osg/Texture2D>
12#include <osg/Material>
13#include <osg/Light>
14#include <osg/LightSource>
15#include <osg/LightModel>
16#include <osg/Billboard>
17#include <osg/LineWidth>
18#include <osg/TexEnv>
19#include <osg/TexEnvCombine>
20
21
22#include <osgUtil/Optimizer>
23
24#include <osgDB/Registry>
25#include <osgDB/ReadFile>
26#include <osgDB/WriteFile>
27
28
29#include <osgGA/NodeTrackerManipulator>
30#include <osgGA/TrackballManipulator>
31#include <osgGA/FlightManipulator>
32#include <osgGA/DriveManipulator>
33
34#include <osgProducer/Viewer>
35
36
37static osg::Vec3 defaultPos( 0.0f, 0.0f, 0.0f );
38static osg::Vec3 centerScope(0.0f, 0.0f, 0.0f);
39
40
41/** create quad at specified position. */
42osg::Drawable* createSquare(const osg::Vec3& corner,const osg::Vec3& width,const osg::Vec3& height, osg::Image* image=NULL)
43{
44    // set up the Geometry.
45    osg::Geometry* geom = new osg::Geometry;
46
47    osg::Vec3Array* coords = new osg::Vec3Array(4);
48    (*coords)[0] = corner;
49    (*coords)[1] = corner+width;
50    (*coords)[2] = corner+width+height;
51    (*coords)[3] = corner+height;
52
53
54    geom->setVertexArray(coords);
55
56    osg::Vec3Array* norms = new osg::Vec3Array(1);
57    (*norms)[0] = width^height;
58    (*norms)[0].normalize();
59   
60    geom->setNormalArray(norms);
61    geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
62
63    osg::Vec2Array* tcoords = new osg::Vec2Array(4);
64    (*tcoords)[0].set(0.0f,0.0f);
65    (*tcoords)[1].set(1.0f,0.0f);
66    (*tcoords)[2].set(1.0f,1.0f);
67    (*tcoords)[3].set(0.0f,1.0f);
68    geom->setTexCoordArray(0,tcoords);
69   
70    osg::Vec4Array* colours = new osg::Vec4Array(1);
71    (*colours)[0].set(1.0f,1.0f,1.0f,1.0f);
72    geom->setColorArray(colours);
73    geom->setColorBinding(osg::Geometry::BIND_OVERALL);
74
75
76    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
77   
78    if (image)
79    {
80        osg::StateSet* stateset = new osg::StateSet;
81        osg::Texture2D* texture = new osg::Texture2D;
82        texture->setImage(image);
83        stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
84        stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
85        stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
86        stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
87        geom->setStateSet(stateset);
88    }
89   
90    return geom;
91}
92
93osg::Image* createBillboardImage(const osg::Vec4& centerColour, unsigned int size, float power)
94{
95    osg::Vec4 backgroundColour = centerColour;
96    backgroundColour[3] = 0.0f;
97   
98    osg::Image* image = new osg::Image;
99    image->allocateImage(size,size,1,
100                         GL_RGBA,GL_UNSIGNED_BYTE);
101     
102     
103    float mid = (float(size)-1)*0.5f;
104    float div = 2.0f/float(size);
105    for(unsigned int r=0;r<size;++r)
106    {
107        unsigned char* ptr = image->data(0,r,0);
108        for(unsigned int c=0;c<size;++c)
109        {
110            float dx = (float(c) - mid)*div;
111            float dy = (float(r) - mid)*div;
112            float r = powf(1.0f-sqrtf(dx*dx+dy*dy),power);
113            if (r<0.0f) r=0.0f;
114            osg::Vec4 color = centerColour*r+backgroundColour*(1.0f-r);
115            // color.set(1.0f,1.0f,1.0f,0.5f);
116            *ptr++ = (unsigned char)((color[0])*255.0f);
117            *ptr++ = (unsigned char)((color[1])*255.0f);
118            *ptr++ = (unsigned char)((color[2])*255.0f);
119            *ptr++ = (unsigned char)((color[3])*255.0f);
120        }
121    }
122    return image;
123
124    //return osgDB::readImageFile("spot.dds");
125}
126
127osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime)
128{
129    // set up the animation path
130    osg::AnimationPath* animationPath = new osg::AnimationPath;
131    animationPath->setLoopMode(osg::AnimationPath::LOOP);
132   
133    int numSamples = 1000;
134    float yaw = 0.0f;
135    float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f);
136    float roll = osg::inDegrees(30.0f);
137   
138    double time=0.0f;
139    double time_delta = looptime/(double)numSamples;
140    for(int i=0;i<numSamples;++i)
141    {
142        osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f));
143        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)));
144       
145        animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation));
146
147        yaw += yaw_delta;
148        time += time_delta;
149
150    }
151    return animationPath;   
152}// end createAnimationPath
153
154
155class SolarSystem
156{
157
158public:
159    double _radiusSpace;
160    double _radiusSun;
161    double _radiusMercury;
162    double _radiusVenus;
163    double _radiusEarth;
164    double _radiusMoon;
165    double _radiusMars;
166    double _radiusJupiter;
167
168    double _RorbitMercury;
169    double _RorbitVenus;
170    double _RorbitEarth;
171    double _RorbitMoon;
172    double _RorbitMars;
173    double _RorbitJupiter;
174
175    double _rotateSpeedMercury;
176    double _rotateSpeedVenus;
177    double _rotateSpeedEarthAndMoon;
178    double _rotateSpeedEarth;
179    double _rotateSpeedMoon;
180    double _rotateSpeedMars;
181    double _rotateSpeedJupiter;
182
183    double _tiltEarth;
184
185    std::string _mapSpace;
186    std::string _mapSun;
187    std::string _mapVenus;   
188    std::string _mapMercury;   
189    std::string _mapEarth;
190    std::string _mapEarthNight;
191    std::string _mapMoon;
192    std::string _mapMars;
193    std::string _mapJupiter;
194   
195    SolarSystem()
196    {
197        _radiusSpace    = 300.0;
198        _radiusSun      = 3.5;
199        _radiusMercury  = 0.7;
200        _radiusVenus    = 1.2;
201        _radiusEarth    = 2.0;
202        _radiusMoon     = 0.5;
203        _radiusMars     = 1.8;
204        _radiusJupiter  = 1.8;
205       
206        _RorbitMercury  = 11.7;
207        _RorbitVenus    = 21.6;
208        _RorbitEarth    = 30.0;
209        _RorbitMoon     = 2.0;
210        _RorbitMars     = 45.0;
211        _RorbitJupiter  = 156.0;
212       
213        _rotateSpeedMercury         = 1.1;
214        _rotateSpeedVenus           = 1.3;
215        _rotateSpeedEarthAndMoon    = 1.0;
216        _rotateSpeedEarth           = 1.0;
217        _rotateSpeedMoon            = 1.2;
218        _rotateSpeedMars            = 1.2;
219        _rotateSpeedJupiter         = 1.2;
220
221        _tiltEarth                  = 18.0; // degrees
222       
223        _mapSpace       = "Images/spacemap2.jpg";
224        _mapSun         = "SolarSystem/sun256128.jpg";
225        _mapMercury     = "SolarSystem/mercury256128.jpg";
226        _mapVenus       = "SolarSystem/venus256128.jpg";
227        _mapEarth       = "Images/land_shallow_topo_2048.jpg";
228        _mapEarthNight  = "Images/land_ocean_ice_lights_2048.jpg";
229        _mapMoon        = "SolarSystem/moon256128.jpg";
230        _mapMars        = "SolarSystem/mars256128.jpg";
231        _mapJupiter     = "SolarSystem/jupiter256128.jpg";
232    }
233   
234    osg::MatrixTransform* createTranslationAndTilt( double translation, double tilt );
235    osg::MatrixTransform* createRotation( double orbit, double speed );
236   
237    osg::Geode* createSpace( const std::string& name, const std::string& textureName );
238    osg::Geode* createPlanet( double radius, const std::string& name, const osg::Vec4& color , const std::string& textureName );
239    osg::Geode* createPlanet( double radius, const std::string& name, const osg::Vec4& color , const std::string& textureName1, const std::string& textureName2);
240    osg::Group* createSunLight();
241   
242    void printParameters();
243       
244};  // end SolarSystem
245
246class FindNamedNodeVisitor : public osg::NodeVisitor
247{
248public:
249    FindNamedNodeVisitor(const std::string& name):
250        osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
251        _name(name) {}
252   
253    virtual void apply(osg::Node& node)
254    {
255        if (node.getName()==_name)
256        {
257            _foundNodes.push_back(&node);
258        }
259        traverse(node);
260    }
261   
262    typedef std::vector< osg::ref_ptr<osg::Node> > NodeList;
263
264    std::string _name;
265    NodeList _foundNodes;
266};
267
268
269osg::MatrixTransform* SolarSystem::createRotation( double orbit, double speed )
270{
271    osg::Vec3 center( 0.0, 0.0, 0.0 );
272    float animationLength = 10.0f;
273    osg::AnimationPath* animationPath = createAnimationPath( center, orbit, animationLength );
274
275    osg::MatrixTransform* rotation = new osg::MatrixTransform;
276    rotation->setUpdateCallback( new osg::AnimationPathCallback( animationPath, 0.0f, speed ) );
277
278    return rotation;
279}// end SolarSystem::createEarthRotation
280
281
282osg::MatrixTransform* SolarSystem::createTranslationAndTilt( double /*translation*/, double tilt )
283{
284    osg::MatrixTransform* moonPositioned = new osg::MatrixTransform;
285    moonPositioned->setMatrix(osg::Matrix::translate(osg::Vec3( 0.0, _RorbitMoon, 0.0 ) )*
286                                 osg::Matrix::scale(1.0, 1.0, 1.0)*
287                                 osg::Matrix::rotate(osg::inDegrees( tilt ),0.0f,0.0f,1.0f));
288
289    return moonPositioned;
290}// end SolarSystem::createTranslationAndTilt
291   
292   
293osg::Geode* SolarSystem::createSpace( const std::string& name, const std::string& textureName )
294{
295    osg::Sphere *spaceSphere = new osg::Sphere( osg::Vec3( 0.0, 0.0, 0.0 ), _radiusSpace );
296
297    osg::ShapeDrawable *sSpaceSphere = new osg::ShapeDrawable( spaceSphere );
298
299    if( !textureName.empty() )
300    {
301        osg::Image* image = osgDB::readImageFile( textureName );
302        if ( image )
303        {
304            sSpaceSphere->getOrCreateStateSet()->setTextureAttributeAndModes( 0, new osg::Texture2D( image ), osg::StateAttribute::ON );
305
306            // reset the object color to white to allow the texture to set the colour.
307            sSpaceSphere->setColor( osg::Vec4(1.0f,1.0f,1.0f,1.0f) );
308        }
309    }
310
311    osg::Geode* geodeSpace = new osg::Geode();
312    geodeSpace->setName( name );
313
314    geodeSpace->addDrawable( sSpaceSphere );
315
316    return( geodeSpace );
317
318}// end SolarSystem::createSpace
319
320   
321osg::Geode* SolarSystem::createPlanet( double radius, const std::string& name, const osg::Vec4& color , const std::string& textureName)
322{
323    // create a container that makes the sphere drawable
324    osg::Geometry *sPlanetSphere = new osg::Geometry();
325
326    {
327        // set the single colour so bind overall
328        osg::Vec4Array* colours = new osg::Vec4Array(1);
329        (*colours)[0] = color;
330        sPlanetSphere->setColorArray(colours);
331        sPlanetSphere->setColorBinding(osg::Geometry::BIND_OVERALL);
332
333
334        // now set up the coords, normals and texcoords for geometry
335        unsigned int numX = 100;
336        unsigned int numY = 50;
337        unsigned int numVertices = numX*numY;
338
339        osg::Vec3Array* coords = new osg::Vec3Array(numVertices);
340        sPlanetSphere->setVertexArray(coords);
341
342        osg::Vec3Array* normals = new osg::Vec3Array(numVertices);
343        sPlanetSphere->setNormalArray(normals);
344        sPlanetSphere->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
345
346        osg::Vec2Array* texcoords = new osg::Vec2Array(numVertices);
347        sPlanetSphere->setTexCoordArray(0,texcoords);
348        sPlanetSphere->setTexCoordArray(1,texcoords);
349
350        double delta_elevation = osg::PI / (double)(numY-1);
351        double delta_azim = 2.0*osg::PI / (double)(numX-1);
352        float delta_tx = 1.0 / (float)(numX-1);
353        float delta_ty = 1.0 / (float)(numY-1);
354
355        double elevation = -osg::PI*0.5;
356        float ty = 0.0;
357        unsigned int vert = 0;
358        unsigned j;
359        for(j=0;
360            j<numY;
361            ++j, elevation+=delta_elevation, ty+=delta_ty )
362        {
363            double azim = 0.0;
364            float tx = 0.0;
365            for(unsigned int i=0;
366                i<numX;
367                ++i, ++vert, azim+=delta_azim, tx+=delta_tx)
368            {
369                osg::Vec3 direction(cos(azim)*cos(elevation), sin(azim)*cos(elevation), sin(elevation));
370                (*coords)[vert].set(direction*radius);
371                (*normals)[vert].set(direction);
372                (*texcoords)[vert].set(tx,ty);
373            }
374        }
375
376        for(j=0;
377            j<numY-1;
378            ++j)
379        {
380            unsigned int curr_row = j*numX;
381            unsigned int next_row = curr_row+numX;
382            osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(GL_QUAD_STRIP);
383            for(unsigned int i=0;
384                i<numX;
385                ++i)
386            {
387                elements->push_back(next_row + i);
388                elements->push_back(curr_row + i);
389            }
390            sPlanetSphere->addPrimitiveSet(elements);
391        }
392    }
393   
394
395    // set the object color
396    //sPlanetSphere->setColor( color );
397
398    // create a geode object to as a container for our drawable sphere object
399    osg::Geode* geodePlanet = new osg::Geode();
400    geodePlanet->setName( name );
401
402    if( !textureName.empty() )
403    {
404        osg::Image* image = osgDB::readImageFile( textureName );
405        if ( image )
406        {
407            geodePlanet->getOrCreateStateSet()->setTextureAttributeAndModes( 0, new osg::Texture2D( image ), osg::StateAttribute::ON );
408
409            // reset the object color to white to allow the texture to set the colour.
410            //sPlanetSphere->setColor( osg::Vec4(1.0f,1.0f,1.0f,1.0f) );
411        }
412    }
413
414    // add our drawable sphere to the geode container
415    geodePlanet->addDrawable( sPlanetSphere );
416
417    return( geodePlanet );
418
419}// end SolarSystem::createPlanet
420   
421osg::Geode* SolarSystem::createPlanet( double radius, const std::string& name, const osg::Vec4& color , const std::string& textureName1, const std::string& textureName2)
422{
423    osg::Geode* geodePlanet = createPlanet( radius, name, color , textureName1);
424   
425    if( !textureName2.empty() )
426    {
427        osg::Image* image = osgDB::readImageFile( textureName2 );
428        if ( image )
429        {
430            osg::StateSet* stateset = geodePlanet->getOrCreateStateSet();
431           
432            osg::TexEnvCombine* texenv = new osg::TexEnvCombine;
433           
434            texenv->setCombine_RGB(osg::TexEnvCombine::INTERPOLATE);
435            texenv->setSource0_RGB(osg::TexEnvCombine::PREVIOUS);
436            texenv->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR);
437            texenv->setSource1_RGB(osg::TexEnvCombine::TEXTURE);
438            texenv->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR);
439            texenv->setSource2_RGB(osg::TexEnvCombine::PRIMARY_COLOR);
440            texenv->setOperand2_RGB(osg::TexEnvCombine::SRC_COLOR);
441
442            stateset->setTextureAttribute( 1, texenv );
443            stateset->setTextureAttributeAndModes( 1, new osg::Texture2D( image ), osg::StateAttribute::ON );
444        }
445    }
446
447    return( geodePlanet );
448
449}// end SolarSystem::createPlanet
450
451osg::Group* SolarSystem::createSunLight()
452{
453
454    osg::LightSource* sunLightSource = new osg::LightSource;
455
456    osg::Light* sunLight = sunLightSource->getLight();
457    sunLight->setPosition( osg::Vec4( 0.0f, 0.0f, 0.0f, 1.0f ) );
458    sunLight->setAmbient( osg::Vec4( 0.0f, 0.0f, 0.0f, 1.0f ) );
459
460    sunLightSource->setLight( sunLight );
461    sunLightSource->setLocalStateSetModes( osg::StateAttribute::ON );
462    sunLightSource->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::ON);
463
464    osg::LightModel* lightModel = new osg::LightModel;
465    lightModel->setAmbientIntensity(osg::Vec4(0.0f,0.0f,0.0f,1.0f));
466    sunLightSource->getOrCreateStateSet()->setAttribute(lightModel);
467
468
469    return sunLightSource;
470}// end SolarSystem::createSunLight
471   
472void SolarSystem::printParameters()
473{
474    std::cout << "radiusSpace(" << _radiusSpace << ")" << std::endl;
475    std::cout << "radiusSun(" << _radiusSun << ")" << std::endl;
476    std::cout << "radiusEarth(" << _radiusEarth << ")" << std::endl;
477    std::cout << "radiusMoon(" << _radiusMoon << ")" << std::endl;
478
479    std::cout << "RorbitEarth(" << _RorbitEarth << ")" << std::endl;
480    std::cout << "RorbitMoon(" << _RorbitMoon << ")" << std::endl;
481
482    std::cout << "rotateSpeedEarthAndMoon(" << _rotateSpeedEarthAndMoon << ")" << std::endl;
483    std::cout << "rotateSpeedEarth(" << _rotateSpeedEarth << ")" << std::endl;
484    std::cout << "rotateSpeedMoon(" << _rotateSpeedMoon << ")" << std::endl;
485    std::cout << "tiltEarth(" << _tiltEarth << ")" << std::endl;
486
487    std::cout << "mapSpace(" << _radiusSpace << ")" << std::endl;
488    std::cout << "mapEarth(" << _radiusSpace << ")" << std::endl;
489    std::cout << "mapEarthNight(" << _radiusSpace << ")" << std::endl;
490    std::cout << "mapMoon(" << _radiusSpace << ")" << std::endl;
491}
492
493
494int main( int argc, char **argv )
495{
496    // use an ArgumentParser object to manage the program arguments.
497    osg::ArgumentParser arguments(&argc,argv);
498
499    // set up the usage document, in case we need to print out how to use this program.
500    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of osg::AnimationPath and UpdateCallbacks for adding animation to your scenes.");
501    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
502    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
503    arguments.getApplicationUsage()->addCommandLineOption("-o <filename>","Write created model to file");
504
505    // initialize the viewer.
506    osgProducer::Viewer viewer(arguments);
507
508    // set up the value with sensible default event handlers.
509    viewer.setUpViewer(osgProducer::Viewer::ESCAPE_SETS_DONE | osgProducer::Viewer::VIEWER_MANIPULATOR | osgProducer::Viewer::STATE_MANIPULATOR);
510
511    // get details on keyboard and mouse bindings used by the viewer.
512    viewer.getUsage(*arguments.getApplicationUsage());
513
514    SolarSystem solarSystem;
515
516    while (arguments.read("--radiusSpace",solarSystem._radiusSpace)) { }
517    while (arguments.read("--radiusSun",solarSystem._radiusSun)) { }
518    while (arguments.read("--radiusEarth",solarSystem._radiusEarth)) { }
519    while (arguments.read("--radiusMoon",solarSystem._radiusMoon)) { }
520   
521    while (arguments.read("--RorbitEarth",solarSystem._RorbitEarth)) { }
522    while (arguments.read("--RorbitMoon",solarSystem._RorbitMoon)) { }
523   
524    while (arguments.read("--rotateSpeedEarthAndMoon",solarSystem._rotateSpeedEarthAndMoon)) { }
525    while (arguments.read("--rotateSpeedEarth",solarSystem._rotateSpeedEarth)) { }
526    while (arguments.read("--rotateSpeedMoon",solarSystem._rotateSpeedMoon)) { }
527    while (arguments.read("--tiltEarth",solarSystem._tiltEarth)) { }
528   
529    while (arguments.read("--mapSpace",solarSystem._mapSpace)) { }
530    while (arguments.read("--mapEarth",solarSystem._mapEarth)) { }
531    while (arguments.read("--mapEarthNight",solarSystem._mapEarthNight)) { }
532    while (arguments.read("--mapMoon",solarSystem._mapMoon)) { }
533   
534    std::string writeFileName;
535    while (arguments.read("-o",writeFileName)) { }
536   
537   
538    osgGA::NodeTrackerManipulator::TrackerMode trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER_AND_ROTATION;
539    std::string mode;
540    while (arguments.read("--tracker-mode",mode))
541    {
542        if (mode=="NODE_CENTER_AND_ROTATION") trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER_AND_ROTATION;
543        else if (mode=="NODE_CENTER_AND_AZIM") trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER_AND_AZIM;
544        else if (mode=="NODE_CENTER") trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER;
545        else
546        {
547            std::cout<<"Unrecognized --tracker-mode option "<<mode<<", valid options are:"<<std::endl;
548            std::cout<<"    NODE_CENTER_AND_ROTATION"<<std::endl;
549            std::cout<<"    NODE_CENTER_AND_AZIM"<<std::endl;
550            std::cout<<"    NODE_CENTER"<<std::endl;
551            return 1;
552        }
553    }
554   
555   
556    osgGA::NodeTrackerManipulator::RotationMode rotationMode = osgGA::NodeTrackerManipulator::TRACKBALL;
557    while (arguments.read("--rotation-mode",mode))
558    {
559        if (mode=="TRACKBALL") rotationMode = osgGA::NodeTrackerManipulator::TRACKBALL;
560        else if (mode=="ELEVATION_AZIM") rotationMode = osgGA::NodeTrackerManipulator::ELEVATION_AZIM;
561        else
562        {
563            std::cout<<"Unrecognized --rotation-mode option "<<mode<<", valid options are:"<<std::endl;
564            std::cout<<"    TRACKBALL"<<std::endl;
565            std::cout<<"    ELEVATION_AZIM"<<std::endl;
566            return 1;
567        }
568    }
569   
570
571    solarSystem.printParameters();
572
573    // if user request help write it out to cout.
574    if (arguments.read("-h") || arguments.read("--help"))
575    {
576        std::cout << "setup the following arguments: " << std::endl;
577        std::cout << "--radiusSpace: double" << std::endl;
578        std::cout << "--radiusSun: double" << std::endl;
579        std::cout << "--radiusEarth: double" << std::endl;
580        std::cout << "--radiusMoon: double" << std::endl;
581       
582        std::cout << "--RorbitEarth: double" << std::endl;
583        std::cout << "--RorbitMoon: double" << std::endl;
584       
585        std::cout << "--rotateSpeedEarthAndMoon: double" << std::endl;
586        std::cout << "--rotateSpeedEarth: double" << std::endl;
587        std::cout << "--rotateSpeedMoon: double" << std::endl;
588        std::cout << "--tiltEarth: double" << std::endl;
589       
590        std::cout << "--mapSpace: string" << std::endl;
591        std::cout << "--mapEarth: string" << std::endl;
592        std::cout << "--mapEarthNight: string" << std::endl;
593        std::cout << "--mapMoon: string" << std::endl;
594                       
595        return 1;
596    }
597
598    // any option left unread are converted into errors to write out later.
599    arguments.reportRemainingOptionsAsUnrecognized();
600
601    // report any errors if they have occured when parsing the program aguments.
602    if (arguments.errors())
603    {
604        arguments.writeErrorMessages(std::cout);
605        return 1;
606    }
607   
608   
609    osg::Group* root = new osg::Group;
610
611    osg::Group* sunLight = solarSystem.createSunLight();
612    root->addChild(sunLight);
613
614    // create the sun
615    osg::Node* solarSun = solarSystem.createPlanet( solarSystem._radiusSun, "Sun", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f), solarSystem._mapSun );
616    osg::StateSet* sunStateSet = solarSun->getOrCreateStateSet();
617    osg::Material* material = new osg::Material;
618    material->setEmission( osg::Material::FRONT_AND_BACK, osg::Vec4( 1.0f, 1.0f, 0.0f, 0.0f ) );
619    sunStateSet->setAttributeAndModes( material, osg::StateAttribute::ON );
620
621    osg::Billboard* sunBillboard = new osg::Billboard();
622    sunBillboard->setMode(osg::Billboard::POINT_ROT_EYE);
623    sunBillboard->addDrawable(
624        createSquare(osg::Vec3(-5.0f,0.0f,-5.0f),osg::Vec3(10.0f,0.0f,0.0f),osg::Vec3(0.0f,0.0f,10.0f),createBillboardImage( osg::Vec4( 1.0, 1.0, 0, 1.0f), 64, 1.0) ),
625        osg::Vec3(0.0f,0.0f,0.0f));
626       
627    sunLight->addChild( sunBillboard );
628
629    // stick sun right under root, no transformations for the sun
630    sunLight->addChild( solarSun );
631
632    // create light source in the sun
633
634/*
635*********************************************
636**  earthMoonGroup and Transformations
637*********************************************
638*/
639    // create earth and moon
640    osg::Node* earth = solarSystem.createPlanet( solarSystem._radiusEarth, "Earth", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f), solarSystem._mapEarth, solarSystem._mapEarthNight );
641    osg::Node* moon = solarSystem.createPlanet( solarSystem._radiusMoon, "Moon", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f), solarSystem._mapMoon );
642
643    // create transformations for the earthMoonGroup
644    osg::MatrixTransform* aroundSunRotationEarthMoonGroup = solarSystem.createRotation( solarSystem._RorbitEarth, solarSystem._rotateSpeedEarthAndMoon );
645    osg::MatrixTransform* earthMoonGroupPosition = solarSystem.createTranslationAndTilt( solarSystem._RorbitEarth, solarSystem._tiltEarth );
646
647
648    //Group with earth and moon under it
649    osg::Group* earthMoonGroup = new osg::Group;
650   
651
652    //transformation to rotate the earth around itself
653    osg::MatrixTransform* earthAroundItselfRotation = solarSystem.createRotation ( 0.0, solarSystem._rotateSpeedEarth );
654
655    //transformations for the moon
656    osg::MatrixTransform* moonAroundEarthRotation = solarSystem.createRotation( solarSystem._RorbitMoon, solarSystem._rotateSpeedMoon );
657    osg::MatrixTransform* moonTranslation = solarSystem.createTranslationAndTilt( solarSystem._RorbitMoon, 0.0f );
658
659
660    moonTranslation->addChild( moon );
661    moonAroundEarthRotation->addChild( moonTranslation );
662    earthMoonGroup->addChild( moonAroundEarthRotation );
663
664    earthAroundItselfRotation->addChild( earth );
665
666    earthMoonGroup->addChild( earthAroundItselfRotation );
667
668    earthMoonGroupPosition->addChild( earthMoonGroup );
669
670    aroundSunRotationEarthMoonGroup->addChild( earthMoonGroupPosition );
671
672    sunLight->addChild( aroundSunRotationEarthMoonGroup );
673/*
674*********************************************
675**  end earthMoonGroup and Transformations
676*********************************************
677*/
678
679/*
680*********************************************
681**  Mercury and Transformations
682*********************************************
683*/
684    osg::Node* mercury = solarSystem.createPlanet( solarSystem._radiusMercury, "Mercury", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ), solarSystem._mapMercury, "" );
685   
686    osg::MatrixTransform* aroundSunRotationMercury = solarSystem.createRotation( solarSystem._RorbitMercury, solarSystem._rotateSpeedMercury );
687    osg::MatrixTransform* mercuryPosition = solarSystem.createTranslationAndTilt( solarSystem._RorbitMercury, 0.0f );
688   
689    mercuryPosition->addChild( mercury );
690    aroundSunRotationMercury->addChild( mercuryPosition );
691   
692    sunLight->addChild( aroundSunRotationMercury );
693/*
694*********************************************
695**  end Mercury and Transformations
696*********************************************
697*/
698
699/*
700*********************************************
701**  Venus and Transformations
702*********************************************
703*/
704    osg::Node* venus = solarSystem.createPlanet( solarSystem._radiusVenus, "Venus", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ), solarSystem._mapVenus, "" );
705   
706    osg::MatrixTransform* aroundSunRotationVenus = solarSystem.createRotation( solarSystem._RorbitVenus, solarSystem._rotateSpeedVenus );
707    osg::MatrixTransform* venusPosition = solarSystem.createTranslationAndTilt( solarSystem._RorbitVenus, 0.0f );
708   
709    venusPosition->addChild( venus );
710    aroundSunRotationVenus->addChild( venusPosition );
711   
712    sunLight->addChild( aroundSunRotationVenus );
713/*
714*********************************************
715**  end Venus and Transformations
716*********************************************
717*/
718
719/*
720*********************************************
721**  Mars and Transformations
722*********************************************
723*/
724    osg::Node* mars = solarSystem.createPlanet( solarSystem._radiusMars, "Mars", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ), solarSystem._mapMars, "" );
725   
726    osg::MatrixTransform* aroundSunRotationMars = solarSystem.createRotation( solarSystem._RorbitMars, solarSystem._rotateSpeedMars );
727    osg::MatrixTransform* marsPosition = solarSystem.createTranslationAndTilt( solarSystem._RorbitMars, 0.0f );
728   
729    marsPosition->addChild( mars );
730    aroundSunRotationMars->addChild( marsPosition );
731   
732    sunLight->addChild( aroundSunRotationMars );
733/*
734*********************************************
735**  end Mars and Transformations
736*********************************************
737*/
738
739/*
740*********************************************
741**  Jupiter and Transformations
742*********************************************
743*/
744    osg::Node* jupiter = solarSystem.createPlanet( solarSystem._radiusJupiter, "Jupiter", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ), solarSystem._mapJupiter, "" );
745   
746    osg::MatrixTransform* aroundSunRotationJupiter = solarSystem.createRotation( solarSystem._RorbitJupiter, solarSystem._rotateSpeedJupiter );
747    osg::MatrixTransform* jupiterPosition = solarSystem.createTranslationAndTilt( solarSystem._RorbitJupiter, 0.0f );
748   
749    jupiterPosition->addChild( jupiter );
750    aroundSunRotationJupiter->addChild( jupiterPosition );
751   
752    sunLight->addChild( aroundSunRotationJupiter );
753/*
754*********************************************
755**  end Jupiter and Transformations
756*********************************************
757*/
758
759/*
760    // add space, but don't light it, as its not illuminated by our sun
761    osg::Node* space = solarSystem.createSpace( "Space", solarSystem._mapSpace );
762    space->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
763    root->addChild( space );
764*/   
765     
766    if (!writeFileName.empty())
767    {
768        osgDB::writeNodeFile(*root, writeFileName);
769        std::cout<<"Written solar system to \""<<writeFileName<<"\""<<std::endl;
770    }
771
772
773    // run optimization over the scene graph
774    osgUtil::Optimizer optimzer;
775    optimzer.optimize( root );
776     
777    // set the scene to render
778    viewer.setSceneData( root );
779
780
781    // set up tracker manipulators, once for each astral body
782   
783    {
784        FindNamedNodeVisitor fnnv("Moon");
785        root->accept(fnnv);
786
787        if (!fnnv._foundNodes.empty())
788        {
789            // set up the node tracker.
790            osgGA::NodeTrackerManipulator* tm = new osgGA::NodeTrackerManipulator;
791            tm->setTrackerMode( trackerMode );
792            tm->setRotationMode( rotationMode );
793            tm->setTrackNode( fnnv._foundNodes.front().get() );
794
795            unsigned int num = viewer.addCameraManipulator( tm );
796            viewer.selectCameraManipulator( num );
797        }
798    }   
799
800    {
801        FindNamedNodeVisitor fnnv("Earth");
802        root->accept(fnnv);
803
804        if (!fnnv._foundNodes.empty())
805        {
806            // set up the node tracker.
807            osgGA::NodeTrackerManipulator* tm = new osgGA::NodeTrackerManipulator;
808            tm->setTrackerMode( trackerMode );
809            tm->setRotationMode( rotationMode );
810            tm->setTrackNode( fnnv._foundNodes.front().get() );
811
812            unsigned int num = viewer.addCameraManipulator( tm );
813            viewer.selectCameraManipulator( num );
814        }
815    }   
816   
817    {
818        FindNamedNodeVisitor fnnv("Sun");
819        root->accept(fnnv);
820
821        if (!fnnv._foundNodes.empty())
822        {
823            // set up the node tracker.
824            osgGA::NodeTrackerManipulator* tm = new osgGA::NodeTrackerManipulator;
825            tm->setTrackerMode( trackerMode );
826            tm->setRotationMode( rotationMode );
827            tm->setTrackNode( fnnv._foundNodes.front().get() );
828
829            unsigned int num = viewer.addCameraManipulator( tm );
830            viewer.selectCameraManipulator( num );
831        }
832    }   
833   
834    // create the windows and run the threads.
835    viewer.realize();
836   
837    viewer.setClearColor(osg::Vec4(0.0f,0.0f,0.0f,1.0f));
838
839    while( !viewer.done() )
840    {
841        // wait for all cull and draw threads to complete.
842        viewer.sync();
843
844        // update the scene by traversing it with the the update visitor which will
845        // call all node update callbacks and animations.
846        viewer.update();
847         
848        // fire off the cull and draw traversals of the scene.
849        viewer.frame();
850       
851    }
852   
853   
854    // wait for all cull and draw threads to complete before exit.
855    viewer.sync();
856
857    return 0;
858}// end main
Note: See TracBrowser for help on using the browser.