Jump to content
  • Advertisement
Sign in to follow this  
Demx

OpenGL Spotlight Shadow map

This topic is 2082 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

hi, 

I have managed to create a directional shadow map with decent results so i decided to go on and try implementing 

shadows for spot lights.

 

From what I understand the basic principle is the same as directional lights but using a perspective projection 

instead of an orthogonic projection.

 

 

So i use glm::perspective to create the new projection matrix..

glm::vec3 slPos = glm::vec3( 1.0f, 3.0f, 0.0f );
glm::vec3 slDir = glm::vec3( glm::normalize( glm::vec3( 0.0f, 0.0f, 0.0f ) - slPos ) );


glm::mat4 depthProjectionMatrix = glm::perspective<float>( 45.0f, 1.0f, 2.0f, 50.0f );
glm::mat4 depthViewMatrix = glm::lookAt( slPos, glm::vec3( 0.0f, 0.0f, 0.0f ), glm::vec3( 0.0f, 1.0f, 0.0f ) );

 

And this is the glsl code I use to access the depth value in the sampler2DShadow texture.

if( texture( shadowMap, vec3( ShadowCoord.xyz ) / ShadowCoord.w ) < ( ShadowCoord.z / ShadowCoord.w ) )
{
     shade = 0.3f;
}

 

Applying these changes i get this result  .. there is the correct shadow but also a long shadow that shouldn't be there and I really can't find

an obvious bug..

 

opengl_test2_2013_10_28_12_15_11_97.png

Edited by Demx

Share this post


Link to post
Share on other sites
Advertisement

i tried with

 

 

void main()
{
vec4 MaterialDiffuseColor = texture( gSampler, texCoord );


//if( texture( shadowMap, vec3( ShadowCoord.xyz ) / ShadowCoord.w ) < ( ShadowCoord.z/ShadowCoord.w ) )
//{
// shade = 0.3f;
//}


vec4 LightColor = vec4( 0.0, 0.0, 0.0, 0.0 );


LightColor += getSpotLightColor( spotLights[0] );


float fDiffuseIntensity = max(0.0, dot( normalize(vNormal), -sunLight.vDirection));
LightColor += vec4(sunLight.vColor*(sunLight.fAmbientIntensity+fDiffuseIntensity), 1.0);


float shade = textureProj( shadowMap, ShadowCoord );


outputColor = MaterialDiffuseColor * LightColor * shade;
}

 

but same result sad.png

Edited by Demx

Share this post


Link to post
Share on other sites

Clamp texture coordinates to 0-1 range.

Also it's good trick to leave 1 pixel borders at edge with depth value of 1 and use scissors when rendering shadow maps. This will prevent some artifacts when samping outside of shadow map. But with spotlight light fallof should do the trick.

 

Ps. shadow2DProj returns shadow value that can be used direclty but textureProj return just depth value!

Edited by kalle_h

Share this post


Link to post
Share on other sites

Clamp texture coordinates to 0-1 range.

Also it's good trick to leave 1 pixel borders at edge with depth value of 1 and use scissors when rendering shadow maps. This will prevent some artifacts when samping outside of shadow map. But with spotlight light fallof should do the trick.

 

Ps. shadow2DProj returns shadow value that can be used direclty but textureProj return just depth value!

 

shadow2DProj doesn't work for me .. 

 

 

should this work in theory?

 

if( ( ShadowCoord.x >= 0 && ShadowCoord.x <= 1 ) &&
    ( ShadowCoord.y >= 0 && ShadowCoord.y <= 1 ) )
{
    if( texture( shadowMap, vec3( ShadowCoord.xyz ) / ShadowCoord.w ) < ( ShadowCoord.z/ShadowCoord.w ) )
    {
        shade = 0.3;
    }
}


outputColor = MaterialDiffuseColor * LightColor * shade;
Edited by Demx

Share this post


Link to post
Share on other sites
Assuming you initialize shade to 0, then yes.
Basically, this check tells you that the current pixel is out of the texture, and hence out of the light frustum.

Share this post


Link to post
Share on other sites

Assuming you initialize shade to 0, then yes.
Basically, this check tells you that the current pixel is out of the texture, and hence out of the light frustum.

 

actually i had to divide the variables in the if by ShadowCoord.w .. apart from that it works well now :D

Share this post


Link to post
Share on other sites

 

Assuming you initialize shade to 0, then yes.
Basically, this check tells you that the current pixel is out of the texture, and hence out of the light frustum.

 

actually i had to divide the variables in the if by ShadowCoord.w .. apart from that it works well now biggrin.png

 

 

Just set texture wrap mode to clamp and you can loose that branch compleatly.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!