Draw first triangle
This commit is contained in:
parent
e622923e19
commit
1c1b611bbb
@ -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
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -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_wireframe_triangle(window_t *wnd, triangle_t triangle,
|
||||||
|
colour_t colour) {
|
||||||
|
draw_line(wnd, (line_t){triangle.p0, triangle.p1}, colour);
|
||||||
|
draw_line(wnd, (line_t){triangle.p1, triangle.p2}, colour);
|
||||||
|
draw_line(wnd, (line_t){triangle.p2, triangle.p0}, 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) {
|
||||||
|
list_float_t *values = NULL;
|
||||||
|
|
||||||
|
if (abs(line.p1.x - line.p0.x) > abs(line.p1.y - line.p0.y)) {
|
||||||
|
if (line.p1.x < line.p0.x) {
|
||||||
|
vec_swap(vec2i_t, line.p0, line.p1);
|
||||||
|
}
|
||||||
|
|
||||||
i32 x0 = line.p0.x;
|
i32 x0 = line.p0.x;
|
||||||
i32 y0 = line.p0.y;
|
i32 y0 = line.p0.y;
|
||||||
i32 x1 = line.p1.x;
|
i32 x1 = line.p1.x;
|
||||||
i32 y1 = line.p1.y;
|
i32 y1 = line.p1.y;
|
||||||
|
|
||||||
list_float_t *values = NULL;
|
|
||||||
|
|
||||||
if (abs(x1 - x0) > abs(y1 - y0)) {
|
|
||||||
if (line.p1.x < line.p0.x) {
|
|
||||||
vec_swap(vec2i_t, line.p0, line.p1);
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
||||||
|
Loading…
Reference in New Issue
Block a user