Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing unfilled rectangle shape in openGL

Tags:

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();   
}
like image 431
Bahaa Avatar asked Jun 13 '14 09:06

Bahaa


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.

like image 55
Andon M. Coleman Avatar answered Sep 22 '22 10:09

Andon M. Coleman


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();   
}
like image 31
rhughes Avatar answered Sep 24 '22 10:09

rhughes


This one worked for me..

glBegin(GL_LINE_LOOP);
    glVertex2f(x1,y1);
    glVertex2f(x2,y1);
    glVertex2f(x2,y2);
    glVertex2f(x1,y2);
glEnd();
like image 42
Sadia Iffat Sultana Avatar answered Sep 22 '22 10:09

Sadia Iffat Sultana