I’ve been trying to implement depth testing in my rendering engine, but I’m running into a problem which I can’t seem to figure out!
I’m rendering my entire scene to a FBO texture at a fixed resolution, and this is later scaled to the window framebuffer to achieve quick and easy resolution independence, and I have had this working for quite some time.
Other than that texture, I have now enabled deth tests and added a renderbuffer to this FBO as such:
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glClearDepth(1.f);
glDepthRangef(0.f,
1.f);
….
glGenRenderbuffers(1,
&g_depth_renderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER,
g_depth_renderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER,
GL_DEPTH_COMPONENT,
a_resolution.x,
a_resolution.y);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,
GL_DEPTH_ATTACHMENT,
GL_RENDERBUFFER,
g_depth_renderbuffer);
Then I have a function to clear all buffers that looks like this:
void
renderer_clear_all_buffers()
{
glBindFramebuffer(GL_FRAMEBUFFER,
0);
glClearColor(1.f,
0.f,
0.f,
1.f);
glClear(GL_COLOR_BUFFER_BIT);
glBindFramebuffer(GL_FRAMEBUFFER,
g_framebuffer);
glClearColor(0.f,
0.f,
0.f,
1.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
and then for rendering the scene I do this, in order to avoid having my scene texture that I scale to the screen fail the depth test:
glDepthFunc(GL_LESS);
/*Render scene here*/
glDepthFunc(GL_ALWAYS);
/*Render final framebuffer texture to a quad covering the screen here*/
Now, with depth testing disabled, if I try to render two object of different sizes at the origin, the one rendered last is always shown above the first one, no matter how I rotate the scene, as expected.
But with depth testing enabled the opposite happens, and the first object is always rendered completely above the other, no matter how the scene is rotated, even tho the two object now should be rendered over different parts of each other.
If I change from
glDepthFunc(GL_LESS);
to
glDepthFunc(GL_LEQUAL);
, the scene is rendered as if depth testing is disabled again, leading me to believe that all fragments that are not at the far plane get the same value, and after some more digging it would seam that all fragments get the depth value of 0.5.
I can’t spot my error here, so any help to straighten this out would be greatly appreciated!