OpenGL depth texture wrong

Posted by CoffeeandCode on Game Development See other posts from Game Development or by CoffeeandCode
Published on 2013-11-03T15:26:40Z Indexed on 2013/11/03 16:12 UTC
Read the original article Hit count: 230

Filed under:
|
|
|

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

ew, bands and shiz

so gamedev.stackexchange, what's wrong with my rendering/glsl?

© Game Development or respective owner

Related posts about c++

Related posts about opengl