Show
Ignore:
Timestamp:
10/29/04 17:42:51 (9 years ago)
Author:
robert
Message:

Added multi-textured city illumination

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgplanets/osgplanets.cpp

    r3558 r3562  
    1212#include <osg/LightSource> 
    1313#include <osg/LightModel> 
     14#include <osg/TexEnv> 
     15#include <osg/TexEnvCombine> 
    1416 
    1517 
     
    9799    osg::Geode* createSpace( const std::string& name, const std::string& textureName ); 
    98100    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); 
    99102    osg::Group* createSunLight(); 
    100103    osg::Group* built(); 
     
    255258 
    256259    // create earth and moon 
    257     osg::Node* earth = solarSystem.createPlanet( solarSystem._radiusEarth, "Earth", osg::Vec4( 0.0f, 0.0f, 1.0f, 1.0f), "Images/land_shallow_topo_2048.jpg" ); 
     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" ); 
    258261    osg::Node* moon = solarSystem.createPlanet( solarSystem._radiusMoon, "Moon", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f), "Images/moon256128.TGA" ); 
    259262 
     
    446449 
    447450     
    448 osg::Geode* SolarSystem::createPlanet( double radius, const std::string& name, const osg::Vec4& color , const std::string& textureName ) 
    449 { 
    450     // create a cube shape 
    451     osg::Sphere *planetSphere = new osg::Sphere( osg::Vec3( 0.0, 0.0, 0.0 ), radius ); 
    452  
     451osg::Geode* SolarSystem::createPlanet( double radius, const std::string& name, const osg::Vec4& color , const std::string& textureName) 
     452{ 
    453453    // create a container that makes the sphere drawable 
    454     osg::ShapeDrawable *sPlanetSphere = new osg::ShapeDrawable( planetSphere ); 
     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     
    455524 
    456525    // set the object color 
    457     sPlanetSphere->setColor( color ); 
    458  
    459     if( !textureName.empty() ) 
    460     { 
    461         osg::Image* image = osgDB::readImageFile( textureName ); 
    462         if ( image ) 
    463         { 
    464             sPlanetSphere->getOrCreateStateSet()->setTextureAttributeAndModes( 0, new osg::Texture2D( image ), osg::StateAttribute::ON ); 
    465  
    466             // reset the object color to white to allow the texture to set the colour. 
    467             sPlanetSphere->setColor( osg::Vec4(1.0f,1.0f,1.0f,1.0f) ); 
    468         } 
    469     } 
    470  
     526    //sPlanetSphere->setColor( color ); 
    471527 
    472528    // create a geode object to as a container for our drawable sphere object 
     
    474530    geodePlanet->setName( name ); 
    475531 
     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 
    476544    // add our drawable sphere to the geode container 
    477545    geodePlanet->addDrawable( sPlanetSphere ); 
     
    481549}// end SolarSystem::createPlanet 
    482550     
     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            stateset->setTextureAttribute( 1, new osg::TexEnv(osg::TexEnv::ADD)); 
     562            stateset->setTextureAttributeAndModes( 1, new osg::Texture2D( image ), osg::StateAttribute::ON ); 
     563        } 
     564    } 
     565 
     566    return( geodePlanet ); 
     567 
     568}// end SolarSystem::createPlanet 
     569 
    483570osg::Group* SolarSystem::createSunLight() 
    484571{