在OpenGL中将背景对象绘制在前景对象的前面吗? [英] Background object is drawn in front of foreground object in OpenGL?

查看:67
本文介绍了在OpenGL中将背景对象绘制在前景对象的前面吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于测试目的,假设我已经用glutSolidTeapot()绘制了两个茶壶,如下所示:

For testing purposes let's assume I've draw 2 teapots with glutSolidTeapot(), like this:

glColor3f(1.0f, 0.0f, 0.0f); // Red teapot
glutWireTeapot(1.0f);

glColor3f(0.0f, 1.0f, 0.0f); // Green teapot
glTranslatef(0.0f, 0.0f, 3.0f);
glutWireTeapot(1.0f);

相机最初位于(x,y,z) = (0.0f, 0.0f, 5.0f),而我正在查看z = -1(这是相机位置1).我确定您了解绿色茶壶是最接近相机的物体,而红色茶壶在相机的后面.问题在于,绘制的红色像在绿色的前面一样,看起来不正确:

The camera is initially located at (x,y,z) = (0.0f, 0.0f, 5.0f) and I'm looking at z = -1 (this is camera position #1). I'm sure you understand that the green teapot is the closest object to the camera and the red one is behind it. The problem is that the red one is drawn like it's in front of the green one, which doesn't look right:

示例: http://i.stack.imgur.com/8WoEn.png

现在,如果我也移动相机(x,y,z) = (0.0f, 0.0f, -5.0f)并查看z = 1(这是相机位置2),我会看到绿色茶壶前面的红色茶壶,这是正常现象.现在红色是最靠近相机的对象,绿色是红色的后面.一切都很好.

Now, if I move the camera too (x,y,z) = (0.0f, 0.0f, -5.0f) and look at z = 1 (this is camera position #2), I will see the red teapot in front of the green one, this is normal behavior. The red is now the object closest to the camera and the green one is behind the red one. Everything is fine.

示例: http://i.stack.imgur.com/eJvPE.png

我知道这与顺序有关,如果我切换上面的代码(首先是绿色茶壶代码),它将解决问题(位于#1摄像机位置),但是现在该问题将在摄像机位置可见#2.

I know this has something to do with the order, if I switch the code above (green teapot code first), than it will fix the problem in the camera position #1, but the problem will now be visible in camera position #2.

为什么会发生这种情况,如何使其他对象后面的对象不被绘制在前面或根本不被绘制?

推荐答案

您需要分配一个深度缓冲区(您很有可能已经这样做了),然后使用glEnable(GL_DEPTH_TEST)

You need to allocate a depth-buffer (you're most likely doing this already) and then use glEnable(GL_DEPTH_TEST)

之所以会这样,是因为没有进行深度测试,OpenGL根本不会对相对于相机的不同深度做出反应.启用深度测试后,GL会拒绝比当前最接近的所有新像素/片段.

This is happening because without depth-testing, OpenGL does simply not react to different depths relative to the camera. When you enable depth testing, GL will reject all new pixels/fragments that further away than the current closest.

如果您想在场景中四处走动,则可能想通过在glClear上添加GL_DEPTH_BUFFER_BIT来清除深度,否则它将记住与前一帧最接近的片段.

If you want to move around in your scene, you probably want to clear the depth by adding GL_DEPTH_BUFFER_BIT to glClear, or else it will remember the closest fragment from the previous frame.

除此之外,您的透视矩阵可能格式错误.这尤其可能,因为红色茶壶位于绿色茶壶的前面,而您在绘制它们时要使绿色茶壶在正确的深度缓冲下会覆盖红色茶壶.因此,深度值本身可能不好.

Other than that, your perspective matrix might be malformed. This is especially likely, since the red teapot is drawn in front of the green one, while you draw them such that the green one would overdraw the red one with correct depth buffering. Hence, the depth values themselves are probably bad.

这篇关于在OpenGL中将背景对象绘制在前景对象的前面吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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