-
Notifications
You must be signed in to change notification settings - Fork 0
/
Triangle.cpp
528 lines (460 loc) · 17.9 KB
/
Triangle.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
#include "Display.h"
#include "Vector.h"
#include "Triangle.h"
#include "Swap.h"
///////////////////////////////////////////////////////////////////////////////
// Return the normal vector of a triangle face
///////////////////////////////////////////////////////////////////////////////
vec3_t get_triangle_normal(vec4_t vertices[3])
{
// Get individual vectors from A, B, and C vertices to compute normal
// We don't need fourth component W of a matrix to do a back-face culling, so we convert Vec4 back to Vec3.
vec3_t vector_a = vec3_from_vec4(vertices[0]); /* A */
vec3_t vector_b = vec3_from_vec4(vertices[1]); /* / \ */
vec3_t vector_c = vec3_from_vec4(vertices[2]); /* C---B */
// Get the vector subtraction of B-A and C-A
vec3_t vector_ab = vec3_sub(vector_b, vector_a);
vec3_t vector_ac = vec3_sub(vector_c, vector_a);
// Always ask youself "Do I need length or magnitude of a vector? Or is direction is enough?"
// If you need only direction, then always normalize vectors.
vec3_normalize(&vector_ab);
vec3_normalize(&vector_ac);
// Compute the face normal (using CROSS PRODUCT to find perpendicular)
// It's not commutative, so coordinate system handness is imoprtant.
vec3_t normal = vec3_cross(vector_ab, vector_ac);
vec3_normalize(&normal);
return normal;
}
///////////////////////////////////////////////////////////////////////////////
// Draw a filled a triangle with a flat bottom
///////////////////////////////////////////////////////////////////////////////
//
// (x0,y0)
// / \
// / \
// / \
// / \
// / \
// (x1,y1)------(x2,y2)
//
///////////////////////////////////////////////////////////////////////////////
void fill_flat_bottom_triangle(int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color)
{
// Find the two slopes (two triangle legs)
float inv_slope_1 = (float)(x1 - x0) / (y1 - y0);
float inv_slope_2 = (float)(x2 - x0) / (y2 - y0);
// Start x_start and x_end from the top vertex (x0,y0)
float x_start = x0;
float x_end = x0;
// Loop all the scanlines from top to bottom
for (int y = y0; y <= y2; y++) {
draw_line(x_start, y, x_end, y, color);
x_start += inv_slope_1;
x_end += inv_slope_2;
}
}
///////////////////////////////////////////////////////////////////////////////
// Draw a filled a triangle with a flat top
///////////////////////////////////////////////////////////////////////////////
//
// (x0,y0)------(x1,y1)
// \ /
// \ /
// \ /
// \ /
// \ /
// (x2,y2)
//
///////////////////////////////////////////////////////////////////////////////
void fill_flat_top_triangle(int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color)
{
// Find the two slopes (two triangle legs)
float inv_slope_1 = (float)(x2 - x0) / (y2 - y0);
float inv_slope_2 = (float)(x2 - x1) / (y2 - y1);
// Start x_start and x_end from the bottom vertex (x2,y2)
float x_start = x2;
float x_end = x2;
// Loop all the scanlines from bottom to top
for (int y = y2; y >= y0; y--) {
draw_line(x_start, y, x_end, y, color);
x_start -= inv_slope_1;
x_end -= inv_slope_2;
}
}
///////////////////////////////////////////////////////////////////////////////
// Draw a triangle using three raw line calls
///////////////////////////////////////////////////////////////////////////////
void draw_triangle(int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color)
{
draw_line(x0, y0, x1, y1, color);
draw_line(x1, y1, x2, y2, color);
draw_line(x2, y2, x0, y0, color);
}
///////////////////////////////////////////////////////////////////////////////
// Draw a filled triangle with the flat-top/flat-bottom method
// We split the original triangle in two, half flat-bottom and half flat-top
///////////////////////////////////////////////////////////////////////////////
//
// (x0,y0)
// / \
// / \
// / \
// / \
// / \
// (x1,y1)------(Mx,My)
// \_ \
// \_ \
// \_ \
// \_ \
// \ \
// \_ \
// \_\
// \
// (x2,y2)
//
///////////////////////////////////////////////////////////////////////////////
void draw_filled_triangle_painters_algorithm(int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color)
{
// We need to sort the vertices by y-coordinate ascending (y0 < y1 < y2)
if (y0 > y1) {
int_swap(&y0, &y1);
int_swap(&x0, &x1);
}
if (y1 > y2) {
int_swap(&y1, &y2);
int_swap(&x1, &x2);
}
if (y0 > y1) {
int_swap(&y0, &y1);
int_swap(&x0, &x1);
}
if (y1 == y2) {
// Draw flat-bottom triangle
fill_flat_bottom_triangle(x0, y0, x1, y1, x2, y2, color);
}
else if (y0 == y1) {
// Draw flat-top triangle
fill_flat_top_triangle(x0, y0, x1, y1, x2, y2, color);
}
else {
// Calculate the new vertex (Mx,My) using triangle similarity
int My = y1;
int Mx = (((x2 - x0) * (y1 - y0)) / (y2 - y0)) + x0;
// Draw flat-bottom triangle
fill_flat_bottom_triangle(x0, y0, x1, y1, Mx, My, color);
// Draw flat-top triangle
fill_flat_top_triangle(x1, y1, Mx, My, x2, y2, color);
}
}
///////////////////////////////////////////////////////////////////////////////
// Return the barycentric weights alpha, beta, and gamma for point p
///////////////////////////////////////////////////////////////////////////////
//
// A
// /|\
// / | \
// / | \
// / (p) \
// / / \ \
// / / \ \
// B-------------C
//
///////////////////////////////////////////////////////////////////////////////
vec3_t barycentric_weights(vec2_t a, vec2_t b, vec2_t c, vec2_t p)
{
// Find the vectors between the vertices ABC and point p
vec2_t ab = vec2_sub(b, a);
vec2_t bc = vec2_sub(c, b);
vec2_t ac = vec2_sub(c, a);
vec2_t ap = vec2_sub(p, a);
vec2_t bp = vec2_sub(p, b);
// Calcualte the area of the full triangle ABC using cross product (area of parallelogram)
float area_triangle_abc = (ab.x * ac.y - ab.y * ac.x);
// Weight alpha is the area of subtriangle BCP divided by the area of the full triangle ABC
float alpha = (bc.x * bp.y - bp.x * bc.y) / area_triangle_abc;
// Weight beta is the area of subtriangle ACP divided by the area of the full triangle ABC
float beta = (ap.x * ac.y - ac.x * ap.y) / area_triangle_abc;
// Weight gamma is easily found since barycentric cooordinates always add up to 1
float gamma = 1 - alpha - beta;
vec3_t weights = { alpha, beta, gamma };
return weights;
}
///////////////////////////////////////////////////////////////////////////////
// Function to draw a solid pixel at position (x,y) using depth interpolation
///////////////////////////////////////////////////////////////////////////////
void draw_triangle_pixel(
int x, int y, uint32_t color,
vec4_t point_a, vec4_t point_b, vec4_t point_c
) {
// Create three vec2 to find the interpolation
vec2_t p = { x, y };
vec2_t a = vec2_from_vec4(point_a);
vec2_t b = vec2_from_vec4(point_b);
vec2_t c = vec2_from_vec4(point_c);
// Calculate the barycentric coordinates of our point 'p' inside the triangle
vec3_t weights = barycentric_weights(a, b, c, p);
float alpha = weights.x;
float beta = weights.y;
float gamma = weights.z;
// Interpolate the value of 1/w for the current pixel
float interpolated_reciprocal_w = (1 / point_a.w) * alpha + (1 / point_b.w) * beta + (1 / point_c.w) * gamma;
// Adjust 1/w so the pixels that are closer to the camera have smaller values
interpolated_reciprocal_w = 1.0 - interpolated_reciprocal_w;
// Only draw the pixel if the depth value is less than the one previously stored in the z-buffer
if (interpolated_reciprocal_w < get_zbuffer_at(x, y)) {
// Draw a pixel at position (x,y) with a solid color
draw_pixel(x, y, color);
// Update the z-buffer value with the 1/w of this current pixel
update_zbuffer_at(x, y, interpolated_reciprocal_w);
}
}
///////////////////////////////////////////////////////////////////////////////
// Draw a filled triangle with the flat-top/flat-bottom method
// We split the original triangle in two, half flat-bottom and half flat-top
///////////////////////////////////////////////////////////////////////////////
//
// (x0,y0)
// / \
// / \
// / \
// / \
// / \
// (x1,y1)---------\
// \_ \
// \_ \
// \_ \
// \_ \
// \ \
// \_ \
// \_\
// \
// (x2,y2)
//
///////////////////////////////////////////////////////////////////////////////
void draw_filled_triangle(
int x0, int y0, float z0, float w0,
int x1, int y1, float z1, float w1,
int x2, int y2, float z2, float w2,
uint32_t color
) {
// We need to sort the vertices by y-coordinate ascending (y0 < y1 < y2)
if (y0 > y1) {
int_swap(&y0, &y1);
int_swap(&x0, &x1);
float_swap(&z0, &z1);
float_swap(&w0, &w1);
}
if (y1 > y2) {
int_swap(&y1, &y2);
int_swap(&x1, &x2);
float_swap(&z1, &z2);
float_swap(&w1, &w2);
}
if (y0 > y1) {
int_swap(&y0, &y1);
int_swap(&x0, &x1);
float_swap(&z0, &z1);
float_swap(&w0, &w1);
}
// Create three vector points after we sort the vertices
vec4_t point_a = { x0, y0, z0, w0 };
vec4_t point_b = { x1, y1, z1, w1 };
vec4_t point_c = { x2, y2, z2, w2 };
///////////////////////////////////////////////////////
// Render the upper part of the triangle (flat-bottom)
///////////////////////////////////////////////////////
float inv_slope_1 = 0;
float inv_slope_2 = 0;
if (y1 - y0 != 0) inv_slope_1 = (float)(x1 - x0) / abs(y1 - y0);
if (y2 - y0 != 0) inv_slope_2 = (float)(x2 - x0) / abs(y2 - y0);
if (y1 - y0 != 0) {
for (int y = y0; y <= y1; y++) {
int x_start = x1 + (y - y1) * inv_slope_1;
int x_end = x0 + (y - y0) * inv_slope_2;
if (x_end < x_start) {
int_swap(&x_start, &x_end); // swap if x_start is to the right of x_end
}
for (int x = x_start; x < x_end; x++) {
// Draw our pixel with a solid color
draw_triangle_pixel(x, y, color, point_a, point_b, point_c);
}
}
}
///////////////////////////////////////////////////////
// Render the bottom part of the triangle (flat-top)
///////////////////////////////////////////////////////
inv_slope_1 = 0;
inv_slope_2 = 0;
if (y2 - y1 != 0) inv_slope_1 = (float)(x2 - x1) / abs(y2 - y1);
if (y2 - y0 != 0) inv_slope_2 = (float)(x2 - x0) / abs(y2 - y0);
if (y2 - y1 != 0) {
for (int y = y1; y <= y2; y++) {
int x_start = x1 + (y - y1) * inv_slope_1;
int x_end = x0 + (y - y0) * inv_slope_2;
if (x_end < x_start) {
int_swap(&x_start, &x_end); // swap if x_start is to the right of x_end
}
for (int x = x_start; x < x_end; x++) {
// Draw our pixel with a solid color
draw_triangle_pixel(x, y, color, point_a, point_b, point_c);
}
}
}
}
///////////////////////////////////////////////////////////////////////////////
// Function to draw the textured pixel at position (x,y) using depth interpolation
///////////////////////////////////////////////////////////////////////////////
void draw_triangle_texel(
int x, int y, lodepng_texture_t* texture,
vec4_t point_a, vec4_t point_b, vec4_t point_c,
tex2_t a_uv, tex2_t b_uv, tex2_t c_uv
)
{
vec2_t p = { x, y };
vec2_t a = vec2_from_vec4(point_a);
vec2_t b = vec2_from_vec4(point_b);
vec2_t c = vec2_from_vec4(point_c);
// Calculate the barycentric coordinates of our point 'p' inside the triangle
vec3_t weights = barycentric_weights(a, b, c, p);
float alpha = weights.x;
float beta = weights.y;
float gamma = weights.z;
// Variables to store the interpolated values of U, V, and also 1/w for the current pixel
float interpolated_u;
float interpolated_v;
float interpolated_reciprocal_w;
// Perform the interpolation of all U/w and V/w values using barycentric weights and a factor of 1/w
interpolated_u = (a_uv.u / point_a.w) * alpha + (b_uv.u / point_b.w) * beta + (c_uv.u / point_c.w) * gamma;
interpolated_v = (a_uv.v / point_a.w) * alpha + (b_uv.v / point_b.w) * beta + (c_uv.v / point_c.w) * gamma;
// Also interpolate the value of 1/w for the current pixel
interpolated_reciprocal_w = (1 / point_a.w) * alpha + (1 / point_b.w) * beta + (1 / point_c.w) * gamma;
// Now we can divide back both interpolated values by 1/w
interpolated_u /= interpolated_reciprocal_w;
interpolated_v /= interpolated_reciprocal_w;
// Map the UV coordinate to the full texture width and height
int tex_x = abs((int)(interpolated_u * texture->width)) % texture->width;
int tex_y = abs((int)(interpolated_v * texture->height)) % texture->height;
// Adjust 1/w so the pixels that are closer to the camera have smaller values
interpolated_reciprocal_w = 1.0 - interpolated_reciprocal_w;
// Only draw the pixel if the depth value is less than the one previously stored in the z-buffer
if (interpolated_reciprocal_w < get_zbuffer_at(x, y))
{
// Draw a pixel at position (x,y) with the color that comes from the mapped texture
draw_pixel(x, y, texture->png_texture[(texture->width * tex_y) + tex_x]);
// Update the z-buffer value with the 1/w of this current pixel
update_zbuffer_at(x, y, interpolated_reciprocal_w);
}
}
///////////////////////////////////////////////////////////////////////////////
// Draw a textured triangle based on a texture array of colors.
// We split the original triangle in two, half flat-bottom and half flat-top.
///////////////////////////////////////////////////////////////////////////////
//
// v0
// /\
// / \
// / \
// / \
// v1--------\
// \_ \
// \_ \
// \_ \
// \_ \
// \\
// \
// v2
//
///////////////////////////////////////////////////////////////////////////////
void draw_textured_triangle(
int x0, int y0, float z0, float w0, float u0, float v0,
int x1, int y1, float z1, float w1, float u1, float v1,
int x2, int y2, float z2, float w2, float u2, float v2,
lodepng_texture_t* texture)
{
// We need to sort the vertices by y-coordinate ascending (y0 < y1 < y2)
if (y0 > y1)
{
int_swap(&y0, &y1);
int_swap(&x0, &x1);
float_swap(&z0, &z1);
float_swap(&w0, &w1);
float_swap(&u0, &u1);
float_swap(&v0, &v1);
}
if (y1 > y2)
{
int_swap(&y1, &y2);
int_swap(&x1, &x2);
float_swap(&z1, &z2);
float_swap(&w1, &w2);
float_swap(&u1, &u2);
float_swap(&v1, &v2);
}
if (y0 > y1) {
int_swap(&y0, &y1);
int_swap(&x0, &x1);
float_swap(&z0, &z1);
float_swap(&w0, &w1);
float_swap(&u0, &u1);
float_swap(&v0, &v1);
}
// Flip the V component to account for inverted UV-coordinates (V grows downwards)
v0 = 1.0 - v0;
v1 = 1.0 - v1;
v2 = 1.0 - v2;
// Create vector points and texture coords after we sort the vertices
vec4_t point_a = { x0, y0, z0, w0 };
vec4_t point_b = { x1, y1, z1, w1 };
vec4_t point_c = { x2, y2, z2, w2 };
tex2_t a_uv = { u0, v0 };
tex2_t b_uv = { u1, v1 };
tex2_t c_uv = { u2, v2 };
///////////////////////////////////////////////////////
// Render the upper part of the triangle (flat-bottom)
///////////////////////////////////////////////////////
float inv_slope_1 = 0;
float inv_slope_2 = 0;
if (y1 - y0 != 0) inv_slope_1 = (float)(x1 - x0) / abs(y1 - y0);
if (y2 - y0 != 0) inv_slope_2 = (float)(x2 - x0) / abs(y2 - y0);
if (y1 - y0 != 0)
{
for (int y = y0; y <= y1; y++)
{
int x_start = x1 + (y - y1) * inv_slope_1;
int x_end = x0 + (y - y0) * inv_slope_2;
if (x_end < x_start)
{
int_swap(&x_start, &x_end); // swap if x_start is to the right of x_end
}
for (int x = x_start; x < x_end; x++)
{
// Draw our pixel with the color that comes from the texture
draw_triangle_texel(x, y, texture, point_a, point_b, point_c, a_uv, b_uv, c_uv);
}
}
}
///////////////////////////////////////////////////////
// Render the bottom part of the triangle (flat-top)
///////////////////////////////////////////////////////
inv_slope_1 = 0;
inv_slope_2 = 0;
if (y2 - y1 != 0) inv_slope_1 = (float)(x2 - x1) / (float)abs(y2 - y1);
if (y2 - y0 != 0) inv_slope_2 = (float)(x2 - x0) / (float)abs(y2 - y0);
if (y2 - y1 != 0)
{
for (int y = y1; y <= y2; y++)
{
int x_start = x1 + (y - y1) * inv_slope_1;
int x_end = x0 + (y - y0) * inv_slope_2;
if (x_end < x_start)
{
int_swap(&x_start, &x_end); // swap if x_start is to the right of x_end
}
for (int x = x_start; x < x_end; x++)
{
// Draw our pixel with the color that comes from the texture
draw_triangle_texel(x, y, texture, point_a, point_b, point_c, a_uv, b_uv, c_uv);
}
}
}
}