| 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" ); |
| 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 | |
| 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 ); |
| | 551 | osg::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 | |