Jump to content
  • Advertisement
Sign in to follow this  
GeneralQuery

Getting muddled with view space lighting

This topic is 2547 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

I'm attempting to implement this tutorial but am having problems with transforming everything into view space. Previously, I've done all my lighting in object space or tangent space (for normal mapping). For object space lighting with a point light, I do something like this:

[source]
// shader setup
objEye = mesh.mtxInvModelView.Transform(camPos);
objLight = mesh.mtxInvModelView.Transform(lightPos));

glUniform3f(ObjLightPos, objLight.x, objLight.y, objLight.z);
glUniform3f(ObjEyePos, objEye.x, objEye.y, objEye.z);

// draw calls...

// vertex shader
LightVectorObj = posLight - iPosition.xyz;
EyeVectorObj = posEye - iPosition.xyz;
Normal = iNormal.xyz;

// fragment shader
// point light shader code here...
[/source]

Everything works as expected. I'm sure the lighting equation in my fragment shader is correct as it delivers expected results and is your typical Phong shader.

Now, for my deferred shader, I fill up my G Buffer and do simple view-space lighting with a Phong shader using the G Buffers to get the elements needed for the lighting equation.
[source]
// shader setup
objEye = camera.mtxInvModelView.Transform(camPos);
objLight = camera.mtxInvRotation.Transform(lightPos)); // if I try mtxInvModelView instead of mtxInvRotation, the lighting moves around as I approach the mesh (?)

glUniform3f(ObjLightPos, objLight.x, objLight.y, objLight.z);
glUniform3f(ObjEyePos, objEye.x, objEye.y, objEye.z);

// draw calls...

// vertex shader (G Buffer)
Diffuse = vec3(1.0, 0.0, 0.0);
Position = (iPosition * gl_ModelViewMatrix).xyz;
Normal = (iNormal * gl_ModelViewMatrix).xyz;

// fragment shader (Phong lighting)
vec4 Albedo = texture2D(TexDiffuse, TexCoords);
vec3 Position = texture2D(TexPosition, TexCoords).xyz;
vec3 Normal = normalize(texture2D(TexNormal, TexCoords).xyz);
// point light shader code here...
[/source]

The results are similiar to my object space forward-shaded lighting but not identical, as in neither looks "wrong", but I'm surprised that the result isn't identical. Furthermore, I have to multiply the light position by only the rotational part which, as others explained in my other thread, is only required for directional lights. The result is definitely point lighting, but multiplying by the full modelview matrix causes the lighting to move around as I approach the mesh (which isn't quite located at origin). Any ideas?

Share this post


Link to post
Share on other sites
Advertisement
Here's some screenshots. You cans see that the diffuse term of both the object space forward shader and the view space deferred shader is identical, it's the specular highlights that are slightly off.

View-space deferred shader:
WOXWu.png

Object-space forward shader:
4UPKg.png

So I guess I have a few questions about my deferred shader:

  1. Why am I getting correct results multiplying the normals by the modelview matrix rather than the normal matrix?
  2. Why are the specular highlights slightly off?
  3. Why do I have to translate my camera's world space position into view space? I though the camera was always located at (0,0,0) in view space?
  4. Why does multiplying my world space point light position by only the rotational part of the modelview matrix work, but when multiplying by the whole modelview matrix it doesn't?
Thanks if anyone can shed some light (no pun intended) on my situation!

Share this post


Link to post
Share on other sites
Ok, so I've moved the world to view space transformation to the Phong vertex shader whereupon I do a straight multiplication with the modelview matrix which gives me the exact same result as my object space forward shader:

[source]
oEye = (vec4(posEye, 1.0) * gl_ModelViewMatrix).xyz;
oLight = (vec4(posLight, 1.0) * gl_ModelViewMatrix).xyz;
[/source]

So I figure there must be something muddled with my camera matrix code. Hmm... at least it's something solid information to get investigating on.

Share this post


Link to post
Share on other sites
Just in case anyone stumbles across this thread in the future, I should really wrap this up with how I actually fixed the problem, seeing as what I said in my previous posts turned out to be incorrect. It turns out that I was doing vertex transformations for my G Buffer and light/cam positions like this: vector * matrix, when I should have been doing it like this: matrix * vector. Now everything renders correctly and the camera is set to (0,0,0) (view space origin) and everything else (light, vertex positions, normals) are transformed into view space by multiplying by the modelview matrix.

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!