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

Revision 3667, 30.7 kB (checked in by robert, 9 years ago)

Added write to file and use of clearnode

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