Added osgshaderpipeline example that will server as a testbed for automatically mapping fixed function pipeline to shaders

This commit is contained in:
Robert Osfield 2016-10-19 20:26:26 +01:00
parent 26a313d549
commit fda7c838a1
3 changed files with 126 additions and 0 deletions

View File

@ -102,6 +102,7 @@ IF(DYNAMIC_OPENSCENEGRAPH)
ADD_SUBDIRECTORY(osgscribe)
ADD_SUBDIRECTORY(osgsequence)
ADD_SUBDIRECTORY(osgshaders)
ADD_SUBDIRECTORY(osgshaderpipeline)
ADD_SUBDIRECTORY(osgshadercomposition)
ADD_SUBDIRECTORY(osgshadergen)
ADD_SUBDIRECTORY(osgshadermultiviewport)

View File

@ -0,0 +1,3 @@
SET(TARGET_SRC osgshaderpipeline.cpp )
SETUP_APPLICATION(osgshaderpipeline)

View File

@ -0,0 +1,122 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
*
* This application is open source and may be redistributed and/or modified
* freely and without restriction, both in commercial and non commercial 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.
*/
#include <osgDB/ReadFile>
#include <osgGA/StateSetManipulator>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <iostream>
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<osg::Program> program = new osg::Program;
bool vertexShadersAssigned = false;
std::string vertexShaderFilename;
while(arguments.read("--vert", vertexShaderFilename))
{
osg::ref_ptr<osg::Shader> vertexShader = osgDB::readRefShaderFile(vertexShaderFilename);
if (vertexShader)
{
vertexShadersAssigned = true;
program->addShader(vertexShader);
}
else
{
OSG_NOTICE<<"Unable to load vertex shader file : "<<vertexShaderFilename<<std::endl;
}
}
if (!vertexShadersAssigned)
{
vertexShaderFilename = "shaders/shaderpipeline.vert";
osg::ref_ptr<osg::Shader> vertexShader = osgDB::readRefShaderFile(vertexShaderFilename);
if (vertexShader)
{
vertexShadersAssigned = true;
program->addShader(vertexShader);
}
else
{
OSG_NOTICE<<"Unable to load vertex shader file : "<<vertexShaderFilename<<std::endl;
return 1;
}
}
bool fragmentShadersAssigned = false;
std::string fragmentShaderFilename;
while(arguments.read("--frag", fragmentShaderFilename))
{
osg::ref_ptr<osg::Shader> fragmentShader = osgDB::readRefShaderFile(fragmentShaderFilename);
if (fragmentShader)
{
fragmentShadersAssigned = true;
program->addShader(fragmentShader);
}
else
{
OSG_NOTICE<<"Unable to load fragment shader file : "<<fragmentShaderFilename<<std::endl;
}
}
if (!fragmentShadersAssigned)
{
fragmentShaderFilename = "shaders/shaderpipeline.vert";
osg::ref_ptr<osg::Shader> fragmentShader = osgDB::readRefShaderFile(fragmentShaderFilename);
if (fragmentShader)
{
fragmentShadersAssigned = true;
program->addShader(fragmentShader);
}
else
{
OSG_NOTICE<<"Unable to load fragment shader file : "<<fragmentShaderFilename<<std::endl;
return 1;
}
}
// assign program to topmost StateSet
viewer.getCamera()->getOrCreateStateSet()->setAttribute(program);
// load the data
osg::ref_ptr<osg::Node> loadedModel = osgDB::readRefNodeFiles(arguments);
if (!loadedModel)
{
std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
return 1;
}
viewer.setSceneData(loadedModel);
viewer.realize();
return viewer.run();
}