2009-05-29 16:24:52 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <osg/Geode>
|
|
|
|
#include <osg/TexGen>
|
|
|
|
#include <osg/Texture2D>
|
|
|
|
#include <osg/MatrixTransform>
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
#include <osgViewer/Viewer>
|
2009-10-10 17:53:10 +08:00
|
|
|
#include <osg/ShapeDrawable>
|
|
|
|
#include <osg/Material>
|
2009-05-29 16:24:52 +08:00
|
|
|
|
2009-10-10 17:53:10 +08:00
|
|
|
extern osg::Node * CreateSimpleHierarchy( osg::Node * model );
|
|
|
|
extern osg::Node * CreateAdvancedHierarchy( osg::Node * model );
|
2009-05-29 16:24:52 +08:00
|
|
|
|
2009-10-10 17:53:10 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
osg::Node * CreateGlobe( void )
|
|
|
|
{
|
2015-10-22 22:14:53 +08:00
|
|
|
// File not found - create textured sphere
|
2009-10-10 17:53:10 +08:00
|
|
|
osg::Geode * geode = new osg::Geode;
|
|
|
|
osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints;
|
|
|
|
hints->setDetailRatio( 0.3 );
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
osg::ref_ptr<osg::ShapeDrawable> shape = new osg::ShapeDrawable
|
|
|
|
( new osg::Sphere(osg::Vec3(0.0f, 0.0f, 0.0f), 4.0 ), hints.get() );
|
|
|
|
#else
|
|
|
|
osg::ref_ptr<osg::ShapeDrawable> shape = new osg::ShapeDrawable
|
|
|
|
( new osg::Box( osg::Vec3(-1.0f, -1.0f, -1.0f), 2.0, 2.0, 2.0 ) );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
shape->setColor(osg::Vec4(0.8f, 0.8f, 0.8f, 1.0f));
|
|
|
|
|
|
|
|
geode->addDrawable( shape.get() );
|
|
|
|
|
|
|
|
osg::StateSet * stateSet = new osg::StateSet;
|
|
|
|
|
2015-10-22 22:14:53 +08:00
|
|
|
osg::Texture2D * texture = new osg::Texture2D(
|
|
|
|
osgDB::readRefImageFile("Images/land_shallow_topo_2048.jpg")
|
2009-10-10 17:53:10 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
osg::Material * material = new osg::Material;
|
|
|
|
|
|
|
|
material->setAmbient
|
|
|
|
( osg::Material::FRONT_AND_BACK, osg::Vec4( 0.9, 0.9, 0.9, 1.0 ) );
|
|
|
|
|
|
|
|
material->setDiffuse
|
|
|
|
( osg::Material::FRONT_AND_BACK, osg::Vec4( 0.9, 0.9, 0.9, 1.0 ) );
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
material->setSpecular
|
|
|
|
( osg::Material::FRONT_AND_BACK, osg::Vec4( 0.7, 0.3, 0.3, 1.0 ) );
|
|
|
|
|
|
|
|
material->setShininess( osg::Material::FRONT_AND_BACK, 25 );
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
stateSet->setAttributeAndModes( material );
|
|
|
|
stateSet->setTextureAttributeAndModes( 0,texture, osg::StateAttribute::ON );
|
|
|
|
|
|
|
|
geode->setStateSet( stateSet );
|
|
|
|
return geode;
|
|
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2009-05-29 16:24:52 +08:00
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
// construct the viewer.
|
|
|
|
osg::ArgumentParser arguments( &argc, argv );
|
|
|
|
osgViewer::Viewer viewer( arguments );
|
|
|
|
|
2009-10-10 17:53:10 +08:00
|
|
|
bool useSimpleExample = arguments.read("-s") || arguments.read("--simple") ;
|
|
|
|
|
2015-10-22 22:14:53 +08:00
|
|
|
osg::ref_ptr<osg::Node> model;
|
2009-05-29 16:24:52 +08:00
|
|
|
|
2015-10-22 22:14:53 +08:00
|
|
|
if (arguments.argc()>1 && !arguments.isOption(1) )
|
|
|
|
{
|
2009-10-10 17:53:10 +08:00
|
|
|
std::string filename = arguments[1];
|
2015-10-22 22:14:53 +08:00
|
|
|
model = osgDB::readRefNodeFile( filename );
|
2009-10-10 17:53:10 +08:00
|
|
|
if ( !model ) {
|
2015-10-22 22:14:53 +08:00
|
|
|
osg::notify( osg::NOTICE )
|
|
|
|
<< "Error, cannot read " << filename
|
2009-10-10 17:53:10 +08:00
|
|
|
<< ". Loading default earth model instead." << std::endl;
|
|
|
|
}
|
2009-05-29 16:24:52 +08:00
|
|
|
}
|
|
|
|
|
2009-10-10 17:53:10 +08:00
|
|
|
if( model == NULL )
|
|
|
|
model = CreateGlobe( );
|
2009-05-29 16:24:52 +08:00
|
|
|
|
2015-10-22 22:14:53 +08:00
|
|
|
osg::ref_ptr<osg::Node> node = useSimpleExample ?
|
|
|
|
CreateSimpleHierarchy( model.get() ):
|
|
|
|
CreateAdvancedHierarchy( model.get() );
|
2009-05-29 16:24:52 +08:00
|
|
|
|
|
|
|
viewer.setSceneData( node );
|
|
|
|
viewer.realize( );
|
|
|
|
viewer.run();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|