8

I wrote this function, where I set up line width to draw a rectangle, but when calling it, the line width doesn't change at all. How can i use glLineWidth correctly?

void drawRect(Rectangle &rect)
{
      double x1 = rect.min.x;
      double y1 = rect.min.y;
      double x2 = rect.max.x;
      double y2 = rect.max.y;

      glLineWidth(3.0f);
      glBegin(GL_LINE_LOOP);
            glVertex2d(x1, y1);
            glVertex2d(x2, y1);
            glVertex2d(x2, y2);
            glVertex2d(x1, y2);
      glEnd();

}  
daydayup
  • 2,049
  • 5
  • 22
  • 47

1 Answers1

18

OpenGL implementations are not required to support rendering of wide lines.

You can query the range of supported line widths with:

GLfloat lineWidthRange[2] = {0.0f, 0.0f};
glGetFloatv(GL_ALIASED_LINE_WIDTH_RANGE, lineWidthRange);
// Maximum supported line width is in lineWidthRange[1].

The required minimum for both limits is 1.0, meaning that support for line widths larger than 1.0 is not required. Also, drawing wide lines is a deprecated feature, and will not be supported anymore if you move to a newer (core profile) version of OpenGL.

An alternative to drawing wide lines is to render thin polygons instead.

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
  • thank you but what do you mean by "support for line widths larger than 1.0 is not required" ? – daydayup Jan 19 '16 at 04:02
  • I tried the range query you provided, but the results are both lineWidthRange[0] and lineWidthRange[1] are 1. So I couldn't change the line width? – daydayup Jan 19 '16 at 04:08
  • Yes, that means that the OpenGL implementation on your machine does not support lines wider than 1. – Reto Koradi Jan 19 '16 at 04:13
  • oh no, is that because the version is too old? – daydayup Jan 19 '16 at 04:31
  • 3
    Not necessarily. It's completely up to the GPU vendor if they want/can support wide lines. Since it's a deprecated feature, and was always optional, it's well possible that it wouldn't be supported on pretty much any GPU. But I really don't know which vendors/GPUs support wide lines, and which don't. – Reto Koradi Jan 19 '16 at 04:49
  • Not recognizing 'GLfloat' for me, but I imagine there's something I have to import or prefix it's not suggesting, 'GLES20.' and 'GLES10.' prefixes didn't work. – Androidcoder Feb 25 '20 at 21:01
  • @Androidcoder in recent versions of opengl, a float can be used in its place. – Coding Mason Aug 15 '21 at 03:40