OGRE - Terrain Shadow ,整合OGRE的地形和PSSM+LiSPSM阴影算法

OGRE版本1.7.1的Sample里包含了地形的示例,且用到了PSSM+LiSPSM。

今天尝试把Character的Sinbad模型摆到地形上,不过模型的材质本身不支持PSSM。

看了下地形示例的实现,发现它在给Shader传PSSM分界点数据时,用了把模板材质clone一份,再设置diffusemap和pssm分界点的方法,相当让人无语....

 

view plain copy to clipboard print ?
  1. MaterialPtr ret = MaterialManager::getSingleton().getByName(matName);   
  2. MaterialPtr baseMat = MaterialManager::getSingleton().getByName("Ogre/shadow/depth/integrated/pssm");   
  3. ret = baseMat->clone(matName);   
  4. Pass* p = ret->getTechnique(0)->getPass(0);   
  5. p->getTextureUnitState("diffuse")->setTextureName(textureName);   
  6. Vector4 splitPoints;   
  7. const PSSMShadowCameraSetup::SplitPointList& splitPointList =    
  8.     static_cast<PSSMShadowCameraSetup*>(mPSSMSetup.get())->getSplitPoints();   
  9. for (int i = 0; i < 3; ++i)   
  10. {   
  11.     splitPoints[i] = splitPointList[i];   
  12. }   
  13. p->getFragmentProgramParameters()->setNamedConstant("pssmSplitPoints", splitPoints);  
view plain
  1. MaterialPtr ret = MaterialManager::getSingleton().getByName(matName);  
  2. MaterialPtr baseMat = MaterialManager::getSingleton().getByName("Ogre/shadow/depth/integrated/pssm");  
  3. ret = baseMat->clone(matName);  
  4. Pass* p = ret->getTechnique(0)->getPass(0);  
  5. p->getTextureUnitState("diffuse")->setTextureName(textureName);  
  6. Vector4 splitPoints;  
  7. const PSSMShadowCameraSetup::SplitPointList& splitPointList =   
  8.     static_cast<PSSMShadowCameraSetup*>(mPSSMSetup.get())->getSplitPoints();  
  9. for (int i = 0; i < 3; ++i)  
  10. {  
  11.     splitPoints[i] = splitPointList[i];  
  12. }  
  13. p->getFragmentProgramParameters()->setNamedConstant("pssmSplitPoints", splitPoints);  
 

 

如果有几百种不同贴图的物件,就得在代码里手动为每一个物件设置diffusemap和pssm分界点,前者其实在材质脚本中可以用继承来覆盖,后者其实整个场景用到的数据都是一样的。好在OGRE增加了shared_params,可以多个shader共享相同的参数,在代码里只设置一遍就可以了。

修改后的fp:

 

view plain copy to clipboard print ?
  1. shared_params pssm_params   
  2. {   
  3.     shared_param_named pssmSplitPoints float4   
  4. }   
  5. fragment_program Ogre/shadow/receiver/depth/pssm3/fp cg   
  6. {   
  7.     source depthshadowobject.cg   
  8.     profiles ps_2_x arbfp1   
  9.     entry_point main_fp   
  10.     compile_arguments -DSHADOWCASTER=0 -DDEPTH_SHADOWCASTER=0 -DDEPTH_SHADOWRECEIVER=1    
  11.     default_params   
  12.     {   
  13.         param_named_auto materialAmbient derived_ambient_light_colour   
  14.         // shadow samplers are indexes 1/2/3   
  15.         param_named_auto inverseShadowmapSize0 inverse_texture_size 1   
  16.         param_named_auto inverseShadowmapSize1 inverse_texture_size 2   
  17.         param_named_auto inverseShadowmapSize2 inverse_texture_size 3   
  18.         //SET THIS MANUALLY!   
  19.         //param_named pssmSplitPoints 0 0 0 0   
  20.         shared_params_ref pssm_params   
  21.     }   
  22. }  
view plain
  1. shared_params pssm_params  
  2. {  
  3.     shared_param_named pssmSplitPoints float4  
  4. }  
  5. fragment_program Ogre/shadow/receiver/depth/pssm3/fp cg  
  6. {  
  7.     source depthshadowobject.cg  
  8.     profiles ps_2_x arbfp1  
  9.     entry_point main_fp  
  10.     compile_arguments -DSHADOWCASTER=0 -DDEPTH_SHADOWCASTER=0 -DDEPTH_SHADOWRECEIVER=1   
  11.     default_params  
  12.     {  
  13.         param_named_auto materialAmbient derived_ambient_light_colour  
  14.         // shadow samplers are indexes 1/2/3  
  15.         param_named_auto inverseShadowmapSize0 inverse_texture_size 1  
  16.         param_named_auto inverseShadowmapSize1 inverse_texture_size 2  
  17.         param_named_auto inverseShadowmapSize2 inverse_texture_size 3  
  18.         //SET THIS MANUALLY!  
  19.         //param_named pssmSplitPoints 0 0 0 0  
  20.         shared_params_ref pssm_params  
  21.     }  
  22. }  
 

 

c++:

 

view plain copy to clipboard print ?
  1. Vector4 splitPoints;   
  2. const PSSMShadowCameraSetup::SplitPointList& splitPointList =    
  3. static_cast<PSSMShadowCameraSetup*>(mPSSMSetup.get())->getSplitPoints();   
  4. for (int i = 0; i < 3; ++i)   
  5. {   
  6.     splitPoints[i] = splitPointList[i];   
  7. }   
  8. GpuSharedParametersPtr p = GpuProgramManager::getSingleton().getSharedParameters("pssm_params");   
  9. p->setNamedConstant("pssmSplitPoints", splitPoints);  
view plain
  1. Vector4 splitPoints;  
  2. const PSSMShadowCameraSetup::SplitPointList& splitPointList =   
  3. static_cast<PSSMShadowCameraSetup*>(mPSSMSetup.get())->getSplitPoints();  
  4. for (int i = 0; i < 3; ++i)  
  5. {  
  6.     splitPoints[i] = splitPointList[i];  
  7. }  
  8. GpuSharedParametersPtr p = GpuProgramManager::getSingleton().getSharedParameters("pssm_params");  
  9. p->setNamedConstant("pssmSplitPoints", splitPoints);  
 

 

OGRE - Terrain Shadow ,整合OGRE的地形和PSSM+LiSPSM阴影算法_第1张图片

 

http://blog.csdn.net/cometeor/archive/2010/06/09/5657168.aspx


你可能感兴趣的:(OGRE - Terrain Shadow ,整合OGRE的地形和PSSM+LiSPSM阴影算法)