-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriangle.c
executable file
·73 lines (62 loc) · 2.15 KB
/
triangle.c
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
#include "triangle.h"
#include "line.h"
#define DISTANCE 1000
// http://www.sunshine2k.de/coding/java/TriangleRasterization/TriangleRasterization.html
void fillTopFlatTriangle(const union Triangle half, const union Triangle *t2D, const struct Triangle3D *t3D)
{
// v2.y == v3.y
float invslope1 = (half.v2.x - half.v1.x) / (half.v2.y - half.v1.y);
float invslope2 = (half.v3.x - half.v1.x) / (half.v2.y - half.v1.y);
if (invslope1 < invslope2)
swap((struct Vertice*)&invslope1, (struct Vertice*)&invslope2);
float curx1 = half.v1.x;
float curx2 = half.v1.x;
for (int i = half.v1.y; i <= half.v2.y; i++) {
draw_line((uint16_t)curx2, i, (uint16_t)curx1, t2D, t3D);
curx1 += invslope1;
curx2 += invslope2;
}
}
void fillBottomFlatTriangle(const union Triangle half, const union Triangle *t2D, const struct Triangle3D *t3D)
{
// v1.y == v2.y
float invslope1 = (half.v3.x - half.v1.x) / (half.v3.y - half.v1.y);
float invslope2 = (half.v3.x - half.v2.x) / (half.v3.y - half.v1.y);
if (invslope1 > invslope2)
swap((struct Vertice*)&invslope1, (struct Vertice*)&invslope2);
float curx1 = half.v3.x;
float curx2 = half.v3.x;
for (int i = half.v3.y; i > half.v1.y; i--) {
draw_line((uint16_t)curx2, i, (uint16_t)curx1, t2D, t3D);
curx1 -= invslope1;
curx2 -= invslope2;
}
}
void draw_triangle(struct Triangle3D *t3D)
{
set_color(t3D->color);
union Triangle this;
for (int j = 0; j < 3; j++) {
float divisor = 1 - (t3D->ar[4*j+2]) / DISTANCE;
this.ar[j*2 ] = (t3D->ar[4*j]) / divisor;
this.ar[j*2+1] = (t3D->ar[4*j+1]) / divisor;
}
if (this.v1.y > this.v2.y) {
swap(&this.v1, &this.v2);
swap3D(&t3D->ar[0], &t3D->ar[4]);
}
if (this.v2.y > this.v3.y) {
swap(&this.v2, &this.v3);
swap3D(&t3D->ar[4], &t3D->ar[8]);
}
if (this.v1.y > this.v2.y) {
swap(&this.v1, &this.v2);
swap3D(&t3D->ar[0], &t3D->ar[4]);
}
// v1.y <= v2.y <= v3.y
float newX = (this.v1.x +
(this.v2.y - this.v1.y) / (this.v3.y - this.v1.y) *
(this.v3.x - this.v1.x));
fillTopFlatTriangle((union Triangle) { .v1 = this.v1, .v2 = this.v2, .v3.x = newX}, &this, t3D);
fillBottomFlatTriangle((union Triangle) { .v1 = this.v2, .v2.x = newX, .v3 = this.v3 }, &this, t3D);
}