Draw first triangle

This commit is contained in:
Abdelrahman Said 2024-06-23 22:33:33 +01:00
parent e622923e19
commit 1c1b611bbb
3 changed files with 32 additions and 24 deletions

View File

@ -13,6 +13,14 @@ typedef struct {
vec2i_t p1; vec2i_t p1;
} line_t; } line_t;
typedef struct {
vec2i_t p0;
vec2i_t p1;
vec2i_t p2;
} triangle_t;
void draw_wireframe_triangle(window_t *wnd, triangle_t triangle,
colour_t colour);
void draw_line(window_t *wnd, line_t line, colour_t colour); void draw_line(window_t *wnd, line_t line, colour_t colour);
#endif // !RASTERISER_H #endif // !RASTERISER_H

View File

@ -23,23 +23,12 @@ int main(void) {
// i32 h_min = ((i32)window.half_height) * -1; // i32 h_min = ((i32)window.half_height) * -1;
// i32 h_max = (i32)window.half_height; // i32 h_max = (i32)window.half_height;
line_t lines[] = { triangle_t triangle = {
(line_t){ .p0 = {-200, -250},
(vec2i_t){.x = -300, .y = 0}, .p1 = {200, 50},
(vec2i_t){.x = 300, .y = 250}, .p2 = {20, 250},
},
(line_t){
(vec2i_t){.x = -200, .y = -100},
(vec2i_t){.x = 240, .y = 120},
},
(line_t){
(vec2i_t){.x = -50, .y = -200},
(vec2i_t){.x = 60, .y = 240},
},
}; };
u32 count = ARR_LEN(lines);
while (running) { while (running) {
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
switch (event.type) { switch (event.type) {
@ -51,9 +40,8 @@ int main(void) {
clear_window(&window, bg); clear_window(&window, bg);
for (u32 i = 0; i < count; ++i) { draw_wireframe_triangle(&window, triangle,
draw_line(&window, lines[i], (colour_t){.colour = 0xffffffff}); (colour_t){.colour = 0xffffffff});
}
swap_buffers(&window); swap_buffers(&window);
} }

View File

@ -9,19 +9,26 @@
internal list_float_t *interpolate(i32 i0, f32 d0, i32 i1, f32 d1); internal list_float_t *interpolate(i32 i0, f32 d0, i32 i1, f32 d1);
void draw_line(window_t *wnd, line_t line, colour_t colour) { void draw_wireframe_triangle(window_t *wnd, triangle_t triangle,
i32 x0 = line.p0.x; colour_t colour) {
i32 y0 = line.p0.y; draw_line(wnd, (line_t){triangle.p0, triangle.p1}, colour);
i32 x1 = line.p1.x; draw_line(wnd, (line_t){triangle.p1, triangle.p2}, colour);
i32 y1 = line.p1.y; draw_line(wnd, (line_t){triangle.p2, triangle.p0}, colour);
}
void draw_line(window_t *wnd, line_t line, colour_t colour) {
list_float_t *values = NULL; list_float_t *values = NULL;
if (abs(x1 - x0) > abs(y1 - y0)) { if (abs(line.p1.x - line.p0.x) > abs(line.p1.y - line.p0.y)) {
if (line.p1.x < line.p0.x) { if (line.p1.x < line.p0.x) {
vec_swap(vec2i_t, line.p0, line.p1); vec_swap(vec2i_t, line.p0, line.p1);
} }
i32 x0 = line.p0.x;
i32 y0 = line.p0.y;
i32 x1 = line.p1.x;
i32 y1 = line.p1.y;
values = interpolate(x0, y0, x1, y1); values = interpolate(x0, y0, x1, y1);
if (!values) { if (!values) {
return; return;
@ -35,6 +42,11 @@ void draw_line(window_t *wnd, line_t line, colour_t colour) {
vec_swap(vec2i_t, line.p0, line.p1); vec_swap(vec2i_t, line.p0, line.p1);
} }
i32 x0 = line.p0.x;
i32 y0 = line.p0.y;
i32 x1 = line.p1.x;
i32 y1 = line.p1.y;
values = interpolate(y0, x0, y1, x1); values = interpolate(y0, x0, y1, x1);
if (!values) { if (!values) {
return; return;