From Mike Weiblen, ported osgshaders example from osgGL2 to core osg based.

This commit is contained in:
Robert Osfield 2005-04-07 20:20:09 +00:00
parent 923048fdb6
commit c8a3198129
6 changed files with 95 additions and 122 deletions

View File

@ -1,5 +1,5 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2004 Robert Osfield /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2004 Robert Osfield
* Copyright (C) 2003-2004 3Dlabs Inc. Ltd. * Copyright (C) 2003-2005 3Dlabs Inc. Ltd.
* *
* This application is open source and may be redistributed and/or modified * This application is open source and may be redistributed and/or modified
* freely and without restriction, both in commericial and non commericial applications, * freely and without restriction, both in commericial and non commericial applications,
@ -10,8 +10,8 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/ */
/* file: examples/osgshaders/GL2Scene.cpp /* file: examples/osgglsl/GL2Scene.cpp
* author: Mike Weiblen 2004-11-09 * author: Mike Weiblen 2005-03-30
* *
* Compose a scene of several instances of a model, with a different * Compose a scene of several instances of a model, with a different
* OpenGL Shading Language shader applied to each. * OpenGL Shading Language shader applied to each.
@ -33,7 +33,10 @@
#include <osgDB/ReadFile> #include <osgDB/ReadFile>
#include <osgDB/FileUtils> #include <osgDB/FileUtils>
#include <osgUtil/Optimizer> #include <osgUtil/Optimizer>
#include <osgGL2/ProgramObject>
#include <osg/Program>
#include <osg/Shader>
#include <osg/Uniform>
#include "GL2Scene.h" #include "GL2Scene.h"
#include "Noise.h" #include "Noise.h"
@ -190,12 +193,12 @@ ModelInstance()
// load source from a file. // load source from a file.
static void static void
LoadShaderSource( osgGL2::ShaderObject* obj, const std::string& fileName ) LoadShaderSource( osg::Shader* shader, const std::string& fileName )
{ {
std::string fqFileName = osgDB::findDataFile(fileName); std::string fqFileName = osgDB::findDataFile(fileName);
if( fqFileName.length() != 0 ) if( fqFileName.length() != 0 )
{ {
obj->loadShaderSourceFromFile( fqFileName.c_str() ); shader->loadShaderSourceFromFile( fqFileName.c_str() );
} }
else else
{ {
@ -207,17 +210,22 @@ LoadShaderSource( osgGL2::ShaderObject* obj, const std::string& fileName )
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// rude but convenient globals // rude but convenient globals
static osgGL2::ProgramObject* BlockyProgObj; static osg::Program* BlockyProgram;
static osgGL2::ShaderObject* BlockyVertObj; static osg::Shader* BlockyVertObj;
static osgGL2::ShaderObject* BlockyFragObj; static osg::Shader* BlockyFragObj;
static osgGL2::ProgramObject* ErodedProgObj; static osg::Program* ErodedProgram;
static osgGL2::ShaderObject* ErodedVertObj; static osg::Shader* ErodedVertObj;
static osgGL2::ShaderObject* ErodedFragObj; static osg::Shader* ErodedFragObj;
static osgGL2::ProgramObject* MarbleProgObj; static osg::Program* MarbleProgram;
static osgGL2::ShaderObject* MarbleVertObj; static osg::Shader* MarbleVertObj;
static osgGL2::ShaderObject* MarbleFragObj; static osg::Shader* MarbleFragObj;
static osg::Uniform* OffsetUniform;
static osg::Uniform* SineUniform;
static osg::Uniform* Color1Uniform;
static osg::Uniform* Color2Uniform;
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// for demo simplicity, this one callback animates all the shaders. // for demo simplicity, this one callback animates all the shaders.
@ -236,13 +244,10 @@ class AnimateCallback: public osg::NodeCallback
float v01 = 0.5f * sine + 0.5f; // 0 -> 1 float v01 = 0.5f * sine + 0.5f; // 0 -> 1
float v10 = 1.0f - v01; // 1 -> 0 float v10 = 1.0f - v01; // 1 -> 0
ErodedProgObj->setUniform( "Offset", osg::Vec3(0.505f, 0.8f*v01, 0.0f) ); OffsetUniform->set( osg::Vec3(0.505f, 0.8f*v01, 0.0f) );
SineUniform->set( sine );
MarbleProgObj->setUniform( "Offset", osg::Vec3(0.505f, 0.8f*v01, 0.0f) ); Color1Uniform->set( osg::Vec3(v10, 0.0f, 0.0f) );
Color1Uniform->set( osg::Vec3(v01, v01, v10) );
BlockyProgObj->setUniform( "Sine", sine );
BlockyProgObj->setUniform( "Color1", osg::Vec3(v10, 0.0f, 0.0f) );
BlockyProgObj->setUniform( "Color2", osg::Vec3(v01, v01, v10) );
} }
traverse(node, nv); traverse(node, nv);
} }
@ -267,41 +272,57 @@ GL2Scene::buildScene()
rootNode = new osg::Group; rootNode = new osg::Group;
rootNode->setUpdateCallback( new AnimateCallback ); rootNode->setUpdateCallback( new AnimateCallback );
// attach some Uniforms to the root, to be inherited by Programs.
{
OffsetUniform = new osg::Uniform( "Offset", osg::Vec3(0.0f, 0.0f, 0.0f) );
SineUniform = new osg::Uniform( "Sine", 0.0f );
Color1Uniform = new osg::Uniform( "Color1", osg::Vec3(0.0f, 0.0f, 0.0f) );
Color2Uniform = new osg::Uniform( "Color2", osg::Vec3(0.0f, 0.0f, 0.0f) );
osg::StateSet* ss = rootNode->getOrCreateStateSet();
ss->addUniform( OffsetUniform );
ss->addUniform( SineUniform );
ss->addUniform( Color1Uniform );
ss->addUniform( Color2Uniform );
}
// the simple Microshader (its source appears earlier in this file) // the simple Microshader (its source appears earlier in this file)
{ {
osg::StateSet* ss = ModelInstance(); osg::StateSet* ss = ModelInstance();
osgGL2::ProgramObject* progObj = new osgGL2::ProgramObject; osg::Program* program = new osg::Program;
_progObjList.push_back( progObj ); _programList.push_back( program );
progObj->addShader( new osgGL2::ShaderObject( program->addShader( new osg::Shader( osg::Shader::VERTEX, microshaderVertSource ) );
osgGL2::ShaderObject::VERTEX, microshaderVertSource ) ); program->addShader( new osg::Shader( osg::Shader::FRAGMENT, microshaderFragSource ) );
progObj->addShader( new osgGL2::ShaderObject( ss->setAttributeAndModes( program, osg::StateAttribute::ON );
osgGL2::ShaderObject::FRAGMENT, microshaderFragSource ) );
ss->setAttributeAndModes( progObj, osg::StateAttribute::ON );
} }
// the "blocky" shader, a simple animation test // the "blocky" shader, a simple animation test
{ {
osg::StateSet* ss = ModelInstance(); osg::StateSet* ss = ModelInstance();
BlockyProgObj = new osgGL2::ProgramObject; BlockyProgram = new osg::Program;
_progObjList.push_back( BlockyProgObj ); _programList.push_back( BlockyProgram );
BlockyVertObj = new osgGL2::ShaderObject( osgGL2::ShaderObject::VERTEX ); BlockyVertObj = new osg::Shader( osg::Shader::VERTEX );
BlockyFragObj = new osgGL2::ShaderObject( osgGL2::ShaderObject::FRAGMENT ); BlockyFragObj = new osg::Shader( osg::Shader::FRAGMENT );
BlockyProgObj->addShader( BlockyFragObj ); BlockyProgram->addShader( BlockyFragObj );
BlockyProgObj->addShader( BlockyVertObj ); BlockyProgram->addShader( BlockyVertObj );
ss->setAttributeAndModes(BlockyProgObj, osg::StateAttribute::ON); ss->setAttributeAndModes(BlockyProgram, osg::StateAttribute::ON);
} }
// the "eroded" shader, uses a noise texture to discard fragments // the "eroded" shader, uses a noise texture to discard fragments
{ {
osg::StateSet* ss = ModelInstance(); osg::StateSet* ss = ModelInstance();
ss->setTextureAttribute(TEXUNIT_NOISE, noiseTexture); ss->setTextureAttribute(TEXUNIT_NOISE, noiseTexture);
ErodedProgObj = new osgGL2::ProgramObject; ErodedProgram = new osg::Program;
_progObjList.push_back( ErodedProgObj ); _programList.push_back( ErodedProgram );
ErodedVertObj = new osgGL2::ShaderObject( osgGL2::ShaderObject::VERTEX ); ErodedVertObj = new osg::Shader( osg::Shader::VERTEX );
ErodedFragObj = new osgGL2::ShaderObject( osgGL2::ShaderObject::FRAGMENT ); ErodedFragObj = new osg::Shader( osg::Shader::FRAGMENT );
ErodedProgObj->addShader( ErodedFragObj ); ErodedProgram->addShader( ErodedFragObj );
ErodedProgObj->addShader( ErodedVertObj ); ErodedProgram->addShader( ErodedVertObj );
ss->setAttributeAndModes(ErodedProgObj, osg::StateAttribute::ON); ss->setAttributeAndModes(ErodedProgram, osg::StateAttribute::ON);
ss->addUniform( new osg::Uniform("LightPosition", osg::Vec3(0.0f, 0.0f, 4.0f)) );
ss->addUniform( new osg::Uniform("Scale", 1.0f) );
ss->addUniform( new osg::Uniform("sampler3d", TEXUNIT_NOISE) );
} }
// the "marble" shader, uses two textures // the "marble" shader, uses two textures
@ -309,13 +330,16 @@ GL2Scene::buildScene()
osg::StateSet* ss = ModelInstance(); osg::StateSet* ss = ModelInstance();
ss->setTextureAttribute(TEXUNIT_NOISE, noiseTexture); ss->setTextureAttribute(TEXUNIT_NOISE, noiseTexture);
ss->setTextureAttribute(TEXUNIT_SINE, sineTexture); ss->setTextureAttribute(TEXUNIT_SINE, sineTexture);
MarbleProgObj = new osgGL2::ProgramObject; MarbleProgram = new osg::Program;
_progObjList.push_back( MarbleProgObj ); _programList.push_back( MarbleProgram );
MarbleVertObj = new osgGL2::ShaderObject( osgGL2::ShaderObject::VERTEX ); MarbleVertObj = new osg::Shader( osg::Shader::VERTEX );
MarbleFragObj = new osgGL2::ShaderObject( osgGL2::ShaderObject::FRAGMENT ); MarbleFragObj = new osg::Shader( osg::Shader::FRAGMENT );
MarbleProgObj->addShader( MarbleFragObj ); MarbleProgram->addShader( MarbleFragObj );
MarbleProgObj->addShader( MarbleVertObj ); MarbleProgram->addShader( MarbleVertObj );
ss->setAttributeAndModes(MarbleProgObj, osg::StateAttribute::ON); ss->setAttributeAndModes(MarbleProgram, osg::StateAttribute::ON);
ss->addUniform( new osg::Uniform("Noise", TEXUNIT_NOISE) );
ss->addUniform( new osg::Uniform("Sine", TEXUNIT_SINE) );
} }
#ifdef INTERNAL_3DLABS //[ #ifdef INTERNAL_3DLABS //[
@ -349,8 +373,6 @@ GL2Scene::~GL2Scene()
{ {
} }
// mew 2003-09-19 : This way of configuring the shaders is temporary,
// pending a move to an osgFX-based approach.
void void
GL2Scene::reloadShaderSource() GL2Scene::reloadShaderSource()
{ {
@ -361,19 +383,14 @@ GL2Scene::reloadShaderSource()
LoadShaderSource( ErodedVertObj, "shaders/eroded.vert" ); LoadShaderSource( ErodedVertObj, "shaders/eroded.vert" );
LoadShaderSource( ErodedFragObj, "shaders/eroded.frag" ); LoadShaderSource( ErodedFragObj, "shaders/eroded.frag" );
ErodedProgObj->setUniform( "LightPosition", osg::Vec3(0.0f, 0.0f, 4.0f) );
ErodedProgObj->setUniform( "Scale", 1.0f );
ErodedProgObj->setSampler( "sampler3d", TEXUNIT_NOISE );
LoadShaderSource( MarbleVertObj, "shaders/marble.vert" ); LoadShaderSource( MarbleVertObj, "shaders/marble.vert" );
LoadShaderSource( MarbleFragObj, "shaders/marble.frag" ); LoadShaderSource( MarbleFragObj, "shaders/marble.frag" );
MarbleProgObj->setSampler( "Noise", TEXUNIT_NOISE );
MarbleProgObj->setSampler( "Sine", TEXUNIT_SINE );
} }
// mew 2003-09-19 : TODO Need to revisit how to better control // mew 2003-09-19 : TODO Need to revisit how to better control
// osgGL2::ProgramObject enable state in OSG core. glProgramObjects are // osg::Program enable state in OSG core. glProgram are
// different enough from other GL state that StateSet::setAttributeAndModes() // different enough from other GL state that StateSet::setAttributeAndModes()
// doesn't fit well, so came up with a local implementation. // doesn't fit well, so came up with a local implementation.
void void
@ -382,11 +399,10 @@ GL2Scene::toggleShaderEnable()
_shadersEnabled = ! _shadersEnabled; _shadersEnabled = ! _shadersEnabled;
osg::notify(osg::WARN) << "shader enable = " << osg::notify(osg::WARN) << "shader enable = " <<
((_shadersEnabled) ? "ON" : "OFF") << std::endl; ((_shadersEnabled) ? "ON" : "OFF") << std::endl;
for( unsigned int i = 0; i < _progObjList.size(); i++ ) for( unsigned int i = 0; i < _programList.size(); i++ )
{ {
_progObjList[i]->enable( _shadersEnabled ); //_programList[i]->enable( _shadersEnabled );
} }
} }
/*EOF*/ /*EOF*/

