I’m trying to render the depth buffer to the main screen by copying it to a texture, but all I’m getting is a black texture as a result; I tried commenting out my render calls so that it goes directly from clearing the buffers to drawing the depth buffer (clear color is set to blue so i know it’s rendering the polygons the depth texture should be attached to), so that it should be a white screen it if works, but I got no results.
This is my code:
glReadBuffer(GL_BACK);
if(s_depthTexture)
{
glBindTexture(GL_TEXTURE_2D, s_depthTexture);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 1024, 1024);
}
else
{
glGenTextures(1, &s_depthTexture);
glBindTexture(GL_TEXTURE_2D, s_depthTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
//doesn't seem to help matters...
// glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 1024, 1024, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 0);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, 1024, 1024, 0);
}
//polygon rendering
Vertex shader:
#version 130
uniform mat4 u_projection;
uniform mat4 u_modelview;
in vec2 v_position;
out vec2 f_uv;
void main()
{
vec4 pos = u_modelview * vec4(v_position, 0, 1.0);
gl_Position = u_projection * pos;
f_uv = (v_position + vec2(1, 1)) * .5;
}
Fragment shader:
#version 130
uniform sampler2D u_depthBuffer;
uniform float u_zNear;
uniform float u_zFar;
in vec2 f_uv;
out vec4 fragColor;
void main()
{
float depth = texture2D(u_depthBuffer, f_uv).x;
// depth = (2.0 * u_zNear) / (u_zFar + u_zNear - depth * (u_zFar - u_zNear));
fragColor = vec4(depth, depth, depth, 1);
// fragColor = vec4(f_uv, 0, 1); /* UVs do seem to render correctly */
}
What am i doing wrong?