| 1 | #include <iostream> |
|---|
| 2 | #include <osg/Geode> |
|---|
| 3 | #include <osg/TexGen> |
|---|
| 4 | #include <osg/Texture2D> |
|---|
| 5 | #include <osg/MatrixTransform> |
|---|
| 6 | #include <osgDB/ReadFile> |
|---|
| 7 | #include <osgViewer/Viewer> |
|---|
| 8 | #include <osg/ShapeDrawable> |
|---|
| 9 | #include <osg/Material> |
|---|
| 10 | |
|---|
| 11 | extern osg::Node * CreateSimpleHierarchy( osg::Node * model ); |
|---|
| 12 | extern osg::Node * CreateAdvancedHierarchy( osg::Node * model ); |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | osg::Node * CreateGlobe( void ) |
|---|
| 16 | { |
|---|
| 17 | |
|---|
| 18 | osg::Geode * geode = new osg::Geode; |
|---|
| 19 | osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints; |
|---|
| 20 | hints->setDetailRatio( 0.3 ); |
|---|
| 21 | |
|---|
| 22 | #if 1 |
|---|
| 23 | osg::ref_ptr<osg::ShapeDrawable> shape = new osg::ShapeDrawable |
|---|
| 24 | ( new osg::Sphere(osg::Vec3(0.0f, 0.0f, 0.0f), 4.0 ), hints.get() ); |
|---|
| 25 | #else |
|---|
| 26 | osg::ref_ptr<osg::ShapeDrawable> shape = new osg::ShapeDrawable |
|---|
| 27 | ( new osg::Box( osg::Vec3(-1.0f, -1.0f, -1.0f), 2.0, 2.0, 2.0 ) ); |
|---|
| 28 | #endif |
|---|
| 29 | |
|---|
| 30 | shape->setColor(osg::Vec4(0.8f, 0.8f, 0.8f, 1.0f)); |
|---|
| 31 | |
|---|
| 32 | geode->addDrawable( shape.get() ); |
|---|
| 33 | |
|---|
| 34 | osg::StateSet * stateSet = new osg::StateSet; |
|---|
| 35 | |
|---|
| 36 | osg::Texture2D * texture = new osg::Texture2D( |
|---|
| 37 | osgDB::readImageFile("Images/land_shallow_topo_2048.jpg") |
|---|
| 38 | ); |
|---|
| 39 | |
|---|
| 40 | osg::Material * material = new osg::Material; |
|---|
| 41 | |
|---|
| 42 | material->setAmbient |
|---|
| 43 | ( osg::Material::FRONT_AND_BACK, osg::Vec4( 0.9, 0.9, 0.9, 1.0 ) ); |
|---|
| 44 | |
|---|
| 45 | material->setDiffuse |
|---|
| 46 | ( osg::Material::FRONT_AND_BACK, osg::Vec4( 0.9, 0.9, 0.9, 1.0 ) ); |
|---|
| 47 | |
|---|
| 48 | #if 1 |
|---|
| 49 | material->setSpecular |
|---|
| 50 | ( osg::Material::FRONT_AND_BACK, osg::Vec4( 0.7, 0.3, 0.3, 1.0 ) ); |
|---|
| 51 | |
|---|
| 52 | material->setShininess( osg::Material::FRONT_AND_BACK, 25 ); |
|---|
| 53 | |
|---|
| 54 | #endif |
|---|
| 55 | |
|---|
| 56 | stateSet->setAttributeAndModes( material ); |
|---|
| 57 | stateSet->setTextureAttributeAndModes( 0,texture, osg::StateAttribute::ON ); |
|---|
| 58 | |
|---|
| 59 | geode->setStateSet( stateSet ); |
|---|
| 60 | return geode; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | int main( int argc, char **argv ) |
|---|
| 64 | { |
|---|
| 65 | |
|---|
| 66 | osg::ArgumentParser arguments( &argc, argv ); |
|---|
| 67 | osgViewer::Viewer viewer( arguments ); |
|---|
| 68 | |
|---|
| 69 | bool useSimpleExample = arguments.read("-s") || arguments.read("--simple") ; |
|---|
| 70 | |
|---|
| 71 | osg::Node * model = NULL; |
|---|
| 72 | |
|---|
| 73 | if (arguments.argc()>1 && !arguments.isOption(1) ) { |
|---|
| 74 | std::string filename = arguments[1]; |
|---|
| 75 | model = osgDB::readNodeFile( filename ); |
|---|
| 76 | if ( !model ) { |
|---|
| 77 | osg::notify( osg::NOTICE ) |
|---|
| 78 | << "Error, cannot read " << filename |
|---|
| 79 | << ". Loading default earth model instead." << std::endl; |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | if( model == NULL ) |
|---|
| 84 | model = CreateGlobe( ); |
|---|
| 85 | |
|---|
| 86 | osg::Node * node = useSimpleExample ? |
|---|
| 87 | CreateSimpleHierarchy( model ): |
|---|
| 88 | CreateAdvancedHierarchy( model ); |
|---|
| 89 | |
|---|
| 90 | viewer.setSceneData( node ); |
|---|
| 91 | viewer.realize( ); |
|---|
| 92 | viewer.run(); |
|---|
| 93 | |
|---|
| 94 | return 0; |
|---|
| 95 | } |
|---|