From 0d3169f645866c91c749ccc0cb6f6017010bde06 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 24 Aug 2011 19:38:58 +0000 Subject: [PATCH] Added support for user defined setting of the shadow texture unit --- examples/osgshadow/osgshadow.cpp | 3 +++ include/osgShadow/ViewDependentShadowMap | 24 ++++++++++++------ src/osgShadow/ViewDependentShadowMap.cpp | 32 +++++++++++++++++++----- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/examples/osgshadow/osgshadow.cpp b/examples/osgshadow/osgshadow.cpp index 3a4e07c86..16074100d 100644 --- a/examples/osgshadow/osgshadow.cpp +++ b/examples/osgshadow/osgshadow.cpp @@ -800,6 +800,9 @@ int main(int argc, char** argv) if (arguments.read("--persp")) vdsm->setShadowMapProjectionHint(osgShadow::ViewDependentShadowMap::PERSPECTIVE_SHADOW_MAP); if (arguments.read("--ortho")) vdsm->setShadowMapProjectionHint(osgShadow::ViewDependentShadowMap::ORTHOGRAPHIC_SHADOW_MAP); + + unsigned int unit=1; + if (arguments.read("--unit",unit)) vdsm->setBaseShadowTextureUnit(unit); shadowedScene->setShadowTechnique(vdsm.get()); } diff --git a/include/osgShadow/ViewDependentShadowMap b/include/osgShadow/ViewDependentShadowMap index 0a27a893b..bfacedefa 100644 --- a/include/osgShadow/ViewDependentShadowMap +++ b/include/osgShadow/ViewDependentShadowMap @@ -140,21 +140,29 @@ class OSGSHADOW_EXPORT ViewDependentShadowMap : public ShadowTechnique ViewDependentData* getViewDependentData(osgUtil::CullVisitor* cv); - virtual void createShaders(); - - virtual bool selectActiveLights(osgUtil::CullVisitor* cv, ViewDependentData* vdd) const; - - virtual osg::Polytope computeLightViewFrustumPolytope(Frustum& frustum, LightData& positionedLight); + /** Set the texture unit that the first shadow map will be placed on, if mulitple shadow maps are + * required then each shadow map will use a allocated a unit relative to this base texture unit.*/ + void setBaseShadowTextureUnit(unsigned int unit) { _baseShadowTextureUnit = unit; } + + /** Get the texture unit that the first shadow map will be placed on.*/ + unsigned int getBaseShadowTextureUnit() const { return _baseShadowTextureUnit; } enum ShadowMapProjectionHint { ORTHOGRAPHIC_SHADOW_MAP, PERSPECTIVE_SHADOW_MAP }; - + void setShadowMapProjectionHint(ShadowMapProjectionHint hint) { _shadowMapProjectionHint = hint; } ShadowMapProjectionHint getShadowMapProjectionHint() const { return _shadowMapProjectionHint; } - + + +virtual void createShaders(); + + virtual bool selectActiveLights(osgUtil::CullVisitor* cv, ViewDependentData* vdd) const; + + virtual osg::Polytope computeLightViewFrustumPolytope(Frustum& frustum, LightData& positionedLight); + virtual bool computeShadowCameraSettings(Frustum& frustum, LightData& positionedLight, osg::Camera* camera); virtual bool adjustPerspectiveShadowMapCameraSettings(osgUtil::RenderStage* renderStage, Frustum& frustum, LightData& positionedLight, osg::Camera* camera); @@ -179,6 +187,8 @@ protected: OpenThreads::Mutex _viewDependentDataMapMutex; ViewDependentDataMap _viewDependentDataMap; + unsigned int _baseShadowTextureUnit; + osg::ref_ptr _shadowRecievingPlaceholderStateSet; osg::ref_ptr _shadowCastingStateSet; diff --git a/src/osgShadow/ViewDependentShadowMap.cpp b/src/osgShadow/ViewDependentShadowMap.cpp index b969e86be..42d747d12 100644 --- a/src/osgShadow/ViewDependentShadowMap.cpp +++ b/src/osgShadow/ViewDependentShadowMap.cpp @@ -21,7 +21,7 @@ using namespace osgShadow; ////////////////////////////////////////////////////////////////// // fragment shader // - +#if 0 static const char fragmentShaderSource_withBaseTexture[] = "uniform sampler2D baseTexture; \n" "uniform sampler2DShadow shadowTexture; \n" @@ -33,7 +33,21 @@ static const char fragmentShaderSource_withBaseTexture[] = " color *= mix( colorAmbientEmissive, gl_Color, shadow2DProj( shadowTexture, gl_TexCoord[1] ).r ); \n" " gl_FragColor = color; \n" "} \n"; - +#else +static const char fragmentShaderSource_withBaseTexture[] = + "uniform sampler2D baseTexture; \n" + "uniform int baseTextureUnit; \n" + "uniform sampler2DShadow shadowTexture; \n" + "uniform int shadowTextureUnit; \n" + " \n" + "void main(void) \n" + "{ \n" + " vec4 colorAmbientEmissive = gl_FrontLightModelProduct.sceneColor; \n" + " vec4 color = texture2D( baseTexture, gl_TexCoord[baseTextureUnit].xy ); \n" + " color *= mix( colorAmbientEmissive, gl_Color, shadow2DProj( shadowTexture, gl_TexCoord[shadowTextureUnit] ).r ); \n" + " gl_FragColor = color; \n" + "} \n"; +#endif template class RenderLeafTraverser : public T @@ -485,6 +499,7 @@ ViewDependentShadowMap::ViewDependentData::ViewDependentData(ViewDependentShadow // ViewDependentShadowMap::ViewDependentShadowMap(): ShadowTechnique(), + _baseShadowTextureUnit(1), _shadowMapProjectionHint(PERSPECTIVE_SHADOW_MAP), _debugDraw(false) { @@ -493,6 +508,7 @@ ViewDependentShadowMap::ViewDependentShadowMap(): ViewDependentShadowMap::ViewDependentShadowMap(const ViewDependentShadowMap& vdsm, const osg::CopyOp& copyop): ShadowTechnique(vdsm,copyop), + _baseShadowTextureUnit(vdsm._baseShadowTextureUnit), _shadowMapProjectionHint(vdsm._shadowMapProjectionHint), _debugDraw(vdsm._debugDraw) { @@ -583,8 +599,7 @@ void ViewDependentShadowMap::cull(osgUtil::CullVisitor& cv) selectActiveLights(&cv, vdd); unsigned int pos_x = 0; - unsigned int baseTextureUnit = 0; - unsigned int textureUnit = baseTextureUnit+1; + unsigned int textureUnit = _baseShadowTextureUnit; unsigned int numValidShadows = 0; Frustum frustum(&cv); @@ -756,7 +771,6 @@ void ViewDependentShadowMap::createShaders() OSG_NOTICE<<"ViewDependentShadowMap::createShaders()"< baseTextureSampler = new osg::Uniform("baseTexture",(int)_baseTextureUnit); _uniforms.push_back(baseTextureSampler.get()); - osg::ref_ptr shadowTextureSampler = new osg::Uniform("shadowTexture",(int)_shadowTextureUnit); + osg::ref_ptr baseTextureUnit = new osg::Uniform("baseTextureUnit",(int)_baseTextureUnit); + _uniforms.push_back(baseTextureUnit.get()); + + osg::ref_ptr shadowTextureSampler = new osg::Uniform("shadowTexture",(int)_baseShadowTextureUnit); _uniforms.push_back(shadowTextureSampler.get()); + osg::ref_ptr shadowTextureUnit = new osg::Uniform("shadowTextureUnit",(int)_baseShadowTextureUnit); + _uniforms.push_back(shadowTextureUnit.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);