OpenGL中背景对象绘制在前景对象前面?

发布于 2024-10-22 19:22:14 字数 935 浏览 7 评论 0原文

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

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)。我相信您明白绿色茶壶是距离相机最近的物体,红色茶壶在它后面。问题是红色的绘制得就像在绿色的前面一样,看起来不正确:

示例: https://i.sstatic.net/8WoEn.png

现在,如果我也移动相机(x,y,z) = (0.0f, 0.0f, -5.0f)< /code> 并查看 z = 1 (这是相机位置#2),我会看到红色茶壶位于绿色茶壶前面,这是正常行为。红色现在是距离相机最近的物体,绿色物体位于红色物体后面。一切都很好。

示例: https://i.sstatic.net/eJvPE.png

我知道这有什么关系按照顺序,如果我切换上面的代码(首先是绿茶壶代码),那么它将解决相机位置#1中的问题,但问题现在在相机位置#2中可见。

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

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

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:

Example: https://i.sstatic.net/8WoEn.png

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.

Example: https://i.sstatic.net/eJvPE.png

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.

Why is this happening and how can I keep the objects behind other objects from being drawn in front, or at all?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦途 2024-10-29 19:22:14

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

发生这种情况是因为如果没有深度测试,OpenGL根本不会对不同的深度做出反应到相机。当您启用深度测试时,GL 将拒绝比当前最近的距离更远的所有新像素/片段。

如果您想在场景中移动,您可能需要通过将 GL_DEPTH_BUFFER_BIT 添加到 glClear 来清除深度,否则它将记住前一帧中最接近的片段。

除此之外,您的透视矩阵可能格式错误。这种情况尤其有可能,因为红色茶壶是在绿色茶壶前面绘制的,而您绘制它们时,绿色茶壶会在正确的深度缓冲下透支红色茶壶。因此,深度值本身可能很糟糕。

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

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.

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.

送舟行 2024-10-29 19:22:14

可能您还没有启用深度缓冲区(又名 z 缓冲区)。
要在初始化部分中执行此操作,请添加类似的内容

glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

在渲染每一帧之前,您还必须使用以下命令清除此缓冲区

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

您可以查看 OpenGL Nehe 教程以获取更多信息 http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=01

Probably you haven't enabled Depth Buffer aka z-buffer.
To do this in initialization part add something like this

glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

And before rendering every frame you have to clear this buffer as well using

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

You could check OpenGL Nehe tutorial for more information http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=01

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文