OpenGL, C++ Cornered Box

OpenGL, C++ Cornered Box

本文关键字:Box Cornered C++ OpenGL      更新时间:2023-10-16

我一直在为一个游戏(学校项目(制作GUI菜单,我们已经准备好了引擎模板,只需要制作一个GUI菜单。我和我的朋友在老师的帮助下成功地制作了一个未填充的盒子。这里是功能:

void BoxTest(float x, float y, float width, float height, float Width, Color color)
{
glLineWidth(3);
glBegin(GL_LINE_LOOP);
glColor4f(0, 0, 0, 1);
glVertex2f(x, y);
glVertex2f(x, y + height);
glVertex2f(x + width, y + height);
glVertex2f(x + width, y);
glEnd();
glLineWidth(1);
glBegin(GL_LINE_LOOP);
glColor4f(color.r, color.g, color.b, color.a);
glVertex2f(x, y);
glVertex2f(x, y + height);
glVertex2f(x + width, y + height);
glVertex2f(x + width, y);
glEnd();
}

这就是现在的样子:http://gyazo.com/c9859e9a8e044e1981b3fe678f4fc9ab

问题是我希望它看起来像这样:http://gyazo.com/0499dd8324d24d63a54225bd3f28463d

因为它看起来好多了,但我和我的朋友已经在这里坐了几天,不知道如何做到这一点。

使用OpenGL行基元,您必须将其分解为多行。GL_LINE_LOOP使一系列线相互连接并在末端闭合。不是你想要的。相反,您应该使用简单的GL_LINES。每两个glVertex调用(顺便说一句:你不应该使用它们,因为glVertex已经过时了;它已经过时近20年了(就有一行。

让我们看看这个ASCII艺术:

0 --- 1    4 --- 3
|                |
2                5
8                b
|                |
6 --- 7    a --- 9

你可以画线段

  • 0–1
  • 0–2
  • 3-4
  • 3-5
  • 6–7
  • 6-8
  • 9–a
  • 9–b

用每个点的坐标替换符号0…b,你就可以制作这个

glBegin(GL_LINES);
glVertex( coords[0] );
glVertex( coords[1] );
glVertex( coords[0] );
glVertex( coords[2] );
glVertex( coords[3] );
glVertex( coords[4] );
glVertex( coords[3] );
glVertex( coords[5] );
glVertex( coords[6] );
glVertex( coords[7] );
glVertex( coords[6] );
glVertex( coords[8] );
glVertex( coords[9] );
glVertex( coords[0xa] );
glVertex( coords[9] );
glVertex( coords[0xb] );
glEnd();

最后,您可以将坐标阵列加载到OpenGL顶点阵列中,并使用glDrawArrays或glDrawElements。