- Timestamp:
- 03/21/12 18:36:20 (14 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/src/osgShadow/ViewDependentShadowMap.cpp
r13032 r13041 177 177 { 178 178 OSG_INFO<<"Pushing custom Polytope"<<std::endl; 179 179 180 180 osg::CullingSet& cs = cv->getProjectionCullingStack().back(); 181 181 … … 200 200 201 201 OSG_INFO<<"VDSM second : _renderStage = "<<_renderStage<<std::endl; 202 202 203 203 if (cv->getComputeNearFarMode() != osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR) 204 204 { … … 256 256 { 257 257 setCullingMode(osg::CullSettings::VIEW_FRUSTUM_CULLING); 258 258 259 259 pushViewport(viewport); 260 260 pushProjectionMatrix(new osg::RefMatrix(projectionMatrix)); 261 261 pushModelViewMatrix(new osg::RefMatrix(viewMatrix),osg::Transform::ABSOLUTE_RF); 262 262 } 263 263 264 264 void apply(osg::Node& node) 265 265 { 266 266 if (isCulled(node)) return; 267 267 268 268 // push the culling mode. 269 269 pushCurrentMask(); … … 305 305 return; 306 306 } 307 307 308 308 void apply(osg::Transform& transform) 309 309 { … … 324 324 popModelViewMatrix(); 325 325 } 326 326 327 327 // pop the culling mode. 328 328 popCurrentMask(); 329 329 330 330 } 331 331 … … 339 339 { 340 340 if (!bb.valid()) return; 341 341 342 342 const osg::Matrix& matrix = *getModelViewMatrix() * *getProjectionMatrix(); 343 343 … … 370 370 osg::BoundingBox _bb; 371 371 }; 372 372 373 373 /////////////////////////////////////////////////////////////////////////////////////////////// 374 374 // … … 432 432 433 433 const ShadowSettings* settings = vdd->getViewDependentShadowMap()->getShadowedScene()->getShadowSettings(); 434 434 435 435 bool debug = settings->getDebugDraw(); 436 436 … … 443 443 osg::Vec2s textureSize = debug ? osg::Vec2s(512,512) : settings->getTextureSize(); 444 444 _texture->setTextureSize(textureSize.x(), textureSize.y()); 445 445 446 446 if (debug) 447 447 { … … 454 454 _texture->setShadowTextureMode(osg::Texture2D::LUMINANCE); 455 455 } 456 456 457 457 _texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR); 458 458 _texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR); … … 463 463 _texture->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); 464 464 //_texture->setBorderColor(osg::Vec4(0.0f,0.0f,0.0f,0.0f)); 465 465 466 466 // set up the camera 467 467 _camera = new osg::Camera; … … 477 477 // switch off small feature culling as this can cull out geometry that will still be large enough once perspective correction takes effect. 478 478 _camera->setCullingMode(_camera->getCullingMode() & ~osg::CullSettings::SMALL_FEATURE_CULLING); 479 479 480 480 // set viewport 481 481 _camera->setViewport(0,0,textureSize.x(),textureSize.y()); … … 498 498 // clear the depth and colour bufferson each clear. 499 499 _camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); 500 500 501 501 // set the camera to render before the main camera. 502 502 _camera->setRenderOrder(osg::Camera::PRE_RENDER); 503 503 504 504 // tell the camera to use OpenGL frame buffer object where supported. 505 505 _camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); … … 508 508 _camera->attach(osg::Camera::DEPTH_BUFFER, _texture.get()); 509 509 //_camera->attach(osg::Camera::COLOR_BUFFER, _texture.get()); 510 } 510 } 511 511 } 512 512 … … 542 542 OSG_INFO<<"Projection matrix after clamping "<<projectionMatrix<<std::endl; 543 543 } 544 544 545 545 corners[0].set(-1.0,-1.0,-1.0); 546 546 corners[1].set(1.0,-1.0,-1.0); … … 696 696 697 697 createShaders(); 698 698 699 699 _dirty = false; 700 700 } … … 752 752 753 753 osg::RefMatrix& viewProjectionMatrix = *cv.getProjectionMatrix(); 754 754 755 755 // check whether this main views projection is perspective or orthographic 756 756 bool orthographicViewFrustum = viewProjectionMatrix(0,3)==0.0 && … … 795 795 cv.computeNearPlane(); 796 796 } 797 797 798 798 Frustum frustum(&cv, minZNear, maxZFar); 799 799 … … 809 809 810 810 ShadowSettings* settings = getShadowedScene()->getShadowSettings(); 811 811 812 812 unsigned int pos_x = 0; 813 813 unsigned int textureUnit = settings->getBaseShadowTextureUnit(); … … 861 861 { 862 862 // osg::ElapsedTime timer; 863 863 864 864 osg::ref_ptr<osg::Viewport> viewport = new osg::Viewport(0,0,2048,2048); 865 865 ComputeLightSpaceBounds clsb(viewport.get(), projectionMatrix, viewMatrix); … … 896 896 // OSG_NOTICE<<" xMid="<<xMid<<", yMid="<<yMid<<", xRange="<<xRange<<", yRange="<<yRange<<std::endl; 897 897 898 projectionMatrix = 898 projectionMatrix = 899 899 projectionMatrix * 900 900 osg::Matrixd::translate(osg::Vec3d(-xMid,-yMid,0.0)) * 901 901 osg::Matrixd::scale(osg::Vec3d(2.0/xRange, 2.0/yRange,1.0)); 902 902 903 903 } 904 904 … … 906 906 907 907 double splitPoint = 0.0; 908 908 909 909 if (numShadowMapsPerLight>1) 910 910 { … … 913 913 osg::Vec3d viewdir_v = center_v-eye_v; viewdir_v.normalize(); 914 914 osg::Vec3d lightdir(0.0,0.0,-1.0); 915 915 916 916 double dotProduct_v = lightdir * viewdir_v; 917 917 double angle = acosf(dotProduct_v); 918 918 919 919 osg::Vec3d eye_ls = eye_v * projectionMatrix; 920 920 921 921 OSG_INFO<<"Angle between view vector and eye "<<osg::RadiansToDegrees(angle)<<std::endl; 922 922 OSG_INFO<<"eye_ls="<<eye_ls<<std::endl; … … 979 979 { 980 980 // compute the start and end range in non-dimensional coords 981 #if 0 981 #if 0 982 982 double r_start = (sm_i==0) ? -1.0 : (double(sm_i)/double(numShadowMapsPerLight)*2.0-1.0); 983 983 double r_end = (sm_i+1==numShadowMapsPerLight) ? 1.0 : (double(sm_i+1)/double(numShadowMapsPerLight)*2.0-1.0); … … 1024 1024 local_polytope.setupMask(); 1025 1025 1026 1026 1027 1027 // OSG_NOTICE<<"Need to adjust RTT camera projection and view matrix here, r_start="<<r_start<<", r_end="<<r_end<<std::endl; 1028 1028 // OSG_NOTICE<<" textureUnit = "<<textureUnit<<std::endl; … … 1037 1037 osg::Matrixd::translate(osg::Vec3d(0.0,-mid_r,0.0)) * 1038 1038 osg::Matrixd::scale(osg::Vec3d(1.0,2.0/range_r,1.0))); 1039 1039 1040 1040 } 1041 1041 … … 1071 1071 // pass on shadow data to ShadowDataList 1072 1072 sd->_textureUnit = textureUnit; 1073 1073 1074 1074 if (textureUnit >= 8) 1075 1075 { … … 1080 1080 sdl.push_back(sd); 1081 1081 } 1082 1082 1083 1083 // increment counters. 1084 1084 ++textureUnit; … … 1089 1089 if (numValidShadows>0) 1090 1090 { 1091 decoratorStateGraph->setStateSet(selectStateSetForRenderingShadow(*vdd)); 1092 } 1093 1091 decoratorStateGraph->setStateSet(selectStateSetForRenderingShadow(*vdd)); 1092 } 1093 1094 1094 // OSG_NOTICE<<"End of shadow setup Projection matrix "<<*cv.getProjectionMatrix()<<std::endl; 1095 1095 } … … 1100 1100 1101 1101 LightDataList& pll = vdd->getLightDataList(); 1102 1102 1103 1103 LightDataList previous_ldl; 1104 1104 previous_ldl.swap(pll); … … 1110 1110 1111 1111 osg::Matrixd modelViewMatrix = *(cv->getModelViewMatrix()); 1112 1112 1113 1113 osgUtil::PositionalStateContainer::AttrMatrixList& aml = 1114 1114 rs->getPositionalStateContainer()->getAttrMatrixList(); … … 1116 1116 1117 1117 const ShadowSettings* settings = getShadowedScene()->getShadowSettings(); 1118 1118 1119 1119 for(osgUtil::PositionalStateContainer::AttrMatrixList::reverse_iterator itr = aml.rbegin(); 1120 1120 itr != aml.rend(); … … 1126 1126 // is LightNum matched to that defined in settings 1127 1127 if (settings && settings->getLightNum()>=0 && light->getLightNum()!=settings->getLightNum()) continue; 1128 1128 1129 1129 LightDataList::iterator pll_itr = pll.begin(); 1130 1130 for(; pll_itr != pll.end(); ++pll_itr) … … 1143 1143 { 1144 1144 OSG_INFO<<"Light num "<<light->getLightNum()<<" already used, ignore light"<<std::endl; 1145 } 1146 } 1147 } 1148 1145 } 1146 } 1147 } 1148 1149 1149 return !pll.empty(); 1150 1150 } … … 1159 1159 1160 1160 ShadowSettings* settings = getShadowedScene()->getShadowSettings(); 1161 1161 1162 1162 if (!settings->getDebugDraw()) 1163 1163 { … … 1178 1178 _shadowCastingStateSet->setMode( GL_CULL_FACE, osg::StateAttribute::OFF ); 1179 1179 } 1180 1180 1181 1181 #if 1 1182 1182 float factor = 1.1; … … 1190 1190 _shadowCastingStateSet->setMode(GL_POLYGON_OFFSET_FILL, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); 1191 1191 1192 1192 1193 1193 _uniforms.clear(); 1194 1194 osg::ref_ptr<osg::Uniform> baseTextureSampler = new osg::Uniform("baseTexture",(int)_baseTextureUnit); … … 1236 1236 _program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource_withBaseTexture)); 1237 1237 } 1238 1238 1239 1239 break; 1240 1240 } … … 1387 1387 1388 1388 const ShadowSettings* settings = getShadowedScene()->getShadowSettings(); 1389 1389 1390 1390 double dotProduct_v = positionedLight.lightDir * frustum.frustumCenterLine; 1391 1391 double gamma_v = acos(dotProduct_v); … … 1411 1411 #endif 1412 1412 1413 1413 1414 1414 if (positionedLight.directionalLight) 1415 1415 { … … 1484 1484 1485 1485 OSG_INFO<<" cornerInLightCoords= "<<cornerInLightCoords<<std::endl; 1486 1486 1487 1487 zMax = osg::maximum( zMax, cornerInLightCoords.z()); 1488 1488 } … … 1572 1572 _edges.push_back( Edge(frustum.corners[3],frustum.corners[7]) ); 1573 1573 } 1574 1574 1575 1575 void transform(const osg::Matrixd& m) 1576 1576 { … … 1583 1583 } 1584 1584 } 1585 1586 void clip(const osg::Plane& plane) 1585 1586 void clip(const osg::Plane& plane) 1587 1587 { 1588 1588 Vertices intersections; 1589 1589 1590 1590 // OSG_NOTICE<<"clip("<<plane<<") edges.size()="<<_edges.size()<<std::endl; 1591 1591 for(Edges::iterator itr = _edges.begin(); … … 1624 1624 v1 = intersection; 1625 1625 } 1626 1626 1627 1627 ++itr; 1628 1628 } … … 1640 1640 return; 1641 1641 } 1642 1642 1643 1643 if (intersections.size() == 3) 1644 1644 { … … 1658 1658 osg::Vec3d side = (side_x.length2()>=side_y.length2()) ? side_x : side_y; 1659 1659 side.normalize(); 1660 1660 1661 1661 osg::Vec3d up = side ^ normal; 1662 1662 up.normalize(); … … 1671 1671 1672 1672 center /= double(intersections.size()); 1673 1673 1674 1674 typedef std::map<double, osg::Vec3d> VertexMap; 1675 1675 VertexMap vertexMap; … … 1694 1694 previous_v = itr->second; 1695 1695 } 1696 1696 1697 1697 // OSG_NOTICE<<" after clip("<<plane<<") edges.size()="<<_edges.size()<<std::endl; 1698 1698 } 1699 1699 1700 1700 void clip(const osg::Polytope& polytope) 1701 1701 { … … 1722 1722 return m; 1723 1723 } 1724 1724 1725 1725 double max(unsigned int index) const 1726 1726 { … … 1835 1835 n = nr; 1836 1836 } 1837 1837 1838 1838 void operator() (const osgUtil::RenderLeaf* renderLeaf) 1839 1839 { 1840 1840 ++numRenderLeaf; 1841 1841 1842 1842 if (renderLeaf->_modelview.get()!=previous_modelview) 1843 1843 { … … 1906 1906 if (z_ratio>max_z_ratio) max_z_ratio = z_ratio; 1907 1907 } 1908 1908 1909 1909 // clip to the light space 1910 1910 if (ls.x()<clip_min_x) ls.x()=clip_min_x; … … 1934 1934 osg::Vec3d eye_ls; 1935 1935 double n; 1936 1936 1937 1937 osg::Matrixd light_mvp; 1938 1938 osg::RefMatrix* previous_modelview; … … 1945 1945 double clip_min_x_ratio, clip_max_x_ratio; 1946 1946 double clip_min_z_ratio, clip_max_z_ratio; 1947 1947 1948 1948 double min_x_ratio, max_x_ratio; 1949 1949 double min_z_ratio, max_z_ratio; … … 1977 1977 //OSG_NOTICE<<"light_v="<<light_v<<std::endl; 1978 1978 //OSG_NOTICE<<"light_p="<<light_p<<std::endl; 1979 1979 1980 1980 ConvexHull convexHull; 1981 1981 convexHull.setToFrustum(frustum); 1982 1982 1983 1983 #if 0 1984 1984 OSG_NOTICE<<"ws ConvexHull xMin="<<convexHull.min(0)<<", xMax="<<convexHull.max(0)<<std::endl; 1985 1985 OSG_NOTICE<<"ws ConvexHull yMin="<<convexHull.min(1)<<", yMax="<<convexHull.max(1)<<std::endl; 1986 1986 OSG_NOTICE<<"ws ConvexHull zMin="<<convexHull.min(2)<<", zMax="<<convexHull.max(2)<<std::endl; 1987 1987 1988 1988 convexHull.output(osg::notify(osg::NOTICE)); 1989 1989 #endif 1990 1990 1991 1991 convexHull.transform(light_vp); 1992 1992 … … 2042 2042 convexHull.clip(osg::Plane(1.0,0.0,0.0,widest_x)); 2043 2043 convexHull.clip(osg::Plane(-1.0,0.0,0.0,widest_x)); 2044 #endif 2044 #endif 2045 2045 #if 1 2046 2046 convexHull.clip(osg::Plane(0.0,1.0,0.0,-rli.min_y)); … … 2057 2057 #endif 2058 2058 2059 #if 0 2059 #if 0 2060 2060 OSG_NOTICE<<"widest_x = "<<widest_x<<std::endl; 2061 2061 OSG_NOTICE<<"widest_y = "<<widest_y<<std::endl; … … 2097 2097 OSG_NOTICE<<"zMin = "<<zMin<<", \tzMax = "<<zMax<<std::endl; 2098 2098 #endif 2099 2099 2100 2100 #if 1 2101 2101 // we always want the lightspace to include the computed near plane. … … 2108 2108 -0.5*(yMax+yMin), 2109 2109 -0.5*(zMax+zMin))); 2110 2110 2111 2111 m.postMultScale(osg::Vec3d(2.0/(xMax-xMin), 2112 2112 2.0/(yMax-yMin), … … 2125 2125 2126 2126 #endif 2127 2127 2128 2128 osg::Vec3d eye_v = frustum.eye * light_v; 2129 2129 //osg::Vec3d centerNearPlane_v = frustum.centerNearPlane * light_v; … … 2142 2142 //OSG_NOTICE<<"eye_v="<<eye_v<<std::endl; 2143 2143 //OSG_NOTICE<<"viewdir_v="<<viewdir_v<<std::endl; 2144 2144 2145 2145 osg::Vec3d eye_ls = frustum.eye * light_vp; 2146 2146 #if 0 … … 2151 2151 } 2152 2152 #endif 2153 2153 2154 2154 //osg::Vec3d centerNearPlane_ls = frustum.centerNearPlane * light_vp; 2155 2155 //osg::Vec3d centerFarPlane_ls = frustum.centerFarPlane * light_vp; … … 2159 2159 osg::Vec3d side = lightdir ^ viewdir_ls; side.normalize(); 2160 2160 osg::Vec3d up = side ^ lightdir; 2161 2161 2162 2162 double d = 2.0; 2163 2163 … … 2179 2179 2180 2180 //n = z_n; 2181 2181 2182 2182 double f = n+d; 2183 2183 … … 2212 2212 OSG_NOTICE<<"convexHull max_z_ratio = "<<max_z_ratio<<std::endl; 2213 2213 #endif 2214 2214 2215 2215 #if 1 2216 2216 if (renderStage) … … 2228 2228 return false; 2229 2229 } 2230 2230 2231 2231 #if 0 2232 2232 OSG_NOTICE<<"Time for RenderLeafTraverser "<<timer.elapsedTime_m()<<"ms, number of render leaves "<<rli.numRenderLeaf<<std::endl; … … 2258 2258 2259 2259 //best_z_ratio *= 10.0; 2260 2260 2261 2261 osg::Matrixd lightPerspective( 1.0/best_x_ratio, 0.0, 0.0, 0.0, 2262 2262 0.0, a, 0.0, 1.0, … … 2303 2303 OSG_INFO<<"cullShadowReceivingScene()"<<std::endl; 2304 2304 2305 #if 0 2305 #if 0 2306 2306 // record the traversal mask on entry so we can reapply it later. 2307 2307 unsigned int traversalMask = cv->getTraversalMask(); … … 2322 2322 { 2323 2323 OSG_INFO<<"cullShadowCastingScene()"<<std::endl; 2324 2324 2325 2325 // record the traversal mask on entry so we can reapply it later. 2326 2326 unsigned int traversalMask = cv->getTraversalMask(); … … 2340 2340 2341 2341 osg::ref_ptr<osg::StateSet> stateset = vdd.getStateSet(); 2342 2342 2343 2343 vdd.getStateSet()->clear(); 2344 2344 2345 2345 vdd.getStateSet()->setTextureAttributeAndModes(0, _fallbackBaseTexture.get(), osg::StateAttribute::ON); 2346 2346 … … 2403 2403 stateset->setTextureMode(sd._textureUnit,GL_TEXTURE_GEN_T,osg::StateAttribute::ON); 2404 2404 stateset->setTextureMode(sd._textureUnit,GL_TEXTURE_GEN_R,osg::StateAttribute::ON); 2405 stateset->setTextureMode(sd._textureUnit,GL_TEXTURE_GEN_Q,osg::StateAttribute::ON); 2405 stateset->setTextureMode(sd._textureUnit,GL_TEXTURE_GEN_Q,osg::StateAttribute::ON); 2406 2406 } 2407 2407 … … 2411 2411 void ViewDependentShadowMap::resizeGLObjectBuffers(unsigned int maxSize) 2412 2412 { 2413 // the way that ViewDependentData is mapped shouldn't 2413 // the way that ViewDependentData is mapped shouldn't 2414 2414 } 2415 2415
