Early OpenGL
Drawing Triangles with Old OpenGL and GLUT (Immediate Mode)
Objective
This tutorial introduces rendering triangles using OpenGL with GLUT, employing the old immediate mode (glBegin/glEnd
). We will start with a single triangle and extend it to multiple triangles.
Prerequisites
C++ development environment (e.g., Visual Studio, Code::Blocks, or GCC)
OpenGL and GLUT installed
Early OpenGL
Immediate mode in OpenGL consists of glBegin
/glEnd
calls, with one or more glVertex
calls (the minimum legal number depending on the mode param of your glBegin
call), and optionally other vertex attribute specification calls (glColor
, glTexCoord
, etc), between them.
glBegin
instructs the GL driver that you're starting to draw a point, line or polygon.glTexCoord
,glColor
,glNormal
, etc just set a "current value" for the texture coordinate, colour, normal, etc.glVertex
takes those "current values", together with the position information that you supply with theglVertex
call, and transfers the lot to the driver.glEnd
completes the point, line or polygon.
The major advantage to immediate mode is that you need to plan absolutely nothing up-front. You can just issue a glBegin
call and start sending aribtrary geometry to your driver and hardware.
The major disadvantages to immediate mode include that it incurs a lot of function call overhead into the GL driver, and that vertex data must always be sent to the graphics hardware, even if that vertex data is unchanged from last time it was used.
Step 1: Setting Up the Environment
Install GLUT if not already installed.
Create a new C++ project and include necessary headers:
Initialize GLUT and create a window:
Step 2: Drawing a Single Triangle
Modify the display()
function to render a single triangle using glBegin(GL_TRIANGLES)
:
This will render a coloured triangle.
Step 3: Drawing Multiple Triangles
Extend display()
to draw multiple triangles:
Now two triangles are drawn.
Step 4: Drawing a Triangle Mesh with Indices
Although immediate mode does not support indexed rendering directly, we can conceptually structure our rendering using indices in an array and manually reference them:
This example simulates indexed rendering by manually referencing vertex data, drawing two triangles to form a quad.
Conclusion
This tutorial demonstrated how to render triangles using OpenGL’s immediate mode with GLUT. Future steps include transitioning to modern OpenGL (VBOs and shaders).
Last updated