View File

@ -1,5 +1,5 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
* Copyright (C) 2003 3Dlabs Inc. Ltd. * Copyright (C) 2003-2005 3Dlabs Inc. Ltd.
* *
* This application is open source and may be redistributed and/or modified * This application is open source and may be redistributed and/or modified
* freely and without restriction, both in commericial and non commericial applications, * freely and without restriction, both in commericial and non commericial applications,
@ -10,8 +10,8 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/ */
/* file: examples/osgshaders/GL2Scene.h /* file: examples/osgglsl/GL2Scene.h
* author: Mike Weiblen 2003-09-18 * author: Mike Weiblen 2005-03-30
* *
* See http://www.3dlabs.com/opengl2/ for more information regarding * See http://www.3dlabs.com/opengl2/ for more information regarding
* the OpenGL Shading Language. * the OpenGL Shading Language.
@ -21,7 +21,7 @@
#include <osg/Referenced> #include <osg/Referenced>
#include <osg/ref_ptr> #include <osg/ref_ptr>
#include <osgGL2/ProgramObject> #include <osg/Program>
class GL2Scene : public osg::Referenced class GL2Scene : public osg::Referenced
{ {
@ -40,7 +40,7 @@ class GL2Scene : public osg::Referenced
private: /*data*/ private: /*data*/
osg::ref_ptr<osg::Group> _rootNode; osg::ref_ptr<osg::Group> _rootNode;
std::vector<osgGL2::ProgramObjectPtr> _progObjList; std::vector< osg::ref_ptr<osg::Program> > _programList;
bool _shadersEnabled; bool _shadersEnabled;
}; };

