From 8ee319bbd521350a08ca1f5fdaeeebd3361712ba Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 2 Nov 2016 11:15:18 +0000 Subject: [PATCH] Added set up of root StateSet in the osg::State to enable shaders to be applied for all rendering on a graphics context. --- .../osgshaderpipeline/osgshaderpipeline.cpp | 111 +++++++++++++----- 1 file changed, 81 insertions(+), 30 deletions(-) diff --git a/examples/osgshaderpipeline/osgshaderpipeline.cpp b/examples/osgshaderpipeline/osgshaderpipeline.cpp index c427b7687..7857cc7f1 100644 --- a/examples/osgshaderpipeline/osgshaderpipeline.cpp +++ b/examples/osgshaderpipeline/osgshaderpipeline.cpp @@ -65,50 +65,25 @@ osg::Image* createFallbackImage() return image; } -int main(int argc, char** argv) +bool setUpStateSet(osg::ArgumentParser& arguments, osg::StateSet* stateset) { - // use an ArgumentParser object to manage the program arguments. - osg::ArgumentParser arguments(&argc,argv); - - osgViewer::Viewer viewer(arguments); - - // add the state manipulator - viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); - - // add the stats handler - viewer.addEventHandler(new osgViewer::StatsHandler); - - osg::ref_ptr program = new osg::Program; if (!readShaderArguments(arguments, "--vert", program, "shaders/shaderpipeline.vert")) { - return 1; + return false; } if (!readShaderArguments(arguments, "--frag", program, "shaders/shaderpipeline.frag")) { - return 1; + return false; } unsigned int maxTextureUnits = 1; while(arguments.read("--units", maxTextureUnits)) {} - // assign program to topmost StateSet - viewer.getCamera()->getOrCreateStateSet()->setAttribute(program); - - // load the data - osg::ref_ptr loadedModel = osgDB::readRefNodeFiles(arguments); - if (!loadedModel) - { - std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; - return 1; - } - - viewer.setSceneData(loadedModel); - - osg::ref_ptr stateset = viewer.getCamera()->getOrCreateStateSet(); + stateset->setAttribute(program); std::stringstream sstream; sstream< ACTIVE_TEXTURE = new osg::Uniform(osg::Uniform::BOOL, "GL_ACTIVE_TEXTURE", maxTextureUnits); osg::ref_ptr TEXTURE_GEN_S = new osg::Uniform(osg::Uniform::BOOL, "GL_TEXTURE_GEN_S", maxTextureUnits); @@ -151,6 +135,7 @@ int main(int argc, char** argv) osg::ref_ptr TEXTURE_GEN_MODE = new osg::Uniform(osg::Uniform::INT, "GL_TEXTURE_GEN_MODE", maxTextureUnits); osg::ref_ptr TEXTURE_ENV_MODE = new osg::Uniform(osg::Uniform::INT, "GL_TEXTURE_ENV_MODE", maxTextureUnits); + osg::ref_ptr TEXTURE_FORMAT = new osg::Uniform(osg::Uniform::INT, "GL_TEXTURE_FORMAT", maxTextureUnits); for(unsigned int i=0; isetElement(i, 0); TEXTURE_GEN_S->setElement(i, false); TEXTURE_GEN_T->setElement(i, false); + TEXTURE_ENV_MODE->setElement(i, GL_MODULATE); + TEXTURE_FORMAT->setElement(i, GL_RGBA); } ACTIVE_TEXTURE->setElement(0, true); @@ -166,12 +153,14 @@ int main(int argc, char** argv) //TEXTURE_GEN_MODE->setElement(0, GL_SPHERE_MAP); TEXTURE_GEN_S->setElement(0, true); TEXTURE_GEN_T->setElement(0, true); + //TEXTURE_FORMAT->setElement(0, GL_ALPHA); stateset->addUniform(ACTIVE_TEXTURE.get()); stateset->addUniform(TEXTURE_GEN_S.get()); stateset->addUniform(TEXTURE_GEN_T.get()); stateset->addUniform(TEXTURE_GEN_MODE.get()); stateset->addUniform(TEXTURE_ENV_MODE.get()); + stateset->addUniform(TEXTURE_FORMAT.get()); for(unsigned int i=0; isetDefine(textureVertBodyDefine, sstream.str()); @@ -234,9 +223,71 @@ int main(int argc, char** argv) osgDB::writeObjectFile(*stateset, "stateset.osgt"); + return true; +} + +struct RealizeOperation : public osg::GraphicsOperation +{ + RealizeOperation(osg::StateSet* stateset) : + osg::GraphicsOperation("RealizeOperation",false), + _stateset(stateset) + { + } + + virtual void operator () (osg::GraphicsContext* gc) + { + OSG_NOTICE<getState()->pushStateSet(_stateset.get()); + gc->getState()->setRootStateSet(_stateset.get()); + } + + osg::ref_ptr _stateset; +}; + +int main(int argc, char** argv) +{ + // use an ArgumentParser object to manage the program arguments. + osg::ArgumentParser arguments(&argc,argv); + + osgViewer::Viewer viewer(arguments); + + // add the state manipulator + viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); + + // add the stats handler + viewer.addEventHandler(new osgViewer::StatsHandler); + + osg::ref_ptr stateset = new osg::StateSet; + stateset->setGlobalDefaults(); + + // set up the topmost StateSet with the shader pipeline settings. + setUpStateSet(arguments, stateset); + + if (arguments.read("--state")) + { + OSG_NOTICE<<"Assigning RealizerOperation"<setStateSet(stateset.get()); + } + viewer.realize(); + + + // load the data + osg::ref_ptr loadedModel = osgDB::readRefNodeFiles(arguments); + if (!loadedModel) + { + std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; + return 1; + } + + viewer.setSceneData(loadedModel); + return viewer.run(); }