Why is openGL glDepthFunc() not working?

纵然是瞬间 提交于 2019-12-07 09:33:37

问题


im playing with openGL and im trying to get rid of blue marked triangles. I use for it this code:

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);

And yes I use

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

in my main loop. I've read the problem can be projection matrix. I use these values:

ProjectionMatrix = glm::perspective(45.5f, 4.0f / 3.0f, 0.1f, 100.0f);

I was trying to change the near and far value but its still the same. I was also trying change parameter of glDepthFunc but it also didnt help me. So, any ideas?? Thanks a lot


回答1:


This is perfectly valid behavior because you are not using filled polygons. Face culling still behaves the way you would expect when you use glPolygonMode (...), but the depth test does not.

The depth test and writes only apply to fragments during rasterization, not primitives during clipping / primitive assembly. In a nutshell, this means that anywhere that is not filled will not be affected by the depth of the primitive (e.g. triangle). So the only place the depth test applies in this example are the very few points on screen where two lines overlap.

If you want to prevent the wireframe overlay from drawing lines for triangles that would not ordinarily be visible, you will need to draw twice:

Pass 1

  1. Set Polygon Mode to FILL
  2. Disable color writes: glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE)
  3. Draw primitives

Pass 2

  1. Set Polygon Mode to LINE
  2. Enable color writes: glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)
  3. Draw primitives

This will work because the first pass fills the depth buffer using filled (solid) primitives but does not write to the color buffer (thus everything is still transparent). The second pass draws lines at the edges of each primitive and these lines will fail a depth test if the interior (unfilled region) of another triangle covers it.

NOTE: You should use a depth test that includes equality (e.g. GL_LEQAUL) for the behavior discussed above to function correctly. So do not use GL_LESS.



来源:https://stackoverflow.com/questions/19942407/why-is-opengl-gldepthfunc-not-working

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