Drawing Unfilled Rectangle Shape in openGL

I want to draw unfilled rectangle shape in OpenGL, but when I used glBegin(GL_QUADS) or glBegin(GL_POLYGON), the resulted shape is filled but I want to be unfilled. How I can draw unfilled rectangle.

void draweRect(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0,0.0,1.0);
    glLineWidth(30);
    glBegin(GL_POLYGON);
        glVertex2i(50,90);
        glVertex2i(100,90);
        glVertex2i(100,150);
        glVertex2i(50,150);
    glEnd();
    glFlush();   
}
3

3 Answers

Use GL_LINE_LOOP (instead of GL_POLYGON) to draw a connected series of line segments on the perimeter of your polygon rather than filling the polygon.

Alternatively, you could use glPolygonMode (GL_FRONT_AND_BACK, GL_LINE)... remember to set it back to the default glPolygonMode (GL_FRONT_AND_BACK, GL_FILL) to resume usual (filled) rendering though.

You need to set the fill mode for your rendering.

You can use the glPolygonMode(...); method.

Try the following:

void draweRect(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0,0.0,1.0);
    glLineWidth(30);

    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

    glBegin(GL_POLYGON);
        glVertex2i(50,90);
        glVertex2i(100,90);
        glVertex2i(100,150);
        glVertex2i(50,150);
    glEnd();

    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

    glFlush();   
}
1

This one worked for me..

glBegin(GL_LINE_LOOP);
    glVertex2f(x1,y1);
    glVertex2f(x2,y1);
    glVertex2f(x2,y2);
    glVertex2f(x1,y2);
glEnd();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest