2

I'm implementing deferred shading in my OpenGL app, and rather than waste waaay too much memory storing position information, I want to reconstruct the view-space position in the fragment shader using information from the depth buffer. It appears as though I have correct x/y, though I'm not completely sure, but I know that my z information is out the window. Here's the part of the fragment shader responsible for reconstructing the position:

vec3 reconstructPositionWithMat(void)
{
    float depth = texture2D(depthBuffer, texCoord).x;
    depth = (depth * 2.0) - 1.0;
    vec2 ndc = (texCoord * 2.0) - 1.0;
    vec4 pos = vec4(ndc, depth, 1.0);
    pos = matInvProj * pos;
    return vec3(pos.xyz / pos.w);
}

matInvProj is the inverse of my projection matrix (calculated on the CPU and uploaded as a uniform mat4). When I try to render my position information (fragColor = vec4(position, 1.0);), the screen is black in the lower-left corner, red in the lower-right corner, green in the upper-left corner, and yellow in the upper-right corner. This is roughly what I would expect to see if my depth was uniformly 0.0 across the entire scene, which it obviously should not be.

What am I doing wrong?

4

1 回答 1

0

我发现了问题,我只是解释错误的数据。通过将其渲染为 ( fragColor = vec4(position.xy, -(position.z + 1.0), 1.0)),结果与我预期的一样。此外,当我将其与缓冲区的位置0.5 + (reconstructedPos - bufferPos)(

于 2014-03-10T01:10:29.090 回答