Weird jagged edges / precision problems

Dear Community,

I’m having strange issues at the edges of my object:
[ATTACH=CONFIG]1657[/ATTACH]
[ATTACH=CONFIG]1658[/ATTACH]

The edges appear to be from the other side of the cube.
Does anyone know what is going on? I’d love to hear it because I’ve been looking around but haven’t been able to find an answer.

Depth testing is enabled. I’ll create a git if access to the code is required.

Best regards,

Jip

Strangely, it always happens to be that once you’ve asked and / or posted a question anywhere you find the answer yourself an hour or two later.

What happens is a Z-buffer conflict. The fragments are fighting over which one is in front of the other one - that is also why this only happens at the edges of the model and not in the middle. The following things can fix this:

  • decrease the difference between the near and far field of the frustum (perspective matrix). Especially very small near values (0.1 or smaller) cause a lot of issues, because the most detail is at the beginning of the frustum.
  • When the values mentioned above cannot be changed, perhaps increase the bit depth of the depth buffer. You can increase this to 24 bits - if this doesn’t help then your buffer was probably already at 24 bits by default.

Hope this helps anyone!

Best regards,

Jip

To elaborate upon that: -Z values which are beyond N times the near distance use approximately 1/N of the range of depth values. So e.g. -Z values beyond 10 times the near distance only use 1/10th of the range of depth values.

So if the near distance is too small, most of the scene will have greatly reduced depth resolution.

The far distance doesn’t actually have that much effect; in fact, it’s entirely possible to have the far plane at infinity, using the projection matrix:


[X 0  0   0]
[0 Y  0   0]
[0 0 -1 -2n]
[0 0 -1   0]

where n is the near distance, and X and Y are the scale factors determined by the field-of-view angle and the aspect ratio. You can see how this arises by examining the matrix constructed by glFrustum and considering that as nearVal becomes small compared to farVal, both nearVal+farVal and nearVal-farVal become approximately equal to farVal, so C and D tend to -1 and -2*nearVal respectively

With the far plane at infinity, the proportion of depth values used by -Z values beyond N times the near distance is exactly 1/N.

@GCElements, thank you for the further elaboration!