forked from pkivolowitz/Shapes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plane.cpp
378 lines (333 loc) · 12.5 KB
/
Plane.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include <iostream>
#include <stdexcept>
#include "Plane.h"
using namespace std;
using namespace glm;
// Width and height are one more than divisionsX and divisionsY.
// Example:
// divisionsX = divisionsY = 2 yields a two by two grid
// with three verticies across and down (for a 9 by 9).
Plane::Plane(int divisionsX, int divisionsY)
{
if (divisionsX < 1 || divisionsY < 1)
throw std::invalid_argument("bad divisionX or divisionY value");
this->divisionsX = divisionsX;
this->divisionsY = divisionsY;
}
void Cube::AddTextureCoordinates()
{
this->data.textures.push_back(vec2(0.0f , 1.0f));
this->data.textures.push_back(vec2(1.0f , 1.0f));
this->data.textures.push_back(vec2(1.0f , 0.0f));
this->data.textures.push_back(vec2(0.0f , 0.0f));
}
// PreGLInitialize is used to establish an initial configuration of vertex
// attributes include position, normal, etc.
bool Cube::PreGLInitialize()
{
// Front
this->data.vertices.push_back(vec3(-1.0f, 1.0f, 1.0f));
this->data.vertices.push_back(vec3( 1.0f, 1.0f, 1.0f));
this->data.vertices.push_back(vec3( 1.0f, -1.0f, 1.0f));
this->data.vertices.push_back(vec3(-1.0f, -1.0f, 1.0f));
for (int i = 0; i < 4; i++)
{
this->data.normals.push_back(vec3(0.0f , 0.0f , 1.0f));
this->data.normal_visualization_coordinates.push_back(this->data.vertices[0 + i]);
this->data.normal_visualization_coordinates.push_back(this->data.vertices[0 + i] + this->data.normals[0 + i] / NORMAL_LENGTH_DIVISOR);
}
AddTextureCoordinates();
// right (facing positive x axis)
this->data.vertices.push_back(vec3(1.0f, 1.0f, 1.0f));
this->data.vertices.push_back(vec3(1.0f, 1.0f, -1.0f));
this->data.vertices.push_back(vec3(1.0f, -1.0f, -1.0f));
this->data.vertices.push_back(vec3(1.0f, -1.0f, 1.0f));
for (int i = 0; i < 4; i++)
{
this->data.normals.push_back(vec3(1.0f , 0.0f , 0.0f));
this->data.normal_visualization_coordinates.push_back(this->data.vertices[4 + i]);
this->data.normal_visualization_coordinates.push_back(this->data.vertices[4 + i] + this->data.normals[4 + i] / NORMAL_LENGTH_DIVISOR);
}
AddTextureCoordinates();
// back (facing negative z axis)
this->data.vertices.push_back(vec3( 1.0f, 1.0f, -1.0f));
this->data.vertices.push_back(vec3(-1.0f, 1.0f, -1.0f));
this->data.vertices.push_back(vec3(-1.0f, -1.0f, -1.0f));
this->data.vertices.push_back(vec3( 1.0f, -1.0f, -1.0f));
for (int i = 0; i < 4; i++)
{
this->data.normals.push_back(vec3(0.0f , 0.0f , -1.0f));
this->data.normal_visualization_coordinates.push_back(this->data.vertices[8 + i]);
this->data.normal_visualization_coordinates.push_back(this->data.vertices[8 + i] + this->data.normals[8 + i] / NORMAL_LENGTH_DIVISOR);
}
AddTextureCoordinates();
// left (facing negative x axis
this->data.vertices.push_back(vec3(-1.0f, 1.0f, -1.0f));
this->data.vertices.push_back(vec3(-1.0f, 1.0f, 1.0f));
this->data.vertices.push_back(vec3(-1.0f, -1.0f, 1.0f));
this->data.vertices.push_back(vec3(-1.0f, -1.0f,-1.0f));
for (int i = 0; i < 4; i++)
{
this->data.normals.push_back(vec3(-1.0f , 0.0f , 0.0f));
this->data.normal_visualization_coordinates.push_back(this->data.vertices[12 + i]);
this->data.normal_visualization_coordinates.push_back(this->data.vertices[12 + i] + this->data.normals[12 + i] / NORMAL_LENGTH_DIVISOR);
}
AddTextureCoordinates();
// top (facing positive y axis)
this->data.vertices.push_back(vec3(-1.0f, 1.0f, -1.0f));
this->data.vertices.push_back(vec3( 1.0f, 1.0f, -1.0f));
this->data.vertices.push_back(vec3( 1.0f, 1.0f, 1.0f));
this->data.vertices.push_back(vec3(-1.0f, 1.0f, 1.0f));
for (int i = 0; i < 4; i++)
{
this->data.normals.push_back(vec3(0.0f , 1.0f , 0.0f));
this->data.normal_visualization_coordinates.push_back(this->data.vertices[16 + i]);
this->data.normal_visualization_coordinates.push_back(this->data.vertices[16 + i] + this->data.normals[16 + i] / NORMAL_LENGTH_DIVISOR);
}
AddTextureCoordinates();
// bottom (facing negative y axis)
this->data.vertices.push_back(vec3(-1.0f, -1.0f, 1.0f));
this->data.vertices.push_back(vec3(1.0f, -1.0f, 1.0f));
this->data.vertices.push_back(vec3(1.0f, -1.0f, -1.0f));
this->data.vertices.push_back(vec3(-1.0f, -1.0f, -1.0f));
for (int i = 0; i < 4; i++)
{
this->data.normals.push_back(vec3(0.0f , -1.0f , 0.0f));
this->data.normal_visualization_coordinates.push_back(this->data.vertices[20 + i]);
this->data.normal_visualization_coordinates.push_back(this->data.vertices[20 + i] + this->data.normals[20 + i] / NORMAL_LENGTH_DIVISOR);
}
AddTextureCoordinates();
// Faces complete.
for (unsigned int i = 0; i < this->data.vertices.size(); i++)
this->data.indices.push_back(i);
this->data.vbackup = this->data.vertices;
return true;
}
// Position vertices and push them onto ->vertices.
// Define colors if we want and push them onto ->colors.
// Define normals - this is easy - they are all the same. Push these on to normals.
// Define all the triangles by specifying indices making up top triangles and bottom triangles.
bool Plane::PreGLInitialize()
{
float deltaX = 2.0f / float(this->divisionsX);
float deltaY = -2.0f / float(this->divisionsY);
vec3 v(-1.0f , 1.0f , 0.0f);
for (int y = 0; y < this->divisionsY + 1; y++)
{
for (int x = 0; x < this->divisionsX + 1; x++)
{
// Sometimes it is useful to push the first coordinate out of its regular position
// so that orientation of the plane can be discerned visually. When the following are
// uncommented, the first vertex is handled differently to permit this.
//if (y == 0 && x == 0)
// this->data.vertices.push_back(vec3(-2.0f, v.y, v.z));
//else
this->data.vertices.push_back(v);
this->data.textures.push_back(vec2(v.x / 2.0f + 0.5f, v.y / 2.0f + 0.5f));
this->data.normals.push_back(vec3(0.0f,0.0f,1.0f));
this->data.colors.push_back(this->RandomColor((this->data.colors.size() > 0 ? *(this->data.colors.end() - 1) : vec4(0.5f , 0.5f , 0.5f , 1.0f)) , -0.2f , 0.2f));
this->data.normal_visualization_coordinates.push_back(*(this->data.vertices.end() - 1));
this->data.normal_visualization_coordinates.push_back(*(this->data.vertices.end() - 1) + vec3(0.0f, 0.0f, 1.0f) / NORMAL_LENGTH_DIVISOR);
v.x = v.x + deltaX;
}
v.x = -1.0f;
v.y = v.y + deltaY;
}
int i = 0;
int w = divisionsX + 1;
for (int y = 0; y < this->divisionsY; y++)
{
for (int x = 0; x < this->divisionsX; x++)
{
// "Top" Triangle - This associates this vertex with the one to its right with the one
// beneath the current vertex. The order defines a clockwise winding.
this->data.indices.push_back(i);
this->data.indices.push_back(i + 1);
this->data.indices.push_back(i + w);
// Bottom triangle - This associates the vertex to our right with the one below it and
// the one below the current vertex. This order defines a clockwise winding.
this->data.indices.push_back(i + 1);
this->data.indices.push_back(i + w + 1);
this->data.indices.push_back(i + w);
i++;
}
i++;
}
this->data.vbackup = this->data.vertices;
return true;
}
void Plane::NonGLTakeDown()
{
}
void Cube::NonGLTakeDown()
{
}
#define RIGHT (1)
#define DOWN (this->divisionsX + 1)
#define DOWN_LEFT (this->divisionsX + 0)
#define LEFT (-1)
#define UP -DOWN
#define UP_RIGHT -DOWN_LEFT
void Plane::SE(vec3 * v, int row , int col , glm::vec3 & sum , float & divisor)
{
vec3 a , b;
if (row == this->divisionsY || col == this->divisionsX)
return;
a = normalize(*(v + RIGHT) - *v);
b = normalize(*(v + DOWN) - *v);
sum += normalize(cross(a , b));
divisor++;
}
void Plane::SSW(vec3 * v , int row , int col , glm::vec3 & sum , float & divisor)
{
vec3 a , b;
if (row == this->divisionsY || col == 0)
return;
a = normalize(*(v + DOWN) - *v);
b = normalize(*(v + DOWN_LEFT) - *v);
sum += normalize(cross(a , b));
divisor++;
}
void Plane::WSW(vec3 * v , int row , int col , glm::vec3 & sum , float & divisor)
{
vec3 a , b;
if (row == this->divisionsY || col == 0)
return;
a = normalize(*(v + DOWN_LEFT) - *v);
b = normalize(*(v + LEFT) - *v);
sum += normalize(cross(a , b));
divisor++;
}
void Plane::NW(vec3 * v , int row , int col , glm::vec3 & sum , float & divisor)
{
vec3 a , b;
if (row == 0 || col == 0)
return;
a = normalize(*(v + LEFT) - *v);
b = normalize(*(v + UP) - *v);
sum += normalize(cross(a , b));
divisor++;
}
void Plane::NNE(vec3 * v , int row , int col , glm::vec3 & sum , float & divisor)
{
vec3 a , b;
if (row == 0 || col == this->divisionsX)
return;
a = normalize(*(v + UP) - *v);
b = normalize(*(v + UP_RIGHT) - *v);
sum += normalize(cross(a , b));
divisor++;
}
void Plane::ENE(vec3 * v , int row , int col , glm::vec3 & sum , float & divisor)
{
vec3 a , b;
if (row == 0 || col == this->divisionsX)
return;
a = normalize(*(v + UP_RIGHT) - *v);
b = normalize(*(v + RIGHT) - *v);
sum += normalize(cross(a , b));
divisor++;
}
void Plane::RecomputeNormals()
{
vec3 sum;
vec3 a;
vec3 b;
int i = 0;
int w = divisionsX + 1;
vector<vec3> & v = data.vertices;
vector<vec3> & n = data.normals;
vector<vec3> & p = data.normal_visualization_coordinates;
// int i indexes the consecutive vertex indices. The row and col values are useful
// to maintain a logical picture of the vertex index i represents. The approach here
// is to examine every triangle in which vertex i participates. Average together all
// the cross products (after normalizing both the input and the output) taken in
// clockwise order. We later determined that it should have been counter clockwise
// order. Rather than changing all the calls to cross(), I negated the assignments
// to data.normals (abbreviated by n).
for (int row = 0; row <= this->divisionsY; row++)
{
for (int col = 0; col < w; col++, i++)
{
sum = vec3(0.0f);
float divisor = 0.0f;
SE(&v[i] , row , col , sum , divisor);
SSW(&v[i] , row , col , sum , divisor);
WSW(&v[i] , row , col , sum , divisor);
NW(&v[i] , row , col , sum , divisor);
NNE(&v[i] , row , col , sum , divisor);
ENE(&v[i] , row , col , sum , divisor);
assert(divisor > 0.0f);
n[i] = -sum / divisor;
// As we leave processing of the current vertex, we take the opportunity to
// update the visualization vectors. The beginning of each visualization vector
// is the position of the vertex itself. The other end of each visualization
// vector is the newly calculated normal modulated by a constant divisor added
// to the position of the vertex.
p[i * 2] = v[i];
p[i * 2 + 1] = v[i] + n[i] / NORMAL_LENGTH_DIVISOR;
}
}
}
void Cube::RecomputeNormals()
{
const int NCORNERS = 4;
vector<vec3> & v = data.vertices;
vector<vec3> & n = data.normals;
vector<vec3> & p = data.normal_visualization_coordinates;
vec3 a , b;
int j = 0;
// A cube is six faces of four vertices each. face_counter iterates
// face indexes. As each face is comprised of four vertices, face_counter
// * 4 is the base index of the 4 vertices in the face. These are i through
// i + 3.
// At each vertex, the line segment ahead is a where as the line segment behind
// is b. cross(a,b) is actually backwards so the negation of the value is used
// as the new normal.
// Every vertex has two associated vertices in the normal visualization array.
// These are indexed by j, allowed to post increment its way up.
for (size_t face_counter = 0; face_counter < n.size() / NCORNERS; face_counter++)
{
int i = face_counter * NCORNERS;
a = normalize(v[i + 1] - v[i + 0]);
b = normalize(v[i + 3] - v[i + 0]);
n[i + 0] = -normalize(cross(a , b));
p[j++] = v[i + 0];
p[j++] = v[i + 0] + n[i + 0] / NORMAL_LENGTH_DIVISOR;
a = normalize(v[i + 2] - v[i + 1]);
b = normalize(v[i + 0] - v[i + 1]);
n[i + 1] = -normalize(cross(a , b));
p[j++] = v[i + 1];
p[j++] = v[i + 1] + n[i + 1] / NORMAL_LENGTH_DIVISOR;
a = normalize(v[i + 3] - v[i + 2]);
b = normalize(v[i + 1] - v[i + 2]);
n[i + 2] = -normalize(cross(a , b));
p[j++] = v[i + 2];
p[j++] = v[i + 2] + n[i + 2] / NORMAL_LENGTH_DIVISOR;
a = normalize(v[i + 0] - v[i + 3]);
b = normalize(v[i + 2] - v[i + 3]);
n[i + 3] = -normalize(cross(a , b));
p[j++] = v[i + 3];
p[j++] = v[i + 3] + n[i + 3] / NORMAL_LENGTH_DIVISOR;
}
}
void Cube::Draw(bool draw_normals)
{
if (this->data.vertices.size() == 0)
{
this->PreGLInitialize();
this->CommonGLInitialization();
}
if (draw_normals && this->data.normal_visualization_coordinates.size() > 0)
{
glBindVertexArray(this->normal_array_handle);
glDrawArrays(GL_LINES, 0, this->data.normal_visualization_coordinates.size());
}
else
{
glBindVertexArray(this->vertex_array_handle);
glDrawElements(GL_QUADS, this->data.indices.size(), GL_UNSIGNED_INT, nullptr);
}
glBindVertexArray(0);
this->GLReturnedError("Cube::Draw() - exiting");
}