-
Notifications
You must be signed in to change notification settings - Fork 0
/
cube.cpp
227 lines (181 loc) · 5.99 KB
/
cube.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <iostream>
using namespace std;
#include "cube.h"
/*
Z
|
7 +-------+ 6
/| /|
/ | / | ---> Y
4 +-------+5 |
| 3+----|--+ 2
| / | /
|/ |/
0 +-------+ 1
/
X
*/
void Cube::draw()
{
glPushMatrix();
// draw immediate (center cube)
drawImmediate(true,true);
/*
// draw arrays (left cube)
glTranslatef(-2, 0, 0);
drawArrays();
// draw elements (right cube)
glTranslatef(+4, 0, 0);
drawElements();
*/
glPopMatrix();
}
//==================== 1. Immediate method ===================================
// immediate definition of individual vertex properties
void Cube::drawImmediate(bool smooth,bool normal)
{
// vertices coordinates can be stored
// GLfloat s0[] = {+0.5, -0.5, -0.5};
GLfloat s1[] = {+0.5, +0.5, -0.5};
GLfloat s2[] = {-0.5, +0.5, -0.5};
GLfloat s3[] = {-0.5, -0.5, -0.5};
GLfloat s4[] = {+0.5, -0.5, +0.5};
GLfloat s5[] = {+0.5, +0.5, +0.5};
GLfloat s6[] = {-0.5, +0.5, +0.5};
GLfloat s7[] = {-0.5, -0.5, +0.5};
// For each vertex of each face, define its normal then its coordinates
// - vertices shared by faces must be reset (glVertex3f) for each face
// - vertices of the same polygon might share the same normal (flat
// surface) or have different normals (curved surface)
glBegin(GL_QUADS);
// for each face, define a normal (first) the a
// 0 3 2 1
glNormal3f(0.0, 0.0, -1.0); // same normal shared by 4 vertices
glVertex3f(+0.5, -0.5, -0.5); // direct coordinates
glVertex3fv(s1); // stored coordinates
glVertex3fv(s2);
glVertex3fv(s3);
// 0 4 7 3
glNormal3f(0.0, -1.0, 0.0);
glVertex3f(+0.5, -0.5, -0.5);
glVertex3fv(s4);
glVertex3fv(s7);
glVertex3fv(s3);
// 1 5 4 0
glNormal3f(1.0, 0.0, 0.0);
glVertex3fv(s1);
glVertex3fv(s5);
glVertex3fv(s4);
glVertex3f(+0.5, -0.5, -0.5);
// 2 6 5 1
glNormal3f(0.0, 1.0, 0.0);
glVertex3fv(s2);
glVertex3fv(s6);
glVertex3fv(s5);
glVertex3fv(s1);
// 3 7 6 2
glNormal3f(-8.0, 0.0, 0.0);
glVertex3fv(s3);
glVertex3fv(s7);
glVertex3fv(s6);
glVertex3fv(s2);
// 4 5 6 7
glNormal3f(0.0, 0.0, 0.8);
glVertex3fv(s4);
glVertex3fv(s5);
glVertex3fv(s6);
glVertex3fv(s7);
glEnd();
}
//==================== 2. Arrays - drawArrays ================================
// - store vertex-related data (coordinates, normals, colors, etc) in arrays
// => reduces the number of function calls
// Define the coordinates of all vertices, ordered face by face.
// Since a vertex is shared by 3 faces, it is repeated 3 times!
static GLfloat allVertices[] = {
+0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, +0.5, -0.5, +0.5, +0.5, -0.5, // 0 3 2 1
+0.5, -0.5, -0.5, +0.5, -0.5, +0.5, -0.5, -0.5, +0.5, -0.5, -0.5, -0.5, // 0 4 7 3
+0.5, +0.5, -0.5, +0.5, +0.5, +0.5, +0.5, -0.5, +0.5, +0.5, -0.5, -0.5, // 1 5 4 0
-0.5, +0.5, -0.5, -0.5, +0.5, +0.5, +0.5, +0.5, +0.5, +0.5, +0.5, -0.5, // 2 6 5 1
-0.5, -0.5, -0.5, -0.5, -0.5, +0.5, -0.5, +0.5, +0.5, -0.5, +0.5, -0.5, // 3 7 6 2
+0.5, -0.5, +0.5, +0.5, +0.5, +0.5, -0.5, +0.5, +0.5, -0.5, -0.5, +0.5, // 4 5 6 7
};
// Define the normals of vertices, using the same vertex order.
static GLfloat allNormals[] = {
0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, // 0 3 2 1
0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, // 0 4 7 3
1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, // 1 5 4 0
0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, // 2 6 5 1
-1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, // 3 7 6 2
0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, // 4 5 6 7
};
void Cube::drawArrays()
{
// activate the use of GL_VERTEX_ARRAY and GL_NORMAL_ARRAY
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
// specify the arrays to use
glNormalPointer(GL_FLOAT, 0, allNormals);
glVertexPointer(3, GL_FLOAT, 0 , allVertices);
// draw quads using 24 data stored in arrays, starting at index 0
glDrawArrays(GL_QUADS, 0, 24);
// disable the use of arrays (do not forget!)
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
// Others arrays could also be used:
// GL_COLOR_ARRAY, GL_INDEX_ARRAY, GL_TEXTURE_COORD_ARRAY, ...
};
//==================== 3. Arrays - drawElements ==============================
// - single definition of shared data
// - draw face by face, using face indices
// vertex coordinates (defined once only)
static GLfloat vertices[] = {
+0.5, -0.5, -0.5, // 0
+0.5, +0.5, -0.5, // 1
-0.5, +0.5, -0.5, // 2
-0.5, -0.5, -0.5, // 3
+0.5, -0.5, +0.5, // 4
+0.5, +0.5, +0.5, // 5
-0.5, +0.5, +0.5, // 6
-0.5, -0.5, +0.5, // 7
};
// indexes of the 6 faces (in a 2D array)
static GLubyte indices[6][4] = {
{0, 3, 2, 1},
{0, 4, 7, 3},
{1, 5, 4, 0},
{2, 6, 5, 1},
{3, 7, 6, 2},
{4, 5, 6, 7}
};
// normals (defined once only)
static GLfloat normals[6][3] = {
{ 0.0, 0.0, -1.0},
{ 0.0, -1.0, 0.0},
{ 1.0, 0.0, 0.0},
{ 0.0, 1.0, 0.0},
{-1.0, 0.0, 0.0},
{ 0.0, 0.0, 1.0}
};
void Cube::drawElements()
{
// activate the use of GL_VERTEX_ARRAY (not GL_NORMALS_ARRAY)
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0 , vertices);
// for each face:
// - set the current normal (state machine)
// - draw a face using glDrawElements with the indices of the face
glNormal3fv(normals[0]);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, indices[0]);
glNormal3fv(normals[1]);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, indices[1]);
glNormal3fv(normals[2]);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, indices[2]);
glNormal3fv(normals[3]);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, indices[3]);
glNormal3fv(normals[4]);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, indices[4]);
glNormal3fv(normals[5]);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, indices[5]);
glDisableClientState(GL_VERTEX_ARRAY);
}