-
Notifications
You must be signed in to change notification settings - Fork 7
/
Dodecahedron.cpp
232 lines (203 loc) · 5.57 KB
/
Dodecahedron.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
225
226
227
228
229
230
231
232
#include "Dodecahedron.h"
using namespace glm;
//Constructor
Dodecahedron::Dodecahedron()
{
//fill all the vectors of vertices
//all vertices
v = {
{ 0.2, 0.2, 0.2 }, // 00
{ 0.2, 0.2, -0.2 }, // 01
{ 0.2, -0.2, 0.2 }, // 02
{ -0.2, 0.2, 0.2 }, // 03
{ -0.2, -0.2, 0.2 }, // 04
{ -0.2, 0.2, -0.2 }, // 05
{ 0.2, -0.2, -0.2 }, // 06
{ -0.2, -0.2, -0.2 }, // 07
{ 0.0, 0.2 / D, 0.2 * D }, // 08
{ 0.0, -0.2 / D, 0.2 * D }, // 09
{ 0.0, 0.2 / D, -0.2 * D }, // 10
{ 0.0, -0.2 / D, -0.2 * D }, // 11
{ 0.2 / D, 0.2*D, 0 }, // 12
{ -0.2 / D, 0.2*D, 0 }, // 13
{ 0.2 / D, -0.2*D, 0 }, // 14
{ -0.2 / D, -0.2*D, 0 }, // 15
{ 0.2*D, 0, 0.2 / D }, // 16
{ -0.2*D, 0, 0.2 / D }, // 17
{ 0.2*D, 0, -0.2 / D }, // 18
{ -0.2*D, 0, -0.2 / D } // 19
};
//the faces, one at a time
face0 = {
{0.2, 0.2, 0.2},
{0.2 / D, 0.2 * D, 0.0},
{0.2, 0.2, -0.2 },
{0.2 * D, 0.0, -0.2 / D},
{0.2 * D, 0.0, 0.2 / D}
};
face1 = {
{0.0, 0.2 / D, 0.2 * D},
{-0.2, 0.2, 0.2 },
{-0.2 / D, 0.2 * D, 0.0},
{0.2 / D, 0.2 * D, 0.0},
{0.2, 0.2, 0.2}
};
face2 = {
{ 0.2 / D, 0.2*D, 0 },
{ -0.2 / D, 0.2*D, 0 },
{ -0.2, 0.2, -0.2 },
{ 0.0, 0.2 / D, -0.2 * D },
{ 0.2, 0.2, -0.2 }
};
face3 = {
{-0.2 * D, 0.0, -0.2 / D},
{-0.2, -0.2, -0.2},
{0.0, -0.2 / D, -0.2 * D},
{0.0, 0.2 / D, -0.2 * D},
{-0.2, 0.2, -0.2}
};
face4 = {
{ -0.2, -0.2, -0.2 },
{ -0.2*D, 0.0, -0.2 / D },
{ -0.2*D, 0.0, 0.2 / D },
{ -0.2, -0.2, 0.2 },
{ -0.2 / D, -0.2*D, 0.0 }
};
face5 = {
{0.2 / D, -0.2 * D, 0.0},
{0.2, -0.2, -0.2},
{0.0, -0.2 / D, -0.2 * D},
{-0.2, -0.2, -0.2},
{-0.2 / D, -0.2 * D, 0.0}
};
face6 = {
{ 0.0, -0.2 / D, -0.2 * D },
{ 0.2, -0.2, -0.2 },
{ 0.2*D, 0, -0.2 / D },
{ 0.2, 0.2, -0.2 },
{ 0.0, 0.2 / D, -0.2 * D }
};
face7 = {
{0.2 / D, -0.2 * D, 0.0},
{0.2, -0.2, 0.2},
{0.2 * D, 0.0, 0.2 / D},
{0.2 * D, 0.0, -0.2 / D},
{0.2, -0.2, -0.2}
};
face8 = {
{-0.2 * D, 0.0, 0.2 / D},
{-0.2 * D, 0.0, -0.2 / D},
{-0.2, 0.2, -0.2},
{-0.2 / D, 0.2 * D, 0.0},
{-0.2, 0.2, 0.2}
};
face9 = {
{0.0, 0.2 / D, 0.2 * D},
{0.0, -0.2 / D, 0.2 * D},
{-0.2, -0.2, 0.2},
{-0.2 * D, 0.0, 0.2 / D},
{-0.2, 0.2, 0.2}
};
face10 = {
{0.2 * D, 0.0, 0.2 / D},
{0.2, -0.2, 0.2},
{0.0, -0.2 / D, 0.2 * D},
{0.0, 0.2 / D, 0.2 * D},
{0.2, 0.2, 0.2}
};
face11 = {
{ -0.2, -0.2, 0.2 },
{ 0.0, -0.2 / D, 0.2 * D },
{ 0.2, -0.2, 0.2 },
{ 0.2 / D, -0.2*D, 0 },
{ -0.2 / D, -0.2*D, 0 }
};
//all the faces to have another way to access them
faces = { face0, face1, face2, face3, face4, face5, face6, face7, face8, face9, face10, face11 };
}
void Dodecahedron::AddTextureCoordinates()
{
//order: bottom right, bottom left, far left, top, far right
this->data.textures.push_back(vec2(1.0f / D + 0.2f, 0.0f));
this->data.textures.push_back(vec2(0.2f, 0.0f));
this->data.textures.push_back(vec2(0.0f, 1.0f / D));
this->data.textures.push_back(vec2(0.5f, 1.0f));
this->data.textures.push_back(vec2(1.0f, 1.0f / D));
}
//Establishes initial vertex attributes (normals, positions, etc)
bool Dodecahedron::PreGLInitialize()
{
//a and b are used to get n, which determines the direction of a face's normals
vec3 a;
vec3 b;
vec3 n;
//used to determine which indices of the vertices vector to grab from to get the triangles
//to draw each face
int index = 0;
//set vertices and attributes, one face at a time
for (int i = 0; i < NUM_FACES; i++)
{
//compute normals
a = normalize(faces[i][1] - faces[i][0]);
b = normalize(faces[i][2] - faces[i][0]);
n = normalize(cross(b, a) / 8.0f);
//loop through each point and store its location, normal, etc.
for (int j = 0; j < VERTICES_PER_FACE; j++)
{
data.vertices.push_back(faces[i][j]);
data.normals.push_back(n);
data.colors.push_back(vec4(0.0f, i / 12.0f, 0.0f, 1.0f));
data.normal_visualization_coordinates.push_back(*(data.vertices.end() - 1));
data.normal_visualization_coordinates.push_back(*(data.vertices.end() - 1) + n / NORMAL_LENGTH_DIVISOR);
}
//store the other important piece of data about each vertex
AddTextureCoordinates();
//figure out the triangles each face is made of
//Triangle 1
data.indices.push_back(index);
data.indices.push_back(index + 1);
data.indices.push_back(index + 2);
//Triangle 2
data.indices.push_back(index);
data.indices.push_back(index + 2);
data.indices.push_back(index + 3);
//Triangle 3
data.indices.push_back(index);
data.indices.push_back(index + 3);
data.indices.push_back(index + 4);
index += VERTICES_PER_FACE;
}
data.vbackup = data.vertices;
return true;
}
void Dodecahedron::NonGLTakeDown()
{
}
void Dodecahedron::RecomputeNormals()
{
//because using one letter is much easier than typing out the full name each time
vector<vec3> & v = data.vertices;
vector<vec3> & n = data.normals;
vector<vec3> & p = data.normal_visualization_coordinates;
//a and b are used to compute n2, which is the direction for a given face's normals
vec3 a, b, n2;
//incrementor variables
int j = 0;
int k = 0;
for (int i = 0; i < NUM_FACES; i++)
{
//compute the normals for face i
a = normalize(v[k + 1] - v[k]);
b = normalize(v[k + 2] - v[k]);
n2 = normalize(cross(b, a) / 8.0f);
//update the normals for each vertex on face i to the newly computed value
for (int ver = 0; ver < VERTICES_PER_FACE; ver++)
{
n[k] = n2;
k++;
}
//update the visualization for the normals
p[j++] = v[i];
p[j++] = v[i] + n[i] / NORMAL_LENGTH_DIVISOR;
}
}