Directional light cookies

Started by
7 comments, last by alkisbkn 11 years, 10 months ago
Hello everyone,

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 smile.png
Advertisement
No ideas at all? :(
You have to manually divide the lightspace position by it's w-value unless you're using a texture function that does that for you, like textureProj (assuming that you don't account for that already within your scaling somewhere).

So without any scaling, your sampling coordinate would be


vec4 lightSpacePos = lightMatrix * invView * fragmentPos;
vec2 projCoords = 0.5 + 0.5 * lightSpacePos.xy / lightSpacePos.w;


The 0.5 + 0.5 * brings the coordinates from [-1, 1] in the [0, 1] range.

I hope that fixes it smile.png
Hi and thank you for your reply!

It still wobbles around when I move the camera. I might be doing the view/projection matrix in a wrong way, do you think I should take into account the center of my frustum as the light's position?

However you were right about the w division, I did not do it.
Is the wobbling some kind of delay?
If so, check if you're using the inverse view matrix of your current frame, after you've already updated it.

It's kinda hard to guess what's wrong without seeing what's going on
No, it is view dependant, as I move the camera around, the cookie moves as well.

I wish I could show a video :(
What about the directional light view and projection matrix? Any comments on those? I have a feeling that the light position must be in the center of the frustum.
I just tried your matrix setup myself (using glm and the same values) and it works fine for me even when moving around.

Since it acts weird when you move your camera, how do you set up the inverse view matrix?
Try glm::inverse(viewMatrix) if you aren't doing that already and make sure your position reconstruction from depth is correct. MJP has some good posts about that on his blog: http://mynameismjp.wordpress.com/2009/03/10/reconstructing-position-from-depth/

If the light view matrix was dependant on your frustum, then the projected cookie texture would move around as you move the camera.
Okay, that is good that it doesn't move with my values, I will keep using those for now.

The light view matrix is the inverse of its world matrix, thus the inverse view is the world.
Below I am showing you how I calculate the position from depth for the directional light, it is actually taken from MJP.

vec3 PositionFromLinearZ (sampler2D depthTexture, vec2 texcoord, vec3 positionVS)
{
float depth = texture(depthTexture, texcoord).r;
return depth * positionVS;
}


The depth in my GBuffer is calculated as ViewSpacePosition.z / farClipPlane, and the "positionVS" that I pass in the PositionFromLinearZ for the directional light position reconstruction is

PositionVS = (inverseProjection * vec4(in_Position, 0, 1)).xyz;

for a fullscreen quad in NDC coordinates.
Oh but it seems I don't need to divide by w, as it's already in clip space and w=1 for fullscreen quad.
Using this order of multiplication:

vec4 lightSpacePos = lightMatrix * invView * fragmentPos;

the cookie does not move when I rotate the camera, however it does go berserk when I move the camera around.

This topic is closed to new replies.

Advertisement