首页 > 解决方案 > 如何在 OpenGL 中将多边形带到前台?

问题描述

下面的代码创建了 2 个正方形多边形,红色和绿色。
我试图在绿色顶部放置一个红色方块,但我做不到。
声明深度缓冲区,在必要时进行清理,正确配置正交系统。
如果我指定一个超出范围 (2;-2) 的值,则多边形会按原样消失。

#include <...>

constexpr auto FPS_RATE = 120;
int windowHeight = 600, windowWidth = 600, windowDepth = 600;

void init();
void idleFunction();
void displayFunction();
double getTime();

double getTime()
{
  using Duration = std::chrono::duration<double>;
  return std::chrono::duration_cast<Duration>(
    std::chrono::high_resolution_clock::now().time_since_epoch()
    ).count();
}

const double frame_delay = 1.0 / FPS_RATE;
double last_render = 0;

void init()
{
  glutDisplayFunc(displayFunction);
  glutIdleFunc(idleFunction);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-windowWidth / 2, windowWidth / 2, -windowHeight / 2, windowHeight / 2, 2, -2);
  glClearColor(0.0, 0.0, 0.0, 0.0);
}

void idleFunction()
{
  const double current_time = getTime();
  if ((current_time - last_render) > frame_delay)
  {
    last_render = current_time;
    glutPostRedisplay();
  }
}

void displayFunction()
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPushMatrix();
  //move the red square to the foreground
  glTranslatef(-32.5, -32.5, 2);
  glColor3f(1, 0, 0);
  glBegin(GL_POLYGON);
  glVertex3i(-150, 150, 0);
  glVertex3i(150, 150, 0);
  glVertex3i(150, -150, 0);
  glVertex3i(-150, -150, 0);
  glEnd();
  glPopMatrix();
  glPushMatrix();
  //move the green square to the background
  glTranslatef(32.5, 32.5, -2);
  glColor3f(0, 1, 0);
  glBegin(GL_POLYGON);
  glVertex3i(-150, 150, 0);
  glVertex3i(150, 150, 0);
  glVertex3i(150, -150, 0);
  glVertex3i(-150, -150, 0);
  glEnd();
  glPopMatrix();
  glutSwapBuffers();
}

int main(int argc, char* argv[])
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  glutInitWindowSize(windowWidth, windowHeight);
  glutInitWindowPosition((GetSystemMetrics(SM_CXSCREEN) - windowWidth) / 2, (GetSystemMetrics(SM_CYSCREEN) - windowHeight) / 2);
  glutCreateWindow("Window");
  init();
  glutMainLoop();
  return 0;
}

标签: c++openglopengl-compatdepth-testing

解决方案


您必须启用深度测试

glEnable( GL_DEPTH_TEST );

默认深度测试函数 ( glDepthFunc) 是<( GL_LESS)。
如果到远平面的距离为 2.0,几何图形的 z 坐标为 2.0,则几何图形被远平面裁剪,因为几何图形的深度不小于深度缓冲区的初始化深度。

将深度函数更改为<=( GL_LEQUAL):

glDepthFunc( GL_LEQUAL );

右手系统中,视空间 z 轴指向视口之外。因此,如果 z 坐标“小于”,则该对象位于另一个对象“后面”。

投影矩阵从视图空间转换到标准化设备空间。与视图空间相比,标准化设备空间是左手系统,其中 z 轴指向视口。范围 [-1, 1](从前到后)内的归一化设备 z 坐标映射到深度值(通常在 [0, 1] 范围内),用于深度测试。
为了处理glOrtho反转 z 轴,如果near参数设置小于far参数(这是建议使用该函数的方式)。
这导致当几何从视图空间转换到标准化设备空间时,深度 (z) 顺序不会改变。

注意,glOrtho(-w, w, -h, h, -z, z)是一样的glScaled(1.0/w, 1.0/h, -1.0/z)

由于在您的示例中,z 轴没有被正交投影反转,因为near > far

glOrtho(-windowWidth / 2, windowWidth / 2, -windowHeight / 2, windowHeight / 2, 2, -2);

z 坐标必须更大,才能“落后”。

如果绿色矩形应该在红色矩形之后,那么您必须更改正交投影(near < far)。例如:

glOrtho(-windowWidth / 2, windowWidth / 2, -windowHeight / 2, windowHeight / 2, -2, 2);

如果您不想更改投影,则必须交换几何的 z 坐标:

glPushMatrix();
//move the red square to the foreground
glTranslatef(-32.5, -32.5, -2.0); // foreground because near > far
// ...
glPopMatrix();

glPushMatrix();
//move the green square to the background
glTranslatef(32.5, 32.5, 2.0);   // background because near > far
// ...
glPopMatrix();

推荐阅读