3

In my scene there is occurrence of Z fighting.

  1. I draw a red band at z value 0.0;
  2. I draw a flair( Rectangle with flair texture) at the top of band at z value 0.1
  3. I draw a flair( Rectangle with flair texture) at the bottom of band at z value 0.2

enter image description here

when i rotate the scene i can see z fighting. enter image description here

if i increase the z distances of the flairs than i get rid of the Z fighting.

  1. Why is there Z fighting happening when z distances are small ?
  2. Is it possible to get rid of Z fighting without increasing the z distances of the flairs ?
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • What are your `near` and `far` projection values? If you have a shallow depth buffer (e.g. 16 bits) and a large depth space (e.g. -10000 to 10000), a difference of `0.1` will be less than one bit. – Bartek Banachewicz Aug 26 '19 at 10:08
  • glm::mat4 projection = glm::perspective(45.0f, 800.0f / 600.0f, 0.1f, 10.0f); –  Aug 26 '19 at 10:09
  • 1
    It's weird that it fights like this instead of clipping... What's the position of this setup? 0.0f lies outside of the view frustum, so you must be translating this +Z, right? How are you doing the rotations? Also I'd use a debugger and/or draw out the depth buffer as color if that's possible. This is a simple scene, so the results should give you the obvious reason there. – Bartek Banachewicz Aug 26 '19 at 10:11
  • 1
    @Bartek Banachewicz Correction please this is the projection matrix glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)800.0 / (float)600.0, 0.1f, 1000.0f); –  Aug 26 '19 at 10:25
  • camera is 500 units in z –  Aug 26 '19 at 10:40
  • 1
    yep that is the problem `zfar/znear = 1000/0.1 = 10000` so if you got 16bit depth buffer: `65536/1000 = 6.5536` units change in case depth is linear but that is not the case so its better near `znear` but still not good enough to handle `0.1` increases. Try to use `znear = 10` and move the camera backwards so `z=0.1` is at least `10` units away from camera and not get clipped .... – Spektre Aug 27 '19 at 08:19
  • 1
    to be more clear `(2^bits)/2 > zfar/znear` beacuse only half of depth range is visible (`Z-`). The non linear depth buffer has best resolution near znear but very soon after it drops the precision and get very bad ... see: [How to correctly linearize depth in OpenGL ES in iOS?](https://stackoverflow.com/a/42515399/2521214) – Spektre Aug 27 '19 at 08:23
  • 1
    try to use 24bit depth buffer (32 is not supported on all cards but 24 should be ...) see [GL+VAO/VBO+GLSL+shaders example in C++](https://stackoverflow.com/a/31913542/2521214) in there look for `gl_init()` and examine how I set the `pfd` ... You can change the bit depths there (also for other buffers like aux,stencil ...) – Spektre Aug 27 '19 at 08:28
  • @Spektre would i be able to change the depth bits as i am using GLFW window ? –  Aug 27 '19 at 08:31
  • 1
    @shomit I do not use GLFW but surely there must be a way ... (some config function or structure before the Window/Context creation) something like `????_hint(????)` I am initializing like this: [What is the proper OpenGL initialisation on Intel HD 3000?](https://stackoverflow.com/q/19099162/2521214) which actually chose the best pixel format supported ... – Spektre Aug 27 '19 at 08:34
  • 1
    @shomit see [GLFW docs](https://www.glfw.org/docs/latest/window_guide.html) (first hit I found on google) there `GLFW_DEPTH_BITS` is the hint you want to use probably with `glfwWindowHint` before the `glfwCreateWindow` However it looks like 24 bits is the default value :( so znear/zfar change is probably the only way to remedy your issue – Spektre Aug 27 '19 at 10:50
  • @Spektre Than you very much. –  Aug 27 '19 at 10:52

0 Answers0