Drawing Basic Shapes in OpenGL
Drawing in computer graphics is based on the idea that complex images are constructed from simpler components known as geometric primitives. These primitives include points, lines, and polygons, and they are defined using vertices, which specify positions in a coordinate system. OpenGL uses these primitives to render shapes on the screen by converting mathematical descriptions into visible pixels.
1. Graphics Primitives
In computer graphics, primitives are the basic building blocks used to create images.
Key Types:
- Point → single location
- Line → connection between two points
- Polygon → closed shape (triangle, rectangle, etc.)
Any complex object (like a car or character) is made by combining these primitives.
Example:
A house drawing:
- Roof → triangle
- Body → rectangle
- Windows → smaller rectangles
2. Point Representation
A point represents a specific position in space and is the simplest primitive. It has no dimension, but when displayed, it corresponds to a pixel on the screen.
Theory Points:
- Defined by coordinates (x, y)
- No length or area
- Smallest graphical unit
Key Idea:
- One point = one pixel (in most cases)
Example (OpenGL):
glPointSize(5.0); // increase visibility
glBegin(GL_POINTS);
glVertex2f(0.0, 0.0);
glEnd();This draws a visible point at the center of the screen.
3. Line Representation
A line connects two points and represents the shortest path between them. In mathematics, a line is continuous, but in computer graphics, it must be approximated using pixels.
Theory Points:
- Defined by two endpoints (x₁, y₁) and (x₂, y₂)
- Appears as a set of pixels on screen
- Created using rasterization
Important Concept:
- Continuous → converted to discrete pixels
- Uses internal algorithms (e.g., DDA, Bresenham)
Types of Lines:
- Simple line
- Connected lines (line strip)
- Closed loop
Example:
glBegin(GL_LINES);
glVertex2f(-0.5, -0.5);
glVertex2f(0.5, 0.5);
glEnd();This draws a diagonal line.
4. Polygon (Rectangle) Representation
A polygon is a closed shape formed by connecting multiple vertices. Unlike points and lines, polygons cover an area.
Theory Points:
- Defined by a sequence of vertices
- Must be closed
- Used to represent surfaces
Rectangle Properties:
- 4 vertices
- Opposite sides equal
- All angles = 90°
Key Idea:
Polygon = boundary + interior area
Example:
glBegin(GL_QUADS);
glVertex2f(-0.5, -0.5);
glVertex2f(0.5, -0.5);
glVertex2f(0.5, 0.5);
glVertex2f(-0.5, 0.5);
glEnd();This creates a square/rectangle shape.
5. Circle Representation
A circle is defined mathematically as all points equidistant from a center. However, since screens are made of pixels, circles must be approximated.
Theory Points:
- Defined using radius and center
- Continuous in math, discrete in graphics
- Approximated using many small segments
Key Concept:
- More points → smoother circle
Example:
glBegin(GL_TRIANGLE_FAN);
glVertex2f(0.0, 0.0);
for(int i=0; i<=360; i++){
float angle = i * 3.1416 / 180;
glVertex2f(cos(angle)*0.5, sin(angle)*0.5);
}
glEnd();This approximates a circle using many vertices.
6. Vertex Concept
A vertex is a fundamental unit used to define shapes.
Theory Points:
- Specifies position in space
- Shapes are built using vertices
- Order of vertices affects shape
Examples:
- Line → 2 vertices
- Triangle → 3 vertices
- Rectangle → 4 vertices
Graphics = collection of vertices
7. Rendering Process
Rendering is the process of converting geometric data into a visible image.
Steps:
- Define vertices
- Specify primitive type
- Convert into fragments (pixels)
- Display on screen
Key Idea:
Math → pixels → image