-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.cpp
618 lines (494 loc) · 19.4 KB
/
graphics.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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#include "graphics.h"
Mesh cube = Mesh({
// SOUTH
{ {0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f, 0.0f} },
{ {0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f} },
// EAST
{ {1.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 0.0f}, {1.0f, 1.0f, 1.0f} },
{ {1.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f, 1.0f} },
// NORTH
{ {1.0f, 0.0f, 1.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f, 1.0f} },
{ {1.0f, 0.0f, 1.0f}, {0.0f, 1.0f, 1.0f}, {0.0f, 0.0f, 1.0f} },
// WEST
{ {0.0f, 0.0f, 1.0f}, {0.0f, 1.0f, 1.0f}, {0.0f, 1.0f, 0.0f} },
{ {0.0f, 0.0f, 1.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 0.0f} },
// TOP
{ {0.0f, 1.0f, 0.0f}, {0.0f, 1.0f, 1.0f}, {1.0f, 1.0f, 1.0f} },
{ {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f, 0.0f} },
// BOTTOM
{ {1.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 0.0f} },
{ {1.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f} }
});
camera globalCamera = camera();
void drawLine(Vec2 v0, Vec2 v1, RGBColor c) {
bool rightCorner;
if (v0.x == v1.x) { // vertical
rightCorner = true;
} else {
float m = (v0.y - v1.y) / (v0.x - v1.x);
rightCorner = !(-1.0f <= m && m <= 1.0f);
}
if (rightCorner)
drawYMajorLine(v0, v1, c);
else drawXMajorLine(v0, v1, c);
}
void drawXMajorLine(Vec2 v0, Vec2 v1, RGBColor c) {
/*
* lower-left edge
* lower-right edge
* bottom corner
*/
// sort by x
if (v0.x > v1.x) std::swap(v0, v1);
// get the slope
float m = slope(v0, v1);
// move along the x axis
int xStart = (int)ceil(v0.x - 0.5f);
int xEnd = (int)ceil(v1.x - 0.5f);
for (int x = xStart; x < xEnd; ++x) {
// determine the y of the current pixel
int y = (int)floor(v0.y + m * ((float)x - v0.x + 0.5f));
Vec2 leftCorner = Vec2((float)x, (float)y + 0.5f);
Vec2 bottomCorner = Vec2((float)x + 0.5f, (float)y);
Vec2 rightCorner = Vec2((float)x + 1.0f, (float)y + 0.5f);
if (intersects(bottomCorner, v0, m)) {
putPixel(x, y, c);
} else if (intersects(v0, v1, bottomCorner, rightCorner)) {
putPixel(x, y, c);
} else if (intersects(v0, v1, bottomCorner, leftCorner)) {
putPixel(x, y, c);
}
}
}
void drawYMajorLine(Vec2 v0, Vec2 v1, RGBColor c) {
/*
* lower-left edge
* lower-right edge
* bottom corner
* right corner
*/
// sort by y
if (v0.y > v1.y) std::swap(v0, v1);
// get the inverse slope to avoid dividing by 0
float m = (v0.x - v1.x) / (v0.y - v1.y);
// move along the y axis
int yStart = (int)ceil(v0.y - 0.5f);
int yEnd = (int)ceil(v1.y - 0.5f);
for (int y = yStart; y < yEnd; ++y) {
// determine the x of the current pixel
int x = (int)floor(v0.x + m * ((float)y - v0.y));
Vec2 leftCorner = Vec2((float)x, (float)y + 0.5f);
Vec2 bottomCorner = Vec2((float)x + 0.5f, (float)y);
Vec2 rightCorner = Vec2((float)x + 1.0f, (float)y + 0.5f);
if (intersects(bottomCorner, v0, m)) {
putPixel(x, y, c);
} else if (intersects(rightCorner, v0, m)) {
putPixel(x, y, c);
} else if (intersects(v0, v1, bottomCorner, rightCorner)) {
putPixel(x, y, c);
} else if (intersects(v0, v1, bottomCorner, leftCorner)) {
putPixel(x, y, c);
}
}
}
void drawMeshTriangle(Triangle2d t, RGBColor c) {
drawLine(t.v[0], t.v[1], c);
drawLine(t.v[0], t.v[2], c);
drawLine(t.v[1], t.v[2], c);
}
//void drawTriangle(Triangle2d t, RGBColor c) {
// if (t.v[0].y == t.v[1].y) {
// drawFlatBottomTriangle(t, c);
// } else if (t.v[1].y == t.v[2].y) {
// drawFlatTopTriangle(t, c);
// } else {
// Vec2 vi = Vec2(0, t.v[1].y);
// if (t.v[0].x == t.v[2].x)
// vi.x = t.v[0].x;
// else {
// vi.x = t.v[0].x + (t.v[1].y - t.v[0].y) * (t.v[2].x - t.v[0].x) / (t.v[2].y - t.v[0].y);
// }
//
// drawFlatTopTriangle(Triangle2d(t.v[0], t.v[1], vi), c);
// drawFlatBottomTriangle(Triangle2d(t.v[1], vi, t.v[2]), c);
// }
//}
//
//void drawFlatBottomTriangle(Triangle2d t, RGBColor c) {
// // calculate the slopes of v0 v2, v1 v2
// float m0 = (t.v[0].x - t.v[2].x) / (t.v[0].y - t.v[2].y);
// float m1 = (t.v[1].x - t.v[2].x) / (t.v[1].y - t.v[2].y);
//
// int yStart = ceil(t.v[0].y - 0.5f);
// int yEnd = ceil(t.v[2].y - 0.5f);
//
// for (int y = yStart; y < yEnd; ++y) {
//
// int xStart = (int)ceil(m0 * (float(y) + 0.5f - t.v[0].y) + t.v[0].x - 0.5f);
// int xEnd = (int)ceil(m1 * (float(y) + 0.5f - t.v[1].y) + t.v[1].x - 0.5f);
//
// for (int x = xStart; x < xEnd; ++x) {
// putPixel(x, y, c);
// }
// }
//}
//
//void drawFlatTopTriangle(Triangle2d t, RGBColor c) {
//
// // calculate the slopes of v0 v1, v0 v2
// float m0 = (t.v[0].x - t.v[1].x) / (t.v[0].y - t.v[1].y);
// float m1 = (t.v[0].x - t.v[2].x) / (t.v[0].y - t.v[2].y);
//
// int yStart = ceil(t.v[0].y - 0.5f);
// int yEnd = ceil(t.v[1].y - 0.5f);
//
// for (int y = yStart; y < yEnd; ++y) {
//
// int xStart = (int)ceil(m0 * (float(y) + 0.5f - t.v[0].y) + t.v[0].x - 0.5f);
// int xEnd = (int)ceil(m1 * (float(y) + 0.5f - t.v[0].y) + t.v[0].x - 0.5f);
//
// for (int x = xStart; x < xEnd; ++x) {
// putPixel(x, y, c);
// }
// }
//}
void drawTriangle(Triangle2d t, Triangle tri, RGBColor c) {
// sort triangle vertices
if (t.v[0].y > t.v[1].y) {
std::swap(t.v[0], t.v[1]);
std::swap(tri.v[0], tri.v[1]);
}
if (t.v[0].y > t.v[2].y) {
std::swap(t.v[0], t.v[2]);
std::swap(tri.v[0], tri.v[2]);
}
if (t.v[1].y > t.v[2].y) {
std::swap(t.v[1], t.v[2]);
std::swap(tri.v[1], tri.v[2]);
}
if (t.v[0].y == t.v[1].y && t.v[0].x > t.v[1].x) {
std::swap(t.v[0], t.v[1]);
std::swap(tri.v[0], tri.v[1]);
}
if (t.v[1].y == t.v[2].y && t.v[1].x > t.v[2].x) {
std::swap(t.v[1], t.v[2]);
std::swap(tri.v[1], tri.v[2]);
}
if (t.v[0].y == t.v[1].y) {
drawFlatBottomTriangle(t, tri, c);
} else if (t.v[1].y == t.v[2].y) {
drawFlatTopTriangle(t, tri, c);
} else {
// split 2d triangle
Vec2 vi = Vec2(0, t.v[1].y);
vi.x = t.v[0].x + (t.v[1].y - t.v[0].y) * (t.v[2].x - t.v[0].x) / (t.v[2].y - t.v[0].y);
// split 3d triangle
Vec3 v0_v2_3d = tri.v[2] - tri.v[0];
Vec3 vi_3d = tri.v[0] + v0_v2_3d * (distance(t.v[0], vi) / distance(t.v[0], t.v[2]));
// sort v1 and vi
if (t.v[1].x > vi.x) {
std::swap(t.v[1], vi);
std::swap(tri.v[1], vi_3d);
}
drawFlatTopTriangle( {t.v[0], t.v[1], vi}, {tri.v[0], tri.v[1], vi_3d}, c );
drawFlatBottomTriangle( {t.v[1], vi, t.v[2]}, {tri.v[1], vi_3d, tri.v[2]}, c) ;
}
}
void drawFlatBottomTriangle(Triangle2d t, Triangle tri, RGBColor c) {
// calculate the slopes of v0 v2, v1 v2
float m0 = (t.v[0].x - t.v[2].x) / (t.v[0].y - t.v[2].y);
float m1 = (t.v[1].x - t.v[2].x) / (t.v[1].y - t.v[2].y);
int yStart = ceil(t.v[0].y - 0.5f);
int yEnd = ceil(t.v[2].y - 0.5f);
Vec3 v2_v0_3d = tri.v[0] - tri.v[2];
Vec3 v2_v1_3d = tri.v[1] - tri.v[2];
for (int y = yStart; y < yEnd; ++y) {
int xStart = (int)ceil(m0 * (float(y) + 0.5f - t.v[0].y) + t.v[0].x - 0.5f);
int xEnd = (int)ceil(m1 * (float(y) + 0.5f - t.v[1].y) + t.v[1].x - 0.5f);
Vec3 horizontal_3d_start = tri.v[2] + v2_v0_3d * (distance(t.v[2], {(float)xStart, (float)y}) / distance(t.v[2], t.v[0]));
Vec3 horizontal_3d_end = tri.v[2] + v2_v1_3d * (distance(t.v[2], {(float)xEnd, (float)y}) / distance(t.v[2], t.v[1]));
Vec3 start_end_3d = horizontal_3d_end - horizontal_3d_start;
for (int x = xStart; x < xEnd; ++x) {
Vec3 currentPixel3dEquivalent = horizontal_3d_start + start_end_3d * ((float)(x - xStart) / (float)(xEnd - xStart));
float zValue = currentPixel3dEquivalent.z;
putPixel(x, y, zValue, c);
}
}
}
void drawFlatTopTriangle(Triangle2d t, Triangle tri, RGBColor c) {
// calculate the slopes of v0 v1, v0 v2
float m0 = (t.v[0].x - t.v[1].x) / (t.v[0].y - t.v[1].y);
float m1 = (t.v[0].x - t.v[2].x) / (t.v[0].y - t.v[2].y);
int yStart = ceil(t.v[0].y - 0.5f);
int yEnd = ceil(t.v[1].y - 0.5f);
Vec3 v0_v1_3d = tri.v[1] - tri.v[0];
Vec3 v0_v2_3d = tri.v[2] - tri.v[0];
for (int y = yStart; y < yEnd; ++y) {
int xStart = (int)ceil(m0 * (float(y) + 0.5f - t.v[0].y) + t.v[0].x - 0.5f);
int xEnd = (int)ceil(m1 * (float(y) + 0.5f - t.v[0].y) + t.v[0].x - 0.5f);
Vec3 horizontal_3d_start = tri.v[0] + v0_v1_3d * (distance(t.v[0], {(float)xStart, (float)y}) / distance(t.v[0], t.v[1]));
Vec3 horizontal_3d_end = tri.v[0] + v0_v2_3d * (distance(t.v[0], {(float)xEnd, (float)y}) / distance(t.v[0], t.v[2]));
Vec3 start_end_3d = horizontal_3d_end - horizontal_3d_start;
for (int x = xStart; x < xEnd; ++x) {
Vec3 currentPixel3dEquivalent = horizontal_3d_start + start_end_3d * ((float)(x - xStart) / (float)(xEnd - xStart));
float zValue = currentPixel3dEquivalent.z;
putPixel(x, y, zValue, c);
}
}
}
Triangle toViewSpace(Triangle t, float m[4][4]) {
return {
matmul(m, t.v[0]),
matmul(m, t.v[1]),
matmul(m, t.v[2])
};
}
Vec2 project(Vec3 v) {
Vec3 aux = matmul(matProj, v, true);
return {aux.x, aux.y};
}
Triangle2d project(Triangle t) {
Vec2 v0 = project(t.v[0]);
Vec2 v1 = project(t.v[1]);
Vec2 v2 = project(t.v[2]);
return {v0, v1, v2};
}
Triangle project3d(Triangle t) {
Triangle2d projected = project(t);
return {
{ projected.v[0].x, projected.v[0].y, t.v[0].z },
{ projected.v[1].x, projected.v[1].y, t.v[1].z },
{ projected.v[2].x, projected.v[2].y, t.v[2].z }
};
}
Triangle2d scaleIntoView(Triangle2d t, int W, int H) {
t.v[0].x += 1.0f; t.v[0].y += 1.0f;
t.v[1].x += 1.0f; t.v[1].y += 1.0f;
t.v[2].x += 1.0f; t.v[2].y += 1.0f;
t.v[0].x *= 0.5f * (float)W; t.v[0].y *= 0.5f * (float)H;
t.v[1].x *= 0.5f * (float)W; t.v[1].y *= 0.5f * (float)H;
t.v[2].x *= 0.5f * (float)W; t.v[2].y *= 0.5f * (float)H;
return t;
}
//void renderTriangle(Triangle t, float matView[4][4], int width, int height) {
//
// t = toViewSpace(t, matView);
//
// Triangle right = { {1, 0, 0}, {1, 0, 1}, {1, 1, 0} };
// Triangle left = { {-1, 0, 0}, {-1, 0, 1}, {-1, 1, 0} };
// Triangle up = { {0, 1, 0}, {1, 1, 0}, {0, 1, 1} };
// Triangle down = { {0, -1, 0}, {1, -1, 0}, {0, -1, 1} };
//
// Plane cameraPlane = Plane( {0, 0, 0.1f}, {0, 0, 1} );
// Plane rightPlane = Plane( {1, 0, 0}, getNormal(right) );
// Plane upPlane = Plane( {0, 1, 0}, getNormal(up) );
// Plane leftPlane = Plane( {-1, 0, 0}, Vec3(0, 0, 0) - getNormal(left) );
// Plane downPlane = Plane( {0, -1, 0}, Vec3(0, 0, 0) - getNormal(down) );
//
// // clip against the camera plane
// std::vector<Triangle> triangles = clip(t, cameraPlane);
//
// for (auto triangle : triangles) {
//
// drawTriangle(scaleIntoView(project(triangle), width, height), triangle);
//// drawMeshTriangle(scaleIntoView(project(triangle), width, height), BLACK);
// }
//
// // TODO: clipping
//
//// for (auto triangle:triangles) {
//// Triangle projected = project3d(triangle);
////
//// // clip against the screen edges
//// std::vector <Triangle> clippedTriangles = { projected };
//// clippedTriangles = clip(clippedTriangles, rightPlane);
//// clippedTriangles = clip(clippedTriangles, leftPlane);
//// clippedTriangles = clip(clippedTriangles, upPlane);
//// clippedTriangles = clip(clippedTriangles, downPlane);
////
//// for (auto clippedTriangle: clippedTriangles) {
//// drawTriangle(scaleIntoView(clippedTriangle.toTriangle2d(), width, height), clippedTriangle, RGBColor::randomColor());
//// drawMeshTriangle(scaleIntoView(clippedTriangle.toTriangle2d(), width, height), BLACK);
//// }
//// }
//}
void renderTriangle(Triangle t, float matView[4][4], int width, int height) {
t = toViewSpace(t, matView);
Triangle right = rotateAboutY( {{0, 0, 0}, {1, 0, 0}, {0, 1, 0}}, -degToRad(fFov) / 2 );
Triangle left = rotateAboutY( {{0, 0, 0}, {1, 0, 0}, {0, 1, 0}}, degToRad(fFov) / 2);
Triangle up = rotateAboutX( {{0, 0, 0}, {1, 0, 0}, {0, 1, 0}}, degToRad(fFov / fAspectRatio) / 2 );
Triangle down = rotateAboutX( {{0, 0, 0}, {1, 0, 0}, {0, 1, 0}}, -degToRad(fFov / fAspectRatio) / 2 );
Plane cameraPlane = Plane( {0, 0, fNear}, {0, 0, 1} );
Plane rightPlane = Plane( {0, 0, 0}, getNormal(right) );
Plane leftPlane = Plane( {0, 0, 0}, getNormal(left) );
Plane upPlane = Plane( {0, 0, 0}, getNormal(up) );
Plane downPlane = Plane( {0, 0, 0}, getNormal(down) );
// clip against the camera plane
std::vector<Triangle> triangles = {t};
triangles = clip(triangles, cameraPlane);
triangles = clip(triangles, rightPlane);
triangles = clip(triangles, leftPlane);
triangles = clip(triangles, upPlane);
triangles = clip(triangles, downPlane);
for (auto triangle : triangles) {
drawTriangle(scaleIntoView(project(triangle), width, height), triangle);
// drawMeshTriangle(scaleIntoView(project(triangle), width, height), BLACK);
}
}
std::vector<Triangle> clip(Triangle t, Plane p) {
std::vector<Triangle> v;
p.normal.normalize();
float d = -p.point.dotProduct(p.normal);
float sign0 = t.v[0].dotProduct(p.normal) + d;
float sign1 = t.v[1].dotProduct(p.normal) + d;
float sign2 = t.v[2].dotProduct(p.normal) + d;
if (sign0 < 0 && sign1 < 0 && sign2 < 0) {
// all points are on invisible side of the plane
return {}; // delete
} else if (sign0 > 0 && sign1 > 0 && sign2 > 0) {
// all points are on the visible side of the plane
v.emplace_back(t);
} else if (sign0 * sign1 * sign2 > 0) {
// one point is on the visible part of the plane
if (sign1 > 0) std::swap(t.v[0], t.v[1]);
else if (sign2 > 0) std::swap(t.v[0], t.v[2]);
v.emplace_back( Triangle({0, 0, 0}, {0, 0, 0}, {0, 0, 0}) );
v.emplace_back( Triangle(t.v[0], intersection(t.v[0], t.v[1], p), intersection(t.v[0], t.v[2], p)) );
} else if (sign0 * sign1 * sign2 < 0) {
if (sign1 < 0) std::swap(t.v[0], t.v[1]);
else if (sign2 < 0) std::swap(t.v[0], t.v[2]);
Vec3 i0 = intersection(t.v[0], t.v[1], p);
Vec3 i1 = intersection(t.v[0], t.v[2], p);
Triangle t1(t.v[1], i0, t.v[2]);
Triangle t2(t.v[2], i0, i1);
v.emplace_back( Triangle({1, 1, 1}, {1, 1, 1}, {1, 1, 1}) );
v.emplace_back(t1);
v.emplace_back(t2);
}
return v;
}
std::vector<Triangle> clip(const std::vector<Triangle> &tris, Plane p) {
std::vector<Triangle> v;
p.normal.normalize();
float d = -p.point.dotProduct(p.normal);
for (auto t: tris) {
float sign0 = t.v[0].dotProduct(p.normal) + d;
float sign1 = t.v[1].dotProduct(p.normal) + d;
float sign2 = t.v[2].dotProduct(p.normal) + d;
if (sign0 < 0 && sign1 < 0 && sign2 < 0) {
// all points are on invisible side of the plane
return {}; // delete
} else if (sign0 > 0 && sign1 > 0 && sign2 > 0) {
// all points are on the visible side of the plane
v.emplace_back(t);
} else if (sign0 * sign1 * sign2 > 0) {
// one point is on the visible part of the plane
if (sign1 > 0) std::swap(t.v[0], t.v[1]);
else if (sign2 > 0) std::swap(t.v[0], t.v[2]);
v.emplace_back(Triangle({0, 0, 0}, {0, 0, 0}, {0, 0, 0}));
v.emplace_back(Triangle(t.v[0], intersection(t.v[0], t.v[1], p), intersection(t.v[0], t.v[2], p)));
} else if (sign0 * sign1 * sign2 < 0) {
if (sign1 < 0) std::swap(t.v[0], t.v[1]);
else if (sign2 < 0) std::swap(t.v[0], t.v[2]);
Vec3 i0 = intersection(t.v[0], t.v[1], p);
Vec3 i1 = intersection(t.v[0], t.v[2], p);
Triangle t1(t.v[1], i0, t.v[2]);
Triangle t2(t.v[2], i0, i1);
v.emplace_back(Triangle({1, 1, 1}, {1, 1, 1}, {1, 1, 1}));
v.emplace_back(t1);
v.emplace_back(t2);
}
}
return v;
}
Triangle translate(Triangle t, Vec3 amt) {
return {t.v[0] + amt, t.v[1] + amt, t.v[2] + amt};
}
Triangle rotate(Triangle t, float a0, float a1, float a2) {
return {rotate(t.v[0], a0, a1, a2), rotate(t.v[1], a0, a1, a2), rotate(t.v[2], a0, a1, a2)};
}
// probably badly implemented
Vec3 rotate(Vec3 v, float a0, float a1, float a2) {
float c0 = cos(a0);
float c1 = cos(a1);
float c2 = cos(a2);
float s0 = sin(a0);
float s1 = sin(a1);
float s2 = sin(a2);
float rot[3][3] = {
{c1*c2, -s2*c0 + s0*s1*c2, s0*s2 + c0*s1*c2},
{c1*s2, c0*c2 + s0*s1*s2, -s0*c2 + c0*s1*s2},
{-s1, s0*c1, c0*c1}
};
return matmul(rot, v);
//
// float rot[3][3] = {
// {c0*c1, -s0*c2 + c0*s1*s2, s0*s2 + c0*s1*c2},
// {s0*c1, c0*c2 + s0*s1*s2, -c0*s2 + s0*s1*c2},
// {-s1, c1*s2, c1*c2}
// };
//
// return matmul(rot, v);
// float rotX[3][3] = { {1, 0, 0},
// {0, cosf(a0), -sinf(a0)},
// {0, sinf(a0), cosf(a0)}};
//
// float rotY[3][3] = { {cosf(a1), 0, sinf(a1) },
// {0, 1, 0},
// {-sinf(a1), 0, cosf(a1)}};
//
// float rotZ[3][3] = { {cosf(a2), -sinf(a2), 0},
// {sinf(a2), cosf(a2), 0},
// {0, 0, 0}};
// float rotXY[3][3] = { 0 };
// float rot[3][3] = { 0 };
//
// matmul(rotXY, rotZ, rotY);
// matmul(rot, rotXY, rotX);
// return matmul(rot, v);
// return matmul(rotY, matmul(rotX, matmul(rotZ, v)));
}
Triangle rotateAboutX(Triangle t, float a) {
return {
rotateAboutX(t.v[0], a),
rotateAboutX(t.v[1], a),
rotateAboutX(t.v[2], a)
};
}
Triangle rotateAboutY(Triangle t, float a) {
return {
rotateAboutY(t.v[0], a),
rotateAboutY(t.v[1], a),
rotateAboutY(t.v[2], a)
};
}
Triangle rotateAboutZ(Triangle t, float a) {
return {
rotateAboutZ(t.v[0], a),
rotateAboutZ(t.v[1], a),
rotateAboutZ(t.v[2], a)
};
}
Vec3 rotateAboutX(Vec3 v, float a) {
float rotX[3][3] = { {1, 0, 0},
{0, cosf(a), -sinf(a)},
{0, sinf(a), cosf(a)}};
return matmul(rotX, v);
}
Vec3 rotateAboutY(Vec3 v, float a) {
float rotY[3][3] = { {cosf(a), 0, sinf(a) },
{0, 1, 0},
{-sinf(a), 0, cosf(a)}};
return matmul(rotY, v);
}
Vec3 rotateAboutZ(Vec3 v, float a) {
float rotZ[3][3] = { {cosf(a), -sinf(a), 0},
{sinf(a), cosf(a), 0},
{0, 0, 1}};
return matmul(rotZ, v);
}
bool isVisible(Triangle t, Vec3 camera) {
Vec3 normal = getNormal(t);
float flag = normal.x * (t.v[0].x - camera.x) + normal.y * (t.v[0].y - camera.y) + normal.z * (t.v[0].z - camera.z);
return flag < 0;
}