e-info128
e-info128

Reputation: 4062

Howto draw a empty rectangle in opengl?

Need draw a hollow rectangle in opengl and i try this using primitive objects:

void Skin::draw()
{
    glColor4f(1.0, 1.0, 1.0, 1.0);
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

    glBegin(GL_POLYGON);

    glVertex3f(-0.8, -0.86, 0); // XYZ left, top
    glVertex3f(-0.7, -0.86, 0); // XYZ right, top
    glVertex3f(-0.7, -0.96, 0); // XYZ right, bottom
    glVertex3f(-0.8, -0.96, 0); // XYZ left, bottom
    glVertex3f(-0.8,  -0.86, 0); // XYZ left, top (close)

    glVertex3f(-0.79, -0.87, 0); // XYZ left, top (diagonal down-right)
    glVertex3f(-0.79, -0.95, 0); // XYZ left, bottom
    glVertex3f(-0.71, -0.95, 0); // XYZ right, bottom
    glVertex3f(-0.71, -0.87, 0); // XYZ right, top
    glVertex3f(-0.79, -0.87, 0); // XYZ left, top (close)

    glVertex3f(-0.8, -0.86, 0); // XYZ left, top (close diagonal top-left)

    glEnd();
}

But the result is a filled rectangle :(

Upvotes: 1

Views: 3716

Answers (1)

Rabbid76
Rabbid76

Reputation: 210877

If you want to draw a rectangle with deprecated OpenGL fixed function pipeline then you can use glRect. But you have to change the polygon mode first. See glPolygonMode. The polygon mode defines, whether there are drawn points on the vertex coordinates (GL_POINT), lines between the vertex coordinates (GL_LINE) or the area enclosed by the primitive is filled (GL_FILL):

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glRectf( -0.8, -0.96, -0.7, -0.86 );
glRectf( -0.79, -0.95, -0.71, -0.87 );

In general there are different types of Primitives in OGL:

  • Point primitives: GL_POINTS

  • Line primitives: GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP

  • Line primitives with adjacency information: GL_LINES_ADJACENCY, GL_LINE_STRIP_ADJACENCY
    Adjacencies do not create a further geometry, but the provide information which can be use in a geometry shader stage.

  • Triangle primitives: GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN

  • Triangle primitives with adjacency information: GL_TRIANGLES_ADJACENCY, GL_TRIANGLE_STRIP_ADJACENCY
    Adjacencies do not create a further geometry, but the provide information which can be use in a geometry shader stage.

For the sickness of completeness there are, the list of deprecated primitive types: GL_QUADS, GL_QUAD_STRIP, and GL_POLYGON:

See further Line primitives:

There are 3 kinds of line primitives, based on different interpretations of a vertex stream.

  • GL_LINES: Vertices 0 and 1 are considered a line. Vertices 2 and 3 are considered a line. And so on. If the user specifies a non-even number of vertices, then the extra vertex is ignored.

  • GL_LINE_STRIP: The adjacent vertices are considered lines. Thus, if you pass n vertices, you will get n-1 lines. If the user only specifies 1 vertex, the drawing command is ignored.

  • GL_LINE_LOOP: As line strips, except that the first and last vertices are also used as a line. Thus, you get n lines for n input vertices. If the user only specifies 1 vertex, the drawing command is ignored. The line between the first and last vertices happens after all of the previous lines in the sequence.


This means, a rectangle can be drawn by a GL_LINE_LOOP with 4 vertices:

glBegin(GL_LINE_LOOP);
glVertex3f(-0.8, -0.86, 0); // XYZ left, top
glVertex3f(-0.7, -0.86, 0); // XYZ right, top
glVertex3f(-0.7, -0.96, 0); // XYZ right, bottom
glVertex3f(-0.8, -0.96, 0); // XYZ left, bottom
glEnd();

glBegin(GL_LINE_LOOP);
glVertex3f(-0.79, -0.87, 0); // XYZ left, top
glVertex3f(-0.79, -0.95, 0); // XYZ left, bottom
glVertex3f(-0.71, -0.95, 0); // XYZ right, bottom
glVertex3f(-0.71, -0.87, 0); // XYZ right, top
glEnd();

Upvotes: 2

Related Questions