3 #include "./renderer.h"
6 int draw_triangle(SDL_Renderer *render,
11 if (SDL_RenderDrawLine(render,
15 (int) roundf(t.p2.y)) < 0) {
16 throw_error(ERROR_TYPE_SDL2);
20 if (SDL_RenderDrawLine(render,
24 (int) roundf(t.p3.y)) < 0) {
25 throw_error(ERROR_TYPE_SDL2);
29 if (SDL_RenderDrawLine(render,
33 (int) roundf(t.p1.y)) < 0) {
34 throw_error(ERROR_TYPE_SDL2);
41 static int fill_bottom_flat_triangle(SDL_Renderer *render,
46 const float invslope1 = (t.p2.x - t.p1.x) / (t.p2.y - t.p1.y);
47 const float invslope2 = (t.p3.x - t.p1.x) / (t.p3.y - t.p1.y);
49 const int y0 = (int) roundf(t.p1.y);
50 const int y1 = (int) roundf(t.p2.y);
55 for (int scanline = y0; scanline < y1; scanline++) {
56 if (SDL_RenderDrawLine(render,
70 static int fill_top_flat_triangle(SDL_Renderer *render,
75 const float invslope1 = (t.p3.x - t.p1.x) / (t.p3.y - t.p1.y);
76 const float invslope2 = (t.p3.x - t.p2.x) / (t.p3.y - t.p2.y);
78 const int y0 = (int) roundf(t.p3.y);
79 const int y1 = (int) roundf(t.p1.y);
84 for (int scanline = y0; scanline > y1; --scanline) {
85 if (SDL_RenderDrawLine(render,
100 int fill_triangle(SDL_Renderer *render,
103 t = triangle_sorted_by_y(t);
105 if (fabs(t.p2.y - t.p3.y) < 1e-6) {
106 if (fill_bottom_flat_triangle(render, t) < 0) {
109 } else if (fabs(t.p1.y - t.p2.y) < 1e-6) {
110 if (fill_top_flat_triangle(render, t) < 0) {
114 const point_t p4 = vec(t.p1.x + ((t.p2.y - t.p1.y) / (t.p3.y - t.p1.y)) * (t.p3.x - t.p1.x), t.p2.y);
116 if (fill_bottom_flat_triangle(render, triangle(t.p1, t.p2, p4)) < 0) {
120 if (fill_top_flat_triangle(render, triangle(t.p2, p4, t.p3)) < 0) {
124 if (SDL_RenderDrawLine(render,
125 (int) roundf(t.p2.x),
126 (int) roundf(t.p2.y),
128 (int) roundf(p4.y)) < 0) {