0
votes

As a simple demonstration of my problem, I am trying to render a large but simple mesh to a texture to be used later, but strangely enough, the further-away-from-the-camera parts of this mesh are displayed in front of the closer-to-the-camera parts, when viewed from a specific angle. Despite the undeniable fact that I do beyond all doubt use depth testing:

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS); 

As an example, I am trying to render a subdivided grid (on the xz plane) centered on the origin, with a smooth "hill" in the middle of the grid.

When rendered to the screen no errors occur and the mesh looks like this (rendered using orthographic projection, and with the greyscale color representing depth, no error will furthermore occur even if the mesh is viewed from any side):

Rendering directly to screen, no errors occur, even when mesh is rotated

Rendering to the screen is of course done by making sure the framebuffer is set to 0 (glBindFramebuffer(GL_FRAMEBUFFER, 0);), but i need to render this scene to another framebuffer which is not my screen, in order to use this render as a texture.
So i have set up another framebuffer, and an output texture, and now i am redering this scene to the framebuffer (with absolutely nothing changed, except the framebuffer and the viewport size, which is set to match the output texture). For the purpose of demonstrating the error, which I am experiancing, I am then then rendering this rendered texture, onto a plane which is then displayed on the screen.

When the mesh is rotated seen from the positive x axis, and rotated around the y axis, centered on its origin between -0.5 π rad and 0.5 π rad, the rendered texture looks exactly identical to the result when rendering to the screen, as seen on the image above.

However when rotation around the y axis is greater than 0.5 π rad or less than -0.5 π rad the closer-to-the-camera hill is rendered behind the further-away-from-the-camera plane (the fact that the hill is closer to the camera can be proven by looking at the color, which represents debth):

enter image description here

(whoops got the title wrong on the window, ignore that)

In the borderregions with a rotation around the y axis of close to 0.5 π rad or -0.5 π rad the scene looks like this.

enter image description here

(whoops got the title wrong on the window again, ignore that again)

To recap. This error with the depth sorting happens only when rendering to a texture using a framebuffer, and only when the object is viewed from a specific angle. When the object is rendered directly to the screen, no error occurs. My question is therefor: why does this happen, and how (if at all) can I avoid avoid it.

1
That first picture… does not look like an orthographic projection. It looks like a perspective projection to me.Dietrich Epp

1 Answers

1
votes

If this problem only happens when you're rendering to the texture framebuffer, you probably don't have a depth attachment properly linked to it.

Make sure that during FBO init you are linking it to a depth texture as well. There's a good example of how to do this here.

Also, check all of the matrices you're using to render -- I've had several cases in the past where improper matrices have thrown off depth calculations.