Jump to content
  • Advertisement
Sign in to follow this  
alkisbkn

Directional light cookies

This topic is 2344 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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 Edited by DrColossus

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!