I’m writing a deferred renderer and run into difficulties with light position in lighting step.
Problem is that light moves with a camera even if I multiply light position with camera matrix. It moves less with multiplying with camera matrix but it still moves. Yes I know that there are lots of threads like this but I didn’t get it solved with two days.
I use MRT in deferred renderer: color, normal and depth buffers. In lighting shader I reconstruct position from depth.
*** My rendering pipeline ***
Clear screen
Start rendering to MRT
Move camera
Get camera matrix
Draw geometry
Finish rendering MRT
Bind lighting shader
Send light parameters (like light position multiplied with camera matrix) to shader
Draw fullscreen quad
Unbind lighting shader
Swap buffers
This is for camera matrix
glGetFloatv(GL_MODELVIEW_MATRIX, m);
Could you tell me what is the clipToCameraMatrix and how do I get it?
It is the inverse of the cameraToClip matrix (the matrix that transforms positions from camera-space to clip-space). Or, using fixed-function parlance, it is the inverse of the GL_PROJECTION matrix.
Alfonse Reinheart, where is float value of depth texture in your code?
There is no depth texture, since the code isn’t doing deferred rendering. It’s simply generating the camera-space position based on gl_FragCoord. Since gl_FragCoord.z is the fragment’s depth, which is what is written into the depth buffer, that should be the Z value you get back when doing your deferred rendering pass.
I get same results with your code. And if I multiply inverse of projection matrix and clip position then I get no shaing (like pixel positions are zeros; nothing when visualizing NdotL). Lighting comes from right direction but moves more or less with camera.
Now the question is: How to get correct clipToCameraMatrix (inverse of projection matrix)? I have tried gl_ProjectionMatrixInverse in GLSL and manually passing inverse of projection matrix.
If i go next to the object and rotate camera then shading changes a bit. When looking down then surfaces are brighter, when looking up then surfaces are darker (looks right for me).
I have tried to implement deferred rendering several months already and still no luck
I don’t even have anything new to read and download in the net already.
It’s obvious that your problem is with your matrices, not your depth. If there was a problem with depth, the lighting would be really wrong, whereas it simply is moving relative to the camera rather than the world.
Bug must be in lighting fragment shader, because in application, after moving camera, I get modelview matrix and multiply light position with it. This is what holds light ALMOST fixed.