Merge branch 'master' into shader_pipeline

This commit is contained in:
Robert Osfield 2018-03-16 13:35:22 +00:00
commit c3af787a3a
7 changed files with 61 additions and 54 deletions

View File

@ -67,14 +67,17 @@ FIND_PATH(COLLADA_INCLUDE_DIR dae.h
/usr/local/include
/usr/local/include/colladadom
/usr/local/include/collada-dom
/usr/local/include/collada-dom2.5
/usr/local/include/collada-dom2.4
/usr/local/include/collada-dom2.2
/opt/local/include/collada-dom
/opt/local/include/collada-dom2.5
/opt/local/include/collada-dom2.4
/opt/local/include/collada-dom2.2
/usr/include/
/usr/include/colladadom
/usr/include/collada-dom
/usr/include/collada-dom2.5
/usr/include/collada-dom2.4
/usr/include/collada-dom2.2
/sw/include # Fink
@ -86,7 +89,7 @@ FIND_PATH(COLLADA_INCLUDE_DIR dae.h
)
FIND_LIBRARY(COLLADA_DYNAMIC_LIBRARY
NAMES collada_dom collada14dom Collada14Dom libcollada14dom21 libcollada14dom22 collada-dom2.4-dp collada-dom2.4-dp-${COLLADA_BOOST_BUILDNAME}-mt
NAMES collada_dom collada14dom Collada14Dom libcollada14dom21 libcollada14dom22 collada-dom2.5-dp collada-dom2.5-dp-${COLLADA_BOOST_BUILDNAME}-mt collada-dom2.4-dp collada-dom2.4-dp-${COLLADA_BOOST_BUILDNAME}-mt
PATHS
${COLLADA_DOM_ROOT}/build/${COLLADA_BUILDNAME}-1.4
${COLLADA_DOM_ROOT}
@ -110,7 +113,7 @@ FIND_LIBRARY(COLLADA_DYNAMIC_LIBRARY
)
FIND_LIBRARY(COLLADA_DYNAMIC_LIBRARY_DEBUG
NAMES collada_dom-d collada14dom-d Collada14Dom-d libcollada14dom21-d libcollada14dom22-d collada-dom2.4-dp-d collada-dom2.4-dp-${COLLADA_BOOST_BUILDNAME}-mt-d
NAMES collada_dom-d collada14dom-d Collada14Dom-d libcollada14dom21-d libcollada14dom22-d collada-dom2.5-dp-d collada-dom2.5-dp-${COLLADA_BOOST_BUILDNAME}-mt-d collada-dom2.4-dp-d collada-dom2.4-dp-${COLLADA_BOOST_BUILDNAME}-mt-d
PATHS
${COLLADA_DOM_ROOT}/build/${COLLADA_BUILDNAME}-1.4-d
${COLLADA_DOM_ROOT}

View File

@ -112,7 +112,13 @@ int main(int argc, char** argv)
// add the screen capture handler
viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
osg::ref_ptr<osg::Program> uberProgram = new osg::Program;
std::string shaderFilename;
while(arguments.read("--shader", shaderFilename))
{
osg::ref_ptr<osg::Shader> shader = osgDB::readShaderFile(shaderFilename);
if (shader.valid()) uberProgram->addShader(shader.get());
}
std::string outputFilename;
if (arguments.read("-o", outputFilename)) {}
@ -125,10 +131,23 @@ int main(int argc, char** argv)
return 1;
}
osg::ref_ptr<osg::StateSet> rootStateSet = viewer.getCamera()->getStateSet();
// run the shadergen on the loaded scene graph, and assign the uber shader
osgUtil::ShaderGenVisitor shadergen;
shadergen.assignUberProgram(viewer.getCamera()->getStateSet());
//shadergen.assignUberProgram(loadedModel->getOrCreateStateSet());
if (uberProgram->getNumShaders()>0)
{
rootStateSet->setAttribute(uberProgram.get());
rootStateSet->addUniform(new osg::Uniform("diffuseMap", 0));
shadergen.remapStateSet(rootStateSet.get());
}
else
{
shadergen.assignUberProgram(rootStateSet.get());
}
loadedModel->accept(shadergen);
if (!outputFilename.empty())

View File

@ -32,6 +32,7 @@
#define SERIALIZER() OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_serializerMutex)
osgDB::ReaderWriter::ReadResult
ReaderWriterDAE::readNode(std::istream& fin,
const osgDB::ReaderWriter::Options* options) const
@ -66,7 +67,7 @@ ReaderWriterDAE::readNode(std::istream& fin,
{
bOwnDAE = true;
#ifdef COLLADA_DOM_2_4_OR_LATER
pDAE = new DAE(NULL,NULL,"1.4.1");
pDAE = new DAE(NULL,NULL,_specversion);
#else
pDAE = new DAE;
#endif
@ -144,7 +145,7 @@ ReaderWriterDAE::readNode(const std::string& fname,
{
bOwnDAE = true;
#ifdef COLLADA_DOM_2_4_OR_LATER
pDAE = new DAE(NULL,NULL,"1.4.1");
pDAE = new DAE(NULL,NULL,_specversion);
#else
pDAE = new DAE;
#endif
@ -241,7 +242,7 @@ ReaderWriterDAE::writeNode( const osg::Node& node,
{
bOwnDAE = true;
#ifdef COLLADA_DOM_2_4_OR_LATER
pDAE = new DAE(NULL,NULL,"1.4.1");
pDAE = new DAE(NULL,NULL,_specversion);
#else
pDAE = new DAE;
#endif

View File

@ -13,6 +13,8 @@ class ReaderWriterDAE : public osgDB::ReaderWriter
public:
ReaderWriterDAE()
{
_specversion = "1.4.1";
// Collada document
supportsExtension("dae","COLLADA 1.4.x DAE format");
// Collada zip archive (contains one or more dae files and a manifest.xml)
@ -48,6 +50,7 @@ public:
private:
mutable OpenThreads::ReentrantMutex _serializerMutex;
const char* _specversion;
};
///////////////////////////////////////////////////////////////////////////

View File

@ -30,21 +30,6 @@ using namespace osgUtil;
namespace osgUtil
{
osg::ref_ptr<osg::Program> s_UberProgram;
struct UberProgramConstructor
{
UberProgramConstructor()
{
s_UberProgram = new osg::Program;
s_UberProgram->addShader(new osg::Shader(osg::Shader::VERTEX, shadergen_vert));
s_UberProgram->addShader(new osg::Shader(osg::Shader::FRAGMENT, shadergen_frag));
}
};
UberProgramConstructor s_UberProgramConstructor;
ShaderGenVisitor::ShaderGenVisitor():
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
{
@ -54,7 +39,11 @@ void ShaderGenVisitor::assignUberProgram(osg::StateSet *stateSet)
{
if (stateSet)
{
stateSet->setAttribute(s_UberProgram.get());
osg::ref_ptr<osg::Program> uberProgram = new osg::Program;
uberProgram->addShader(new osg::Shader(osg::Shader::VERTEX, shadergen_vert));
uberProgram->addShader(new osg::Shader(osg::Shader::FRAGMENT, shadergen_frag));
stateSet->setAttribute(uberProgram.get());
stateSet->addUniform(new osg::Uniform("diffuseMap", 0));
remapStateSet(stateSet);

View File

@ -4,16 +4,7 @@ char shadergen_frag[] = "// ShaderGen shader\n"
" precision highp float;\n"
"#endif\n"
"\n"
"#pragma import_defines(GL_LIGHTING, GL_TEXTURE_2D, GL_FOG)\n"
"\n"
"#if defined(GL_LIGHTING)\n"
"varying vec3 normalDir;\n"
"varying vec3 lightDir;\n"
"#endif\n"
"\n"
"#if defined(GL_LIGHTING) || defined(GL_FOG)\n"
"varying vec3 viewDir;\n"
"#endif\n"
"#pragma import_defines(GL_TEXTURE_2D)\n"
"\n"
"varying vec4 vertexColor;\n"
"\n"
@ -29,7 +20,6 @@ char shadergen_frag[] = "// ShaderGen shader\n"
" color = color * texture2D(diffuseMap, gl_TexCoord[0].st);\n"
"#endif\n"
"\n"
"\n"
" gl_FragColor = color;\n"
"}\n"
"\n";

View File

@ -4,20 +4,31 @@ char shadergen_vert[] = "// ShaderGen shader\n"
" precision highp float;\n"
"#endif\n"
"\n"
"#pragma import_defines(GL_LIGHTING, GL_TEXTURE_2D, GL_FOG)\n"
"#pragma import_defines(GL_LIGHTING, GL_TEXTURE_2D)\n"
"\n"
"#if defined(GL_LIGHTING)\n"
"varying vec3 normalDir;\n"
"varying vec3 lightDir;\n"
"#endif\n"
"\n"
"#if defined(GL_LIGHTING) || defined(GL_FOG)\n"
"varying vec3 viewDir;\n"
"#ifdef GL_LIGHTING\n"
"void directionalLight( int lightNum, vec3 normal, inout vec4 color )\n"
"{\n"
" vec3 n = normalize(gl_NormalMatrix * normal);\n"
"\n"
" float NdotL = dot( n, normalize(gl_LightSource[lightNum].position.xyz) );\n"
" NdotL = max( 0.0, NdotL );\n"
"\n"
" float NdotHV = dot( n, gl_LightSource[lightNum].halfVector.xyz );\n"
" NdotHV = max( 0.0, NdotHV );\n"
"\n"
" color *= gl_FrontLightModelProduct.sceneColor +\n"
" gl_FrontLightProduct[lightNum].ambient +\n"
" gl_FrontLightProduct[lightNum].diffuse * NdotL;\n"
"\n"
" if ( NdotL * NdotHV > 0.0 )\n"
" color += gl_FrontLightProduct[lightNum].specular * pow( NdotHV, gl_FrontMaterial.shininess );\n"
"}\n"
"#endif\n"
"\n"
"varying vec4 vertexColor;\n"
"\n"
"\n"
"void main()\n"
"{\n"
" gl_Position = ftransform();\n"
@ -26,19 +37,10 @@ char shadergen_vert[] = "// ShaderGen shader\n"
" gl_TexCoord[0] = gl_MultiTexCoord0;\n"
"#endif\n"
"\n"
"#if defined(GL_LIGHTING) || defined(GL_FOG)\n"
" viewDir = -vec3(gl_ModelViewMatrix * gl_Vertex);\n"
"#endif\n"
" vertexColor = gl_Color;\n"
"\n"
"#if defined(GL_LIGHTING)\n"
" normalDir = gl_NormalMatrix * gl_Normal;\n"
" vec4 lpos = gl_LightSource[0].position;\n"
" if (lpos.w == 0.0)\n"
" lightDir = lpos.xyz;\n"
" else\n"
" lightDir = lpos.xyz + viewDir;\n"
" directionalLight(0, gl_Normal, vertexColor);\n"
"#endif\n"
"\n"
" vertexColor = gl_Color;\n"
"}\n"
"\n";