Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* 'viewPosition' is saved in GBuffer and written as ViewMatrix * ModelMatrix * VertexPosition
  2.  * 'linearViewDepth' is saved in GBuffer and written as LinearizeDepth(gl_FragCoord.z, nearclip, farclip)
  3.  * 'uniform_shadowmap' is a texturesampler for the shadow depth map
  4.  * 'uniform_snearclip' and 'uniform_sfarclip' are the near and far clip planes for the shadowmaps projection
  5.  * 'uniform_invViewMatrix' is the inverse of ViewMatrix
  6.  * 'uniform_lightViewProjMatrix' is the viewprojection matrix for the spotlight "camera"
  7.  */
  8.  
  9. float LinearizeDepth(float depth, float near, float far)
  10. {
  11.   float z = depth * 2.0 - 1.0;
  12.   return (2.0 * near * far) / (far + near - z * (far - near));
  13. }
  14.  
  15. void checkShadow(vec3 viewPosition, float linearViewDepth)
  16. {
  17.   vec4 worldPosition = uniform_invViewMatrix * vec4(viewPosition, 1);
  18.   vec4 lightPosition = uniform_lightViewProjMatrix * worldPosition;
  19.   lightPosition.xyz /= lightPosition.w;
  20.   vec2 shadowUV = lightPosition.xy * vec2(0.5) + vec2(0.5);
  21.   float linearShadowDepth = LinearizeDepth(texture(uniform_shadowmap, shadowUV).r, uniform_snearclip, uniform_sfarclip);
  22.   if (linearShadowDepth < linearViewDepth)
  23.    discard;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement