- 2071 Shrawan · 4 marks
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.glVertex2ftakes 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_LOOPinstead ofGL_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)
Related questions
- OpenGL syntax: 3D vertex · Computer Graphics, chapter 9
- Drawing primitives in OpenGL · Computer Graphics, chapter 9 · asked 4×
- Construct geometric table, 6 vertices · Computer Graphics, chapter 6