From 6b7175baac6b2a1b84dba09efd89b698a8a85155 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 12 Aug 2011 14:53:40 +0000 Subject: [PATCH] Implemented first pass at shaders for new ViewDepedentShadow map class --- include/osgShadow/ViewDependentShadowMap | 10 +++- src/osgShadow/ViewDependentShadowMap.cpp | 67 +++++++++++++++++++++++- 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/include/osgShadow/ViewDependentShadowMap b/include/osgShadow/ViewDependentShadowMap index 1ba7e8731..fc3604909 100644 --- a/include/osgShadow/ViewDependentShadowMap +++ b/include/osgShadow/ViewDependentShadowMap @@ -18,6 +18,7 @@ #include #include #include +#include #include @@ -166,8 +167,15 @@ protected: OpenThreads::Mutex _viewDependentDataMapMutex; ViewDependentDataMap _viewDependentDataMap; - osg::ref_ptr _shadowRecievingPlaceholderStateSet; + osg::ref_ptr _shadowRecievingPlaceholderStateSet; + osg::ref_ptr _shadowCastingStateSet; + osg::ref_ptr _polygonOffset; + + typedef std::vector< osg::ref_ptr > Uniforms; + Uniforms _uniforms; + osg::ref_ptr _program; + bool _debugDraw; }; diff --git a/src/osgShadow/ViewDependentShadowMap.cpp b/src/osgShadow/ViewDependentShadowMap.cpp index dfddaa8ec..e2141c78f 100644 --- a/src/osgShadow/ViewDependentShadowMap.cpp +++ b/src/osgShadow/ViewDependentShadowMap.cpp @@ -13,10 +13,27 @@ #include #include +#include #include using namespace osgShadow; +////////////////////////////////////////////////////////////////// +// fragment shader +// + +static const char fragmentShaderSource_withBaseTexture[] = + "uniform sampler2D baseTexture; \n" + "uniform sampler2DShadow shadowTexture; \n" + " \n" + "void main(void) \n" + "{ \n" + " vec4 colorAmbientEmissive = gl_FrontLightModelProduct.sceneColor; \n" + " vec4 color = texture2D( baseTexture, gl_TexCoord[0].xy ); \n" + " color *= mix( colorAmbientEmissive, gl_Color, shadow2DProj( shadowTexture, gl_TexCoord[1] ).r ); \n" + " gl_FragColor = color; \n" + "} \n"; + /////////////////////////////////////////////////////////////////////////////////////////////// // // VDSMCameraCullCallback @@ -471,7 +488,7 @@ void ViewDependentShadowMap::cull(osgUtil::CullVisitor& cv) unsigned int pos_x = 0; unsigned int baseTextureUnit = 0; - unsigned int textureUnit = baseTextureUnit; + unsigned int textureUnit = baseTextureUnit+1; unsigned int numValidShadows = 0; Frustum frustum(&cv); @@ -548,8 +565,12 @@ void ViewDependentShadowMap::cull(osgUtil::CullVisitor& cv) // 4.3 traverse RTT camera // + cv.pushStateSet(_shadowCastingStateSet.get()); + cullShadowCastingScene(&cv, camera.get()); + cv.popStateSet(); + // 4.4 compute main scene graph TexGen + uniform settings + setup state // @@ -629,6 +650,41 @@ bool ViewDependentShadowMap::selectActiveLights(osgUtil::CullVisitor* cv, ViewDe void ViewDependentShadowMap::createShaders() { OSG_NOTICE<<"ViewDependentShadowMap::createShaders()"< cull_face = new osg::CullFace; + cull_face->setMode(osg::CullFace::FRONT); + _shadowCastingStateSet->setAttribute(cull_face.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); + _shadowCastingStateSet->setMode(GL_CULL_FACE, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); +#endif + +#if 1 + float factor = 1.1; + float units = 4.0; + _polygonOffset = new osg::PolygonOffset(factor, units); + _shadowCastingStateSet->setAttribute(_polygonOffset.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); + _shadowCastingStateSet->setMode(GL_POLYGON_OFFSET_FILL, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); +#endif + + _uniforms.clear(); + osg::ref_ptr baseTextureSampler = new osg::Uniform("baseTexture",(int)_baseTextureUnit); + _uniforms.push_back(baseTextureSampler.get()); + + osg::ref_ptr shadowTextureSampler = new osg::Uniform("shadowTexture",(int)_shadowTextureUnit); + _uniforms.push_back(shadowTextureSampler.get()); + + //osg::ref_ptr fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource_noBaseTexture); + osg::ref_ptr fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource_withBaseTexture); + + _program = new osg::Program; + _program->addShader(fragment_shader.get()); + } osg::Polytope ViewDependentShadowMap::computeLightViewFrustumPolytope(Frustum& frustum, LightData& positionedLight) @@ -958,6 +1014,15 @@ osg::StateSet* ViewDependentShadowMap::selectStateSetForRenderingShadow(ViewDepe vdd.getStateSet()->clear(); //vdd.getStateSet()->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE); + for(Uniforms::const_iterator itr=_uniforms.begin(); + itr!=_uniforms.end(); + ++itr) + { + stateset->addUniform(itr->get()); + } + + stateset->setAttribute(_program.get()); + LightDataList& pll = vdd.getLightDataList(); for(LightDataList::iterator itr = pll.begin(); itr != pll.end();