From 161e5c885a1d8de535a0e07a5df69c8b5bbe9a0a Mon Sep 17 00:00:00 2001 From: cspark Date: Fri, 2 Aug 2024 19:05:33 +0100 Subject: [PATCH] Get basic graph rendering functionality setup and a basic test --- lib/bmp.c | 8 ++++---- lib/bmp.h | 2 +- lib/grapher.c | 38 ++++++++++++++++++-------------------- lib/grapher.h | 3 ++- main.c | 24 +++++++++++++++++++++--- 5 files changed, 46 insertions(+), 29 deletions(-) diff --git a/lib/bmp.c b/lib/bmp.c index 38baae0..f88b77f 100644 --- a/lib/bmp.c +++ b/lib/bmp.c @@ -84,12 +84,12 @@ bitmap_file init_bitmap_file(const bitmap_header *bitmap_in, const char *filenam return new_bitmap_file; }; -int write_bitmap_pixel(const bitmap_file *bitmap_file_in, const bitmap_pixel_color *bitmap_pixel_in, const int32_t x_in, const int32_t y_in) { +int write_bitmap_pixel(const bitmap_file bitmap_file_in, const bitmap_pixel_color bitmap_pixel_in, const int32_t x_in, const int32_t y_in) { int8_t write_status; - int32_t byte_position_start = calc_bitmap_row_byte_position_start(bitmap_file_in->bitmap_metadata, y_in) + ((x_in - 1) * 3); - uint8_t *bitmap_pixel_in_byte_ptr = (uint8_t *)bitmap_pixel_in; + int32_t byte_position_start = calc_bitmap_row_byte_position_start(bitmap_file_in.bitmap_metadata, y_in) + ((x_in - 1) * 3); + uint8_t *bitmap_pixel_in_byte_ptr = (uint8_t *)&bitmap_pixel_in; - write_status = pwrite(bitmap_file_in->fd, bitmap_pixel_in_byte_ptr, 3, bitmap_file_in->bitmap_metadata->image_data_start_offset + byte_position_start); + write_status = pwrite(bitmap_file_in.fd, bitmap_pixel_in_byte_ptr, 3, bitmap_file_in.bitmap_metadata->image_data_start_offset + byte_position_start); return 0; }; diff --git a/lib/bmp.h b/lib/bmp.h index d2c0c5a..8f1fd36 100644 --- a/lib/bmp.h +++ b/lib/bmp.h @@ -67,6 +67,6 @@ uint32_t calc_bitmap_pixel_array_size_bytes(const bitmap_header *bitmap_in); uint32_t calc_bitmap_header_size_bytes(); /* Write bitmap pixel at particular coordinate */ -int write_bitmap_pixel(const bitmap_file *bitmap_file_in, const bitmap_pixel_color *bitmap_pixel_in, const int32_t x_in, const int32_t y_in); +int write_bitmap_pixel(const bitmap_file bitmap_file_in, const bitmap_pixel_color bitmap_pixel_in, const int32_t x_in, const int32_t y_in); #endif diff --git a/lib/grapher.c b/lib/grapher.c index 2055cf5..910e054 100644 --- a/lib/grapher.c +++ b/lib/grapher.c @@ -2,26 +2,24 @@ #include "bmp.h" #include "math.h" -void grapher_draw_line(bitmap_file *bitmap_file_in, int32_t x_origin_in, int32_t y_origin_in, int32_t x_end_in, int32_t y_end_in) { - float x_step = 0; - float y_step = 0; - int32_t x_origin_lerped; - int32_t y_origin_lerped; - bitmap_pixel_color target_pixel; - target_pixel.red = 0; - target_pixel.green = 0; - target_pixel.blue = 0; - +void grapher_draw_line_linear(const bitmap_file bitmap_file_in, const bitmap_pixel_color bitmap_pixel_in, int32_t x_origin_in, int32_t y_origin_in, const int32_t x_end_in, const int32_t y_end_in) { while (x_origin_in < x_end_in || y_origin_in < y_end_in) { - x_origin_lerped = lerp(x_origin_in, x_end_in, x_step); - y_origin_lerped = lerp(y_origin_in, y_end_in, y_step); - - x_step += 0.1; - y_step += 0.1; - - if (x_step >= 1 || y_step >= 1) - return; - - write_bitmap_pixel(bitmap_file_in, &target_pixel, x_origin_in, y_origin_in); + write_bitmap_pixel(bitmap_file_in, bitmap_pixel_in, x_origin_in, y_origin_in); + if (x_origin_in < x_end_in) + x_origin_in++; + if (y_origin_in < y_end_in) + y_origin_in++; + }; +}; + +void grapher_draw_line_lerp(const bitmap_file bitmap_file_in, const bitmap_pixel_color bitmap_pixel_in, int32_t x_origin_in, int32_t y_origin_in, const int32_t x_end_in, const int32_t y_end_in, const float lerp_step_in) { + int32_t x_origin_lerped = 1; + int32_t y_origin_lerped = 1; + float position = 0; + + for (position = 0; x_origin_lerped < x_end_in || y_origin_lerped < y_end_in; position += lerp_step_in) { + x_origin_lerped = lerp(x_origin_in, x_end_in, position); + y_origin_lerped = lerp(y_origin_in, y_end_in, position); + write_bitmap_pixel(bitmap_file_in, bitmap_pixel_in, x_origin_lerped, y_origin_lerped); }; }; diff --git a/lib/grapher.h b/lib/grapher.h index e57663d..414373a 100644 --- a/lib/grapher.h +++ b/lib/grapher.h @@ -4,6 +4,7 @@ #include "bmp.h" #include -void grapher_draw_line(const bitmap_file *bitmap_file_in, const int32_t x_origin_in, const int32_t y_origin_in, const int32_t x_end_in, const int32_t y_end_in); +void grapher_draw_line_linear(const bitmap_file bitmap_file_in, const bitmap_pixel_color pixel_in, const int32_t x_origin_in, const int32_t y_origin_in, const int32_t x_end_in, const int32_t y_end_in); +void grapher_draw_line_lerp(const bitmap_file bitmap_file_in, const bitmap_pixel_color bitmap_pixel_in, int32_t x_origin_in, int32_t y_origin_in, const int32_t x_end_in, const int32_t y_end_in, const float lerp_step_in); #endif diff --git a/main.c b/main.c index fe624da..71db18c 100755 --- a/main.c +++ b/main.c @@ -59,13 +59,31 @@ int main() { /* Simple skybox generator */ target_pixel.red = ((float)height / (float)test_file.bitmap_metadata->image_height) * 255; target_pixel.green = ((float)height / (float)test_file.bitmap_metadata->image_height) * 255; - target_pixel.blue = 255; + target_pixel.blue = 150; - write_bitmap_pixel(&test_file, &target_pixel, width, height); + write_bitmap_pixel(test_file, target_pixel, width, height); } } - grapher_draw_line(&test_file, 50, 50, 200, 50); + target_pixel.red = 255; + target_pixel.green = 0; + target_pixel.blue = 0; + grapher_draw_line_lerp(test_file, target_pixel, 10, 10, 100, 245, 0.01); + target_pixel.red = 0; + target_pixel.green = 255; + target_pixel.blue = 0; + grapher_draw_line_lerp(test_file, target_pixel, 10, 10, 245, 245, 0.01); + target_pixel.red = 0; + target_pixel.green = 0; + target_pixel.blue = 255; + grapher_draw_line_lerp(test_file, target_pixel, 10, 10, 245, 100, 0.01); + + /* Draw graph */ + target_pixel.red = 0; + target_pixel.green = 0; + target_pixel.blue = 0; + grapher_draw_line_linear(test_file, target_pixel, 10, 10, 245, 10); + grapher_draw_line_linear(test_file, target_pixel, 10, 10, 10, 245); close(test_file.fd);