在 OpenGL 中,如何制作一个简单的背景四边形?

发布于 2024-07-12 10:30:01 字数 328 浏览 6 评论 0原文

假设我想绘制一个像这样的简单四边形:

glBegin(GL_QUADS);
glVertex2f(-1.0,-1.0);
glVertex2f(1.0,-1.0);
glVertex2f(1.0, 1.0);
glVertex2f(-1.0, 1.0);
glEnd();

有没有办法绘制它,使其出现在所有 3D 对象后面并填满整个屏幕? 我的相机随着鼠标移动,因此这个四边形也必须随着相机移动而保持静止。 目的是制作一个简单的背景。

我是否必须根据眼睛位置/旋转进行一些疯狂的转换,或者是否有使用 glMatrixMode() 的更简单的方法?

Let's say I want to draw a simple quad like this:

glBegin(GL_QUADS);
glVertex2f(-1.0,-1.0);
glVertex2f(1.0,-1.0);
glVertex2f(1.0, 1.0);
glVertex2f(-1.0, 1.0);
glEnd();

Is there a way to draw this such that it appears behind all 3D objects and fills up the whole screen? My camera moves with the mouse, so this quad also has to appear stationary with camera movement. The purpose is to make a simple background.

Do I have to do some crazy transformations based on eye position/rotation, or is there a simpler way using glMatrixMode()?

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

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

发布评论

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

评论(3

天赋异禀 2024-07-19 10:30:01

听起来您想要做的事情类似于在简单游戏中绘制 2D HUD 时所做的事情,或者只是一个持久的背景。 我不是专家,但我最近才这么做。 您想要做的是将投影矩阵更改为正交投影,渲染四边形,然后切换回之前的任何投影。 您可以使用矩阵堆栈来完成此操作,并且它完全独立于任何相机。

所以,首先:

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
int w = glutGet(GLUT_WINDOW_WIDTH);
int h = glutGet(GLUT_WINDOW_HEIGHT);
gluOrtho2D(0, w, h, 0);

这会将一个新的投影矩阵推送到投影堆栈上(我在这个项目中使用 glut,但它应该转换为普通的 OpenGL)。 然后,我获取窗口的宽度和高度,并使用 gluOrtho2D 设置正交投影。

下一步:

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// Draw your quad here in screen coordinates

在这里,我只是将一个新的、干净的矩阵推送到模型视图上。 您可能不需要这样做。

最后:

glPopMatrix() // Pops the matrix that we used to draw the quad
glMatrixMode(GL_PROJECTION);
glPopMatrix(); // Pops our orthographic projection matrix, which restores the old one
glMatrixMode(GL_MODELVIEW); // Puts us back into GL_MODELVIEW since this is probably what you want

我希望这会有所帮助。 如您所知,它不需要使用眼睛位置。 这主要是因为当我们使用完全适合我们窗口的新正交投影时,这是无关紧要的。 但是,如果这是在其他绘制调用之后执行的,则这可能不会将您的四边形放在后面。

编辑:正如 Boojum 建议的那样,关闭深度测试并清除深度缓冲区也可能有所帮助。

It sounds like what you want to do is similar to what you would when drawing a 2D HUD in a simple game, or just a background that is persistent. I'm no expert, but I did just that relatively recently. What you want to do is change the projection matrix to an orthographic projection, render your quad, and then switch back to whatever projection you had before. You can do this with the matrix stack, and it is completely independent of any camera.

So, first:

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
int w = glutGet(GLUT_WINDOW_WIDTH);
int h = glutGet(GLUT_WINDOW_HEIGHT);
gluOrtho2D(0, w, h, 0);

This pushes a new projection matrix onto the projection stack (I was using glut for this project, but it should translate to just normal OpenGL). Then, I get the window width and height, and use gluOrtho2D to set up my orthographic projection.

Next:

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// Draw your quad here in screen coordinates

Here I just push a new, clean matrix onto the modelview. You may not need to do this.

Lastly:

glPopMatrix() // Pops the matrix that we used to draw the quad
glMatrixMode(GL_PROJECTION);
glPopMatrix(); // Pops our orthographic projection matrix, which restores the old one
glMatrixMode(GL_MODELVIEW); // Puts us back into GL_MODELVIEW since this is probably what you want

I hope this helps. As you can tell, it requires no use of the eye position. This is mainly because when we use a new orthographic projection that perfectly fits our window, this is irrelevant. This may not put your quad in the back however, if this is executed after the other draw calls.

EDIT: Turning off depth testing and clearing the depth buffer could help as well, as Boojum suggests.

烟沫凡尘 2024-07-19 10:30:01

以下是我对每个帧使用的步骤:

  1. 使用 glClear(GL_DEPTH_BUFFER_BIT); 清除深度缓冲区;
  2. glDisable(GL_DEPTH_TEST); 关闭深度测试
  3. 使用 使用 gluOrtho2D(-1.0, 1.0, -1.0, 1.0); 修复正交投影
  4. 按照您的描述绘制四边形。
  5. 重新打开深度测试。
  6. 加载常规透视投影并绘制视图的其余部分。

Here are the steps that I'd use for each frame:

  1. Clear the depth buffer with glClear(GL_DEPTH_BUFFER_BIT);
  2. Turn off depth testing with glDisable(GL_DEPTH_TEST);
  3. Load in a fixed orthographic projection with gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
  4. Draw your quad as you described it.
  5. Turn depth testing back on.
  6. Load your your regular perspective projections and draw the rest of the view.
甲如呢乙后呢 2024-07-19 10:30:01

如果你想让它围绕在你周围,你不能把它放在背景中吗? 您需要先绘制它,使其位于所有对象的后面。

Cant you just place it in the background, box yourself in if you want it all around you. You need to draw it first to make it behind all the objects.

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