From Wojciech Lewandowski, "ref_ptr usage changes made on 9th of June broke VirtualProgram used in osgShaderCompositor example. Taking the opportunity I modified the code a little to use earth sphere as default model. Cow.osg used previously was not looking good in advanced example. I also changed default to advanced example, because its more informative with labels describing virtual programs used to render the objects."
This commit is contained in:
parent
590aabb4fa
commit
ae440bfaef
@ -1,17 +1,11 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <osg/Geode>
|
#include <osg/Geode>
|
||||||
#include <osg/TexGen>
|
#include <osg/TexGen>
|
||||||
#include <osg/Material>
|
|
||||||
#include <osg/Texture2D>
|
#include <osg/Texture2D>
|
||||||
#include <osg/MatrixTransform>
|
#include <osg/MatrixTransform>
|
||||||
#include <osg/ShapeDrawable>
|
|
||||||
#include <osg/BlendFunc>
|
#include <osg/BlendFunc>
|
||||||
#include <osgText/Text>
|
#include <osgText/Text>
|
||||||
#include <osgDB/ReadFile>
|
#include <osgDB/ReadFile>
|
||||||
#include <osgViewer/Viewer>
|
|
||||||
#include <osgViewer/View>
|
|
||||||
#include <osgViewer/CompositeViewer>
|
|
||||||
#include <osgGA/TrackballManipulator>
|
|
||||||
|
|
||||||
#include "VirtualProgram.h"
|
#include "VirtualProgram.h"
|
||||||
|
|
||||||
@ -139,61 +133,6 @@ char PerFragmentDirectionalLightingFragmentShaderSource[] =
|
|||||||
" pow( NdotHV, gl_FrontMaterial.shininess ); \n" //17
|
" pow( NdotHV, gl_FrontMaterial.shininess ); \n" //17
|
||||||
"} \n";//18
|
"} \n";//18
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
osg::Node * CreateModel( const char * file )
|
|
||||||
{
|
|
||||||
if ( file ) {
|
|
||||||
osg::Node * node = NULL;
|
|
||||||
node = osgDB::readNodeFile( file );
|
|
||||||
if( node )
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
// File not found - create textured sphere
|
|
||||||
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;
|
|
||||||
|
|
||||||
osg::Texture2D * texture = new osg::Texture2D(
|
|
||||||
osgDB::readImageFile("Images/land_shallow_topo_2048.jpg")
|
|
||||||
);
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// Convenience method to simplify code a little ...
|
// Convenience method to simplify code a little ...
|
||||||
void SetVirtualProgramShader( VirtualProgram * virtualProgram,
|
void SetVirtualProgramShader( VirtualProgram * virtualProgram,
|
||||||
@ -240,9 +179,8 @@ void AddLabel( osg::Group * group, const std::string & label, float offset )
|
|||||||
text->setText( label );
|
text->setText( label );
|
||||||
}
|
}
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
osg::Node * CreateAdvancedHierarchy( const char * file = NULL )
|
osg::Node * CreateAdvancedHierarchy( osg::Node * model )
|
||||||
{
|
{
|
||||||
osg::Node * model = CreateModel( file );
|
|
||||||
if( !model ) return NULL;
|
if( !model ) return NULL;
|
||||||
float offset = model->getBound().radius() * 1.3; // diameter
|
float offset = model->getBound().radius() * 1.3; // diameter
|
||||||
|
|
||||||
|
@ -11,12 +11,10 @@
|
|||||||
using osgCandidate::VirtualProgram;
|
using osgCandidate::VirtualProgram;
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
osg::Node * CreateSimpleHierarchy( const char * file )
|
osg::Node * CreateSimpleHierarchy( osg::Node * node )
|
||||||
{
|
{
|
||||||
osg::Node * node = osgDB::readNodeFile( file );
|
|
||||||
if( !node ) return NULL;
|
if( !node ) return NULL;
|
||||||
float r = node->getBound().radius(); // diameter
|
float r = node->getBound().radius(); // diameter
|
||||||
|
|
||||||
osg::Group * root = new osg::Group();
|
osg::Group * root = new osg::Group();
|
||||||
osg::Group * group = new osg::Group();
|
osg::Group * group = new osg::Group();
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ osg::Shader * VirtualProgram::setShader
|
|||||||
shaderCurrent = shaderNew;
|
shaderCurrent = shaderNew;
|
||||||
}
|
}
|
||||||
|
|
||||||
return shaderCurrent.release();
|
return shaderCurrent.get();
|
||||||
}
|
}
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
void VirtualProgram::apply( osg::State & state ) const
|
void VirtualProgram::apply( osg::State & state ) const
|
||||||
|
@ -5,33 +5,87 @@
|
|||||||
#include <osg/MatrixTransform>
|
#include <osg/MatrixTransform>
|
||||||
#include <osgDB/ReadFile>
|
#include <osgDB/ReadFile>
|
||||||
#include <osgViewer/Viewer>
|
#include <osgViewer/Viewer>
|
||||||
|
#include <osg/ShapeDrawable>
|
||||||
|
#include <osg/Material>
|
||||||
|
|
||||||
extern osg::Node * CreateSimpleHierarchy( const char * file );
|
extern osg::Node * CreateSimpleHierarchy( osg::Node * model );
|
||||||
extern osg::Node * CreateAdvancedHierarchy( const char * file );
|
extern osg::Node * CreateAdvancedHierarchy( osg::Node * model );
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
osg::Node * CreateGlobe( void )
|
||||||
|
{
|
||||||
|
// File not found - create textured sphere
|
||||||
|
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;
|
||||||
|
|
||||||
|
osg::Texture2D * texture = new osg::Texture2D(
|
||||||
|
osgDB::readImageFile("Images/land_shallow_topo_2048.jpg")
|
||||||
|
);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
int main( int argc, char **argv )
|
int main( int argc, char **argv )
|
||||||
{
|
{
|
||||||
// construct the viewer.
|
// construct the viewer.
|
||||||
osg::ArgumentParser arguments( &argc, argv );
|
osg::ArgumentParser arguments( &argc, argv );
|
||||||
osgViewer::Viewer viewer( arguments );
|
osgViewer::Viewer viewer( arguments );
|
||||||
|
|
||||||
std::string filename = "cow.osg";
|
bool useSimpleExample = arguments.read("-s") || arguments.read("--simple") ;
|
||||||
bool useAdvancedTechnique = arguments.read("-a") || arguments.read("--advanced") ;
|
|
||||||
|
|
||||||
if (arguments.argc()>1)
|
osg::Node * model = NULL;
|
||||||
{
|
|
||||||
if (!arguments.isOption(1)) filename = arguments[1];
|
if (arguments.argc()>1 && !arguments.isOption(1) ) {
|
||||||
|
std::string filename = arguments[1];
|
||||||
|
model = osgDB::readNodeFile( filename );
|
||||||
|
if ( !model ) {
|
||||||
|
osg::notify( osg::NOTICE )
|
||||||
|
<< "Error, cannot read " << filename
|
||||||
|
<< ". Loading default earth model instead." << std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// osg::Node * node = CreateSimpleHierarchy( file );
|
if( model == NULL )
|
||||||
osg::Node * node = useAdvancedTechnique ?
|
model = CreateGlobe( );
|
||||||
CreateAdvancedHierarchy( filename.c_str() ) :
|
|
||||||
CreateSimpleHierarchy( filename.c_str() );
|
|
||||||
|
|
||||||
if ( !node ) {
|
osg::Node * node = useSimpleExample ?
|
||||||
osg::notify( osg::NOTICE ) << "Error, cannot read " << filename << std::endl;
|
CreateSimpleHierarchy( model ):
|
||||||
return 1;
|
CreateAdvancedHierarchy( model );
|
||||||
}
|
|
||||||
|
|
||||||
viewer.setSceneData( node );
|
viewer.setSceneData( node );
|
||||||
viewer.realize( );
|
viewer.realize( );
|
||||||
|
Loading…
Reference in New Issue
Block a user