I have been writing a game engine for a while now and have decided to reconstruct my positions from depth... but how I read the depth seems to be wrong :/
What is wrong in my rendering?
How I init my depth texture in the FBO
gl::BindTexture(gl::TEXTURE_2D, this->textures[0]); // Depth
gl::TexImage2D(
gl::TEXTURE_2D, 0,
gl::DEPTH32F_STENCIL8,
width, height, 0,
gl::DEPTH_STENCIL,
gl::FLOAT_32_UNSIGNED_INT_24_8_REV, nullptr
);
gl::TexParameterf(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::NEAREST);
gl::TexParameterf(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::NEAREST);
gl::TexParameterf(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE);
gl::TexParameterf(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE);
gl::FramebufferTexture2D(
gl::FRAMEBUFFER, gl::DEPTH_STENCIL_ATTACHMENT,
gl::TEXTURE_2D, this->textures[0],
0
);
Linear depth readings in my shader
Vertex
#version 150
layout(location = 0) in vec3 position;
layout(location = 1) in vec2 uv;
out vec2 uv_f;
void main(){
uv_f = uv;
gl_Position = vec4(position, 1.0);
}
Fragment
(where the issue probably is)
#version 150\n
uniform sampler2D depth_texture;
in vec2 uv_f;
out vec4 Screen;
void main(){
float n = 0.00001;
float f = 100.0;
float z = texture(depth_texture, uv_f).x;
float linear_depth = (n * z)/(f - z * (f - n));
Screen = vec4(linear_depth); // It ISN'T because I don't separate alpha
}
When Rendered
so gamedev.stackexchange, what's wrong with my rendering/glsl?