3

I've just worked my way through this OpenGL shadow mapping tutorial. While I understand the basic algorithm, one thing puzzles me: During the 2nd render pass all vertices are transformed into the clip space of the light source. This is done by multiplying them with the light's view-projection matrix in the vertex shader:

vs_out.FragPosLightSpace = lightSpaceMatrix * vec4(vs_out.FragPos, 1.0);

However, for texture lookup into the shadow map a perspective division is needed. This is done in the fragment shader:

float ShadowCalculation(vec4 fragPosLightSpace)
{
    // perform perspective divide
    vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
    //continue w. texture lookup
    [...]
}

So my question is - why can't I perform the perspective division in the vertex shader? I did try moving the division from fragment to vertex shader in my otherwise finished shadow mapping code, and ended up with some really weird artifacts. So I guess it has something to do with the interpolation performed by the rasterizer, but I would like a more detailed explanation if possible.

rapunzel
  • 31
  • 1
  • 1
    "*This is done in the fragment shader*" That's silly. OpenGL has [dedicated `Proj` texture accessing functions that do the divide for you](https://www.khronos.org/opengl/wiki/Sampler_(GLSL)#Projective_texture_access). I'm surprised that the tutorial doesn't bother to use them, since letting you know they exist is half the point of such a tutorial in the first place. – Nicol Bolas Nov 24 '19 at 14:46

0 Answers0