Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is openGL glDepthFunc() not working?

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

my result

like image 291
matlos Avatar asked Mar 21 '23 16:03

matlos


1 Answers

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.

like image 146
Andon M. Coleman Avatar answered Apr 25 '23 05:04

Andon M. Coleman