Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


Spotlight Shadow map


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Demx   Members   -  Reputation: 361

Like
0Likes
Like

Posted 28 October 2013 - 06:18 AM

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, 28 October 2013 - 06:21 AM.


Sponsor:

#2 N.I.B.   Members   -  Reputation: 1214

Like
0Likes
Like

Posted 28 October 2013 - 06:33 AM

Look here:

http://www.gamedev.net/topic/278954-sampler2dshadow---glsl/



#3 Demx   Members   -  Reputation: 361

Like
0Likes
Like

Posted 28 October 2013 - 07:44 AM

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, 28 October 2013 - 07:45 AM.


#4 kalle_h   Members   -  Reputation: 1573

Like
3Likes
Like

Posted 28 October 2013 - 02:42 PM

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, 28 October 2013 - 02:44 PM.


#5 Demx   Members   -  Reputation: 361

Like
0Likes
Like

Posted 29 October 2013 - 01:01 PM

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, 29 October 2013 - 01:06 PM.


#6 N.I.B.   Members   -  Reputation: 1214

Like
0Likes
Like

Posted 29 October 2013 - 01:09 PM

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.

#7 Demx   Members   -  Reputation: 361

Like
0Likes
Like

Posted 29 October 2013 - 01:19 PM

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



#8 kalle_h   Members   -  Reputation: 1573

Like
0Likes
Like

Posted 06 November 2013 - 05:09 AM

 

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS