为什么openGL glDepthFunc()不起作用? [英] Why is openGL glDepthFunc() not working?

查看:126
本文介绍了为什么openGL glDepthFunc()不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与openGL一起玩,并试图摆脱蓝色标记的三角形.我将其用于此代码:

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);

是的,我使用

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);

我试图更改近距和远距值,但仍保持不变.我也在尝试更改glDepthFunc的参数,但它也没有帮助我. 那么,有什么想法吗?非常感谢

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

推荐答案

这是完全有效的行为,因为您没有使用填充的多边形.使用glPolygonMode (...)时,人脸剔除仍然可以达到预期的效果,但深度测试则不然.

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:

  1. 将多边形模式设置为填充
  2. 禁用颜色写入:glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE)
  3. 绘制图元
  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. 将多边形模式设置为 LINE
  2. 启用颜色写入:glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)
  3. 绘制图元
  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.

注意::对于上述行为,您应使用包含相等性(例如GL_LEQAUL)的深度测试以使其正常运行.因此,请勿使用GL_LESS.

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.

这篇关于为什么openGL glDepthFunc()不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