diff --git a/src/osgShadow/ParallelSplitShadowMap.cpp b/src/osgShadow/ParallelSplitShadowMap.cpp index 57ce2c7e4..2f822103e 100644 --- a/src/osgShadow/ParallelSplitShadowMap.cpp +++ b/src/osgShadow/ParallelSplitShadowMap.cpp @@ -62,7 +62,7 @@ using namespace osgShadow; //#define SHADOW_TEXTURE_DEBUG // COLOR instead of DEPTH #ifndef SHADOW_TEXTURE_DEBUG - #define SHADOW_TEXTURE_GLSL +#define SHADOW_TEXTURE_GLSL #endif ////////////////////////////////////////////////////////////////////////// @@ -74,115 +74,115 @@ std::string ParallelSplitShadowMap::FragmentShaderGenerator::generateGLSL_Fragme bool filtered, unsigned int nbrSplits, unsigned int textureOffset -) { - std::stringstream sstr; + ) { + std::stringstream sstr; - /// base texture - sstr << "uniform sampler2D baseTexture; " << std::endl; - sstr << "uniform float enableBaseTexture; " << std::endl; - sstr << "uniform vec2 ambientBias;" << std::endl; - - for (unsigned int i=0;isetTextureSize(pssmShadowSplitTexture._resolution, pssmShadowSplitTexture._resolution); - #ifndef SHADOW_TEXTURE_DEBUG - pssmShadowSplitTexture._texture->setInternalFormat(GL_DEPTH_COMPONENT); - pssmShadowSplitTexture._texture->setShadowComparison(true); - pssmShadowSplitTexture._texture->setShadowTextureMode(osg::Texture2D::LUMINANCE); - #else - pssmShadowSplitTexture._texture->setInternalFormat(GL_RGBA); - #endif +#ifndef SHADOW_TEXTURE_DEBUG + pssmShadowSplitTexture._texture->setInternalFormat(GL_DEPTH_COMPONENT); + pssmShadowSplitTexture._texture->setShadowComparison(true); + pssmShadowSplitTexture._texture->setShadowTextureMode(osg::Texture2D::LUMINANCE); +#else + pssmShadowSplitTexture._texture->setInternalFormat(GL_RGBA); +#endif pssmShadowSplitTexture._texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::NEAREST); pssmShadowSplitTexture._texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::NEAREST); pssmShadowSplitTexture._texture->setBorderColor(osg::Vec4(1.0,1.0,1.0,1.0)); @@ -320,27 +320,27 @@ void ParallelSplitShadowMap::init(){ pssmShadowSplitTexture._camera->setCullCallback(new CameraCullCallback(this)); - #ifndef SHADOW_TEXTURE_DEBUG - pssmShadowSplitTexture._camera->setClearMask(GL_DEPTH_BUFFER_BIT); +#ifndef SHADOW_TEXTURE_DEBUG + pssmShadowSplitTexture._camera->setClearMask(GL_DEPTH_BUFFER_BIT); + pssmShadowSplitTexture._camera->setClearColor(osg::Vec4(1.0,1.0,1.0,1.0)); +#else + pssmShadowSplitTexture._camera->setClearMask(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); + switch(iCameras) + { + case 0: + pssmShadowSplitTexture._camera->setClearColor(osg::Vec4(1.0,0.0,0.0,1.0)); + break; + case 1: + pssmShadowSplitTexture._camera->setClearColor(osg::Vec4(0.0,1.0,0.0,1.0)); + break; + case 2: + pssmShadowSplitTexture._camera->setClearColor(osg::Vec4(0.0,0.0,1.0,1.0)); + break; + default: pssmShadowSplitTexture._camera->setClearColor(osg::Vec4(1.0,1.0,1.0,1.0)); - #else - pssmShadowSplitTexture._camera->setClearMask(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); - switch(iCameras) - { - case 0: - pssmShadowSplitTexture._camera->setClearColor(osg::Vec4(1.0,0.0,0.0,1.0)); - break; - case 1: - pssmShadowSplitTexture._camera->setClearColor(osg::Vec4(0.0,1.0,0.0,1.0)); - break; - case 2: - pssmShadowSplitTexture._camera->setClearColor(osg::Vec4(0.0,0.0,1.0,1.0)); - break; - default: - pssmShadowSplitTexture._camera->setClearColor(osg::Vec4(1.0,1.0,1.0,1.0)); - break; - } - #endif + break; + } +#endif pssmShadowSplitTexture._camera->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR); pssmShadowSplitTexture._camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF_INHERIT_VIEWPOINT); @@ -354,23 +354,25 @@ void ParallelSplitShadowMap::init(){ pssmShadowSplitTexture._camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); // attach the texture and use it as the color buffer. - #ifndef SHADOW_TEXTURE_DEBUG - pssmShadowSplitTexture._camera->attach(osg::Camera::DEPTH_BUFFER, pssmShadowSplitTexture._texture.get()); - #else - pssmShadowSplitTexture._camera->attach(osg::Camera::COLOR_BUFFER, pssmShadowSplitTexture._texture.get()); - #endif +#ifndef SHADOW_TEXTURE_DEBUG + pssmShadowSplitTexture._camera->attach(osg::Camera::DEPTH_BUFFER, pssmShadowSplitTexture._texture.get()); +#else + pssmShadowSplitTexture._camera->attach(osg::Camera::COLOR_BUFFER, pssmShadowSplitTexture._texture.get()); +#endif osg::StateSet* stateset = pssmShadowSplitTexture._camera->getOrCreateStateSet(); ////////////////////////////////////////////////////////////////////////// - float factor = _polgyonOffset.x(); - float units = _polgyonOffset.y(); - osg::ref_ptr polygon_offset = new osg::PolygonOffset; - polygon_offset->setFactor(factor); - polygon_offset->setUnits(units); - stateset->setAttribute(polygon_offset.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); - stateset->setMode(GL_POLYGON_OFFSET_FILL, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); + if ( _user_polgyonOffset_set ) { + float factor = _polgyonOffset.x(); + float units = _polgyonOffset.y(); + osg::ref_ptr polygon_offset = new osg::PolygonOffset; + polygon_offset->setFactor(factor); + polygon_offset->setUnits(units); + stateset->setAttribute(polygon_offset.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); + stateset->setMode(GL_POLYGON_OFFSET_FILL, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); + } ////////////////////////////////////////////////////////////////////////// @@ -407,90 +409,90 @@ void ParallelSplitShadowMap::init(){ ////////////////////////////////////////////////////////////////////////// // set up shader (GLSL) - #ifdef SHADOW_TEXTURE_GLSL +#ifdef SHADOW_TEXTURE_GLSL - osg::Program* program = new osg::Program; - pssmShadowSplitTexture._stateset->setAttribute(program); + osg::Program* program = new osg::Program; + pssmShadowSplitTexture._stateset->setAttribute(program); - ////////////////////////////////////////////////////////////////////////// - // GLSL PROGRAMS - osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, - _FragmentShaderGenerator->generateGLSL_FragmentShader_BaseTex( - _debug_color_in_GLSL, - iCameras, - pssmShadowSplitTexture._resolution, - _GLSL_shadow_filtered, - _number_of_splits, - _textureUnitOffset - ).c_str()); - program->addShader(fragment_shader); + ////////////////////////////////////////////////////////////////////////// + // GLSL PROGRAMS + osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, + _FragmentShaderGenerator->generateGLSL_FragmentShader_BaseTex( + _debug_color_in_GLSL, + iCameras, + pssmShadowSplitTexture._resolution, + _GLSL_shadow_filtered, + _number_of_splits, + _textureUnitOffset + ).c_str()); + program->addShader(fragment_shader); - ////////////////////////////////////////////////////////////////////////// - // UNIFORMS - std::stringstream strST; strST << "shadowTexture" << (pssmShadowSplitTexture._textureUnit-_textureUnitOffset); - osg::Uniform* shadowTextureSampler = new osg::Uniform(strST.str().c_str(),(int)(pssmShadowSplitTexture._textureUnit)); - pssmShadowSplitTexture._stateset->addUniform(shadowTextureSampler); + ////////////////////////////////////////////////////////////////////////// + // UNIFORMS + std::stringstream strST; strST << "shadowTexture" << (pssmShadowSplitTexture._textureUnit-_textureUnitOffset); + osg::Uniform* shadowTextureSampler = new osg::Uniform(strST.str().c_str(),(int)(pssmShadowSplitTexture._textureUnit)); + pssmShadowSplitTexture._stateset->addUniform(shadowTextureSampler); - //TODO: NOT YET SUPPORTED in the current version of the shader - if ( ! _ambientBiasUniform ) { - _ambientBiasUniform = new osg::Uniform("ambientBias",_ambientBias); - pssmShadowSplitTexture._stateset->addUniform(_ambientBiasUniform); - } + //TODO: NOT YET SUPPORTED in the current version of the shader + if ( ! _ambientBiasUniform ) { + _ambientBiasUniform = new osg::Uniform("ambientBias",_ambientBias); + pssmShadowSplitTexture._stateset->addUniform(_ambientBiasUniform); + } - std::stringstream strzShadow; strzShadow << "zShadow" << (pssmShadowSplitTexture._textureUnit-_textureUnitOffset); - pssmShadowSplitTexture._farDistanceSplit = new osg::Uniform(strzShadow.str().c_str(),1.0f); - pssmShadowSplitTexture._stateset->addUniform(pssmShadowSplitTexture._farDistanceSplit); + std::stringstream strzShadow; strzShadow << "zShadow" << (pssmShadowSplitTexture._textureUnit-_textureUnitOffset); + pssmShadowSplitTexture._farDistanceSplit = new osg::Uniform(strzShadow.str().c_str(),1.0f); + pssmShadowSplitTexture._stateset->addUniform(pssmShadowSplitTexture._farDistanceSplit); - osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",0); - pssmShadowSplitTexture._stateset->addUniform(baseTextureSampler); + osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",0); + pssmShadowSplitTexture._stateset->addUniform(baseTextureSampler); - osg::Uniform* randomTextureSampler = new osg::Uniform("randomTexture",(int)(_textureUnitOffset+_number_of_splits)); - pssmShadowSplitTexture._stateset->addUniform(randomTextureSampler); + osg::Uniform* randomTextureSampler = new osg::Uniform("randomTexture",(int)(_textureUnitOffset+_number_of_splits)); + pssmShadowSplitTexture._stateset->addUniform(randomTextureSampler); - if ( _textureUnitOffset > 0 ) { - osg::Uniform* enableBaseTexture = new osg::Uniform("enableBaseTexture",1.0f); - pssmShadowSplitTexture._stateset->addUniform(enableBaseTexture); - } else { - osg::Uniform* enableBaseTexture = new osg::Uniform("enableBaseTexture",0.0f); - pssmShadowSplitTexture._stateset->addUniform(enableBaseTexture); - } + if ( _textureUnitOffset > 0 ) { + osg::Uniform* enableBaseTexture = new osg::Uniform("enableBaseTexture",1.0f); + pssmShadowSplitTexture._stateset->addUniform(enableBaseTexture); + } else { + osg::Uniform* enableBaseTexture = new osg::Uniform("enableBaseTexture",0.0f); + pssmShadowSplitTexture._stateset->addUniform(enableBaseTexture); + } - for (unsigned int textLoop(0);textLoop<_textureUnitOffset;textLoop++) - { - // fake texture for baseTexture, add a fake texture - // we support by default at least one texture layer - // without this fake texture we can not support - // textured and not textured scene + for (unsigned int textLoop(0);textLoop<_textureUnitOffset;textLoop++) + { + // fake texture for baseTexture, add a fake texture + // we support by default at least one texture layer + // without this fake texture we can not support + // textured and not textured scene - // TODO: at the moment the PSSM supports just one texture layer in the GLSL shader, multitexture are - // not yet supported ! + // TODO: at the moment the PSSM supports just one texture layer in the GLSL shader, multitexture are + // not yet supported ! - osg::Image* image = new osg::Image; - // allocate the image data, noPixels x 1 x 1 with 4 rgba floats - equivalent to a Vec4! - int noPixels = 1; - image->allocateImage(noPixels,1,1,GL_RGBA,GL_FLOAT); - image->setInternalTextureFormat(GL_RGBA); - // fill in the image data. - osg::Vec4* dataPtr = (osg::Vec4*)image->data(); - osg::Vec4f color(1.0f,1.0f,1.0f,0.0f); - *dataPtr = color; - // make fake texture - osg::Texture2D* texture = new osg::Texture2D; - texture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP_TO_BORDER); - texture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP_TO_BORDER); - texture->setBorderColor(osg::Vec4(1.0,1.0,1.0,1.0)); - texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR); - texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR); - texture->setImage(image); - // add fake texture - pssmShadowSplitTexture._stateset->setTextureAttribute(textLoop,texture,osg::StateAttribute::ON); - pssmShadowSplitTexture._stateset->setTextureMode(textLoop,GL_TEXTURE_1D,osg::StateAttribute::OFF); - pssmShadowSplitTexture._stateset->setTextureMode(textLoop,GL_TEXTURE_2D,osg::StateAttribute::ON); - pssmShadowSplitTexture._stateset->setTextureMode(textLoop,GL_TEXTURE_3D,osg::StateAttribute::OFF); - } - #endif + osg::Image* image = new osg::Image; + // allocate the image data, noPixels x 1 x 1 with 4 rgba floats - equivalent to a Vec4! + int noPixels = 1; + image->allocateImage(noPixels,1,1,GL_RGBA,GL_FLOAT); + image->setInternalTextureFormat(GL_RGBA); + // fill in the image data. + osg::Vec4* dataPtr = (osg::Vec4*)image->data(); + osg::Vec4f color(1.0f,1.0f,1.0f,0.0f); + *dataPtr = color; + // make fake texture + osg::Texture2D* texture = new osg::Texture2D; + texture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP_TO_BORDER); + texture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP_TO_BORDER); + texture->setBorderColor(osg::Vec4(1.0,1.0,1.0,1.0)); + texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR); + texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR); + texture->setImage(image); + // add fake texture + pssmShadowSplitTexture._stateset->setTextureAttribute(textLoop,texture,osg::StateAttribute::ON); + pssmShadowSplitTexture._stateset->setTextureMode(textLoop,GL_TEXTURE_1D,osg::StateAttribute::OFF); + pssmShadowSplitTexture._stateset->setTextureMode(textLoop,GL_TEXTURE_2D,osg::StateAttribute::ON); + pssmShadowSplitTexture._stateset->setTextureMode(textLoop,GL_TEXTURE_3D,osg::StateAttribute::OFF); + } +#endif ////////////////////////////////////////////////////////////////////////// @@ -500,12 +502,12 @@ void ParallelSplitShadowMap::init(){ pssmShadowSplitTexture._debug_textureUnit = 1; pssmShadowSplitTexture._debug_texture = new osg::Texture2D; pssmShadowSplitTexture._debug_texture->setTextureSize(TEXTURE_RESOLUTION, TEXTURE_RESOLUTION); - #ifdef SHOW_SHADOW_TEXTURE_DEBUG - pssmShadowSplitTexture._debug_texture->setInternalFormat(GL_DEPTH_COMPONENT); - pssmShadowSplitTexture._debug_texture->setShadowTextureMode(osg::Texture2D::LUMINANCE); - #else - pssmShadowSplitTexture._debug_texture->setInternalFormat(GL_RGBA); - #endif +#ifdef SHOW_SHADOW_TEXTURE_DEBUG + pssmShadowSplitTexture._debug_texture->setInternalFormat(GL_DEPTH_COMPONENT); + pssmShadowSplitTexture._debug_texture->setShadowTextureMode(osg::Texture2D::LUMINANCE); +#else + pssmShadowSplitTexture._debug_texture->setInternalFormat(GL_RGBA); +#endif pssmShadowSplitTexture._debug_texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR); pssmShadowSplitTexture._debug_texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR); // create the camera @@ -522,11 +524,11 @@ void ParallelSplitShadowMap::init(){ // tell the camera to use OpenGL frame buffer object where supported. pssmShadowSplitTexture._debug_camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); // attach the texture and use it as the color buffer. - #ifdef SHOW_SHADOW_TEXTURE_DEBUG - pssmShadowSplitTexture._debug_camera->attach(osg::Camera::DEPTH_BUFFER, pssmShadowSplitTexture._debug_texture.get()); - #else - pssmShadowSplitTexture._debug_camera->attach(osg::Camera::COLOR_BUFFER, pssmShadowSplitTexture._debug_texture.get()); - #endif +#ifdef SHOW_SHADOW_TEXTURE_DEBUG + pssmShadowSplitTexture._debug_camera->attach(osg::Camera::DEPTH_BUFFER, pssmShadowSplitTexture._debug_texture.get()); +#else + pssmShadowSplitTexture._debug_camera->attach(osg::Camera::COLOR_BUFFER, pssmShadowSplitTexture._debug_texture.get()); +#endif osg::StateSet* stateset = pssmShadowSplitTexture._debug_camera->getOrCreateStateSet(); pssmShadowSplitTexture._debug_stateset = new osg::StateSet; @@ -801,7 +803,7 @@ void ParallelSplitShadowMap::calculateFrustumCorners( } - pssmShadowSplitTexture._split_far = camFar; + pssmShadowSplitTexture._split_far = camFar; @@ -839,7 +841,7 @@ void ParallelSplitShadowMap::calculateLightInitialPosition(PSSMShadowSplitTextur pssmShadowSplitTexture._frustumSplitCenter /= 8.0; - // + // // To avoid edge problems, scale the frustum so // that it's at least a few pixels larger // @@ -903,7 +905,7 @@ void ParallelSplitShadowMap::calculateLightViewProjectionFormFrustum(PSSMShadowS // use quaternion -> numerical more robust osg::Quat qRot; qRot.makeRotate(viewDir,pssmShadowSplitTexture._lightDirection); - osg::Vec3d top = qRot * camUp; + osg::Vec3d top = qRot * camUp; osg::Vec3d right = qRot * camRight; // calculate the camera's frustum right,right,bottom,top parameters @@ -914,7 +916,7 @@ void ParallelSplitShadowMap::calculateLightViewProjectionFormFrustum(PSSMShadowS { osg::Vec3d diffCorner(pssmShadowSplitTexture._lightCameraSource - frustumCorners[i]); - double lright(diffCorner*right); + double lright(diffCorner*right); double lTop(diffCorner*top); if ( lright > maxRight ) maxRight = lright; @@ -931,11 +933,11 @@ void ParallelSplitShadowMap::calculateLightViewProjectionFormFrustum(PSSMShadowS pssmShadowSplitTexture._camera->setProjectionMatrixAsOrtho(minRight,maxRight,minTop,maxTop,pssmShadowSplitTexture._lightNear,pssmShadowSplitTexture._lightFar); - #ifdef SHADOW_TEXTURE_GLSL - // get user cameras - osg::Vec3d vProjCamFraValue = (camEye + viewDir * pssmShadowSplitTexture._split_far) * (pssmShadowSplitTexture._cameraView * pssmShadowSplitTexture._cameraProj); - pssmShadowSplitTexture._farDistanceSplit->set((float)vProjCamFraValue.z()); - #endif +#ifdef SHADOW_TEXTURE_GLSL + // get user cameras + osg::Vec3d vProjCamFraValue = (camEye + viewDir * pssmShadowSplitTexture._split_far) * (pssmShadowSplitTexture._cameraView * pssmShadowSplitTexture._cameraProj); + pssmShadowSplitTexture._farDistanceSplit->set((float)vProjCamFraValue.z()); +#endif }