enormously z-fighting

there is a big problem with z fighting. A picture is worth a thousand words, so here it comes respectivly they come.


First I draw kinda gradient background color. The background colors are the frustum vectors transformed form clipspace to objectspace. So they are the nearplane. Depth test is disabled and depth buffer writing is disabled as well. Then I draw the bounding geometry (backfaces->culling frontfaces with GL_FRONT) with enabled depth buffer, enabled depth buffer writing and disabled color buffer wirting. That only writes the depth values for the closes backfaces. Then I draw the fornt faces(glCullFace(GL_BACK)) with enabled color buffer, enabled depth buffer. In theory this should draw frontfaces between the color “background” and the closes backfaces. But there I have heavily z fighting.
The top line in the image shows a cube wiht just 8 boxes. But also shows the problem of z fighting. Actually when intersecting the bounding geometry with the neaplane,the bounding geometry gets clipped. Actually I fixed this with a
shader. But this time I have more than one bounding box that represents the bounding geometry. So I cant use the shader anymore. If a frontface is clipped by the nearplane it can happen that the next front face is visisble. The bottom line shows a finer bounding geometry but still with z fighting.

For drawing the background I use following settings


drawing the closest backface with respect to the camera


drawing the closest front faces


Any idea how to solve this z fighting ?


It is not at all clear what it is you’re trying to render. I don’t know what it is that’s wrong about the images that should be corrected, since I don’t know what it is supposed to look like. What are you drawing to the depth buffer and what geometry are you drawing on top of that?

Also, try to get rid of the gradient; it makes it harder to make out the details of the image. Just use black and white, or solid pastels.

Thanks for the fast answer.

I need the colors and the gradient for volume rendering. The color coded bounding geometry describe entrypositions for raycasting. And when moving the camera inside a volume, the rays need to be started either. without that correction, the raycasting will fail at the clipped positions.

To summerize…

First I transform the vectors of the nearclipping plane from clipspace (-1,-1,-1 to 1,1,1) with the inverse modelview projection matrix to object space. Then I draw a quad that covers the entire viewport and asign the transformed nearclipping plane vectors as colors to this full screen quad.
Then I draw the bounding geometry. Let’s take the case with just one cube. If it intersectes with the nearplane then there is a hole. But with this colored quad in the background the hole is “fixed” with the respective colors. Previously I did this in a shader. But now Im working with a finer bounding geometry to increase speed while raycasting. So rays only get start at position with visible data. But fixing holes in such a fine bounding geometry needs a little bit more work to do.
So I render this background as first, then render the bounding geometry with cullface set to GL_FRONT and depth test GL_LESS.
This draws all backface depthvalues of the cubes that are pretty close to the viewer. Then I draw the frontfaces with GL_BACK. Then it draws the frontfaces (raycast entry points). but the part of the geometry that has been clipped will be filled with the gradient background color and shoudl result in a smooth transition as you can see in the pictures in the first line.

but there is this z-fighting problem. I hope that made it clear.
If not, I’ll try to explain better.