View File

@ -6,7 +6,7 @@ CXXFILES =\
Noise.cpp \ Noise.cpp \
osgshaders.cpp osgshaders.cpp
LIBS += -losgProducer -lProducer -losgText -losgGL2 -losgGA -losgDB -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS) LIBS += -losgProducer -lProducer -losgText -losgGA -losgDB -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
INSTFILES = \ INSTFILES = \
$(CXXFILES)\ $(CXXFILES)\

View File

@ -5,7 +5,7 @@ CXXFILES =\
GL2Scene.cpp \ GL2Scene.cpp \
osgshaders.cpp osgshaders.cpp
LIBS += -losgProducer -lProducer -losgDB -losgText -losgGL2 -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS) LIBS += -losgProducer -lProducer -losgDB -losgText -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
EXEC = osgshaders EXEC = osgshaders

View File

@ -1,50 +1,9 @@
osgshaders/README.txt - mike.weiblen@3dlabs.com - 2003-09-30 osgglsl/README.txt - mike.weiblen@3dlabs.com - 2005-03-30
Copyright 2003 3Dlabs Inc. Copyright 2005 3Dlabs Inc.
osgshaders is a simple demo of the OpenGL Shading Language, as supported in the osgglsl began as a port of the old osgshaders example from the deprecated
Open Scene Graph using the "osgGL2" library. osgGL2 classes to the new core GLSL classes (osg::Program, osg::Shader).
Demonstrated are: This app preserves the dynamically changing Uniforms of the original version,
- loading, compiling and linking OpenGL Shading Language (OGLSL) shaders. but uses the new osg::Uniform classes.
- multicontext support (use different Producer configurations to create
multiple windows)
- "uniform" variable support: enables an application to set parameters for
shaders at runtime for animation, etc.
- multi-texture support: using textures both as applied imagery and as lookup
tables for controlling shader behavior.
The two screen grabs show the demo at the extremes of a periodic animation
parameter.
WHAT's ON THE SCREEN?
1st row: the Marble shader, consisting of white marble with green veins.
The veins are animated to slosh back and forth, and look somewhat like
bands of stormclouds on Jupiter.
2nd row: the Eroded shader, which demonstrates the OGLSL "discard" command.
The animation erodes the surface of the objects based on a noise texture,
for an effect similar to rusting.
3rd row: the Blocky shader, which demonstrates animated scaling in the vertex
shader, as well as color animation using uniform variables set by the app.
4th row: the Microshader, an extremely simple shader which colors fragments
based on their position. It's small codesize makes it convenient to hardcode
directly into the application source.
RUNTIME KEY COMMANDS
These key commands are included in the onscreen help (as displayed by pressing
the "h" key):
x - Reload and recompile shader source code from their files.
You may edit the shader source code file, and recompile the shaders
while the application runs.
y - toggle shader enable.
#EOF

