Jump to content
  • Advertisement
aejt

OpenGL Yet another "reconstructing position from depth" thread

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement

I managed to solve the issue already. It was because of the line

float eyeZ = persMatrix[3][2] / ((persMatrix[2][3] * ndcZ) - persMatrix[2][2]);

If I instead linearized it like this it seems to work like it should (hard-coded values for near/far) :

float eyeZ = (2.0 * 0.1 * 100.0) / (100.0 + 0.1 - ndcZ * (100.0 - 0.1));

 

I don't really know exactly how the perspective projection matrix works so I can't tell what's wrong with the first line. Is there an easy fix to it so I could avoid hard-coding the near/far values? If this image is correct, those two lines should be equivalent?

Edit 2:

Example on Khronos wiki has the wrong sign in front it seems. The line below works for me.

float eyeZ = -persMatrix[3][2] / ((persMatrix[2][3] * ndcZ) - persMatrix[2][2]);

 

Edited by aejt

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!