c++opengldepth-testingzbuffer

Depth-fighting solution using custom depth testing


The core of my problem is that I have troubles with depth-fighting in pure OpenGL. I have two identical geometries, but one is simpler than the other. That forms a set of perfectly coplanar polygons, and I want to display the complex geometry on top of the simpler geometry.

Unsurprisingly, it leads me to depth-fighting when I draw sequentially the two sets of triangles using the OpenGL depth buffer. At the moment, I've patched it using glPolygonOffset but this solution is not suitable for me (I want the polygons the be exactly coplanar).

My idea is to temporary use a custom depth test when drawing the second set of triangles. I would like to save the depth of the fragments during the rendering of the first set. Next, I would use glDepthFunc(GL_ALWAYS) to disable the depth buffer (but still writing in it). When rendering the second set, I would discard fragments that have a greater z than the memory I just created, but with a certain allowance (at least one time the precision of the Z-buffer at the specific z, I guess). Then I would reset depth function to GL_LEQUAL.

Actually, I just want to force a certain allowance for the depth test.

Is it a possible approach ? The problem is that I have no idea how to pass information (custom depth buffer) from one program to another.

Thanks

PS : I also looked into Frame Buffer Objects and Deferred Rendering because apparently it allows passing information via a 'G-buffer', but once I write:

unsigned int gBuffer;
glGenFramebuffers(1, &gBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, gBuffer);

My window goes black... Sorry if things are obvious I'm not familiar yet with OpenGL


Solution

  • As Rabbid76 said, I can simply disable depth writing using glDepthMask(GL_FALSE). Now, I can draw several layers of coplanar polygons using the same offset. Solved.