Jump to content
  • Advertisement
Sign in to follow this  
Xzatrox

Problem with Cube Shadow Maping

This topic is 4204 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Good day! Help me please! I am try to implement Cube Shadow Map for Omni Lights it seems to me that I have problem with self shadowing or may be with depth buffer this is my code that I am use for rendering in the depth buffer and after that using it for shadowing... I am use right-handed coordinate system pass ShadowMap { VertexShader = compile vs_1_1 ShadowMap_VS(); PixelShader = compile ps_2_0 ShadowMap_PS(); CullMode = CW; ZEnable = true; ZWriteEnable = true; ZFunc = Less; AlphaBlendEnable = false; } void ShadowMap_VS(VS_INPUT_ShadowMap In, out VS_OUTPUT_ShadowMap Out) { Out.Pos = mul(float4(In.Pos, 1.0f), WorldViewProjection); Out.WorldPos = mul(In.Pos, World); } void ShadowMap_PS(VS_OUTPUT_ShadowMap In, out PS_OUTPUT_ShadowMap Out) { g_LightPosition.w = 1.0f; float3 lightVec = In.WorldPos.xyz - g_LightPosition; float distanceToLight = dot(lightVec, lightVec)/400; Out.DistanceToLight = float4(distanceToLight, 0, 0, 0); } // For debug purposes only pass ShowStencilClippedPixels { VertexShader = compile vs_1_1 LightVS(); PixelShader = compile ps_2_0 StrongAmbientPS(); ZWriteEnable = false; ZEnable = false; AlphaBlendEnable = true; SrcBlend = One; DestBlend = One; CullMode = CCW; ColorWriteEnable = 0x0000000F; // Set stencil test to pass when equals 1 (and zeroe all values) StencilEnable = true; StencilFunc = Equal; StencilFail = Zero; StencilZFail = Keep; StencilPass = Zero; } void LightVS(VS_INPUT_Light In, out VS_OUTPUT_Light Out) { float4 pos = mul(In.Pos, WorldViewProjection); Out.Pos = pos; Out.Pos2 = pos; // Convert projection space position within: // (-1, 1) -> (-1, 1) // to tex-projection space position (offset to texel's center) within: // (0.5 / width, 0.5 / height) -> (1 + 0.5 / width, 1 + 0.5 / height) float4 vPos = mul(In.Pos, WorldView); Out.PosProj = mul(pos, PostProjRescale); } #define DEBUG_VIEW void StrongAmbientPS(VS_OUTPUT_Light In, out PS_OUTPUT_Light Out) { // Get color only half4 color = tex2Dproj(g_SamplerSrcColor, In.PosProj); half depth = tex2Dproj(g_SamplerSrcPosition, In.PosProj); // Reconstruct original view space position half4 position = mul(half4(In.Pos2.xy / In.Pos2.w, depth, 1), ProjI); position.xyz /= position.w; position.w = 1; #ifdef DEBUG_VIEW half3 worldPosition = mul(position, ViewI).xyz; half3 worldPosToLight = worldPosition.xyz - g_LightPosition.xyz; worldPosToLight.z = -worldPosToLight.z; Out.Color = texCUBE(g_SamplerShadowMap, worldPosToLight); // show the RGB render instead #else /*!DEBUG_VIEW */ half shadow = calculateShadow(mul(position, ViewI).xyz); Out.Color = g_LightColor * color * 0.33 * shadow; #endif } half calculateShadow(half3 worldPosition) { half3 worldPosToLight = worldPosition.xyz - g_LightPosition.xyz; worldPosToLight.z = -worldPosToLight.z; half shadowDistanceToLight = texCUBE(g_SamplerShadowMap, worldPosToLight).r*400 + 0.1; half pixelDistanceToLight = dot(worldPosToLight, worldPosToLight); return (-pixelDistanceToLight + shadowDistanceToLight) > -0.0001f; } case D3DCUBEMAP_FACE_POSITIVE_X: vLookatPt = D3DXVECTOR3(1.0f, 0.0f, 0.0f); vUpVec = D3DXVECTOR3(0.0f, 1.0f, 0.0f); break; case D3DCUBEMAP_FACE_NEGATIVE_X: vLookatPt = D3DXVECTOR3(-1.0f, 0.0f, 0.0f); vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f); break; case D3DCUBEMAP_FACE_POSITIVE_Y: vLookatPt = D3DXVECTOR3(0.0f, 1.0f, 0.0f); vUpVec = D3DXVECTOR3(0.0f, 0.0f,1.0f); break; case D3DCUBEMAP_FACE_NEGATIVE_Y: vLookatPt = D3DXVECTOR3(0.0f,-1.0f, 0.0f); vUpVec = D3DXVECTOR3(0.0f, 0.0f, -1.0f); break; case D3DCUBEMAP_FACE_POSITIVE_Z: vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, -1.0f); vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f); break; case D3DCUBEMAP_FACE_NEGATIVE_Z: vLookatPt = D3DXVECTOR3(0.0f, 0.0f,1.0f); vUpVec = D3DXVECTOR3(0.0f, 1.0f, 0.0f); break; // Post projection rescale matrix needed for appropriate render target textures lookup // Converts projection space position within: // (-1, 1) -> (-1, 1) // to tex-projection space position (offset to texel's center) within: // (0.5 / width, 0.5 / height) -> (1 + 0.5 / width, 1 + 0.5 / height) unsigned int range = 1; float fBias = -0.001f * (float)range; D3DXMATRIX texScaleBiasMat( 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, (float)range, 0.0f, fOffsetX, fOffsetY, fBias, 1.0f );

Share this post


Link to post
Share on other sites
Advertisement
Maybe you could explain a bit more about the problem you're having? Do you see rendering artifacts, or doesn't it work at all? How about a couple of screenshots that illustrate your problem?

Regards

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!