1

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

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Kiord
  • 79
  • 7
  • How would your depth approach be different from using `glPolygonOffset`? In the end, I don't think what you want is possible since while drawing the second object you'd have to read and write to the depth-buffer at the same time which is not easily possible. – BDL Jun 18 '19 at 10:31
  • 1
    *"`glPolygonOffset` but this solution is not suitable for me (I want the polygons the be exactly coplanar)."* - `glPolygonOffset` only changes the depth value and doesn't gain any distortion in the projection to the viewport. – Rabbid76 Jun 18 '19 at 10:38
  • If I understood correctly ```glPolygonOffset```, it basically move the vertices to the camera. I will be drawing several layers of objects (I don't know excalty what for the moment) on top of the second object, that's why I would like not to stack the offsets. – Kiord Jun 18 '19 at 10:39
  • "you'd have to read and write to the depth-buffer at the same time which is not easily possible" That's why I would like to keep in memory the zbuffer previously computed, if possible. – Kiord Jun 18 '19 at 10:43
  • @Kiord `glPolygonOffset` does not *"move the vertices"*, it adds an offset to the depth value, before it is compared to the value in the depth buffer, that is something completely different. – Rabbid76 Jun 18 '19 at 10:45
  • @Kiord: Keeping the old depth-buffer in memory wouldn't help since you would then not depth-test fragments of the second pass against each other. – BDL Jun 18 '19 at 10:49
  • @Rabbid76 Oh ok. I see I'm trying to do the exact same thing with my custom zbuffer then. lol. – Kiord Jun 18 '19 at 10:52
  • @Kiord You can't do this in general, because `glPolygonOffset` adds an offset which depends on basic machine units, on the format (bits) of the depth buffer. All you can do is to implement a in shader solution for one know depth buffer format, which at the end is slower than `glPolygonOffset`. – Rabbid76 Jun 18 '19 at 10:55
  • It seems that the value written in the zbuffer is the shifted value... https://gamedev.stackexchange.com/questions/113795/little-question-about-glpolygonoffset . – Kiord Jun 18 '19 at 12:54
  • @Kord Yes of course. – Rabbid76 Jun 18 '19 at 13:08
  • @Rabbid76 Well, I need to stack several layers of polygons (I don't know how many, that's the point). That means I need to call ```glPolygonOffset``` each time with a greater offset. At some point, I will be unable to draw a polygon I front of my main mesh because the offset will be too high, right ? That's why I was planning to manually do the depth test using a custom depth-buffer. – Kiord Jun 18 '19 at 13:20
  • @Kiord Disable the depth test and draw the polygons in layered order, then you don't need any depth offset at all. – Rabbid76 Jun 18 '19 at 13:28
  • @Rabbid76 Unfortunately, my main mesh can hide itself. Disabling the depth test will show some polygons that shouldn't be visible. – Kiord Jun 18 '19 at 13:36
  • @Rabbid76 Absolutely. But if there is a second layer triangle the other side (non visible side) of the mesh, it shouldn't appear on screen. Disabling the depth-test will make it visible, right ? – Kiord Jun 18 '19 at 13:42
  • 2
    @Kiord So you've to keep the depth buffer as it is after the "main" object. Enable depth test, but set [`glDepthMask(GL_FALSE)`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glDepthMask.xhtml) after the main object and draw in layered order. – Rabbid76 Jun 18 '19 at 13:49
  • 1
    @Rabbid76 LOL I just realized I can do that. Thank you ! Problem solved ;) – Kiord Jun 18 '19 at 13:54
  • @Rabbid76 Just a last point, I know that zbuffer precision is decreasing when moving toward z axis... Does ```glPolygonOffset``` take that in consideration ? I mean, does the offset depends of the distance ? I just need to be sure that it will work at any distance – Kiord Jun 18 '19 at 14:00
  • @Kiord The offset is linear it is the minimum amount which is necessary to gain a difference in depth buffer, based on the format of the buffer. In other words, the minimum depth offset ensures that there is difference of 1 "bit". – Rabbid76 Jun 18 '19 at 14:06
  • 1
    @Rabbid76 Thanks ! – Kiord Jun 18 '19 at 14:14

1 Answers1

1

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.

Kiord
  • 79
  • 7