How to get the rid of the z-fighting problem in OpenGL?

笑着哭i 提交于 2019-12-06 14:25:45

Don't clear the screen between the two passes. If you just want to clear the depth buffer, then clear just the depth buffer. Don't pass GL_COLOR_BUFFER_BIT to glClear.

In any case, a better way to do this (so that you don't have to re-render everything) is to employ a proper depth range. Since your cockpit cannot intersect with the scene, there's no reason to draw it into the scene's depth range.

So first, you draw your scene, using a reasonable perspective matrix (ie: one with a z-near that is reasonably large. On the order of several feet or so). Your scene does not consist of your cockpit. The glDepthRange for this rendering should be something like [0.05, 1.0].

After that, you draw your cockpit, using a reasonable perspective matrix for just the cockpit. The glDepthRange for this would be [0, 0.05]. This should give you plenty of depth bits of precision for both the scene and the cockpit.

Oh, and make sure you're getting a 24-bit depth buffer.

Cockpit versus outside is a classic case for the stencil buffer, much like HUD vs. scene. The advantage of stencil is that you don't need z at all, so you can set the near plane much farther away. Also, the Windows that you see through don't change (except when you rotate your virtual head inside the cockpit), so that's a draw-once-reuse-many thing.

Also, logarithmic z may be an interesting read for you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!