This page was generated from a source code file. Click here to download the file this page was generated from.
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.
1 | #include |
2 | #include "mini.h" |
First, let's draw a simple multi-colored cube.
3 | void 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:
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:
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:
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:
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:
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:
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 | } |