Changeset 7462
- Timestamp:
- 09/24/07 17:24:23
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
OpenSceneGraph/trunk/examples/osgshadow/osgshadow.cpp
r7452 r7462 500 500 arguments.getApplicationUsage()->addCommandLineOption("--mapres", "ParallelSplitShadowMap texture resolution.");//ADEGLI 501 501 arguments.getApplicationUsage()->addCommandLineOption("--debug-color", "ParallelSplitShadowMap display debugging color (only the first 3 maps are color r=0,g=1,b=2.");//ADEGLI 502 502 arguments.getApplicationUsage()->addCommandLineOption("--minNearSplit", "ParallelSplitShadowMap shadow map near offset.");//ADEGLI 503 arguments.getApplicationUsage()->addCommandLineOption("--maxFarDist", "ParallelSplitShadowMap max far distance to shadow.");//ADEGLI 504 arguments.getApplicationUsage()->addCommandLineOption("--NVidea", "ParallelSplitShadowMap set default PolygonOffset for NVidea.");//ADEGLI 505 arguments.getApplicationUsage()->addCommandLineOption("--PolyOffset-Factor", "ParallelSplitShadowMap set PolygonOffset factor.");//ADEGLI 506 arguments.getApplicationUsage()->addCommandLineOption("--PolyOffset-Unit", "ParallelSplitShadowMap set PolygonOffset unit.");//ADEGLI 507 arguments.getApplicationUsage()->addCommandLineOption("--CullFaceFront", "ParallelSplitShadowMap add a cull face: front.");//ADEGLI 508 509 503 510 arguments.getApplicationUsage()->addCommandLineOption("-1", "Use test model one."); 504 511 arguments.getApplicationUsage()->addCommandLineOption("-2", "Use test model two."); … … 626 633 osg::ref_ptr<osgShadow::ParallelSplitShadowMap> pssm = new osgShadow::ParallelSplitShadowMap(NULL,mapcount); 627 634 628 unsignedint mapres = 1024;629 while (arguments.read("--mapres", mapres)) ;630 pssm->setTextureResolution(mapres);635 int mapres = 1024; 636 while (arguments.read("--mapres", mapres)) 637 pssm->setTextureResolution(mapres); 631 638 632 639 while (arguments.read("--debug-color")) { pssm->setDebugColorOn(); } 633 640 641 642 int minNearSplit=0; 643 while (arguments.read("--minNearSplit", minNearSplit)) 644 if ( minNearSplit > 0 ) { 645 pssm->setMinNearDistanceForSplits(minNearSplit); 646 std::cout << "ParallelSplitShadowMap : setMinNearDistanceForSplits(" << minNearSplit <<")" << std::endl; 647 } 648 649 int maxfardist = 0; 650 while (arguments.read("--maxFarDist", maxfardist)) 651 if ( maxfardist > 0 ) { 652 pssm->setMaxFarDistance(maxfardist); 653 std::cout << "ParallelSplitShadowMap : setMaxFarDistance(" << maxfardist<<")" << std::endl; 654 } 655 656 double polyoffsetfactor = -0.02; 657 double polyoffsetunit = 1.0; 658 while (arguments.read("--PolyOffset-Factor", polyoffsetfactor)); 659 while (arguments.read("--PolyOffset-Unit", polyoffsetunit)); 660 pssm->setPolygonOffset(osg::Vec2(polyoffsetfactor,polyoffsetunit)); //ATI Radeon 661 662 if (arguments.read("--NVidea")){ 663 //pssm->setPolygonOffset(osg::Vec2(-0.02,1.0)); //ATI Radeon 664 pssm->setPolygonOffset(osg::Vec2(10.0f,20.0f)); //NVidea 665 } 666 667 if ( arguments.read("--CullFaceFront") ) { 668 pssm->forceFrontCullFace(); 669 } 634 670 635 671 shadowedScene->setShadowTechnique(pssm.get()); OpenSceneGraph/trunk/include/osgShadow/ParallelSplitShadowMap
r7452 r7462 21 21 22 22 #include <osgShadow/ShadowTechnique> 23 23 24 24 namespace osgShadow { 25 25 26 class OSGSHADOW_EXPORT ParallelSplitShadowMap : public ShadowTechnique 26 27 { … … 57 58 inline void setTextureResolution(unsigned int resolution) { _resolution = resolution; } 58 59 60 /** Set the max far distance */ 61 inline void setMaxFarDistance(double farDist) { _setMaxFarDistance = farDist; _isSetMaxFarDistance = true; } 62 63 /** Force to add a osg::CullFace::FRONT state */ 64 inline void forceFrontCullFace() { _useFrontCullFace = true; } 65 66 /** Set min near distance for splits */ 67 inline void setMinNearDistanceForSplits(double nd){ _split_min_near_dist=nd; } 68 69 /** use linear split (default: linear) */ 70 inline void useLinearSplit(bool flag) { _linearSplit = flag;} 71 72 73 /** 74 light / | 75 \ / | 76 min near dist / | 77 for splits / | 78 \ / Š | 79 ./ \ Š | 80 | \ Š | 81 | x Š | 82 | Š | 83 . | | 84 \ Š | 85 \ Š | 86 \ | 87 \ | 88 \ | 89 90 .<- max far dist. ->. 91 */ 59 92 protected : 60 93 … … 120 153 unsigned int _resolution; 121 154 155 double _setMaxFarDistance; 156 bool _isSetMaxFarDistance; 157 158 bool _useFrontCullFace; 159 160 double _split_min_near_dist; 161 162 bool _linearSplit; 163 122 164 }; 123 165 } OpenSceneGraph/trunk/src/osgShadow/ParallelSplitShadowMap.cpp
r7452 r7462 20 20 21 21 #include <osgShadow/ParallelSplitShadowMap> 22 22 23 #include <osgShadow/ShadowedScene> 23 24 #include <osg/Notify> … … 33 34 #include <osg/Texture1D> 34 35 36 37 35 38 using namespace osgShadow; 39 36 40 37 41 … … 110 114 111 115 112 sstr << " float c0=0.5;" << std::endl; 113 sstr << " float c1=0.5;" << std::endl; 114 sstr << " float c2=0.5;" << std::endl; 116 sstr << " float c0=0.0;" << std::endl; 117 sstr << " float c1=0.0;" << std::endl; 118 sstr << " float c2=0.0;" << std::endl; 119 120 sstr << " float sumTerm=0.0;" << std::endl; 115 121 116 122 for (unsigned int i=0;i<_number_of_splits;i++) { 117 if ( i < 3 ) sstr << " c" << i << "= 0.5+0.5*term" << i << ";" << std::endl;118 }119 120 sstr << " vec4 color = gl_Color*vec4(c0,c1,c2,1.0); " << std::endl; 121 122 sstr << " vec4 texcolor = vec4(1,1,1,1); " << std::endl; 123 if ( i < 3 ) sstr << " c" << i << "=term" << i << ";" << std::endl; 124 sstr << " sumTerm=sumTerm+term" << i << ";" << std::endl; 125 } 126 127 sstr << " vec4 color = gl_Color * ( 1.0 - sumTerm ) + (sumTerm)* gl_Color*vec4(c0,(1.0-c0)*c1,(1.0-c0)*(1.0-c1)*c2,1.0); " << std::endl; 128 123 129 } else { 124 130 sstr << " vec4 color = gl_Color; "<< std::endl; 125 sstr << " vec4 texcolor = texture2D(baseTexture,gl_TexCoord[0].st); " << std::endl;126 } 127 128 131 } 132 133 134 sstr << " vec4 texcolor = texture2D(baseTexture,gl_TexCoord[0].st); " << std::endl; 129 135 130 136 131 137 sstr << " float enableBaseTextureFilter = enableBaseTexture*(1.0 - step(texcolor.x+texcolor.y+texcolor.z+texcolor.a,0.0)); " << std::endl; //18 132 sstr << "vec4 colorTex = color*texcolor;" << std::endl;138 sstr << " vec4 colorTex = color*texcolor;" << std::endl; 133 139 sstr << " gl_FragColor = (color*(1.0-enableBaseTextureFilter) + colorTex*enableBaseTextureFilter)*(1.0-0.30*v); "<< std::endl; 134 135 136 140 137 141 … … 160 164 _debug_color_in_GLSL(false), 161 165 _user_polgyonOffset_set(false), 162 _resolution(TEXTURE_RESOLUTION) 166 _resolution(TEXTURE_RESOLUTION), 167 _isSetMaxFarDistance(false), 168 _useFrontCullFace(false), 169 _split_min_near_dist(ZNEAR_MIN_FROM_LIGHT_SOURCE), 170 _linearSplit(LINEAR_SPLIT) 163 171 { 164 172 _displayTexturesGroupingNode = gr; … … 172 180 ShadowTechnique(copy,copyop), 173 181 _textureUnitOffset(copy._textureUnitOffset), 174 _number_of_splits(copy._number_of_splits) 182 _debug_color_in_GLSL(copy._debug_color_in_GLSL), 183 _user_polgyonOffset_set(copy._user_polgyonOffset_set), 184 _resolution(copy._resolution), 185 _isSetMaxFarDistance(copy._isSetMaxFarDistance), 186 _useFrontCullFace(copy._useFrontCullFace), 187 _split_min_near_dist(copy._split_min_near_dist), 188 _linearSplit(copy._linearSplit), 189 _number_of_splits(copy._number_of_splits), 190 _polgyonOffset(copy._polgyonOffset), 191 _setMaxFarDistance(copy._setMaxFarDistance) 192 175 193 { 176 194 } … … 244 262 } 245 263 #endif 246 pssmShadowSplitTexture._camera->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR);264 pssmShadowSplitTexture._camera->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR); 247 265 248 266 // set viewport … … 274 292 stateset->setMode(GL_POLYGON_OFFSET_FILL, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); 275 293 } 294 295 if ( _useFrontCullFace ) { 296 osg::ref_ptr<osg::CullFace> cull_face = new osg::CullFace; 297 cull_face->setMode(osg::CullFace::FRONT); 298 stateset->setAttribute(cull_face.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); 299 stateset->setMode(GL_CULL_FACE, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); 300 } 276 301 } 277 302 // set up stateset and append texture, texGen ,... … … 305 330 pssmShadowSplitTexture._stateset->addUniform(shadowTextureSampler); 306 331 307 //TODO: NOT YET SUPPORTED in the current version of the shader 332 //TODO: NOT YET SUPPORTED in the current version of the shader 308 333 //std::stringstream strAB; strAB << "ambientBias" << (pssmShadowSplitTexture._textureUnit-1); 309 334 //osg::Uniform* ambientBias = new osg::Uniform(strAB.str().c_str(),pssmShadowSplitTexture._ambientBias); … … 331 356 { 332 357 // fake texture for baseTexture, add a fake texture 333 // we support by default at least one texture layer 334 // without this fake texture we can not support 335 // textured and not textured scene 336 337 // TODO: at the moment the PSSM supports just one texture layer in the GLSL shader, multitexture are 358 // we support by default at least one texture layer 359 // without this fake texture we can not support 360 // textured and not textured scene 361 362 // TODO: at the moment the PSSM supports just one texture layer in the GLSL shader, multitexture are 338 363 // not yet supported ! 339 364 … … 642 667 pssmShadowSplitTexture._cameraProj.getPerspective(fovy,aspectRatio,camNear,camFar); 643 668 669 670 671 if ( _isSetMaxFarDistance ) { 672 if ( camNear + _setMaxFarDistance < camFar) camFar = camNear + _setMaxFarDistance; 673 } 674 644 675 ////////////////////////////////////////////////////////////////////////// 645 676 /// CALCULATE SPLIT … … 647 678 double minNear = camNear; 648 679 double camNearFar_Dist = maxFar - camNear; 649 bool linear = LINEAR_SPLIT; 650 if ( linear ) { 680 if ( _linearSplit ) { 651 681 camFar = camNear + (camNearFar_Dist) * ((double)(pssmShadowSplitTexture._splitID+1))/((double)(_number_of_splits)); 652 682 camNear = camNear + (camNearFar_Dist) * ((double)(pssmShadowSplitTexture._splitID))/((double)(_number_of_splits)); … … 687 717 ////////////////////////////////////////////////////////////////////////// 688 718 /// TRANSFORM frustum corners (Optimized for Orthogonal) 689 osg::Matrixf projMat; 690 projMat.makePerspective(fovy,aspectRatio,camNear,camFar); 691 692 osg::Matrixf projViewMat = pssmShadowSplitTexture._cameraView*projMat; 719 693 720 osg::Matrixf invProjViewMat; 721 722 osg::Matrixf projMat; 723 projMat.makePerspective(fovy,aspectRatio,camNear,camFar); 724 osg::Matrixf projViewMat(pssmShadowSplitTexture._cameraView*projMat); 694 725 invProjViewMat.invert(projViewMat); 726 727 728 695 729 696 730 //transform frustum vertices to world space … … 710 744 // compute directional light inital postion; 711 745 void ParallelSplitShadowMap::calculateLightInitalPosition(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners){ 712 pssmShadowSplitTexture._frustumSplitCenter = frustumCorners[0];746 pssmShadowSplitTexture._frustumSplitCenter = frustumCorners[0]; 713 747 for(int i=1;i<8;i++) { 714 748 pssmShadowSplitTexture._frustumSplitCenter +=frustumCorners[i]; … … 733 767 // set 2.0m distance to the nearest point 734 768 int count = 0; 735 while (zNear <= ZNEAR_MIN_FROM_LIGHT_SOURCE&& count++ < 10) {769 while (zNear <= _split_min_near_dist && count++ < 10) { 736 770 zNear= DBL_MAX; 737 771 zFar =-DBL_MAX; … … 742 776 } 743 777 744 if ( zNear <= ZNEAR_MIN_FROM_LIGHT_SOURCE){745 osg::Vec3 dUpdate = - pssmShadowSplitTexture._lightDirection*(fabs(zNear)+ ZNEAR_MIN_FROM_LIGHT_SOURCE);778 if ( zNear <= _split_min_near_dist ){ 779 osg::Vec3 dUpdate = - pssmShadowSplitTexture._lightDirection*(fabs(zNear)+_split_min_near_dist); 746 780 pssmShadowSplitTexture._lightCameraSource = pssmShadowSplitTexture._lightCameraSource + dUpdate; 747 781 }
