From Mike Weiblen's osgGL2 node kit which support the latest OpenGL 2.0 extensions.
This commit is contained in:
174
examples/osgshaders/GL2Scene.cpp
Normal file
174
examples/osgshaders/GL2Scene.cpp
Normal file
@@ -0,0 +1,174 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
|
||||
* Copyright (C) 2003 3Dlabs Inc. Ltd.
|
||||
*
|
||||
* This application is open source and may be redistributed and/or modified
|
||||
* freely and without restriction, both in commericial and non commericial applications,
|
||||
* as long as this copyright notice is maintained.
|
||||
*
|
||||
* This application is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/* file: examples/osgshaders/GL2Scene.cpp
|
||||
* author: Mike Weiblen 2003-07-14
|
||||
*
|
||||
* Compose a scene of several instances of a model, with a different
|
||||
* OpenGL Shading Language shader applied to each.
|
||||
*
|
||||
* See http://www.3dlabs.com/opengl2/ for more information regarding
|
||||
* the OpenGL Shading Language.
|
||||
*/
|
||||
|
||||
#include <osg/ShapeDrawable>
|
||||
#include <osg/PositionAttitudeTransform>
|
||||
#include <osg/Geode>
|
||||
#include <osg/Notify>
|
||||
#include <osgDB/ReadFile>
|
||||
#include <osgDB/FileUtils>
|
||||
#include <osgUtil/Optimizer>
|
||||
#include <osgGL2/ProgramObject>
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// OpenGL Shading Language source code for the "microshader" example.
|
||||
|
||||
static const char *microshaderVertSource = {
|
||||
"varying vec3 color;"
|
||||
"void main(void)"
|
||||
"{"
|
||||
"gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"
|
||||
"color = gl_Vertex.zyx * 1.0;"
|
||||
"}"
|
||||
};
|
||||
|
||||
static const char *microshaderFragSource = {
|
||||
"varying vec3 color;"
|
||||
"void main(void)"
|
||||
"{"
|
||||
"gl_FragColor = vec4(color, 1.0);"
|
||||
"}"
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static osg::Group* rootNode;
|
||||
static osg::Node* masterModel;
|
||||
|
||||
// Add a reference to the masterModel at the specified translation, and
|
||||
// return its StateSet so we can easily attach StateAttributes.
|
||||
static osg::StateSet*
|
||||
CloneMaster(float x, float y, float z )
|
||||
{
|
||||
osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform();
|
||||
xform->setPosition(osg::Vec3(x, y, z));
|
||||
xform->addChild(masterModel);
|
||||
rootNode->addChild(xform);
|
||||
return xform->getOrCreateStateSet();
|
||||
}
|
||||
|
||||
// Create some geometry upon which to render GL2 shaders.
|
||||
static osg::Geode*
|
||||
CreateModel()
|
||||
{
|
||||
osg::Geode* geode = new osg::Geode();
|
||||
geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),1.0f)));
|
||||
geode->addDrawable(new osg::ShapeDrawable(new osg::Cone(osg::Vec3(2.2f,0.0f,-0.4f),0.9f,1.8f)));
|
||||
geode->addDrawable(new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(4.4f,0.0f,0.0f),1.0f,1.4f)));
|
||||
return geode;
|
||||
}
|
||||
|
||||
// read vert & frag shader source code from a pair of files.
|
||||
static void
|
||||
LoadShaderSource( osgGL2::ProgramObject* progObj, std::string baseFileName )
|
||||
{
|
||||
std::string vertFileName = osgDB::findDataFile(baseFileName + ".vert");
|
||||
if( vertFileName.length() != 0 )
|
||||
{
|
||||
osgGL2::ShaderObject* vertObj = new osgGL2::ShaderObject( osgGL2::ShaderObject::VERTEX );
|
||||
vertObj->loadShaderSourceFromFile( vertFileName.c_str() );
|
||||
progObj->addShader( vertObj );
|
||||
}
|
||||
else
|
||||
{
|
||||
osg::notify(osg::WARN) << "Warning: file \"" << baseFileName+".vert" << "\" not found." << std::endl;
|
||||
}
|
||||
|
||||
std::string fragFileName = osgDB::findDataFile(baseFileName + ".frag");
|
||||
if( fragFileName.length() != 0 )
|
||||
{
|
||||
osgGL2::ShaderObject* fragObj = new osgGL2::ShaderObject( osgGL2::ShaderObject::FRAGMENT );
|
||||
fragObj->loadShaderSourceFromFile( fragFileName.c_str() );
|
||||
progObj->addShader( fragObj );
|
||||
}
|
||||
else
|
||||
{
|
||||
osg::notify(osg::WARN) << "Warning: file \"" << baseFileName+".frag" << "\" not found." << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// Compose a scenegraph with examples of GL2 shaders
|
||||
|
||||
#define ZGRID 2.2
|
||||
|
||||
osg::Node*
|
||||
GL2Scene()
|
||||
{
|
||||
osg::StateSet* ss;
|
||||
osgGL2::ProgramObject* progObj;
|
||||
|
||||
// the rootNode of our created graph.
|
||||
rootNode = new osg::Group;
|
||||
ss = rootNode->getOrCreateStateSet();
|
||||
|
||||
// attach an "empty" ProgramObject to the rootNode as a default
|
||||
// StateAttribute. An empty ProgramObject (ie without any attached
|
||||
// ShaderObjects) is a special case, which means to use the
|
||||
// OpenGL 1.x "fixed functionality" rendering pipeline.
|
||||
progObj = new osgGL2::ProgramObject;
|
||||
ss->setAttributeAndModes(progObj, osg::StateAttribute::ON);
|
||||
|
||||
// put the unadorned masterModel at the origin for comparison.
|
||||
masterModel = CreateModel();
|
||||
rootNode->addChild(masterModel);
|
||||
|
||||
// add logo overlays
|
||||
//rootNode->addChild( osgDB::readNodeFile( "3dl_ogl.logo" ) );
|
||||
|
||||
//
|
||||
// create references to the masterModel and attach shaders
|
||||
//
|
||||
|
||||
// apply the simple microshader example
|
||||
// (the shader sources are hardcoded above in this .cpp file)
|
||||
ss = CloneMaster(0,0,ZGRID*1);
|
||||
progObj = new osgGL2::ProgramObject;
|
||||
progObj->addShader( new osgGL2::ShaderObject( osgGL2::ShaderObject::VERTEX, microshaderVertSource ) );
|
||||
progObj->addShader( new osgGL2::ShaderObject( osgGL2::ShaderObject::FRAGMENT, microshaderFragSource ) );
|
||||
ss->setAttributeAndModes(progObj, osg::StateAttribute::ON);
|
||||
|
||||
// load the "specular brick" shader from a pair of source files.
|
||||
ss = CloneMaster(0,0,ZGRID*2);
|
||||
progObj = new osgGL2::ProgramObject;
|
||||
LoadShaderSource( progObj, "shaders/brick" );
|
||||
ss->setAttributeAndModes(progObj, osg::StateAttribute::ON);
|
||||
|
||||
// load the "gold screen" shader from a pair of source files.
|
||||
ss = CloneMaster(0,0,ZGRID*3);
|
||||
progObj = new osgGL2::ProgramObject;
|
||||
LoadShaderSource( progObj, "shaders/screen" );
|
||||
ss->setAttributeAndModes(progObj, osg::StateAttribute::ON);
|
||||
|
||||
return rootNode;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
GL2Update()
|
||||
{
|
||||
/* TODO : update uniform values for shader animation */
|
||||
}
|
||||
|
||||
/*EOF*/
|
||||
|
||||
20
examples/osgshaders/GNUmakefile
Normal file
20
examples/osgshaders/GNUmakefile
Normal file
@@ -0,0 +1,20 @@
|
||||
TOPDIR = ../..
|
||||
include $(TOPDIR)/Make/makedefs
|
||||
|
||||
CXXFILES =\
|
||||
GL2Scene.cpp \
|
||||
osgshaders.cpp
|
||||
|
||||
LIBS += -losgProducer -lProducer -losgText -losgGL2 -losgGA -losgDB -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
|
||||
|
||||
INSTFILES = \
|
||||
$(CXXFILES)\
|
||||
GNUmakefile.inst=GNUmakefile
|
||||
|
||||
EXEC = osgshaders
|
||||
|
||||
INC += $(PRODUCER_INCLUDE_DIR) -I/usr/X11R6/include
|
||||
LDFLAGS += $(PRODUCER_LIB_DIR)
|
||||
|
||||
include $(TOPDIR)/Make/makerules
|
||||
|
||||
15
examples/osgshaders/GNUmakefile.inst
Normal file
15
examples/osgshaders/GNUmakefile.inst
Normal file
@@ -0,0 +1,15 @@
|
||||
TOPDIR = ../..
|
||||
include $(TOPDIR)/Make/makedefs
|
||||
|
||||
CXXFILES =\
|
||||
GL2Scene.cpp \
|
||||
osgshaders.cpp
|
||||
|
||||
LIBS += -losgProducer -lProducer -losgDB -losgText -losgGL2 -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
|
||||
|
||||
EXEC = osgshaders
|
||||
|
||||
INC += $(PRODUCER_INCLUDE_DIR) -I/usr/X11R6/include
|
||||
LDFLAGS += $(PRODUCER_LIB_DIR)
|
||||
|
||||
include $(TOPDIR)/Make/makerules
|
||||
123
examples/osgshaders/osgshaders.cpp
Normal file
123
examples/osgshaders/osgshaders.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
|
||||
* Copyright (C) 2003 3Dlabs Inc. Ltd.
|
||||
*
|
||||
* This application is open source and may be redistributed and/or modified
|
||||
* freely and without restriction, both in commericial and non commericial applications,
|
||||
* as long as this copyright notice is maintained.
|
||||
*
|
||||
* This application is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/* file: examples/osgshaders/osgshaders.cpp
|
||||
* author: Mike Weiblen 2003-07-14
|
||||
*
|
||||
* A simple app skeleton for viewing OpenGL Shading Language shaders;
|
||||
* derived from osgviewer.cpp from OSG 0.9.4-2
|
||||
*
|
||||
* See http://www.3dlabs.com/opengl2/ for more information regarding
|
||||
* the OpenGL Shading Language.
|
||||
*/
|
||||
|
||||
#include <osgDB/ReadFile>
|
||||
#include <osgUtil/Optimizer>
|
||||
#include <osgProducer/Viewer>
|
||||
|
||||
#define GL2SCENE
|
||||
osg::Node* GL2Scene();
|
||||
void GL2Update();
|
||||
|
||||
int main( int argc, char **argv )
|
||||
{
|
||||
|
||||
// use an ArgumentParser object to manage the program arguments.
|
||||
osg::ArgumentParser arguments(&argc,argv);
|
||||
|
||||
// set up the usage document, in case we need to print out how to use this program.
|
||||
arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
|
||||
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models.");
|
||||
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
|
||||
arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
|
||||
|
||||
|
||||
// construct the viewer.
|
||||
osgProducer::Viewer viewer(arguments);
|
||||
|
||||
// set up the value with sensible default event handlers.
|
||||
viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
|
||||
|
||||
// get details on keyboard and mouse bindings used by the viewer.
|
||||
viewer.getUsage(*arguments.getApplicationUsage());
|
||||
|
||||
// if user request help write it out to cout.
|
||||
if (arguments.read("-h") || arguments.read("--help"))
|
||||
{
|
||||
arguments.getApplicationUsage()->write(std::cout);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// any option left unread are converted into errors to write out later.
|
||||
arguments.reportRemainingOptionsAsUnrecognized();
|
||||
|
||||
// report any errors if they have occured when parsing the program aguments.
|
||||
if (arguments.errors())
|
||||
{
|
||||
arguments.writeErrorMessages(std::cout);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifdef GL2SCENE //(
|
||||
osg::ref_ptr<osg::Node> loadedModel = GL2Scene();
|
||||
#else //)(
|
||||
if (arguments.argc()<=1)
|
||||
{
|
||||
arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// read the scene from the list of file specified commandline args.
|
||||
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
|
||||
|
||||
// if no model has been successfully loaded report failure.
|
||||
if (!loadedModel)
|
||||
{
|
||||
std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// optimize the scene graph, remove rendundent nodes and state etc.
|
||||
osgUtil::Optimizer optimizer;
|
||||
optimizer.optimize(loadedModel.get());
|
||||
#endif //)
|
||||
|
||||
// set the scene to render
|
||||
viewer.setSceneData(loadedModel.get());
|
||||
|
||||
// create the windows and run the threads.
|
||||
viewer.realize();
|
||||
|
||||
while( !viewer.done() )
|
||||
{
|
||||
// wait for all cull and draw threads to complete.
|
||||
viewer.sync();
|
||||
|
||||
// update the scene by traversing it with the the update visitor which will
|
||||
// call all node update callbacks and animations.
|
||||
viewer.update();
|
||||
#ifdef GL2SCENE //(
|
||||
GL2Update();
|
||||
#endif //)
|
||||
|
||||
// fire off the cull and draw traversals of the scene.
|
||||
viewer.frame();
|
||||
|
||||
}
|
||||
|
||||
// wait for all cull and draw threads to complete before exit.
|
||||
viewer.sync();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user