for my deferred rendering engine I am adding cookies for directional lights, the process should be easy - but I am not getting proper results.
Okay, so from the C++ side, I create my directional light View and Projection matrices as such:
mat4 projection = glm::ortho<float>(-1,1,-1,1,-1,1);
mat4 view = glm::lookAt(vec3(0,0,0), direction, vec3(0,1,0));
where direction is normalized. The View and Projection matrices are passed in the shader as Projection * View.
Now we go in the directional light shader where the magic happens. I reconstruct the view space position from the depth buffer (which works as the light renders fine without the cookie), and I embark to sample the cookie texture like this:
float attenuateFromCookie(sampler2D cookieSampler, vec4 fragmentPos, mat4 lightMatrix)
{
vec4 lightSpacePos = lightMatrix * invView * fragmentPos;
vec2 projCoords = lightSpacePos.xy * projectionScale - 0.5/IN.GBufferSize;
return texture(cookieSampler, projCoords).r;
}
where projectionScale is just a vec2 to scale the cookie, so it repeats several times to look good (clouds for example).
Now, the cookie renders fine, however when I move my camera, the cookie goes berserk, moving around very fast. It should be noted that the cookie does not move when my camera is rotated, since I multiply by invView. However, wouldn't this fix the camera translation issue as well?
My guess is I am doing something wrong in the light view matrix.
Can someone help me?
Thanks a lot