I'm trying to go from clip space to world space and then to light clip space in a fragment shader (what you would want to do if you do shadow mapping with a deferred shader), but I can't get it working. My generated world space coordinates don't stand still. They get more and more misaligned the farther I move the camera from the world origin.
This is a fragment shader that only goes from clip space to world space, but it still doesn't work very well (same problem).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| uniform sampler2D depth; uniform mat4x4 inverseProjectionMatrix, inverseViewMatrix; in vec2 position; layout(location = 0) out vec4 fragColor;
void main() { float z = texture2D(depth, texCoords).z; vec4 eyeSpace = inverseProjectionMatrix * vec4(position.x, position.y, z, 1); vec4 worldSpace = inverseViewMatrix * eyeSpace; worldSpace /= worldSpace.w; fragColor = vec4(worldSpace.xyz, 1); } |
Am I doing something wrong?
Matrix setup:
1 2 3 4 5 6 7 8 9
| Matrix4f.invert(projectionMatrix, null).store(matrixBuffer); matrixBuffer.flip(); GL20.glUniformMatrix4(volumetricShader.getUniformLocation("inverseProjectionMatrix"), false, matrixBuffer); matrixBuffer.clear();
Matrix4f.invert(viewMatrix, null).store(matrixBuffer); matrixBuffer.flip(); GL20.glUniformMatrix4(volumetricShader.getUniformLocation("inverseViewMatrix"), false, matrixBuffer); matrixBuffer.clear(); |
Jeez... Matrices are so annoying sometimes...