Question

I'm trying to avoid the uses of a Position Buffer by projecting Screen Space Points back into View Space to use with lighting. I have tried multiplying by the inverse projection matrix, but this does not give back the View Space point. Is it worth it to add matrix multiplication to avoid the Position Buffer?

Final-pass Shader:

vec3 ScreenSpace = vec3(0.0,0.0,0.0);
ScreenSpace.xy = (texcoord.xy * 2.0) - 1.0;
ScreenSpace.z  = texture2D(depthtex, texcoord.xy);
vec4 ViewSpace = InvProjectionMatrix * vec3(ScreenSpace, 1.0);
ViewSpace.xyz = ViewSpace.w;
Was it helpful?

Solution

Most of your answer can be found on this answer, which is far too long and involved to repost. However, part of your problem is that you're using texcoord and not gl_FragCoord.

You want to use gl_FragCoord, because this is guaranteed by OpenGL to be the right value (assuming your deferred pass and your lighting pass use images with the same size), no matter what. Also, it keeps you from having to pass a value from the vertex shader to the fragment shader.

The downside is that you need the size of the output screen to interpret it. But that's easy enough, assuming again that the two passes use images of the same size:

ivec2 size = textureSize(depthtex, 0);

You can use size for the size of the viewport to convert gl_FragCoord.xy into texture coordinates and window-space positions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top