OpenGL Having trouble transforming light position into view space

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

Recommended Posts

I'm coding a deferred renderer that uses view space normals and positions, so I'm trying to transform my light's world position into view space as well. However, things have not been going too smoothly.

It's my understanding that to transform positions into view space I need to multiply the world space position by my View matrix, but this hasn't been working out for me. After too many hours of frustration, I've come here to ask for help.

The problem is that the light's position seems to be marginally changing based on the camera's rotation.

Full images: left, right

Currently I'm lighting using full-screen quads, however if I were to change to light volumes I would still have this problem while transforming directional lights, transforming their world-space directions to view space, so I'd really just prefer to find the source of the problem rather than just flat out avoid it.

glm::mat4 Projection = glm::perspective(fov, aspectRatio, zNear, zFar); glm::mat4 RotateX = glm::rotate(glm::mat4(1.0f), xrot, glm::vec3(1.0f, 0.0f, 0.0f)); glm::mat4 RotateY = glm::rotate(RotateX, yrot, glm::vec3(0.0f, 1.0f, 0.0f)); glm::mat4 View = glm::translate(RotateY, glm::vec3(-xpos, -ypos, -zpos)); glLoadMatrixf(glm::value_ptr(View)); 

Transforming my light's world space position to view space:
glm::vec4 LightPos = glm::vec4(5.0f, 1.0f, -15.0f, 1.0f); LightPos = View * LightPos;

My best guess was that the light's position is being affected by the FOV as that's kind of what it looks like--everything's fine in the center of the screen, it's just around the edges that the problem occurs. However, I'm not touching the Projection matrix for anything other than transforming gl_Vertex positions in my geometry's vertex shader. I'm almost positive that the matrices I'm creating work fine because I'm using the same ones to rotate my geometry and they work fine there. I'm using GLM for matrices and vectors in C++.

Just to isolate the variables here: What values all need to be in view space for my lighting to work? Vertex positions, normals, and my light position right? I've been questioning if I'm doing the other two correctly (in view space), but from my tests they both seem to check out okay.

View space normals are just gl_NormalMatrix * gl_Normal, right? As for my positions, I'm getting them from depth, however if I visually compare the output of my positions from depth to (gl_ModelViewMatrix * gl_Vertex).xyz directly from the geometry shader stored in a render target, they're nearly identical--the only difference being the empty background in mine isn't black.

Any ideas? I'm really on my last straw here. I've been trying to fix this problem for several days now, and it truly is driving me insane. Help would be greatly appreciated.

Update:
It's been suggested by a friend that I may be reconstructing geometry positions wrong in my light shader. Since it's a deferred renderer, I'm rendering the geometry to a series of textures, then sampling them while lighting to get the appropriate information needed to light properly.

This is how I'm reconstructing view space positions from depth:
float depthToZ = -zNear / (zFar - depth.x * (zFar - zNear )) * zFar; vec3 position = vec3( ( ( gl_FragCoord.x / screenSize.x ) - 0.5 ) * 2.0, ( ( -gl_FragCoord.y / screenSize.y ) + 0.5 ) * 2.0, depthToZ ); position.x *= -position.z; position.y *= position.z;

My theory is that since I'm setting the gl_Position of each vertex to ModelViewProjection * gl_Vertex in the geometry stage (so it gets projected to the texture) and not just ModelView * gl_Vertex, it's being projected with FOV but I'm not taking FOV into account while reconstructing their positions. I'm not sure if this is actually the cause of the problem though, and I'd have no clue how to fix it if it was.

• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 12
• 9
• 11
• 15