The indigoparadox Web Zone

OpenGL 1.0: Polygons

Back to tutorials

Web Zone Navigation

Related Web Zones

Other Web Zones

This page was generated from a source code file. Click here to download the file this page was generated from.

1. Preamble

Include The system OpenGL header for constants and functions used below. Also include mini.h, a header with some program-specific constants we'll also be using.

2. Polygon: Cube

oglpoly.c
1#include
2#include "mini.h"

First, let's draw a simple multi-colored cube.

oglpoly.c
3void mini_cube() {

Now begin drawing the faces of the cube.

For each face, please note that the normals are orthogonal to the face. If the face is at the base of the normal, the normal points directly "upwards."

Please also note that the faces are defined in terms of triangles. For most implementations of older OpenGL versions, quads are also an option, but triangles are generally the most portable.

Draw the back face:

oglpoly.c
4 glBegin( GL_TRIANGLES );
5 glNormal3f( 0, 0, 1.0f );
6 glColor3f( 1.0f, 0, 0 );
7 glVertex3f( 1.0f, -1.0f, 1.0f );
8 glVertex3f( 1.0f, 1.0f, 1.0f );
9 glVertex3f( -1.0f, 1.0f, 1.0f );
10 glVertex3f( -1.0f, 1.0f, 1.0f );
11 glVertex3f( -1.0f, -1.0f, 1.0f );
12 glVertex3f( 1.0f, -1.0f, 1.0f );
13 glEnd();

Draw the right face:

oglpoly.c
14 glBegin( GL_TRIANGLES );
15 glNormal3f( 1.0f, 0, 0 );
16 glColor3f( 0 , 1.0f, 0 );
17 glVertex3f( 1.0f, -1.0f, -1.0f );
18 glVertex3f( 1.0f, 1.0f, -1.0f );
19 glVertex3f( 1.0f, 1.0f, 1.0f );
20 glVertex3f( 1.0f, 1.0f, 1.0f );
21 glVertex3f( 1.0f, -1.0f, 1.0f );
22 glVertex3f( 1.0f, -1.0f, -1.0f );
23 glEnd();

Draw the left face:

oglpoly.c
24 glBegin( GL_TRIANGLES );
25 glNormal3f( -1.0f, 0, 0 );
26 glColor3f( 0, 0, 1.0f );
27 glVertex3f( -1.0f, -1.0f, 1.0f );
28 glVertex3f( -1.0f, 1.0f, 1.0f );
29 glVertex3f( -1.0f, 1.0f, -1.0f );
30 glVertex3f( -1.0f, 1.0f, -1.0f );
31 glVertex3f( -1.0f, -1.0f, -1.0f );
32 glVertex3f( -1.0f, -1.0f, 1.0f );
33 glEnd();

Draw the front face:

oglpoly.c
34 glBegin( GL_TRIANGLES );
35 glNormal3f( 0, 0, -1.0f );
36 glColor3f( 1.0f, 1.0f, 0 );
37 glVertex3f( -1.0f, -1.0f, -1.0f );
38 glVertex3f( -1.0f, 1.0f, -1.0f );
39 glVertex3f( 1.0f, 1.0f, -1.0f );
40 glVertex3f( 1.0f, 1.0f, -1.0f );
41 glVertex3f( 1.0f, -1.0f, -1.0f );
42 glVertex3f( -1.0f, -1.0f, -1.0f );
43 glEnd();

Draw the top face:

oglpoly.c
44 glBegin( GL_TRIANGLES );
45 glNormal3f( 0, 1.0f, 0 );
46 glColor3f( 0, 1.0f, 1.0f );
47 glVertex3f( 1.0f, 1.0f, 1.0f );
48 glVertex3f( 1.0f, 1.0f, -1.0f );
49 glVertex3f( -1.0f, 1.0f, -1.0f );
50 glVertex3f( -1.0f, 1.0f, -1.0f );
51 glVertex3f( -1.0f, 1.0f, 1.0f );
52 glVertex3f( 1.0f, 1.0f, 1.0f );
53 glEnd();

Draw the bottom face:

oglpoly.c
54 glBegin( GL_TRIANGLES );
55 glNormal3f( 0, -1.0f, 0 );
56 glColor3f( 1.0f, 1.0f, 1.0f );
57 glVertex3f( 1.0f, -1.0f, -1.0f );
58 glVertex3f( 1.0f, -1.0f, 1.0f );
59 glVertex3f( -1.0f, -1.0f, 1.0f );
60 glVertex3f( -1.0f, -1.0f, 1.0f );
61 glVertex3f( -1.0f, -1.0f, -1.0f );
62 glVertex3f( 1.0f, -1.0f, -1.0f );
63 glEnd();
64}

Table of Contents

  1. Preamble
  2. Polygon: Cube