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

Revision 3563, 24.5 kB (checked in by robert, 9 years ago)

Added useage of TexEnvCombine?

  • 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#include <osg/Texture2D>
10#include <osg/Material>
11#include <osg/Light>
12#include <osg/LightSource>
13#include <osg/LightModel>
14#include <osg/TexEnv>
15#include <osg/TexEnvCombine>
16
17
18#include <osgUtil/Optimizer>
19
20#include <osgDB/Registry>
21#include <osgDB/ReadFile>
22
23#include <osgGA/NodeTrackerManipulator>
24#include <osgGA/TrackballManipulator>
25#include <osgGA/FlightManipulator>
26#include <osgGA/DriveManipulator>
27
28#include <osgProducer/Viewer>
29
30
31static osg::Vec3 defaultPos( 0.0f, 0.0f, 0.0f );
32static osg::Vec3 centerScope(0.0f, 0.0f, 0.0f);
33   
34
35
36
37
38osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime)
39{
40    // set up the animation path
41    osg::AnimationPath* animationPath = new osg::AnimationPath;
42    animationPath->setLoopMode(osg::AnimationPath::LOOP);
43   
44    int numSamples = 40;
45    float yaw = 0.0f;
46    float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f);
47    float roll = osg::inDegrees(30.0f);
48   
49    double time=0.0f;
50    double time_delta = looptime/(double)numSamples;
51    for(int i=0;i<numSamples;++i)
52    {
53        osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f));
54        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)));
55       
56        animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation));
57
58        yaw += yaw_delta;
59        time += time_delta;
60
61    }
62    return animationPath;   
63}// end createAnimationPath
64
65
66
67class SolarSystem
68{
69
70public:
71    double _radiusSun;
72    double _radiusEarth;
73    double _RorbitEarth;
74    double _tiltEarth;
75    double _rotateSpeedEarthAndMoon;
76    double _rotateSpeedEarth;
77    double _radiusMoon;
78    double _RorbitMoon;
79    double _rotateSpeedMoon;
80    double _radiusSpace;
81   
82    SolarSystem()
83    {
84        _radiusSun = 5.0;
85        _radiusEarth = 2.0;
86        _RorbitEarth = 10.0;
87        _tiltEarth = 18.0;
88        _rotateSpeedEarthAndMoon = 1.0;
89        _rotateSpeedEarth = 1.0;
90        _radiusMoon = 0.5;
91        _RorbitMoon = 2.0;
92        _rotateSpeedMoon = 1.0;
93        _radiusSpace = 300.0;
94    }
95   
96    osg::MatrixTransform* createEarthTranslationAndTilt();
97    osg::MatrixTransform* createRotation( double orbit, double speed );
98    osg::MatrixTransform* createMoonTranslation();
99    osg::Geode* createSpace( const std::string& name, const std::string& textureName );
100    osg::Geode* createPlanet( double radius, const std::string& name, const osg::Vec4& color , const std::string& textureName );
101    osg::Geode* createPlanet( double radius, const std::string& name, const osg::Vec4& color , const std::string& textureName1, const std::string& textureName2);
102    osg::Group* createSunLight();
103    osg::Group* built();
104   
105    void printParameters()
106    {
107        std::cout << "radiusSun\t= " << _radiusSun << std::endl;
108        std::cout << "radiusEarth\t= " << _radiusEarth << std::endl;
109        std::cout << "RorbitEarth\t= " << _RorbitEarth << std::endl;
110        std::cout << "tiltEarth\t= " << _tiltEarth << std::endl;
111        std::cout << "rotateSpeedEarthAndMoon= " << _rotateSpeedEarthAndMoon     << std::endl;
112        std::cout << "rotateSpeedEarth= " << _rotateSpeedEarth << std::endl;
113        std::cout << "radiusMoon\t= " << _radiusMoon << std::endl;
114        std::cout << "RorbitMoon\t= " << _RorbitMoon << std::endl;
115        std::cout << "rotateSpeedMoon\t= " << _rotateSpeedMoon << std::endl;
116        std::cout << "radiusSpace\t= " << _radiusSpace << std::endl;
117       
118    }
119   
120};  // end SolarSystem
121
122class FindNamedNodeVisitor : public osg::NodeVisitor
123{
124public:
125    FindNamedNodeVisitor(const std::string& name):
126        osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
127        _name(name) {}
128   
129    virtual void apply(osg::Node& node)
130    {
131        if (node.getName()==_name)
132        {
133            _foundNodes.push_back(&node);
134        }
135        traverse(node);
136    }
137   
138    typedef std::vector< osg::ref_ptr<osg::Node> > NodeList;
139
140    std::string _name;
141    NodeList _foundNodes;
142};
143
144int main( int argc, char **argv )
145{
146    // use an ArgumentParser object to manage the program arguments.
147    osg::ArgumentParser arguments(&argc,argv);
148
149    // set up the usage document, in case we need to print out how to use this program.
150    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of osg::AnimationPath and UpdateCallbacks for adding animation to your scenes.");
151    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
152    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
153
154    // initialize the viewer.
155    osgProducer::Viewer viewer(arguments);
156
157    // set up the value with sensible default event handlers.
158    viewer.setUpViewer(osgProducer::Viewer::ESCAPE_SETS_DONE | osgProducer::Viewer::VIEWER_MANIPULATOR | osgProducer::Viewer::STATE_MANIPULATOR);
159
160    // get details on keyboard and mouse bindings used by the viewer.
161    viewer.getUsage(*arguments.getApplicationUsage());
162
163    SolarSystem solarSystem;
164
165    while (arguments.read("--radiusSun",solarSystem._radiusSun)) { }
166    while (arguments.read("--radiusEarth",solarSystem._radiusEarth)) { }
167    while (arguments.read("--RorbitEarth",solarSystem._RorbitEarth)) { }
168    while (arguments.read("--tiltEarth",solarSystem._tiltEarth)) { }
169    while (arguments.read("--rotateSpeedEarthAndMoon",solarSystem._rotateSpeedEarthAndMoon)) { }
170    while (arguments.read("--rotateSpeedEarth",solarSystem._rotateSpeedEarth)) { }
171    while (arguments.read("--radiusMoon",solarSystem._radiusMoon)) { }
172    while (arguments.read("--RorbitMoon",solarSystem._RorbitMoon)) { }
173    while (arguments.read("--rotateSpeedMoon",solarSystem._rotateSpeedMoon)) { }
174    while (arguments.read("--radiusSpace",solarSystem._radiusSpace)) { }
175   
176   
177    osgGA::NodeTrackerManipulator::TrackerMode trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER_AND_ROTATION;
178    std::string mode;
179    while (arguments.read("--tracker-mode",mode))
180    {
181        if (mode=="NODE_CENTER_AND_ROTATION") trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER_AND_ROTATION;
182        else if (mode=="NODE_CENTER_AND_AZIM") trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER_AND_AZIM;
183        else if (mode=="NODE_CENTER") trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER;
184        else
185        {
186            std::cout<<"Unrecognized --tracker-mode option "<<mode<<", valid options are:"<<std::endl;
187            std::cout<<"    NODE_CENTER_AND_ROTATION"<<std::endl;
188            std::cout<<"    NODE_CENTER_AND_AZIM"<<std::endl;
189            std::cout<<"    NODE_CENTER"<<std::endl;
190            return 1;
191        }
192    }
193   
194   
195    osgGA::NodeTrackerManipulator::RotationMode rotationMode = osgGA::NodeTrackerManipulator::TRACKBALL;
196    while (arguments.read("--rotation-mode",mode))
197    {
198        if (mode=="TRACKBALL") rotationMode = osgGA::NodeTrackerManipulator::TRACKBALL;
199        else if (mode=="ELEVATION_AZIM") rotationMode = osgGA::NodeTrackerManipulator::ELEVATION_AZIM;
200        else
201        {
202            std::cout<<"Unrecognized --rotation-mode option "<<mode<<", valid options are:"<<std::endl;
203            std::cout<<"    TRACKBALL"<<std::endl;
204            std::cout<<"    ELEVATION_AZIM"<<std::endl;
205            return 1;
206        }
207    }
208   
209
210    solarSystem.printParameters();
211
212    // if user request help write it out to cout.
213    if (arguments.read("-h") || arguments.read("--help"))
214    {
215        std::cout << "setup the following arguments: " << std::endl;
216        std::cout << "--radiusSun: double" << std::endl;
217        std::cout << "--radiusEarth: double" << std::endl;
218        std::cout << "--RorbitEarth: double" << std::endl;
219        std::cout << "--tiltEarth: double" << std::endl;
220        std::cout << "--rotateSpeedEarthAndMoon: double" << std::endl;
221        std::cout << "--rotateSpeedEarth: double" << std::endl;
222        std::cout << "--radiusMoon: double" << std::endl;
223        std::cout << "--RorbitMoon: double" << std::endl;
224        std::cout << "--rotateSpeedMoon: double" << std::endl;
225        std::cout << "--radiusSpace: double" << std::endl;
226       
227               
228        return 1;
229    }
230
231    // any option left unread are converted into errors to write out later.
232    arguments.reportRemainingOptionsAsUnrecognized();
233
234    // report any errors if they have occured when parsing the program aguments.
235    if (arguments.errors())
236    {
237        arguments.writeErrorMessages(std::cout);
238        return 1;
239    }
240   
241   
242    osg::Group* root = new osg::Group;
243
244    osg::Group* sunLight = solarSystem.createSunLight();
245    root->addChild(sunLight);
246
247    // create the sun
248    osg::Node* sun = solarSystem.createPlanet( solarSystem._radiusSun, "Sun", osg::Vec4( 1.0, 1.0, 0, 1.0f), "" );
249    osg::StateSet* sunStateSet = sun->getOrCreateStateSet();
250    osg::Material* material = new osg::Material;
251    material->setEmission( osg::Material::FRONT_AND_BACK, osg::Vec4( 1.0f, 1.0f, 0.0f, 0.0f ) );
252    sunStateSet->setAttributeAndModes( material, osg::StateAttribute::ON );
253
254    // stick sun right under root, no transformations for the sun
255    sunLight->addChild( sun );
256
257    // create light source in the sun
258
259    // create earth and moon
260    osg::Node* earth = solarSystem.createPlanet( solarSystem._radiusEarth, "Earth", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f), "Images/land_shallow_topo_2048.jpg", "Images/land_ocean_ice_lights_2048.jpg" );
261    osg::Node* moon = solarSystem.createPlanet( solarSystem._radiusMoon, "Moon", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f), "Images/moon256128.TGA" );
262
263    // create transformations for the earthMoonGroup
264    osg::MatrixTransform* aroundSunRotation = solarSystem.createRotation( solarSystem._RorbitEarth, solarSystem._rotateSpeedEarthAndMoon );
265    osg::MatrixTransform* earthPosition = solarSystem.createEarthTranslationAndTilt();
266
267
268    //Group with earth and moon under it
269    osg::Group* earthMoonGroup = new osg::Group;
270   
271
272    //transformation to rotate the earth around itself
273    osg::MatrixTransform* earthRotationAroundItself = solarSystem.createRotation ( 0.0, solarSystem._rotateSpeedEarth );
274
275    //transformations for the moon
276    osg::MatrixTransform* moonAroundEarthXform = solarSystem.createRotation( solarSystem._RorbitMoon, solarSystem._rotateSpeedMoon );
277    osg::MatrixTransform* moonTranslation = solarSystem.createMoonTranslation();
278
279
280    moonTranslation->addChild( moon );
281    moonAroundEarthXform->addChild( moonTranslation );
282    earthMoonGroup->addChild( moonAroundEarthXform );
283
284    earthRotationAroundItself->addChild( earth );
285
286    earthMoonGroup->addChild( earthRotationAroundItself );
287
288    earthPosition->addChild( earthMoonGroup );
289
290    aroundSunRotation->addChild( earthPosition );
291
292    sunLight->addChild( aroundSunRotation );
293
294#if 0
295    // add space, but don't light it, as its not illuminated by our sun
296    osg::Node* space = solarSystem.createSpace( "Space", "Images/spacemap.jpg" );
297    space->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
298    root->addChild( space );
299#endif
300   
301    // run optimization over the scene graph
302    osgUtil::Optimizer optimzer;
303    //optimzer.optimize( dynamic_cast<osg::CoordinateSystemNode*>( root.get() ) );
304    optimzer.optimize( root );
305     
306    // set the scene to render
307    viewer.setSceneData( root );
308
309
310    // set up tracker manipulators, once for each astral body
311    {
312        FindNamedNodeVisitor fnnv("Sun");
313        root->accept(fnnv);
314
315        if (!fnnv._foundNodes.empty())
316        {
317            // set up the node tracker.
318            osgGA::NodeTrackerManipulator* tm = new osgGA::NodeTrackerManipulator;
319            tm->setTrackerMode( trackerMode );
320            tm->setRotationMode( rotationMode );
321            tm->setTrackNode( fnnv._foundNodes.front().get() );
322
323            unsigned int num = viewer.addCameraManipulator( tm );
324            viewer.selectCameraManipulator( num );
325        }
326    }   
327
328    {
329        FindNamedNodeVisitor fnnv("Moon");
330        root->accept(fnnv);
331
332        if (!fnnv._foundNodes.empty())
333        {
334            // set up the node tracker.
335            osgGA::NodeTrackerManipulator* tm = new osgGA::NodeTrackerManipulator;
336            tm->setTrackerMode( trackerMode );
337            tm->setRotationMode( rotationMode );
338            tm->setTrackNode( fnnv._foundNodes.front().get() );
339
340            unsigned int num = viewer.addCameraManipulator( tm );
341            viewer.selectCameraManipulator( num );
342        }
343    }   
344
345    {
346        FindNamedNodeVisitor fnnv("Earth");
347        root->accept(fnnv);
348
349        if (!fnnv._foundNodes.empty())
350        {
351            // set up the node tracker.
352            osgGA::NodeTrackerManipulator* tm = new osgGA::NodeTrackerManipulator;
353            tm->setTrackerMode( trackerMode );
354            tm->setRotationMode( rotationMode );
355            tm->setTrackNode( fnnv._foundNodes.front().get() );
356
357            unsigned int num = viewer.addCameraManipulator( tm );
358            viewer.selectCameraManipulator( num );
359        }
360    }   
361   
362    // create the windows and run the threads.
363    viewer.realize();
364   
365    viewer.setClearColor(osg::Vec4(0.0f,0.0f,0.0f,1.0f));
366
367    while( !viewer.done() )
368    {
369        // wait for all cull and draw threads to complete.
370        viewer.sync();
371
372        // update the scene by traversing it with the the update visitor which will
373        // call all node update callbacks and animations.
374        viewer.update();
375         
376        // fire off the cull and draw traversals of the scene.
377        viewer.frame();
378       
379    }
380   
381    // wait for all cull and draw threads to complete before exit.
382    viewer.sync();
383
384    return 0;
385}// end main
386
387
388osg::MatrixTransform* SolarSystem::createEarthTranslationAndTilt()
389{
390        osg::MatrixTransform* earthPositioned = new osg::MatrixTransform;
391        earthPositioned->setMatrix(osg::Matrix::translate(osg::Vec3( 0.0, _RorbitEarth, 0.0 ) )*
392                                     osg::Matrix::scale(1.0, 1.0, 1.0)*
393                                     osg::Matrix::rotate(osg::inDegrees( _tiltEarth ),0.0f,0.0f,1.0f));
394
395        return earthPositioned;
396}// end SolarSystem::createEarthTranslationAndTilt
397
398
399osg::MatrixTransform* SolarSystem::createRotation( double orbit, double speed )
400{
401    osg::Vec3 center( 0.0, 0.0, 0.0 );
402    float animationLength = 10.0f;
403    osg::AnimationPath* animationPath = createAnimationPath( center, orbit, animationLength );
404
405    osg::MatrixTransform* rotation = new osg::MatrixTransform;
406    rotation->setUpdateCallback( new osg::AnimationPathCallback( animationPath, 0.0f, speed ) );
407
408    return rotation;
409}// end SolarSystem::createEarthRotation
410
411
412osg::MatrixTransform* SolarSystem::createMoonTranslation()
413{
414    osg::MatrixTransform* moonPositioned = new osg::MatrixTransform;
415    moonPositioned->setMatrix(osg::Matrix::translate(osg::Vec3( 0.0, _RorbitMoon, 0.0 ) )*
416                                 osg::Matrix::scale(1.0, 1.0, 1.0)*
417                                 osg::Matrix::rotate(osg::inDegrees(0.0f),0.0f,0.0f,1.0f));
418
419    return moonPositioned;
420}// end SolarSystem::createMoonTranslation
421   
422   
423osg::Geode* SolarSystem::createSpace( const std::string& name, const std::string& textureName )
424{
425    osg::Sphere *spaceSphere = new osg::Sphere( osg::Vec3( 0.0, 0.0, 0.0 ), _radiusSpace );
426
427    osg::ShapeDrawable *sSpaceSphere = new osg::ShapeDrawable( spaceSphere );
428
429    if( !textureName.empty() )
430    {
431        osg::Image* image = osgDB::readImageFile( textureName );
432        if ( image )
433        {
434            sSpaceSphere->getOrCreateStateSet()->setTextureAttributeAndModes( 0, new osg::Texture2D( image ), osg::StateAttribute::ON );
435
436            // reset the object color to white to allow the texture to set the colour.
437            sSpaceSphere->setColor( osg::Vec4(1.0f,1.0f,1.0f,1.0f) );
438        }
439    }
440
441    osg::Geode* geodeSpace = new osg::Geode();
442    geodeSpace->setName( name );
443
444    geodeSpace->addDrawable( sSpaceSphere );
445
446    return( geodeSpace );
447
448}// end SolarSystem::createSpace
449
450   
451osg::Geode* SolarSystem::createPlanet( double radius, const std::string& name, const osg::Vec4& color , const std::string& textureName)
452{
453    // create a container that makes the sphere drawable
454    osg::Geometry *sPlanetSphere = new osg::Geometry();
455
456    {
457        // set the single colour so bind overall
458        osg::Vec4Array* colours = new osg::Vec4Array(1);
459        (*colours)[0] = color;
460        sPlanetSphere->setColorArray(colours);
461        sPlanetSphere->setColorBinding(osg::Geometry::BIND_OVERALL);
462
463
464        // now set up the coords, normals and texcoords for geometry
465        unsigned int numX = 100;
466        unsigned int numY = 50;
467        unsigned int numVertices = numX*numY;
468
469        osg::Vec3Array* coords = new osg::Vec3Array(numVertices);
470        sPlanetSphere->setVertexArray(coords);
471
472        osg::Vec3Array* normals = new osg::Vec3Array(numVertices);
473        sPlanetSphere->setNormalArray(normals);
474        sPlanetSphere->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
475
476        osg::Vec2Array* texcoords = new osg::Vec2Array(numVertices);
477        sPlanetSphere->setTexCoordArray(0,texcoords);
478        sPlanetSphere->setTexCoordArray(1,texcoords);
479
480        double delta_elevation = osg::PI / (double)(numY-1);
481        double delta_azim = 2.0*osg::PI / (double)(numX-1);
482        float delta_tx = 1.0 / (float)(numX-1);
483        float delta_ty = 1.0 / (float)(numY-1);
484
485        double elevation = -osg::PI*0.5;
486        float ty = 0.0;
487        unsigned int vert = 0;
488        unsigned j;
489        for(j=0;
490            j<numY;
491            ++j, elevation+=delta_elevation, ty+=delta_ty )
492        {
493            double azim = 0.0;
494            float tx = 0.0;
495            for(unsigned int i=0;
496                i<numX;
497                ++i, ++vert, azim+=delta_azim, tx+=delta_tx)
498            {
499                osg::Vec3 direction(cos(azim)*cos(elevation), sin(azim)*cos(elevation), sin(elevation));
500                (*coords)[vert].set(direction*radius);
501                (*normals)[vert].set(direction);
502                (*texcoords)[vert].set(tx,ty);
503            }
504        }
505
506        for(j=0;
507            j<numY-1;
508            ++j)
509        {
510            unsigned int curr_row = j*numX;
511            unsigned int next_row = curr_row+numX;
512            osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(GL_QUAD_STRIP);
513            for(unsigned int i=0;
514                i<numX;
515                ++i)
516            {
517                elements->push_back(next_row + i);
518                elements->push_back(curr_row + i);
519            }
520            sPlanetSphere->addPrimitiveSet(elements);
521        }
522    }
523   
524
525    // set the object color
526    //sPlanetSphere->setColor( color );
527
528    // create a geode object to as a container for our drawable sphere object
529    osg::Geode* geodePlanet = new osg::Geode();
530    geodePlanet->setName( name );
531
532    if( !textureName.empty() )
533    {
534        osg::Image* image = osgDB::readImageFile( textureName );
535        if ( image )
536        {
537            geodePlanet->getOrCreateStateSet()->setTextureAttributeAndModes( 0, new osg::Texture2D( image ), osg::StateAttribute::ON );
538
539            // reset the object color to white to allow the texture to set the colour.
540            //sPlanetSphere->setColor( osg::Vec4(1.0f,1.0f,1.0f,1.0f) );
541        }
542    }
543
544    // add our drawable sphere to the geode container
545    geodePlanet->addDrawable( sPlanetSphere );
546
547    return( geodePlanet );
548
549}// end SolarSystem::createPlanet
550   
551osg::Geode* SolarSystem::createPlanet( double radius, const std::string& name, const osg::Vec4& color , const std::string& textureName1, const std::string& textureName2)
552{
553    osg::Geode* geodePlanet = createPlanet( radius, name, color , textureName1);
554   
555    if( !textureName2.empty() )
556    {
557        osg::Image* image = osgDB::readImageFile( textureName2 );
558        if ( image )
559        {
560            osg::StateSet* stateset = geodePlanet->getOrCreateStateSet();
561           
562            osg::TexEnvCombine* texenv = new osg::TexEnvCombine;
563           
564            texenv->setCombine_RGB(osg::TexEnvCombine::INTERPOLATE);
565            texenv->setSource0_RGB(osg::TexEnvCombine::PREVIOUS);
566            texenv->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR);
567            texenv->setSource1_RGB(osg::TexEnvCombine::TEXTURE);
568            texenv->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR);
569            texenv->setSource2_RGB(osg::TexEnvCombine::PRIMARY_COLOR);
570            texenv->setOperand2_RGB(osg::TexEnvCombine::SRC_COLOR);
571/*
572            texenv->setCombine_Alpha(osg::TexEnvCombine::INTERPOLATE);
573            texenv->setSource0_Alpha(osg::TexEnvCombine::PREVIOUS);
574            texenv->setOperand0_Alpha(osg::TexEnvCombine::SRC_ALPHA);
575            texenv->setSource1_Alpha(osg::TexEnvCombine::TEXTURE);
576            texenv->setOperand1_Alpha(osg::TexEnvCombine::ONE_MINUS_SRC_ALPHA);
577            texenv->setSource2_RGB(osg::TexEnvCombine::PRIMARY_COLOR);
578            texenv->setOperand2_RGB(osg::TexEnvCombine::SRC_ALPHA);
579*/
580            stateset->setTextureAttribute( 1, texenv );
581            stateset->setTextureAttributeAndModes( 1, new osg::Texture2D( image ), osg::StateAttribute::ON );
582        }
583    }
584
585    return( geodePlanet );
586
587}// end SolarSystem::createPlanet
588
589osg::Group* SolarSystem::createSunLight()
590{
591
592    osg::LightSource* sunLightSource = new osg::LightSource;
593
594    osg::Light* sunLight = sunLightSource->getLight();
595    sunLight->setPosition( osg::Vec4( 0.0f, 0.0f, 0.0f, 1.0f ) );
596    sunLight->setAmbient( osg::Vec4( 0.0f, 0.0f, 0.0f, 1.0f ) );
597
598    sunLightSource->setLight( sunLight );
599    sunLightSource->setLocalStateSetModes( osg::StateAttribute::ON );
600    sunLightSource->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::ON);
601
602    osg::LightModel* lightModel = new osg::LightModel;
603    lightModel->setAmbientIntensity(osg::Vec4(0.0f,0.0f,0.0f,1.0f));
604    sunLightSource->getOrCreateStateSet()->setAttribute(lightModel);
605
606
607    return sunLightSource;
608}// end SolarSystem::createSunLight
609   
610
611/*
612osg::Group* SolarSystem::built()
613{
614    osg::Group* thisSystem = new osg::Group;
615
616    // create light source in the sun
617    osg::Group* sunLight = createSunLight();
618    thisSystem->addChild( sunLight );
619
620
621    // create the sun
622    osg::Node* sun = createPlanet( _radiusSun, "Sun", osg::Vec4( 0, 0, 0, 1.0f), "" );
623    osg::StateSet* sunStateSet = sun->getOrCreateStateSet();
624    osg::Material* material = new osg::Material;
625    material->setEmission( osg::Material::FRONT_AND_BACK, osg::Vec4( 1.0f, 1.0f, 0.0f, 0.0f ) );
626    sunStateSet->setAttributeAndModes( material, osg::StateAttribute::ON );
627
628    if( !sun )
629    {
630        std::cout << "Sonne konnte nicht erstellt werden!" << std::endl;
631        exit(0);
632    }
633    sun->setStateSet( sunStateSet );
634
635    // stick sun right under root, no transformations for the sun
636    sunLight->addChild(sun);
637
638
639    //creating right side of the graph with earth and moon and the rotations above it
640
641    // create earth and moon
642    osg::Node* earth = createPlanet( _radiusEarth, "Earth", osg::Vec4( 0.0f, 0.0f, 1.0f, 1.0f), "Images/land_shallow_topo_2048.jpg" );
643    osg::Node* moon = createPlanet( _radiusMoon, "Moon", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f), "Images/moon256128.TGA" );
644
645    // create transformations for the earthMoonGroup
646    osg::MatrixTransform* aroundSunRotation = createRotation( _RorbitEarth, _rotateSpeedEarthAndMoon );
647    osg::MatrixTransform* earthPosition = createEarthTranslationAndTilt( _RorbitEarth, _tiltEarth );
648
649    //Group with earth and moon under it
650    osg::Group* earthMoonGroup = new osg::Group;
651
652    //transformation to rotate the earth around itself
653    osg::MatrixTransform* earthRotationAroundItself = createRotation ( 0.0, _rotateSpeedEarth );
654
655    //transformations for the moon
656    osg::MatrixTransform* moonAroundEarthXform = createRotation( _RorbitMoon, _rotateSpeedMoon );
657    osg::MatrixTransform* moonTranslation = createMoonTranslation( _RorbitMoon );
658
659
660    moonTranslation->addChild( moon );
661    moonAroundEarthXform->addChild( moonTranslation );
662    earthMoonGroup->addChild( moonAroundEarthXform );
663
664    earthRotationAroundItself->addChild( earth );
665
666    earthMoonGroup->addChild( earthRotationAroundItself );
667
668    earthPosition->addChild( earthMoonGroup );
669
670
671    aroundSunRotation->addChild( earthPosition );
672
673    sunLight->addChild( aroundSunRotation );
674
675    // add space, but don't light it, as its not illuminated by our sun
676    osg::Node* space = createSpace( _radiusSpace, "Space", "Images/spacemap.jpg" );
677    space->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
678    thisSystem->addChild( space );
679
680    return( thisSystem );
681}// end SolarSystem::built()
682*/
Note: See TracBrowser for help on using the browser.