View File

@ -1,5 +1,5 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
* Copyright (C) 2003 3Dlabs Inc. Ltd. * Copyright (C) 2003-2005 3Dlabs Inc. Ltd.
* *
* This application is open source and may be redistributed and/or modified * This application is open source and may be redistributed and/or modified
* freely and without restriction, both in commericial and non commericial applications, * freely and without restriction, both in commericial and non commericial applications,
@ -10,10 +10,10 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/ */
/* file: examples/osgshaders/osgshaders.cpp /* file: examples/osgglsl/osgshaders.cpp
* author: Mike Weiblen 2003-09-18 * author: Mike Weiblen 2005-04-05
* *
* A demo of the OpenGL Shading Language shaders using osgGL2. * A demo of the OpenGL Shading Language shaders using core OSG.
* *
* See http://www.3dlabs.com/opengl2/ for more information regarding * See http://www.3dlabs.com/opengl2/ for more information regarding
* the OpenGL Shading Language. * the OpenGL Shading Language.
@ -25,7 +25,6 @@
#include <osgDB/ReadFile> #include <osgDB/ReadFile>
#include <osgUtil/Optimizer> #include <osgUtil/Optimizer>
#include <osgProducer/Viewer> #include <osgProducer/Viewer>
#include <osgGL2/Version>
#include "GL2Scene.h" #include "GL2Scene.h"
@ -71,7 +70,7 @@ int main( int argc, char **argv )
// set up the usage document // set up the usage document
args.getApplicationUsage()->setApplicationName(args.getApplicationName()); args.getApplicationUsage()->setApplicationName(args.getApplicationName());
args.getApplicationUsage()->setDescription(args.getApplicationName() + args.getApplicationUsage()->setDescription(args.getApplicationName() +
" demonstrates the OpenGL Shading Language using osgGL2"); " demonstrates the OpenGL Shading Language using core OSG");
args.getApplicationUsage()->setCommandLineUsage(args.getApplicationName()); args.getApplicationUsage()->setCommandLineUsage(args.getApplicationName());
args.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); args.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
@ -98,7 +97,6 @@ int main( int argc, char **argv )
} }
// create the scene // create the scene
osg::notify(osg::NOTICE) << "osgGL2 version " << osgGL2GetVersion() << std::endl;
GL2ScenePtr gl2Scene = new GL2Scene; GL2ScenePtr gl2Scene = new GL2Scene;
viewer.setSceneData( gl2Scene->getRootNode().get() ); viewer.setSceneData( gl2Scene->getRootNode().get() );