Skip to main content

OpenGL syntax: parallelogram

Short answer

Call glBegin(GL_POLYGON); list the vertices in counter‑clockwise order with glVertex2f: (0.0,0.0), (1.0,0.0), (1.5,1.2), (0.5,1.2); then glEnd() and glFlush().

Explain OpenGL syntax to draw a parallelogram having vertices (0.0, 0.0), (1.0, 0.0), (1.5, 1.2) and (0.5, 1.2).

A parallelogram is a four-sided polygon, so it can be drawn with GL_POLYGON or GL_QUADS between glBegin() and glEnd(), with the vertices given in order (counter-clockwise) using glVertex2f(x, y).

void display(void) {
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0f, 0.6f, 1.0f);          // fill colour
    glBegin(GL_POLYGON);                   // or GL_QUADS
        glVertex2f(0.0f, 0.0f);
        glVertex2f(1.0f, 0.0f);
        glVertex2f(1.5f, 1.2f);
        glVertex2f(0.5f, 1.2f);
    glEnd();
    glFlush();
}

Explanation:

  • glBegin(GL_POLYGON) starts a convex polygon; glEnd() ends it.
  • glVertex2f takes floating-point x and y (z is taken as 0).
  • Vertices are listed in order around the figure, so the sides are (0,0)-(1,0), (1,0)-(1.5,1.2), (1.5,1.2)-(0.5,1.2) and (0.5,1.2)-(0,0).
  • To get only the outline, use GL_LINE_LOOP instead of GL_POLYGON. The viewing volume must include x up to 1.5 and y up to 1.2 (for example, gluOrtho2D(-1, 2, -1, 2)).

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Graphics chapter 9 questions →