-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.cpp
More file actions
599 lines (551 loc) · 20.7 KB
/
Copy pathrender.cpp
File metadata and controls
599 lines (551 loc) · 20.7 KB
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
#include "render.h"
const int WINDOW_WIDTH = 1920;
const int WINDOW_HEIGHT = 1080;
int gFrameBuffer[WINDOW_WIDTH * WINDOW_HEIGHT];
double zBuffer[WINDOW_WIDTH * WINDOW_HEIGHT];
const float FOV = 90.0f * 3.14159f / 180.0f;//90 degree fov
const double F = 1.0f / tan(FOV / 2.0f);
const float ASPECT_RATIO = (float)WINDOW_WIDTH / (float)WINDOW_HEIGHT;
const int NEAR = 10;
const int FAR = 2000;
const float projectionMatrix[4][4] = {
{F / ASPECT_RATIO, 0, 0, 0},
{0, F, 0, 0},
{0, 0, -((FAR + NEAR) / (NEAR - FAR)), -((2 * FAR * NEAR) / (NEAR - FAR))},
{0, 0, -1, 0}
};
const SDL_PixelFormatDetails* PIXELFORMAT = SDL_GetPixelFormatDetails(SDL_PIXELFORMAT_RGBA8888);
void swapVec2d_t(VecP_t* a, VecP_t* b) {//in go pointer to passed thingy so it doesnt need wierd returning, swap(&a, &b); )
VecP_t temp = *a;
*a = *b;
*b = temp;
}
int setPixel(int X, int Y, Uint32 Colour) {//sets a pixel in the frame buffer to a colour, w/ some limits to avoid index errors
if (Y == 0) Y++; if (X == 0) X++;
if (X < 0 || X > WINDOW_WIDTH - 1)
return -1;
if (Y < 0 || Y > WINDOW_HEIGHT - 1)
return -1;
gFrameBuffer[X + ((Y - 1) * WINDOW_WIDTH)] = Colour;
return 1;
}
int setZPixel(int x, int y, double z, Uint32 Colour) {//z will be inverted as 1/z
if (y == 0) y++; if (x == 0) x++;
int pos = x + ((y - 1) * WINDOW_WIDTH);
if (z > (zBuffer[pos]) + DBL_EPSILON) {
setPixel(x, y, Colour);
zBuffer[pos] = z;
return 1;
}
return 0;
}
void interpolatei(std::vector<int>& values, int i0, float d0, int i1, float d1) {//pass in a vector interpolates whole integers
if (i0 == i1) {
values.clear();
values.push_back(d0);
}
size_t size = abs(i1 - i0) + 1;
values.resize(size);
float a = (d1 - d0) / (i1 - i0);
float d = d0;
int i, j;
for (i = i0, j = 0; i <= (int)i1; i++, j++) {
values[j] = d;
d += a;
}
}
void interpolatef(std::vector<float>& values, int i0, float d0, int i1, float d1) {//pass in a vector interpolates floats
if (i0 == i1) {
values.clear();
values.push_back(d0);
}
size_t size = abs(i1 - i0) + 1;
values.resize(size);
float a = (d1 - d0) / (i1 - i0);
float d = d0;
int i, j;
for (i = i0, j = 0; i <= (int)i1; i++, j++) {
values[j] = d;
d += a;
}
}
void interpolated(std::vector<double>& values, int i0, double d0, int i1, double d1) {//pass in a vector interpolates doubles
if (i0 == i1) {
values.clear();
values.push_back(d0);
}
size_t size = abs(i1 - i0) + 1;
values.resize(size);
double a = (d1 - d0) / (i1 - i0);
double d = d0;
int i, j;
for (i = i0, j = 0; i <= (int)i1; i++, j++) {
values[j] = d;
d += a;
}
}
Uint32 multiplyColour(Uint32 colour1, float n) {//multiplies two colours based off RGBA format
if (n == 1) return colour1;
Uint8 r1, g1, b1, a1;
SDL_GetRGBA(colour1, PIXELFORMAT, NULL, &r1, &g1, &b1, &a1);
Uint8 r = (r1 * n);
Uint8 g = (g1 * n);
Uint8 b = (b1 * n);
Uint8 a = (a1 * n);
return(SDL_MapRGBA(PIXELFORMAT, NULL, r, g, b, a));
}
Vec4d_t matrixMult4x4Vec4d_t(const float multMatrix[4][4], Vec4d_t v) {//matrix multiplication of a 4x4 matrix and a 1x4 coordinate
int i, j;
Vec4d_t tv = { 0,0,0,0 };
for (i = 0; i < 4; i++) {
float sum = 0;
for (j = 0; j < 4; j++) {
sum += *(float*)((unsigned char*)&v + (j * 4)) * multMatrix[i][j];
}
*(float*)((unsigned char*)&tv + (i * 4)) = sum;
}
return tv;
}
float signedDistance(Plane_t p, Vec4d_t v) {//signed distance between a point and a plane in 4d space
float distance = (v.x * p.normal.x) + (v.y * p.normal.y) + (v.z * p.normal.z) + (v.w * p.normal.w);
return distance;
}
Vec4d_t linePlaneIntersection(Vec4d_t a, Vec4d_t b, Plane_t p) {//intersection of a plane and two points in 4d space
float t, sum1, sum2;
Vec4d_t i;
sum1 = (a.x * p.normal.x) + (a.y * p.normal.y) + (a.z * p.normal.z) + (a.w * p.normal.w);
sum2 = (b.x * p.normal.x) + (b.y * p.normal.y) + (b.z * p.normal.z) + (b.w * p.normal.w);
if (sum2 == 0) {//the line is parallel with the plane
return a;
}
t = sum1 / (sum1 - sum2);
i.x = a.x + (b.x - a.x) * t;
i.y = a.y + (b.y - a.y) * t;
i.z = a.z + (b.z - a.z) * t;
i.w = a.w + (b.w - a.w) * t;
return i;
}
void projectMeshMatrix(std::vector<Tri4d_t>& Mesh) {//projects the matrix according to the projection matrix...
int i;
for (i = 0; i < Mesh.size(); i++) {
Mesh[i].v0 = matrixMult4x4Vec4d_t(projectionMatrix, Mesh[i].v0);
Mesh[i].v1 = matrixMult4x4Vec4d_t(projectionMatrix, Mesh[i].v1);
Mesh[i].v2 = matrixMult4x4Vec4d_t(projectionMatrix, Mesh[i].v2);
}
}
std::vector<Tri3d_t> perspectiveDivide(std::vector<Tri4d_t> t) {//scales the coordinates in accordance to w
std::vector<Tri3d_t> output;
output.resize(t.size());
int i;
for (i = 0; i < t.size(); i++) {
output[i].v0.x = t[i].v0.x / t[i].v0.w;
output[i].v0.y = t[i].v0.y / t[i].v0.w;
output[i].v0.z = t[i].v0.z / t[i].v0.w;
output[i].v1.x = t[i].v1.x / t[i].v1.w;
output[i].v1.y = t[i].v1.y / t[i].v1.w;
output[i].v1.z = t[i].v1.z / t[i].v1.w;
output[i].v2.x = t[i].v2.x / t[i].v2.w;
output[i].v2.y = t[i].v2.y / t[i].v2.w;
output[i].v2.z = t[i].v2.z / t[i].v2.w;
}
return output;
}
std::vector<Tri2d_t> viewportTransformation(std::vector<Tri3d_t> t) {//scales coordinates based on viewport size
std::vector<Tri2d_t> output;
output.resize(t.size());
int i;
double sum = 0;
for (i = 0; i < t.size(); i++) {
output[i].v0.x = (t[i].v0.x + 1) / 2 * WINDOW_WIDTH;
output[i].v0.y = (1 - t[i].v0.y) / 2 * WINDOW_HEIGHT;
output[i].v1.x = (t[i].v1.x + 1) / 2 * WINDOW_WIDTH;
output[i].v1.y = (1 - t[i].v1.y) / 2 * WINDOW_HEIGHT;
output[i].v2.x = (t[i].v2.x + 1) / 2 * WINDOW_WIDTH;
output[i].v2.y = (1 - t[i].v2.y) / 2 * WINDOW_HEIGHT;
output[i].v0.z = (1 / -t[i].v0.z);
output[i].v1.z = (1 / -t[i].v1.z);
output[i].v2.z = (1 / -t[i].v2.z);
sum += output[i].v0.z;
sum += output[i].v1.z;
sum += output[i].v2.z;
}
sum = sum / (t.size() * 3);
return output;
}
void translateMesh(std::vector<Tri4d_t>& Mesh, Vec4d_t Transform) {//adds two matricies, for translation
int i;
for (i = 0; i < Mesh.size(); i++) {
Mesh[i].v0.x += Transform.x;
Mesh[i].v0.y += Transform.y;
Mesh[i].v0.z += Transform.z;
Mesh[i].v1.x += Transform.x;
Mesh[i].v1.y += Transform.y;
Mesh[i].v1.z += Transform.z;
Mesh[i].v2.x += Transform.x;
Mesh[i].v2.y += Transform.y;
Mesh[i].v2.z += Transform.z;
}
}
void rotateMesh(std::vector<Tri4d_t>& Mesh, Vec4d_t Rotation) {//multiplies two matricies for rotation
int i, j, k, l; //w doesnt change with rotation, no need to alter mult matrix since we dodge the 4th coordinate (w) with horrible pointer offset code
Tri4d_t Rtri;
float sinRZ = sin(Rotation.x);//alpha
float cosRZ = cos(Rotation.x);
float sinRY = sin(Rotation.y);//beta
float cosRY = cos(Rotation.y);
float sinRX = sin(Rotation.z);//gamma
float cosRX = cos(Rotation.z);
float multMatrix[3][3] = {
{cosRZ * cosRY, cosRZ * sinRY * sinRX - sinRZ * cosRX, cosRZ * sinRY * cosRX + sinRZ * sinRX},
{sinRZ * cosRY, sinRZ * sinRY * sinRX + cosRZ * cosRX, sinRZ * sinRY * cosRX - cosRZ * sinRX},
{-sinRY, cosRY * sinRX, cosRY * cosRX}
};
for (i = 0; i < Mesh.size(); i++) {
std::memcpy(&Rtri, &Mesh[i], sizeof(Tri4d_t));
for (l = 0; l < 3; l++) {//iterate through triangle vertice pairs
for (j = 0; j < 3; j++) {//dot product loop
float sum = 0;
for (k = 0; k < 3; k++) {//very safe memory accessing inbound
//so its accessing the pointer to the start of Mesh[] and adjusted the pointer offset by + ((...)) and reading the resulting address as a pointer to a float :)
sum += *(float*)((unsigned char*)&Mesh[i] + ((l * 16) + (k * 4))) * multMatrix[j][k];
}//some more too
//same thing here, fun fact, l is the y value of the matrix and k is the x of the matrix, i think ||| ITS NOT
*(float*)((unsigned char*)&Rtri + ((l * 16) + (j * 4))) = sum;
}
}
std::memcpy(&Mesh[i], &Rtri, sizeof(Tri4d_t));
}
}
void worldSpaceMesh(std::vector<Tri4d_t>& Mesh, MeshInstance_t Instance) {//move a mesh based on its own position and rotation as an instance of a parent mesh
rotateMesh(Mesh, Instance.rot);
translateMesh(Mesh, Instance.pos);
}
void cameraSpaceMesh(std::vector<Tri4d_t>& Mesh, Camera_t camera) {//move the inverted position and rotation to the world scene
Vec4d_t tPos = { -camera.position.x, -camera.position.y, -camera.position.z, -camera.position.w };
Vec4d_t tRot = { -camera.rotation.x, -camera.rotation.y, -camera.rotation.z, -camera.rotation.w };
translateMesh(Mesh, tPos);
rotateMesh(Mesh, tRot);
}
int clipOrCull(MeshInstance_t mesh, Camera_t camera) {//early rejection of meshes based on set bounding box coordinates and clip plane inequalities
/*if fully in = 8
if fully out = 0
if parial = < 8 */
int i;
std::vector<Tri4d_t> tempBounds = mesh.parentMesh.boundBox;
worldSpaceMesh(tempBounds, mesh);
cameraSpaceMesh(tempBounds, camera);
projectMeshMatrix(tempBounds);
//we in clip space now
int result = 0;
for (i = 0; i < tempBounds.size(); i++) {
if (tempBounds[i].v0.w <= 0) result;//axe this soon
if ((tempBounds[i].v0.x >= -tempBounds[i].v0.w) && (tempBounds[i].v0.x <= tempBounds[i].v0.w) &&
(tempBounds[i].v0.y >= -tempBounds[i].v0.w) && (tempBounds[i].v0.y <= tempBounds[i].v0.w) &&
(tempBounds[i].v0.z >= -tempBounds[i].v0.w) && (tempBounds[i].v0.z <= tempBounds[i].v0.w)) {
result += 1;//above test for inside, inside on all planes -> point is inside
}
}
return result;
}
void clipTriangle(std::vector<Tri4d_t>& output, Tri4d_t input, Plane_t plane) {//clip and redraw triangles along a clipping plane
Vec4d_t A, B, C, TA, TB, TC;
float d0, d1, d2;
d0 = signedDistance(plane, input.v0);
d1 = signedDistance(plane, input.v1);
d2 = signedDistance(plane, input.v2);
if (d0 >= 0 && d1 >= 0 && d2 >= 0) {//no clipping needed, do nothing
output.push_back(input);
}
else if (d0 < 0 && d1 < 0 && d2 < 0) {//all out of the selection
return;
}
else if (((d0 > 0) + (d1 > 0) + (d2 > 0)) == 1) {//only one positive
//A will be the positive vertex
if (d0 > d1) {
if (d0 > d2) {
A = input.v0;
B = input.v1;
C = input.v2;
}
else {
A = input.v2;
B = input.v1;
C = input.v0;
}
}
else {
if (d1 > d2) {
A = input.v1;
B = input.v2;
C = input.v0;
}
else {
A = input.v2;
B = input.v1;
C = input.v0;
}
}
TB = linePlaneIntersection(A, B, plane);
TC = linePlaneIntersection(A, C, plane);
output.push_back(Tri4d_t{ A, TB, TC });
}
else if (((d0 < 0) + (d1 < 0) + (d2 < 0)) == 1) {//only one negative
//c will be the negative one
if (d0 < d1) {
if (d0 < d2) {
C = input.v0;
B = input.v1;
A = input.v2;
}
else {
C = input.v2;
B = input.v1;
A = input.v0;
}
}
else {
if (d1 < d2) {
C = input.v1;
B = input.v2;
A = input.v0;
}
else {
C = input.v2;
B = input.v1;
A = input.v0;
}
}
TA = linePlaneIntersection(A, C, plane);
TB = linePlaneIntersection(B, C, plane);
output.push_back(Tri4d_t{ A, B, TA });
output.push_back(Tri4d_t{ TA, B, TB });
}
}
std::vector<Tri4d_t> clipMesh(std::vector<Tri4d_t>& mesh, Camera_t camera) {//handle passing a mesh through the clipper along all its planes without any data hazards
int i, j;
Vec4d_t A, B, C, TA, TB, TC;
std::vector<Tri4d_t> cInput = mesh;
std::vector<Tri4d_t> cOutput;
for (j = 0; j < camera.clippingPlanes.size(); j++) {
cOutput.clear();
for (i = 0; i < cInput.size(); i++) {
clipTriangle(cOutput, cInput[i], camera.clippingPlanes[j]);
}
cInput = cOutput;
}
return cInput;
}
Vec4d_t crossProduct(Vec4d_t a, Vec4d_t b) {
Vec4d_t c = {
{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 c;
}
int isFrontFacing(Tri4d_t t) {
float vP;
Vec4d_t vec1, vec2, vC;
vec1 = { //b - a
{t.v1.x - t.v0.x},{t.v1.y - t.v0.y},{t.v1.z - t.v0.z}
};
vec2 = { //c - a
{t.v2.x - t.v0.x},{t.v2.y - t.v0.y},{t.v2.z - t.v0.z}
};
vC = crossProduct(vec1, vec2);
//dot product of camera and vecCross but we assume camera is at the origin
vP = (-t.v0.x * vC.x) + (-t.v0.y * vC.y) + (-t.v0.z * vC.z);
if (vP <= 0) return 0;//back facing
if (vP > 0) return 1;//front facing
}
std::vector<Tri4d_t> backFaceCullMesh(std::vector<Tri4d_t> mesh) {
int i, result;
std::vector<Tri4d_t> output;
for (i = 0; i < mesh.size(); i++) {
if (isFrontFacing(mesh[i])) {
output.push_back(mesh[i]);
}
}
return output;
}
std::vector<Tri4d_t> rotTranClipCullMesh(struct MeshInstance_t mesh, struct Camera_t camera) {//handles the entire clipping pipeline with a mesh and a camera
int i, cResult;
std::vector<Tri4d_t> clipped, bFCulled;
cResult = clipOrCull(mesh, camera);
if (cResult == 0) {//fully out
std::vector<Tri4d_t> empty;
return empty;
}
std::vector<Tri4d_t> processing = mesh.parentMesh.Triangles;
worldSpaceMesh(processing, mesh);
cameraSpaceMesh(processing, camera);
bFCulled = backFaceCullMesh(processing);
projectMeshMatrix(bFCulled);//moved into clip space
if (cResult < 8) {//partial
clipped = clipMesh(bFCulled, camera);
}
else {//fully in
clipped = bFCulled;
}
return clipped;
}
void drawLine(float x0, float y0, float x1, float y1) {//bresenham line drawing between two points
float x, y, dx, dy, step;
int i;
dx = (x1 - x0);
dy = (y1 - y0);
if (fabs(dx) >= fabs(dy)) {
step = fabs(dx);
}
else {
step = fabs(dy);
}
dx = dx / step;
dy = dy / step;
x = x0;
y = y0;
i = 0;
int xmod, ymod;
while (i <= step) {
int xr = round(x);
int yr = round(y);
setPixel(xr, yr, 0xffffffff);
x = x + dx;
y = y + dy;
i++;
}
}
void drawWireTri(Tri2d_t Tri) {//handles drawing three lines between verticies of a triangle
drawLine(Tri.v0.x, Tri.v0.y, Tri.v1.x, Tri.v1.y);
drawLine(Tri.v1.x, Tri.v1.y, Tri.v2.x, Tri.v2.y);
drawLine(Tri.v2.x, Tri.v2.y, Tri.v0.x, Tri.v0.y);
}
void drawFullTri(Tri2d_t Tri, Uint32 Colour) {//draws a filled in triangle of a set colour, no zbuffering im lazy
//swap so we get a nice y ordered tringle
if (Tri.v1.y < Tri.v0.y) { swapVec2d_t(&Tri.v1, &Tri.v0); }
if (Tri.v2.y < Tri.v0.y) { swapVec2d_t(&Tri.v2, &Tri.v0); }
if (Tri.v2.y < Tri.v1.y) { swapVec2d_t(&Tri.v2, &Tri.v1); }
std::vector<int> x01, x12, x02, x012, x_left, x_right;
//generates three arrays of every x value that lies on the triangle edges
interpolatei(x01, Tri.v0.y, Tri.v0.x, Tri.v1.y, Tri.v1.x);
interpolatei(x12, Tri.v1.y, Tri.v1.x, Tri.v2.y, Tri.v2.x);
interpolatei(x02, Tri.v0.y, Tri.v0.x, Tri.v2.y, Tri.v2.x);
x01.pop_back();
x012.reserve(x01.size() + x12.size());
x012.insert(x012.end(), x01.begin(), x01.end());
x012.insert(x012.end(), x12.begin(), x12.end());
//find which is to the left or right
int m = floor(x02.size() / 2);
if (x02[m] < x012[m]) {
x_left = x02;
x_right = x012;
}
else {
x_left = x012;
x_right = x02;
}
//actual drawing
int y, x, xmod, ymod;
for (y = Tri.v0.y; y < Tri.v2.y; y++) {
for (x = x_left[y - Tri.v0.y]; x < x_right[y - Tri.v0.y]; x++) {
setPixel(x, y, Colour);
}
}
x01.clear();
x02.clear();
x12.clear();
x012.clear();
x_left.clear();
x_right.clear();
}
void drawShadedTri(Tri2d_t Tri, int Colour) {//draws a triangle with interpolated colour values corresponding to vx.h
//swap so we get a nice y ordered tringle
if (Tri.v1.y < Tri.v0.y) { swapVec2d_t(&Tri.v1, &Tri.v0); }
if (Tri.v2.y < Tri.v0.y) { swapVec2d_t(&Tri.v2, &Tri.v0); }
if (Tri.v2.y < Tri.v1.y) { swapVec2d_t(&Tri.v2, &Tri.v1); }
std::vector<int> x01, x12, x02, x012, x_left, x_right;
std::vector<float> h01, h12, h02, h012, h_segment, h_right, h_left;
std::vector<double> z01, z12, z02, z012, z_segment, z_right, z_left;
//generates three arrays of every x value that lies on the triangle edges
interpolatei(x01, Tri.v0.y, Tri.v0.x, Tri.v1.y, Tri.v1.x);
interpolatef(h01, Tri.v0.y, Tri.v0.h, Tri.v1.y, Tri.v1.h);
interpolated(z01, Tri.v0.y, Tri.v0.z, Tri.v1.y, Tri.v1.z);
interpolatei(x12, Tri.v1.y, Tri.v1.x, Tri.v2.y, Tri.v2.x);
interpolatef(h12, Tri.v1.y, Tri.v1.h, Tri.v2.y, Tri.v2.h);
interpolated(z12, Tri.v1.y, Tri.v1.z, Tri.v2.y, Tri.v2.z);
interpolatei(x02, Tri.v0.y, Tri.v0.x, Tri.v2.y, Tri.v2.x);
interpolatef(h02, Tri.v0.y, Tri.v0.h, Tri.v2.y, Tri.v2.h);
interpolated(z02, Tri.v0.y, Tri.v0.z, Tri.v2.y, Tri.v2.z);
if (x01.size() != 0) { x01.pop_back(); }
if (h01.size() != 0) { h01.pop_back(); }
if (z01.size() != 0) { z01.pop_back(); }
x012.reserve(x01.size() + x12.size());
x012.insert(x012.end(), x01.begin(), x01.end());
x012.insert(x012.end(), x12.begin(), x12.end());
h012.reserve(h01.size() + h12.size());
h012.insert(h012.end(), h01.begin(), h01.end());
h012.insert(h012.end(), h12.begin(), h12.end());
z012.reserve(z01.size() + z12.size());
z012.insert(z012.end(), z01.begin(), z01.end());
z012.insert(z012.end(), z12.begin(), z12.end());
//find which is to the left or right
int m = floor(x02.size() / 2);
if (x02[m] < x012[m]) {
x_left = x02;
h_left = h02;
z_left = z02;
x_right = x012;
h_right = h012;
z_right = z012;
}
else {
x_left = x012;
h_left = h012;
z_left = z012;
x_right = x02;
h_right = h02;
z_right = z02;
}
//actual drawing
int y, x, xmod, ymod, x_l, x_r, shaded_colour;
for (y = Tri.v0.y; y < Tri.v2.y; y++) {
x_l = x_left[y - Tri.v0.y];
x_r = x_right[y - Tri.v0.y];
interpolatef(h_segment, x_l, h_left[y - Tri.v0.y], x_r, h_right[y - Tri.v0.y]);
interpolated(z_segment, x_l, z_left[y - Tri.v0.y], x_r, z_right[y - Tri.v0.y]);
y = y;
for (x = x_l; x <= x_r; x++) {
shaded_colour = multiplyColour(Colour, h_segment[x - x_l]);
shaded_colour = shaded_colour | 0x000000ff;
setZPixel(x, y, z_segment[x - x_l], shaded_colour);
}
h_segment.clear();
}
x01.clear(); x12.clear(); x02.clear(); x012.clear(); x_left.clear(); x_right.clear();
h02.clear(); h12.clear(); h02.clear(); h012.clear(); h_left.clear(); h_right.clear();
z02.clear(); z12.clear(); z02.clear(); z012.clear(); z_left.clear(); z_right.clear();
}
void render_scene(struct Scene_t scene) {//rendering pipeline brought together
int i, j, c;
for (int i = 0, c = 0; i < WINDOW_HEIGHT; i++)//bg black and zbuffer
{
for (int j = 0; j < WINDOW_WIDTH; j++, c++)
{
gFrameBuffer[c] = 0x000000ff;
zBuffer[c] = -FAR;
}
}
std::vector<Tri3d_t> tempProjected;
for (i = 0; i < scene.meshInstanceCount; i++) {
std::vector<Tri4d_t> tempTri = rotTranClipCullMesh(scene.meshInstances[i], scene.camera);
tempProjected = perspectiveDivide(tempTri);
std::vector<Tri2d_t> tempScreen = viewportTransformation(tempProjected);
for (j = 0; j < tempScreen.size(); j++) {
drawShadedTri(tempScreen[j], (multiplyColour(0x110000ff, j + 3)));
}
//for (j = 0; j < tempScreen.size(); j++) {
//drawWireTri(tempScreen[j]);
//}
tempTri.clear(); tempProjected.clear(); tempScreen.clear();
}
}