OpenGL-如何绘制完整的窗口矩形

OpenGL - how to draw a full window rectangle

本文关键字:窗口 何绘制 绘制 OpenGL-      更新时间:2023-10-16

实际上,我要做的是显示一个与我创建的窗口相同的图像。但是我发现带有图像纹理的矩形将略微放大,以使图像的边缘看不见。

因此,我进行了以下实验:对于我的窗口尺寸500x500,我先绘制一个尺寸为500x500的矩形,并首先绘制第二个矩形,并用(500-2) *(500-2)用黑色绘制。我得到的只是一个黑色窗口。通过将代码中D的值增加到8,红线在左/右和底部显示,顶部显示直到d = 33。

我认为这是因为深度或投影,但我不知道如何修复它。有没有建议?

以下是我的示例代码:

#include <GL/glut.h> /* glut.h includes gl.h and glu.h*/
void rectangle(float x, float y, float x1, double y1, float r, float g, float b)
{
  glColor3f(r, g, b);
  glBegin(GL_QUADS);
  glVertex2f(x, y);
  glVertex2f(x, y1);
  glVertex2f(x1, y1);
  glVertex2f(x1, y);
  glEnd();
}
void display()
{
  /* clear window */
  glClear(GL_COLOR_BUFFER_BIT);
  int d = 33;
  // draw rectangle in red
  rectangle(d, d, 500 - d, 500 - d, 1, 0, 0);
  // draw rectangle in black
  rectangle(d + 1, d + 1, 500 - d - 1, 500 - d - 1, 0, 0, 0);
  /* flush GL buffers */
  glFlush();
}
void init()
{
  /* set clear color to black */
  glClearColor(0.0, 0.0, 0.0, 0.0);
  /* set fill color to white */
  /* set up standard orthogonal view with clipping */
  /* box as cube of side 2 centered at origin */
  /* This is default view and these statements could be removed */
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(0, 500, 0, 500);
}
int main(int argc, char** argv)
{
  /* Initialize mode and open a window in upper left corner of
  /* screen */
  /* Window title is name of program (arg[0]) */
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowSize(500, 500);
  glutInitWindowPosition(0, 0);
  glutCreateWindow("simple");
  glutDisplayFunc(display);
  init();
  glutMainLoop();
}

我不确定这是否是过多的错误,但是我发现偏移值仅等于窗口边框的厚度。也就是说,对于左/右/底部边缘,偏移= 9像素,顶部的偏移= 32。

看起来,虽然Glutinitwindowsize(500,500)用500x500初始化窗口的黑色区域,但它还假定包含边界为500x500的整个窗口大小。然后,当我绘制500x500的矩形时,它将拉伸矩形以适合创建窗口的轮廓,但不适合内部黑色区域。

我的解决方法是将Gluortho2D设置为使用(-window_left,500 window_right,0 -Windows_bottom,500 window_top)的区域,但仍在(0,0,500,500)绘制矩形:

):
gluOrtho2D(0 - 9, 500 + 9, 0 - 9, 500 + 40);  // 40 is an empirical value for showing the red rectangle completely