Sorry for making a new thread about this, but I have a specific question which I couldn't find an answer to in any of the other threads I've looked at.
I've been trying to get the method shown here to work several days now and I've run out of things to try.
I've more or less resorted to using the barebones example shown there (with some very minor modifications as it wouldn't run otherwise), but I still can't get it to work. Either I have misunderstood something completely, or there's a mistake somewhere.
My shader code looks like this:
Vertex shader:
#version 330 core
//Vertex shader
//Half the size of the near plane {tan(fovy/2.0) * aspect, tan(fovy/2.0) }
uniform vec2 halfSizeNearPlane;
layout (location = 0) in vec3 clipPos;
//UV for the depth buffer/screen access.
//(0,0) in bottom left corner (1, 1) in top right corner
layout (location = 1) in vec2 texCoord;
out vec3 eyeDirection;
out vec2 uv;
void main()
{
uv = texCoord;
eyeDirection = vec3((2.0 * halfSizeNearPlane * texCoord) - halfSizeNearPlane , -1.0);
gl_Position = vec4(clipPos.xy, 0, 1);
}
Fragment shader:
#version 330 core
//Fragment shader
layout (location = 0) out vec3 fragColor;
in vec3 eyeDirection;
in vec2 uv;
uniform mat4 persMatrix;
uniform vec2 depthrange;
uniform sampler2D depth;
vec4 CalcEyeFromWindow(in float windowZ, in vec3 eyeDirection, in vec2 depthrange)
{
float ndcZ = (2.0 * windowZ - depthrange.x - depthrange.y) / (depthrange.y - depthrange.x);
float eyeZ = persMatrix[3][2] / ((persMatrix[2][3] * ndcZ) - persMatrix[2][2]);
return vec4(eyeDirection * eyeZ, 1);
}
void main()
{
vec4 eyeSpace = CalcEyeFromWindow(texture(depth, uv).x, eyeDirection, depthrange);
fragColor = eyeSpace.rbg;
}
Where my camera settings are:
float fov = glm::radians(60.0f);
float aspect = 800.0f / 600.0f;
And my uniforms equal:
uniform mat4 persMatrix = glm::perspective(fov, aspect, 0.1f, 100.0f)
uniform vec2 halfSizeNearPlane = glm::vec2(glm::tan(fov/2.0) * aspect, glm::tan(fov/2.0))
uniform vec2 depthrange = glm::vec2(0.0f, 1.0f)
uniform sampler2D depth is a GL_DEPTH24_STENCIL8 texture which has depth values from an earlier pass (if I linearize it and set fragColor = vec3(linearizedZ), it shows up like it should, so nothing seems wrong there).
I can confirm that it's wrong because it doesn't give me similar results to what saving position in the G-buffer or reconstructing using inverse matrices does.
Is there something obvious I'm missing? To me the logic seems sound, and from the description on the Khronos wiki I can't see where I go wrong.
Thanks!
↧