OpenGL:如何绘制带边框的矩形?

5

在此输入图片描述

请看我制作的这张图片,但我想要做的是制作带边框的矩形,并将背景颜色设置为另一种颜色。我应该怎么做?

glRectf(top_left_x, top_left_y, bottom_right_x, bottom_right_y)?




if loop==0:
            ratio = 0.10
            glBegin(GL_QUADS)
            while ratio <= 1.0:
                width = window_width/2
                height = window_height
                long_length = width * ratio
                short_length = height* (1.0 - ratio)
                top_left_x = (width - long_length) / 2.0
                top_left_y = (height - window_height * (1.0-ratio)) /2
                bottom_right_x = top_left_x + long_length
                bottom_right_y = top_left_y + short_length
                glColor(1.0,1.0,1.0,0.5)
                glVertex3f(top_left_x, top_left_y, 0.0)
                glVertex3f(top_left_x + long_length, top_left_y, 0.0)
                glVertex3f(bottom_right_x,bottom_right_y, 0.0)
                glVertex3f(bottom_right_x-long_length,bottom_right_y, 0.0)
                ratio += 0.05
            glEnd()
1个回答

10

你可以这样绘制一个未填充的矩形:

glBegin(GL_LINES);



glVertex2d(top_left_x, top_left_y);             
glVertex2d( top_right_x, top_right_y);              
glVertex2d( bottom_right_x,bottom_right_y);             
glVertex2d(bottom_left_x,bottom_left_y);
glVertex2d(top_left_x, top_left_y);                 
glEnd();    

OpenGL使用状态机。所以要改变颜色,只需使用:

glColor3f (R, G, B);

在绘制图形基元之前。

因此,混合它,您的步骤应该是:

  1. 选择填充颜色
  2. 使用glRectf绘制填充矩形
  3. 选择边框颜色
  4. 使用我发布的代码绘制未填充的矩形

当然,对于您要绘制的每个矩形都要重复执行这些步骤。


我已经按照你提供的方式进行了更改,但是我得到的图像完全相同。还有其他问题吗? - user469652
@user469652:你还没有发布任何代码。只有这一行,我无法告诉你更多。 - Heisenbug
@user469652:首先,在glBegin之前放置glColor。 - Heisenbug
如果使用glColor不能解决问题,请检查您的坐标值。我建议您先绘制一个单独的矩形,当您满意后再尝试绘制完整的图形。 - Heisenbug
@user469652:或者使用GL_LINE_STRIP并删除最后一个glVertex调用。 - Heisenbug
嘿 @Heisenbug,我想你是想用GL_LINE_STRIP而不是GL_LINES,但还是谢谢你的代码。 - Canella

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接