diff --git a/src/main.c b/src/main.c index 7997046..e27ab03 100644 --- a/src/main.c +++ b/src/main.c @@ -33,7 +33,7 @@ int main(void) { } clear_image(&img, bg); - render_model(&model, &img, orange, RENDER_TYPE_WIREFRAME); + render_model(&model, &img, orange, RENDER_TYPE_FILLED); save_image(&img, "result.pam"); wapp_mem_arena_destroy(&arena); diff --git a/src/obj.c b/src/obj.c index 21b4276..e2ce4fb 100644 --- a/src/obj.c +++ b/src/obj.c @@ -18,6 +18,8 @@ struct triangle_bbox { internal void render_triangle(const Triangle *triangle, const Model *model, Image *img, Colour colour, RenderType type); +internal TriangleBBox get_triangle_bbox(const Image *img, + Vertex vertices[TRIANGLE_VERTICES]); internal void get_image_coordinates(const Vertex *vertex, const Image *img, u64 *x, u64 *y); internal u64 ndc_to_image_coordinate(f32 value, u64 max); @@ -102,9 +104,30 @@ internal void render_triangle(const Triangle *triangle, const Model *model, draw_line(img, x0, y0, x1, y1, colour); } } else if (type == RENDER_TYPE_FILLED) { + TriangleBBox bbox = get_triangle_bbox(img, vertices); + for (u64 y = bbox.y0; y < bbox.y1; ++y) { + draw_line(img, bbox.x0, y, bbox.x1, y, colour); + } } } +internal TriangleBBox get_triangle_bbox(const Image *img, + Vertex vertices[TRIANGLE_VERTICES]) { + f32 x0 = min(vertices[0].x, min(vertices[1].x, vertices[2].x)); + f32 x1 = max(vertices[0].x, max(vertices[1].x, vertices[2].x)); + // NOTE (Abdelrahman): Because y is flipped, we use max for the minimum and + // min for the maximum + f32 y0 = max(vertices[0].y, max(vertices[1].y, vertices[2].y)); + f32 y1 = min(vertices[0].y, min(vertices[1].y, vertices[2].y)); + + return (TriangleBBox){ + .x0 = ndc_to_image_coordinate(x0, img->width), + .y0 = ndc_to_image_coordinate(0.0f - y0, img->height), + .x1 = ndc_to_image_coordinate(x1, img->width), + .y1 = ndc_to_image_coordinate(0.0f - y1, img->height), + }; +} + internal void get_image_coordinates(const Vertex *vertex, const Image *img, u64 *x, u64 *y) { *x = ndc_to_image_coordinate(vertex->x, img->width); @@ -112,6 +135,6 @@ internal void get_image_coordinates(const Vertex *vertex, const Image *img, } internal u64 ndc_to_image_coordinate(f32 value, u64 max) { - f32 result = (value + 1.0f) * max / 2.0f; + f32 result = (value + 1.0f) * max * 0.5f; return clamp((u64)result, 0, max); }