Start implementing filled rendering

This commit is contained in:
Abdelrahman Said 2024-07-28 20:31:48 +01:00
parent 3f04b7b5b0
commit 6537753a3a
2 changed files with 25 additions and 2 deletions

View File

@ -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);

View File

@ -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,7 +104,28 @@ 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,
@ -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);
}