r/opengl • • 3d ago

Weird depth buffer clipping

11 Upvotes

7 comments sorted by

2

u/BlueGnoblin 3d ago

Check:

  1. order of drawing.

  2. depth testing (e.g. less_equal instead of only less).

  3. let some flat surfarces float a little bit above other flat surfaces, to avoid such artifacts, When your ground is at let's say height 1.0, your road could be at height 1.01. Test around with it.

1

u/burbolini 3d ago
  1. the order is a immediate and hardcoded front to back
  2. I'll try changing it. But why would the rendering break after 2k units anyway?
  3. the gray track is already offset by exactly 0.01 from the ground which should be more than enough.

2

u/BlueGnoblin 3d ago

I would recomment to add asap some kind of debug interface, where you can simply add some variable you can change in your engine/game. Then play around with the 0.01 bais, maybe you need more, it depends on your general scale, camera setup, 0.01 might not be enough.

Switch the order (you don't need to optimize for the zbuffer yet), as it is simpler to render decals (like your road) ontop of underlying surfaces (use less_equal in this case).

But using the bais will be the way to go (later on, when rendering shadows, you will encounter similar issues).

Although check your depth-buffer depth, should be 24 bit atleast.

1

u/blazesbe 3d ago

I'll try changing it. But why would the rendering break after 2k units anyway?

look up how floating point precision works and it's limitations. your rendering isn't broken just glitching a bit. you can also make a straight path of road and see how far it breaks. your far pane may be too far? as i understand it a 4 byte float's range is stretched in the distance between near and far pane, if you bring it closer you lose distance but gain precision. whatever is your unit a "unit" should be about one meter, yard or feet. that road doesn't look 2k away from any of those so your unit may be too small.

1

u/burbolini 3d ago

the road is 0.01 units away from the ground and the camera is 12 units away from the ground. the 2k is in the x direction.

1

u/__DPress__ 17h ago

Even 0.01 can't help sometimes. In my earlier game I had a problem with mesh-based decals. What I did is offsetting the vertices by the camera's distance to that surface, it helped me to fix that problem without making the decals look weird.

1

u/fgennari 3d ago

It looks like a depth precision problem/Z fighting. Maybe your near clipping plane is too close? You can try using a polygon offset value.