---
title: "Drawing primitives in OpenGL"
subject: "Computer Graphics"
chapter: 9
key: "semester_5/computer_graphics:opengl-draw-primitives-2d"
source: https://www.bctengineer.com/q/semester_5/computer_graphics/opengl-draw-primitives-2d
site: BCT Engineering
---

# Drawing primitives in OpenGL

Computer Graphics · Chapter 9: Introduction to Open GL

- Asked in: 2078 Bhadra, 5 marks; 2078 Kartik, 4 marks; 2076 Asoj, 5 marks; 2075 Chaitra, 2 marks (asked 4 times)
- Video: [OpenGL Primitives](https://www.youtube.com/watch?v=SJUVx84hh7k) — UCSC CSE160
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_5/computer_graphics/opengl-draw-primitives-2d
- Whole chapter: https://www.bctengineer.com/md/semester_5/computer_graphics/chapter-9

## Question

How are points, lines and polygons drawn in OpenGL? Write the OpenGL syntax to draw basic 2D geometric primitives (coloured lines and polygons) with examples.

## Short answer

Points, lines, and polygons are drawn between glBegin(mode) and glEnd(): GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP, GL_TRIANGLES, GL_QUADS, GL_POLYGON. Vertices specified with glVertex*, colors with glColor*; line width and point size set by glLineWidth/glPointSize. Example: glBegin(GL_LINES); glColor3f(1,0,0); glVertex2f(...); ... glEnd();

## Answer

In OpenGL, primitives are drawn between `glBegin(mode)` and `glEnd()`, giving the vertices with `glVertex*()` and colours with `glColor*()`.

| Mode | Draws |
|---|---|
| GL_POINTS | Individual points |
| GL_LINES | Separate line segments (pairs of vertices) |
| GL_LINE_STRIP / GL_LINE_LOOP | Connected lines (open / closed) |
| GL_TRIANGLES | Triangles |
| GL_QUADS | Quadrilaterals |
| GL_POLYGON | A single convex polygon |

Other useful functions: `glPointSize(w)`, `glLineWidth(w)`, `glColor3f(r,g,b)` (values 0 to 1).

**Example**

```c
#include <GL/glut.h>

void display(void) {
    glClear(GL_COLOR_BUFFER_BIT);

    glPointSize(5.0f);
    glBegin(GL_POINTS);                 // points
        glColor3f(1, 1, 1);
        glVertex2f(-0.8f, 0.8f);
    glEnd();

    glLineWidth(2.0f);
    glBegin(GL_LINES);                  // coloured line
        glColor3f(1, 0, 0); glVertex2f(-0.9f, -0.9f);
        glColor3f(0, 0, 1); glVertex2f(-0.1f, -0.1f);
    glEnd();

    glBegin(GL_POLYGON);                // coloured polygon
        glColor3f(0, 1, 0); glVertex2f(0.1f, 0.1f);
        glColor3f(1, 1, 0); glVertex2f(0.8f, 0.1f);
        glColor3f(0, 1, 1); glVertex2f(0.8f, 0.8f);
        glColor3f(1, 0, 1); glVertex2f(0.1f, 0.8f);
    glEnd();
    glFlush();
}
int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutCreateWindow("Primitives");
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}
```

The line goes from red to blue (colours are interpolated) and the polygon is shaded with the four vertex colours.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Between which calls are vertices of a primitive given?
   A. glClear and glFlush
   B. glBegin(mode) and glEnd()
   C. glutInit and glutMainLoop
   D. glColor3f and glVertex2f only
   Answer: B. Vertices go between glBegin and glEnd. (Key point: Drawing structure)

2. Which mode draws separate line segments from pairs of vertices?
   A. GL_LINE_LOOP
   B. GL_POLYGON
   C. GL_LINES
   D. GL_POINTS
   Answer: C. GL_LINES treats each pair as a segment. (Key point: Modes)

3. Which mode draws a closed set of connected lines?
   A. GL_LINE_STRIP
   B. GL_LINES
   C. GL_TRIANGLES
   D. GL_LINE_LOOP
   Answer: D. GL_LINE_STRIP is open; GL_LINE_LOOP closes it. (Key point: Open vs closed)

4. In glColor3f(r,g,b), what is the range of the values?
   A. 0 to 1
   B. 0 to 255
   C. -1 to 1
   D. 0 to 100
   Answer: A. Colour components are floats from 0 to 1. (Key point: Colour)

5. What happens if two ends of a line have different colours?
   A. The line takes the first colour
   B. The colour is interpolated along the line
   C. The line is not drawn
   D. The line takes the last colour only
   Answer: B. Smooth shading blends the vertex colours. (Key point: Interpolation)

## Related in Computer Graphics

- [OpenGL syntax: parallelogram](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Aopengl-parallelogram-syntax) — Computer Graphics, chapter 9
- [OpenGL syntax: 3D vertex](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Aopengl-3d-float-vertex-syntax) — Computer Graphics, chapter 9
- [Composite rotation and shear in 2D](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Acomposite-rotation-and-shear-2d) — Computer Graphics, chapter 3

## Related in other subjects

- [Stagnation points in 2D velocity field](https://www.bctengineer.com/question.md?key=semester_3%2Ffluid_mechanics%3Astagnation-points-velocity-field) — Fluid Mechanics, chapter 4
- [Ruled surface between two lines: points](https://www.bctengineer.com/question.md?key=semester_4%2Fcomputer_aided_design%3Aruled-surface-numerical) — Computer Aided Design, chapter 4
- [Percent cold work of a drawn wire](https://www.bctengineer.com/question.md?key=semester_3%2Fmaterial_science%3Apercent-cold-work-numerical) — Material Science, chapter 4
- [Couple, wrench, moment of P about points and lines](https://www.bctengineer.com/question.md?key=semester_1%2Fapplied_mechanics%3Acouple-free-vector-wrench-cube-force-p) — Applied Mechanics, chapter 3
