/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2011 Robert Osfield * * This library is open source and may be redistributed and/or modified under * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or * (at your option) any later version. The full license is in LICENSE file * included with this distribution, and on the openscenegraph.org website. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * OpenSceneGraph Public License for more details. */ #include #include #include #include using namespace osgShadow; ////////////////////////////////////////////////////////////////// // fragment shader // #if 0 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"; #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 { public: RenderLeafTraverser() { } void traverse(const osgUtil::RenderStage* rs) { traverse(static_cast(rs)); } void traverse(const osgUtil::RenderBin* renderBin) { const osgUtil::RenderBin::RenderBinList& rbl = renderBin->getRenderBinList(); for(osgUtil::RenderBin::RenderBinList::const_iterator itr = rbl.begin(); itr != rbl.end(); ++itr) { traverse(itr->second.get()); } const osgUtil::RenderBin::RenderLeafList& rll = renderBin->getRenderLeafList(); for(osgUtil::RenderBin::RenderLeafList::const_iterator itr = rll.begin(); itr != rll.end(); ++itr) { handle(*itr); } const osgUtil::RenderBin::StateGraphList& rgl = renderBin->getStateGraphList(); for(osgUtil::RenderBin::StateGraphList::const_iterator itr = rgl.begin(); itr != rgl.end(); ++itr) { traverse(*itr); } } void traverse(const osgUtil::StateGraph* stateGraph) { const osgUtil::StateGraph::ChildList& cl = stateGraph->_children; for(osgUtil::StateGraph::ChildList::const_iterator itr = cl.begin(); itr != cl.end(); ++itr) { traverse(itr->second.get()); } const osgUtil::StateGraph::LeafList& ll = stateGraph->_leaves; for(osgUtil::StateGraph::LeafList::const_iterator itr = ll.begin(); itr != ll.end(); ++itr) { handle(itr->get()); } } inline void handle(const osgUtil::RenderLeaf* renderLeaf) { this->operator()(renderLeaf); } }; /////////////////////////////////////////////////////////////////////////////////////////////// // // VDSMCameraCullCallback // class VDSMCameraCullCallback : public osg::NodeCallback { public: VDSMCameraCullCallback(ViewDependentShadowMap* vdsm, osg::Polytope& polytope); virtual void operator()(osg::Node*, osg::NodeVisitor* nv); osg::RefMatrix* getProjectionMatrix() { return _projectionMatrix.get(); } osgUtil::RenderStage* getRenderStage() { return _renderStage.get(); } protected: ViewDependentShadowMap* _vdsm; osg::ref_ptr _projectionMatrix; osg::ref_ptr _renderStage; osg::Polytope _polytope; }; VDSMCameraCullCallback::VDSMCameraCullCallback(ViewDependentShadowMap* vdsm, osg::Polytope& polytope): _vdsm(vdsm), _polytope(polytope) { } void VDSMCameraCullCallback::operator()(osg::Node* node, osg::NodeVisitor* nv) { osgUtil::CullVisitor* cv = dynamic_cast(nv); osg::Camera* camera = dynamic_cast(node); OSG_INFO<<"VDSMCameraCullCallback::operator()(osg::Node* "<getProjectionCullingStack().back(); cs.setFrustum(_polytope); cv->pushCullingSet(); } #endif if (_vdsm->getShadowedScene()) { _vdsm->getShadowedScene()->osg::Group::traverse(*nv); } #if 1 if (!_polytope.empty()) { OSG_INFO<<"Popping custom Polytope"<popCullingSet(); } #endif _renderStage = cv->getCurrentRenderBin()->getStage(); OSG_INFO<<"VDSM second : _renderStage = "<<_renderStage<getComputeNearFarMode() != osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR) { // make sure that the near plane is computed correctly. cv->computeNearPlane(); osg::Matrixd projection = *(cv->getProjectionMatrix()); OSG_INFO<<"RTT Projection matrix "<setProjectionMatrix(projection); _projectionMatrix = cv->getProjectionMatrix(); } } /////////////////////////////////////////////////////////////////////////////////////////////// // // LightData // ViewDependentShadowMap::LightData::LightData(ViewDependentShadowMap::ViewDependentData* vdd): _viewDependentData(vdd), directionalLight(false) { } void ViewDependentShadowMap::LightData::setLightData(osg::RefMatrix* lm, const osg::Light* l, const osg::Matrixd& modelViewMatrix) { lightMatrix = lm; light = l; lightPos = light->getPosition(); directionalLight = (light->getPosition().w()== 0.0); if (directionalLight) { lightPos3.set(0.0, 0.0, 0.0); // directional light has no destinct position lightDir.set(-lightPos.x(), -lightPos.y(), -lightPos.z()); lightDir.normalize(); OSG_INFO<<" Directional light, lightPos="<setReferenceFrame(osg::Camera::ABSOLUTE_RF_INHERIT_VIEWPOINT); //_camera->setClearColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); _camera->setClearColor(osg::Vec4(0.0f,0.0f,0.0f,0.0f)); _camera->setComputeNearFarMode(osg::Camera::COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES); //_camera->setComputeNearFarMode(osg::Camera::COMPUTE_NEAR_FAR_USING_PRIMITIVES); // set viewport _camera->setViewport(0,0,textureSize.x(),textureSize.y()); if (debug) { // clear just the depth buffer _camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); // render after the main camera _camera->setRenderOrder(osg::Camera::POST_RENDER); // attach the texture and use it as the color buffer. //_camera->attach(osg::Camera::DEPTH_BUFFER, _texture.get()); _camera->attach(osg::Camera::COLOR_BUFFER, _texture.get()); } else { // clear the depth and colour bufferson each clear. _camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); // set the camera to render before the main camera. _camera->setRenderOrder(osg::Camera::PRE_RENDER); // tell the camera to use OpenGL frame buffer object where supported. _camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); // attach the texture and use it as the color buffer. _camera->attach(osg::Camera::DEPTH_BUFFER, _texture.get()); //_camera->attach(osg::Camera::COLOR_BUFFER, _texture.get()); } } void ViewDependentShadowMap::ShadowData::releaseGLObjects(osg::State* state) const { OSG_INFO<<"ViewDependentShadowMap::ShadowData::releaseGLObjects"<releaseGLObjects(state); _camera->releaseGLObjects(state); } /////////////////////////////////////////////////////////////////////////////////////////////// // // Frustum // ViewDependentShadowMap::Frustum::Frustum(osgUtil::CullVisitor* cv, double minZNear, double maxZFar): corners(8), faces(6), edges(12) { projectionMatrix = *(cv->getProjectionMatrix()); modelViewMatrix = *(cv->getModelViewMatrix()); OSG_INFO<<"Projection matrix "<getComputeNearFarMode()!=osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR) { double zNear = osg::maximum(static_cast(cv->getCalculatedNearPlane()),minZNear); double zFar = osg::minimum(static_cast(cv->getCalculatedFarPlane()),maxZFar); cv->clampProjectionMatrix(projectionMatrix, zNear, zFar); OSG_INFO<<"zNear = "<releaseGLObjects(state); } } /////////////////////////////////////////////////////////////////////////////////////////////// // // ViewDependentShadowMap // ViewDependentShadowMap::ViewDependentShadowMap(): ShadowTechnique() { _shadowRecievingPlaceholderStateSet = new osg::StateSet; } ViewDependentShadowMap::ViewDependentShadowMap(const ViewDependentShadowMap& vdsm, const osg::CopyOp& copyop): ShadowTechnique(vdsm,copyop) { _shadowRecievingPlaceholderStateSet = new osg::StateSet; } ViewDependentShadowMap::~ViewDependentShadowMap() { } void ViewDependentShadowMap::init() { if (!_shadowedScene) return; OSG_NOTICE<<"ViewDependentShadowMap::init()"< lock(_viewDependentDataMapMutex); ViewDependentDataMap::iterator itr = _viewDependentDataMap.find(cv); if (itr!=_viewDependentDataMap.end()) return itr->second.get(); osg::ref_ptr vdd = createViewDependentData(cv); _viewDependentDataMap[cv] = vdd; return vdd.release(); } void ViewDependentShadowMap::update(osg::NodeVisitor& nv) { OSG_INFO<<"ViewDependentShadowMap::update(osg::NodeVisitor& "<<&nv<<")"<osg::Group::traverse(nv); } void ViewDependentShadowMap::cull(osgUtil::CullVisitor& cv) { OSG_INFO<osg::Group::traverse(cv); return; } ViewDependentData* vdd = getViewDependentData(&cv); if (!vdd) { OSG_NOTICE<<"Warning, now ViewDependentData created, unable to create shadows."<osg::Group::traverse(cv); return; } OSG_INFO<<"cv->getProjectionMatrix()="<<*cv.getProjectionMatrix()< camera = sd->_camera; if (settings->getDebugDraw()) { camera->getViewport()->x() = pos_x; pos_x += camera->getViewport()->width() + 40; } osg::ref_ptr texgen = sd->_texgen; // 4.1 compute light space polytope // osg::Polytope polytope = computeLightViewFrustumPolytope(frustum, pl); // if polytope is empty then no rendering. if (polytope.empty()) { OSG_NOTICE<<"Polytope empty no shadow to render"<getViewMatrix()); polytope.transformProvidingInverse(invertModelView); osg::ref_ptr vdsmCallback = new VDSMCameraCullCallback(this, polytope); camera->setCullCallback(vdsmCallback.get()); // 4.3 traverse RTT camera // cv.pushStateSet(_shadowCastingStateSet.get()); cullShadowCastingScene(&cv, camera.get()); cv.popStateSet(); if (!orthographicViewFrustum && settings->getShadowMapProjectionHint()==ShadowSettings::PERSPECTIVE_SHADOW_MAP) { adjustPerspectiveShadowMapCameraSettings(vdsmCallback->getRenderStage(), frustum, pl, camera.get()); if (vdsmCallback->getProjectionMatrix()) { vdsmCallback->getProjectionMatrix()->set(camera->getProjectionMatrix()); } } // 4.4 compute main scene graph TexGen + uniform settings + setup state // assignTexGenSettings(&cv, camera.get(), textureUnit, texgen.get()); // mark the light as one that has active shadows and requires shaders pl.textureUnits.push_back(textureUnit); // pass on shadow data to ShadowDataList sd->_textureUnit = textureUnit; sdl.push_back(sd); // increment counters. ++textureUnit; ++numValidShadows ; } } if (numValidShadows>0) { decoratorStateGraph->setStateSet(selectStateSetForRenderingShadow(*vdd)); } // OSG_NOTICE<<"End of shadow setup Projection matrix "<<*cv.getProjectionMatrix()<getLightDataList(); LightDataList previous_ldl; previous_ldl.swap(pll); //MR testing giving a specific light osgUtil::RenderStage * rs = cv->getCurrentRenderBin()->getStage(); OSG_INFO<<"selectActiveLights osgUtil::RenderStage="<getModelViewMatrix()); osgUtil::PositionalStateContainer::AttrMatrixList& aml = rs->getPositionalStateContainer()->getAttrMatrixList(); const ShadowSettings* settings = getShadowedScene()->getShadowSettings(); for(osgUtil::PositionalStateContainer::AttrMatrixList::reverse_iterator itr = aml.rbegin(); itr != aml.rend(); ++itr) { const osg::Light* light = dynamic_cast(itr->first.get()); if (light) { // is LightNum matched to that defined in settings if (settings && settings->getLightNum()>=0 && light->getLightNum()!=settings->getLightNum()) continue; LightDataList::iterator pll_itr = pll.begin(); for(; pll_itr != pll.end(); ++pll_itr) { if ((*pll_itr)->light->getLightNum()==light->getLightNum()) break; } if (pll_itr==pll.end()) { OSG_INFO<<"Light num "<getLightNum()<setLightData(itr->second.get(), light, modelViewMatrix); pll.push_back(ld); } else { OSG_INFO<<"Light num "<getLightNum()<<" already used, ignore light"<getShadowSettings(); if (!settings->getDebugDraw()) { // note soft (attribute only no mode override) setting. When this works ? // 1. for objects prepared for backface culling // because they usually also set CullFace and CullMode on in their state // For them we override CullFace but CullMode remains set by them // 2. For one faced, trees, and similar objects which cannot use // backface nor front face so they usually use CullMode off set here. // In this case we will draw them in their entirety. _shadowCastingStateSet->setAttribute( new osg::CullFace( osg::CullFace::FRONT ), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE ); // make sure GL_CULL_FACE is off by default // we assume that if object has cull face attribute set to back // it will also set cull face mode ON so no need for override _shadowCastingStateSet->setMode( GL_CULL_FACE, osg::StateAttribute::OFF ); } #if 1 float factor = 1.1; float units = 4.0; #else float factor = -1.1; float units = -4.0; #endif _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); _uniforms.clear(); osg::ref_ptr baseTextureSampler = new osg::Uniform("baseTexture",(int)_baseTextureUnit); _uniforms.push_back(baseTextureSampler.get()); osg::ref_ptr baseTextureUnit = new osg::Uniform("baseTextureUnit",(int)_baseTextureUnit); _uniforms.push_back(baseTextureUnit.get()); osg::ref_ptr shadowTextureSampler = new osg::Uniform("shadowTexture",(int)(settings->getBaseShadowTextureUnit())); _uniforms.push_back(shadowTextureSampler.get()); osg::ref_ptr shadowTextureUnit = new osg::Uniform("shadowTextureUnit",(int)(settings->getBaseShadowTextureUnit())); _uniforms.push_back(shadowTextureUnit.get()); switch(settings->getShaderHint()) { case(ShadowSettings::NO_SHADERS): { OSG_NOTICE<<"No shaders provided by, user must supply own shaders"< 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()); break; } } { osg::ref_ptr image = new osg::Image; image->allocateImage( 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE ); *(osg::Vec4ub*)image->data() = osg::Vec4ub( 0xFF, 0xFF, 0xFF, 0xFF ); _fallbackBaseTexture = new osg::Texture2D(image.get()); _fallbackBaseTexture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::REPEAT); _fallbackBaseTexture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::REPEAT); _fallbackBaseTexture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::NEAREST); _fallbackBaseTexture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::NEAREST); _fallbackShadowMapTexture = new osg::Texture2D(image.get()); _fallbackShadowMapTexture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::REPEAT); _fallbackShadowMapTexture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::REPEAT); _fallbackShadowMapTexture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::NEAREST); _fallbackShadowMapTexture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::NEAREST); } } osg::Polytope ViewDependentShadowMap::computeLightViewFrustumPolytope(Frustum& frustum, LightData& positionedLight) { OSG_INFO<<"computeLightViewFrustumPolytope()"<getShadowSettings(); double dotProduct_v = positionedLight.lightDir * frustum.frustumCenterLine; double gamma_v = acos(dotProduct_v); if (gamma_vgetPerspectiveShadowMapCutOffAngle()) || gamma_v>osg::DegreesToRadians(180.0-settings->getPerspectiveShadowMapCutOffAngle())) { OSG_INFO<<"View direction and Light direction below tolerance"<setProjectionMatrixAsOrtho(xMin,xMax, yMin, yMax,0.0,zMax-zMin); camera->setViewMatrixAsLookAt(frustum.center+positionedLight.lightDir*zMin, frustum.center, lightUp); } } else { double zMax=-DBL_MAX; OSG_INFO<<"lightDir = "<=0.0 && d1>=0.0) { // OSG_NOTICE<<" Edge completely inside"<first; osg::Vec3d& v1 = itr->second; osg::Vec3d intersection = v0 - (v1-v0)*(d0/(d1-d0)); // OSG_NOTICE<<" Edge across clip plane, v0="<_modelview.get()!=previous_modelview) { previous_modelview = renderLeaf->_modelview.get(); if (previous_modelview) { light_mvp.mult(*renderLeaf->_modelview, light_p); } else { // no modelview matrix (such as when LightPointNode is in the scene graph) so assume // that modelview matrix is indentity. light_mvp = light_p; } // OSG_INFO<<"Computing new light_mvp "<_drawable->getBound(); if (bb.valid()) { // OSG_NOTICE<<"checked extents of "<_drawable->getName()<setProjectionMatrix(light_p); } #endif osg::Vec3d eye_v = frustum.eye * light_v; osg::Vec3d centerNearPlane_v = frustum.centerNearPlane * light_v; osg::Vec3d center_v = frustum.center * light_v; osg::Vec3d viewdir_v = center_v-eye_v; viewdir_v.normalize(); double dotProduct_v = lightdir * viewdir_v; double gamma_v = acos(dotProduct_v); if (gamma_vgetPerspectiveShadowMapCutOffAngle()) || gamma_v>osg::DegreesToRadians(180-settings->getPerspectiveShadowMapCutOffAngle())) { // OSG_NOTICE<<"Light and view vectors near parrallel - use standard shadow map."<getMinimumShadowMapNearFarRatio()); if (nsetMode(osg::TexGen::EYE_LINEAR); // compute the matrix which takes a vertex from local coords into tex coords // We actually use two matrices one used to define texgen // and second that will be used as modelview when appling to OpenGL texgen->setPlanesFromMatrix( camera->getProjectionMatrix() * osg::Matrix::translate(1.0,1.0,1.0) * osg::Matrix::scale(0.5f,0.5f,0.5f) ); // Place texgen with modelview which removes big offsets (making it float friendly) osg::ref_ptr refMatrix = new osg::RefMatrix( camera->getInverseViewMatrix() * (*(cv->getModelViewMatrix())) ); cv->getRenderStage()->getPositionalStateContainer()->addPositionedTextureAttribute( textureUnit, refMatrix.get(), texgen ); return true; } void ViewDependentShadowMap::cullShadowReceivingScene(osgUtil::CullVisitor* cv) const { OSG_INFO<<"cullShadowReceivingScene()"<getTraversalMask(); cv->setTraversalMask( traversalMask & _shadowedScene->getReceivesShadowTraversalMask() ); #endif _shadowedScene->osg::Group::traverse(*cv); #if 0 cv->setTraversalMask( traversalMask ); #endif return; } void ViewDependentShadowMap::cullShadowCastingScene(osgUtil::CullVisitor* cv, osg::Camera* camera) const { OSG_INFO<<"cullShadowCastingScene()"<getTraversalMask(); cv->setTraversalMask( traversalMask & _shadowedScene->getCastsShadowTraversalMask() ); if (camera) camera->accept(*cv); cv->setTraversalMask( traversalMask ); return; } osg::StateSet* ViewDependentShadowMap::selectStateSetForRenderingShadow(ViewDependentData& vdd) const { OSG_INFO<<" selectStateSetForRenderingShadow() "< stateset = vdd.getStateSet(); vdd.getStateSet()->clear(); vdd.getStateSet()->setTextureAttributeAndModes(0, _fallbackBaseTexture.get(), osg::StateAttribute::ON); for(Uniforms::const_iterator itr=_uniforms.begin(); itr!=_uniforms.end(); ++itr) { stateset->addUniform(itr->get()); } if (_program.valid()) { stateset->setAttribute(_program.get()); } LightDataList& pll = vdd.getLightDataList(); for(LightDataList::iterator itr = pll.begin(); itr != pll.end(); ++itr) { // 3. create per light/per shadow map division of lightspace/frustum // create a list of light/shadow map data structures LightData& pl = (**itr); // if no texture units have been activated for this light then no shadow state required. if (pl.textureUnits.empty()) continue; for(LightData::ActiveTextureUnits::iterator atu_itr = pl.textureUnits.begin(); atu_itr != pl.textureUnits.end(); ++atu_itr) { OSG_INFO<<" Need to assign state for "<<*atu_itr<setTextureAttributeAndModes(sd._textureUnit, sd._texture.get(), osg::StateAttribute::ON/* | osg::StateAttribute::OVERRIDE*/); stateset->setTextureMode(sd._textureUnit,GL_TEXTURE_GEN_S,osg::StateAttribute::ON); stateset->setTextureMode(sd._textureUnit,GL_TEXTURE_GEN_T,osg::StateAttribute::ON); stateset->setTextureMode(sd._textureUnit,GL_TEXTURE_GEN_R,osg::StateAttribute::ON); stateset->setTextureMode(sd._textureUnit,GL_TEXTURE_GEN_Q,osg::StateAttribute::ON); } return vdd.getStateSet(); } void ViewDependentShadowMap::resizeGLObjectBuffers(unsigned int maxSize) { // the way that ViewDependentData is mapped shouldn't } void ViewDependentShadowMap::releaseGLObjects(osg::State* state) const { OpenThreads::ScopedLock lock(_viewDependentDataMapMutex); for(ViewDependentDataMap::const_iterator itr = _viewDependentDataMap.begin(); itr != _viewDependentDataMap.end(); ++itr) { ViewDependentData* vdd = itr->second.get(); if (vdd) { vdd->releaseGLObjects(state); } } }