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
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:
glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE)
glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With