Jump to content

  • Log In with Google      Sign In   
  • Create Account


Directional light cookies


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 alkisbkn   Members   -  Reputation: 349

Like
0Likes
Like

Posted 03 June 2012 - 03:56 PM

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 Posted Image

Sponsor:

#2 alkisbkn   Members   -  Reputation: 349

Like
0Likes
Like

Posted 08 June 2012 - 08:09 AM

No ideas at all? :(

#3 DrColossus   Members   -  Reputation: 466

Like
0Likes
Like

Posted 08 June 2012 - 02:32 PM

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 Posted Image

Edited by DrColossus, 08 June 2012 - 02:33 PM.


#4 alkisbkn   Members   -  Reputation: 349

Like
0Likes
Like

Posted 08 June 2012 - 02:55 PM

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.

#5 DrColossus   Members   -  Reputation: 466

Like
0Likes
Like

Posted 08 June 2012 - 03:16 PM

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

#6 alkisbkn   Members   -  Reputation: 349

Like
0Likes
Like

Posted 08 June 2012 - 03:20 PM

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.

#7 DrColossus   Members   -  Reputation: 466

Like
0Likes
Like

Posted 09 June 2012 - 03:47 AM

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.

#8 alkisbkn   Members   -  Reputation: 349

Like
0Likes
Like

Posted 09 June 2012 - 05:04 AM

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.

#9 alkisbkn   Members   -  Reputation: 349

Like
0Likes
Like

Posted 13 June 2012 - 07:31 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS