-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStructAndVecOp.h
508 lines (429 loc) · 13.8 KB
/
StructAndVecOp.h
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#include <vector>
#include <math.h>
#include <algorithm>
#include <fstream>
#include <strstream>
using namespace std;
struct Color {
int r;
int g;
int b;
int a;
};
const Color COLOR_BLUE = { 0,0,255,255 };
const Color COLOR_RED = { 255,0,0,255 };
const Color COLOR_GREEN = { 0,255,0,255 };
const Color COLOR_WHITE = { 255,255,255,255 };
const Color COLOR_PURPLE = { 150,0,255,255 };
const Color COLOR_YELLOW = { 255,150,0,255 };
const Color COLOR_CELESTE = { 0,255,255,255 };
struct vec2d {
int x;
int y;
};
struct vec3d {
float x,y,z;
bool equals(vec3d a) {
return a.x == x&& a.y == y&& a.z == z;
}
};
static vec2d mousePos;
struct Triangle {
vec2d p1;
vec2d p2;
vec2d p3;
};
struct triangle3d {
vec3d p[3];
};
struct ColoredTriangle {
triangle3d tri;
Color c;
};
struct Mesh {
vector<triangle3d> tris;
Color color;
vec3d position = { 0,0,0 };
vec3d rotation = { 0,0,0 };
bool water = false;
bool LoadFromObjectFile(string sFilename)
{
ifstream f(sFilename);
if (!f.is_open())
return false;
// Local cache of verts
vector<vec3d> verts;
while (!f.eof())
{
char line[128];
f.getline(line, 128);
strstream s;
s << line;
char junk;
if (line[0] == 'v')
{
vec3d v;
s >> junk >> v.x >> v.y >> v.z;
verts.push_back(v);
}
if (line[0] == 'f')
{
int f[3];
s >> junk >> f[0] >> f[1] >> f[2];
tris.push_back({ verts[f[0] - 1], verts[f[1] - 1], verts[f[2] - 1] });
}
}
return true;
}
};
struct mat4x4 {
float m[4][4] = { 0 };
};
struct mat3x3 {
float m[3][3] = { 1 };
float det() {
return m[0][0] * m[1][1] * m[2][2] +
m[0][1] * m[1][2] * m[2][0] +
m[0][2] * m[1][0] * m[2][1] -
(m[0][2] * m[1][1] * m[2][0]
+ m[0][1] * m[1][0] * m[2][2]
+ m[0][0] * m[1][2] * m[2][1]);
}
};
int orientation(triangle3d tri)
{
vec3d p1 = tri.p[0];
vec3d p2 = tri.p[1];
vec3d p3 = tri.p[2];
int val = (p2.y - p1.y) * (p3.x - p2.x)
- (p2.x - p1.x) * (p3.y - p2.y);
if (val == 0)
return 0; // collinear
return (val > 0) ? 1 : 2; // clock or counterclock wise
}
static void printMousePosition() {
cout << "\nx : " << mousePos.x << " y : " << mousePos.y;
}
static float fgetMax(vector<float> v) {
float max = v[0];
for (auto f : v) {
if (f > max)max = f;
}
return max;
}
static float dotProdutct(vec3d a, vec3d b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
static float fgetMin(vector<float> v) {
float min = v[0];
for (auto f : v) {
if (f < min)min = f;
}
return min;
}
/*
vec3d lowestLeftPoint(triangle3d tri) {
vector<vec3d> lowest;
vector<float> yValues;
yValues.push_back(tri.p[0].y); yValues.push_back(tri.p[1].y); yValues.push_back(tri.p[2].y);
float minY = fgetMin(yValues);
for (int i = 0; i < 3; i++) {
if (tri.p[i].y == minY) lowest.push_back(tri.p[i]);
}
if (lowest.size() == 1)return lowest[0];
if (lowest[0].x < lowest[1].x) return lowest[0];
return lowest[1];
}
vec3d highestLeftPoint(triangle3d tri) {
vector<vec3d> lowest;
vector<float> yValues;
yValues.push_back(tri.p[0].y); yValues.push_back(tri.p[1].y); yValues.push_back(tri.p[2].y);
float minY = fgetMax(yValues);
for (int i = 0; i < 3; i++) {
if (tri.p[i].y == minY) lowest.push_back(tri.p[i]);
}
if (lowest.size() == 1)return lowest[0];
if (lowest[0].x < lowest[1].x) return lowest[0];
return lowest[1];
}
static int getHighestPointIndicies(triangle3d tri) {
//Seleziono il punto più basso
int lowestIndicies; //Indice del punto più basso e più a sinistra
vec3d lowestTriangle = highestLeftPoint(tri);
if (lowestTriangle.equals(tri.p[0]))return 0;
if (lowestTriangle.equals(tri.p[1]))return 1;
if (lowestTriangle.equals(tri.p[2]))return 2;
return 0;
}
static int getLowestPointIndicies(triangle3d tri) {
//Seleziono il punto più basso
int lowestIndicies; //Indice del punto più basso e più a sinistra
vec3d lowestTriangle = lowestLeftPoint(tri);
if (lowestTriangle.equals(tri.p[0]))return 0;
if (lowestTriangle.equals(tri.p[1]))return 1;
if (lowestTriangle.equals(tri.p[2]))return 2;
return 0;
}
static void adjustMeshTrisOrderNew(Mesh& mesh) {
vector<triangle3d> newTrianglePool;
for (auto tri : mesh.tris) {
int lowestPointIndicies = getLowestPointIndicies(tri); // è l'indice del punto più in basso
int nextInd = (lowestPointIndicies + 1) % 3;
int nextIndPreviewed = getHighestPointIndicies(tri);
bool eq = nextIndPreviewed == nextInd;
cout << "\n" << eq;
}
}
*/
static vec3d crossProduct(vec3d a, vec3d b) {
vec3d result = { a.y * b.z - a.z * b.y,a.z * b.x - a.x * b.z,a.x * b.y - a.y * b.x };
return result;
}
static vec3d crossProduct(vec2d v1, vec2d v2) {
vec3d a = { v1.x,v1.y,0 };
vec3d b = { v2.x,v2.y,0 };
vec3d result = { a.y * b.z - a.z * b.y,a.z * b.x - a.x * b.z,a.x * b.y - a.y * b.x };
return result;
}
static vec3d crossProduct(vec3d a, vec2d v2) {
vec3d b = { v2.x,v2.y,0 };
vec3d result = { a.y * b.z - a.z * b.y,a.z * b.x - a.x * b.z,a.x * b.y - a.y * b.x };
return result;
}
static vec3d crossProduct(vec2d v1, vec3d b) {
vec3d a = { v1.x,v1.y,0 };
vec3d result = { a.y * b.z - a.z * b.y,a.z * b.x - a.x * b.z,a.x * b.y - a.y * b.x };
return result;
}
static float edge_cross(vec2d a, vec2d b, vec2d p) { //return the z value of the cross product from 2 specified 3D vector
vec2d ab = { b.x - a.x,b.y - a.y };
vec2d ap = { p.x - a.x,p.y - a.y };
return crossProduct(ab, ap).z;
}
static bool pointIsInTriangle(triangle3d tri, vec2d p, int b0, int b1, int b2) { //Check if a point is in a triangle
vec2d v0 = { tri.p[0].x,tri.p[0].y };
vec2d v1 = { tri.p[1].x,tri.p[1].y };
vec2d v2 = { tri.p[2].x,tri.p[2].y };
float w_0 = edge_cross(v1, v2, p) + b0;
float w_1 = edge_cross(v2, v0, p) + b1;
float w_2 = edge_cross(v0, v1, p) + b2;
return w_0 >= 0 && w_1 >= 0 && w_2 >= 0;
}
static vec3d normalizeVec(vec3d normal) {
float normalLenght = sqrtf(powf(normal.x, 2) + powf(normal.y, 2) + powf(normal.z, 2));
return { normal.x /= normalLenght, normal.y /= normalLenght, normal.z /= normalLenght };
}
static void MultiplyMatVec(vec3d& in, vec3d& out, mat4x4& m) { //Moltiplica matrice e vettore
out.x = in.x * m.m[0][0] + in.y * m.m[1][0] + in.z * m.m[2][0] + m.m[3][0];
out.y = in.x * m.m[0][1] + in.y * m.m[1][1] + in.z * m.m[2][1] + m.m[3][1];
out.z = in.x * m.m[0][2] + in.y * m.m[1][2] + in.z * m.m[2][2] + m.m[3][2];
float w = in.x * m.m[0][3] + in.y * m.m[1][3] + in.z * m.m[2][3] + m.m[3][3];
if (w != 0) {
out.x /= w;
out.y /= w;
out.z /= w;
}
}
static mat4x4 matrixMoltiplication(mat4x4 a, mat4x4 b) {
mat4x4 c;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
int sum = 0;
for (int k = 0; k < 4; k++) {
sum += a.m[i][k] * b.m[k][j];
}
c.m[i][j] = sum;
}
}
return c;
}
static void multiplyMatTri(triangle3d in, triangle3d& o, mat4x4 mat) {
for (int i = 0; i < 3; i++) {
MultiplyMatVec(in.p[i], o.p[i], mat);
}
}
static triangle3d rotateTri(triangle3d& tri, mat4x4 FixedMatRotX, mat4x4 FixedMatRotY, mat4x4 FixedMatRotZ) {
triangle3d triFixedRotatedZ, triFixedRotatedX, triFixedRotatedY;
multiplyMatTri(tri, triFixedRotatedX, FixedMatRotX);
multiplyMatTri(triFixedRotatedX, triFixedRotatedZ, FixedMatRotZ);
multiplyMatTri(triFixedRotatedZ, triFixedRotatedY, FixedMatRotY);
return triFixedRotatedY;
}
static vec3d getNormalVector(triangle3d tri) {
vec3d normal, line1, line2;
line1.x = tri.p[1].x - tri.p[0].x;
line1.y = tri.p[1].y - tri.p[0].y;
line1.z = tri.p[1].z - tri.p[0].z;
line2.x = tri.p[2].x - tri.p[0].x;
line2.y = tri.p[2].y - tri.p[0].y;
line2.z = tri.p[2].z - tri.p[0].z;
normal = crossProduct(line1, line2);
return normalizeVec(normal);
}
class gameObject {
public:
string name;
vec3d position;
vec3d rotation;
Mesh mesh;
gameObject();
gameObject(string n) {
name = n;
}
bool physics;
};
static class meshGenerator {
private:
int ord = 1;
public:
vector<int> getHypotenuse(triangle3d tri) { //This function get a triangle and returns de index of the 2 points of the hypotenuse
float dist01 = sqrtf(powf((tri.p[1].x - tri.p[0].x),2)+ powf((tri.p[1].y - tri.p[0].y), 2)
+ powf((tri.p[1].z - tri.p[0].z), 2));
float dist02 = sqrtf(powf((tri.p[2].x - tri.p[0].x), 2) + powf((tri.p[2].y - tri.p[0].y), 2)
+ powf((tri.p[2].z - tri.p[0].z), 2));
float dist12 = sqrtf(powf((tri.p[2].x - tri.p[1].x), 2) + powf((tri.p[2].y - tri.p[1].y), 2)
+ powf((tri.p[2].z - tri.p[1].z), 2));
vector<int> v;
if ((dist01>dist02) && (dist01 > dist12)) {
v.push_back(0);
v.push_back(1);
v.push_back(2);
ord = 1;
return v;
}
if ((dist12 > dist02) && (dist12 > dist01)) {
v.push_back(1);
v.push_back(2);
v.push_back(0);
ord = 2;
return v;
}
if ((dist02 > dist01) && (dist02 > dist12)) {
v.push_back(0);
v.push_back(2);
v.push_back(1);
ord = 3;
return v;
}
v.push_back(0);
v.push_back(1);
v.push_back(2);
ord = 1;
return v;
}
static vec3d midPointVector3D(vec3d a,vec3d b){
vec3d mid;
mid = { (a.x + b.x),(a.y + b.y),a.z + b.z };
mid.x *= 0.5f;
mid.y *= 0.5f;
mid.z *= 0.5f;
return mid;
}
/*
Quando prendo un triangolo e creo i due nuovi triangoli, per selezionare il lato dell'ipotenusa come
dalla quale selezionare il midpoint, inverto l'ordine dei vertici del triangolo, ciò causa un problema nel momento
in cui necessito di calcolare il vettore normale, in quanto per i triangoli che hanno i vertici ordinati diversamente, la
normale risulta inversa, quindi ho bisogno di tener conto dell'ordine, ed in base a come ho selezionato i vertici, disporli diversamente.
*/
vector<triangle3d> getOrderedTriangle(triangle3d tri, vec3d mid,vector<int> hypVertices) {
triangle3d tri1;
triangle3d tri2;
switch (ord)
{
case 1:
tri1 = { tri.p[hypVertices[2]],tri.p[hypVertices[0]],mid };
tri2 = { mid,tri.p[hypVertices[1]],tri.p[hypVertices[2]] };
break;
case 2:
tri1 = { mid,tri.p[hypVertices[1]],tri.p[hypVertices[2]] };
tri2 = { tri.p[hypVertices[2]],tri.p[hypVertices[0]] ,mid };
break;
case 3:
tri1 = { mid,tri.p[hypVertices[0]],tri.p[hypVertices[2]] };
tri2 = { tri.p[hypVertices[2]],tri.p[hypVertices[1]] ,mid };
break;
default:
break;
}
return { tri1,tri2 };
}
void subsectMesh(Mesh &o) {
vector<triangle3d> newTris;
for (auto tri : o.tris) {
vector<int> hypVertices = getHypotenuse(tri);
//Divido il triangolo creado il "midPoint" nel lato dell'ipotenusa
vec3d mid = midPointVector3D(tri.p[hypVertices[0]], tri.p[hypVertices[1]]);//divide tri
vector<triangle3d> newTriangles= getOrderedTriangle(tri, mid, hypVertices);
newTris.insert(newTris.end(), newTriangles[0]);
newTris.insert(newTris.end(), newTriangles[1]);
}
o.tris = newTris;
}
static Mesh creatRect(float w,float h) {
Mesh meshRect;
float start = w * -0.5f;
float start2 = h * -0.5f;
meshRect.tris = {
// SOUTH
{ start, start2, start, start, h, start, w, h, start },
{ start, start2, start, w, h, start, w, start2,start },
// EAST
{ w, start2, start, w, h, start, w, h, w },
{ w, start2, start, w, h, w, w, start2, w },
// NORTH
{ w, start2, w, w, h, w, start, h, w },
{ w, start2, w, start, h, w, start, start2, w },
// WEST
{ start, start2, w, start, h, w, start, h, start },
{ start, start2, w, start, h, start, start, start2, start },
// TOP
{ start, h, start, start, h, w, w, h, w },
{ start, h, start, w, h, w, w, h, start },
// BOTTOM
{ w, start2, w, start, start2, w, start, start2, start },
{ w, start2, w, start, start2, start, w, start2, start },
};
meshRect.position = { 0,0,3 };
return meshRect;
}
static Mesh creatCube(float size) {
Mesh meshCube;
float start =size * -0.5f;
meshCube.tris = {
// SOUTH
{ start, start, start, start, size, start, size, size, start },
{ start, start, start, size, size, start, size, start,start },
// EAST
{ size, start, start, size, size, start, size, size, size },
{ size, start, start, size, size, size, size, start, size },
// NORTH
{ size, start, size, size, size, size, start, size, size },
{ size, start, size, start, size, size, start, start, size },
// WEST
{ start, start, size, start, size, size, start, size, start },
{ start, start, size, start, size, start, start, start, start },
// TOP
{ start, size, start, start, size, size, size, size, size },
{ start, size, start, size, size, size, size, size, start },
// BOTTOM
{ size, start, size, start, start, size, start, start, start },
{ size, start, size, start, start, start, size, start, start },
};
meshCube.position = { 0,0,3 };
return meshCube;
}
static Mesh createPyramid(float size) {
Mesh meshPyr;
meshPyr.tris = {
// SOUTH
{ 0.0f, 0.0f, 0.0f, size, 0.0f, 0.0f, size, 0.f, size },
{ 0.0f, 0.0f, 0.0f, 0.f, 0.f, size, size, 0.f, size },
{ 0.0f, 0.0f, 0.0f, 0.f, 0.f, size, size*0.5f, size, size * 0.5f },
{ 0.0f, 0.0f, 0.0f, size, 0.f, 0.f, size * 0.5f, size, size * 0.5f },
{ size, 0.f, 0.f, size, 0.f, size, size * 0.5f, size, size * 0.5f },
{ 0.0f, 0.0f, size, size, 0.f, size, size * 0.5f, size, size * 0.5f },
};
meshPyr.position = { 0,0,3 };
return meshPyr;
}
};