Added support for user defined setting of the shadow texture unit

This commit is contained in:
Robert Osfield
2011-08-24 19:38:58 +00:00
parent 26e2106636
commit 0d3169f645
3 changed files with 46 additions and 13 deletions

View File

@@ -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());
}

View File

@@ -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<osg::StateSet> _shadowRecievingPlaceholderStateSet;
osg::ref_ptr<osg::StateSet> _shadowCastingStateSet;

View File

@@ -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 T>
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()"<<std::endl;
unsigned int _baseTextureUnit = 0;
unsigned int _shadowTextureUnit = 1;
_shadowCastingStateSet = new osg::StateSet;
@@ -795,9 +809,15 @@ void ViewDependentShadowMap::createShaders()
osg::ref_ptr<osg::Uniform> baseTextureSampler = new osg::Uniform("baseTexture",(int)_baseTextureUnit);
_uniforms.push_back(baseTextureSampler.get());
osg::ref_ptr<osg::Uniform> shadowTextureSampler = new osg::Uniform("shadowTexture",(int)_shadowTextureUnit);
osg::ref_ptr<osg::Uniform> baseTextureUnit = new osg::Uniform("baseTextureUnit",(int)_baseTextureUnit);
_uniforms.push_back(baseTextureUnit.get());
osg::ref_ptr<osg::Uniform> shadowTextureSampler = new osg::Uniform("shadowTexture",(int)_baseShadowTextureUnit);
_uniforms.push_back(shadowTextureSampler.get());
osg::ref_ptr<osg::Uniform> shadowTextureUnit = new osg::Uniform("shadowTextureUnit",(int)_baseShadowTextureUnit);
_uniforms.push_back(shadowTextureUnit.get());
//osg::ref_ptr<osg::Shader> fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource_noBaseTexture);
osg::ref_ptr<osg::Shader> fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource_withBaseTexture);