From b40c175e75c9c7b617b087169f94bb18055ebd90 Mon Sep 17 00:00:00 2001 From: cspark Date: Thu, 1 Aug 2024 12:36:19 +0100 Subject: [PATCH] Init grapher --- lib/array.c | 12 ++++++------ lib/array.h | 14 +++++++------- lib/grapher.c | 27 +++++++++++++++++++++++++++ lib/grapher.h | 9 +++++++++ lib/math.c | 9 +++++++-- lib/math.h | 4 +++- lib/raytracer.c | 28 ++++++++++++++++++++++++++++ lib/raytracer.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ main.c | 21 +++++++++++++++++---- 9 files changed, 153 insertions(+), 20 deletions(-) create mode 100644 lib/grapher.c create mode 100644 lib/grapher.h create mode 100644 lib/raytracer.c create mode 100644 lib/raytracer.h diff --git a/lib/array.c b/lib/array.c index 85af2c1..37602af 100755 --- a/lib/array.c +++ b/lib/array.c @@ -9,7 +9,7 @@ int sizeof_str(char in[]) { return i; } -void print_strArray(char in[]) { +void print_str_array(char in[]) { int i; printf("{ "); for (i = 0; i < sizeof_str(in); ++i) { @@ -22,7 +22,7 @@ void print_strArray(char in[]) { } } -void print_intArray(intArray *in) { +void print_int_array(int_array *in) { int i; printf("{ "); for (i = 0; i < in->length; ++i) { @@ -35,7 +35,7 @@ void print_intArray(intArray *in) { } } -int check_intArray_sorted(intArray *in) { +int check_int_array_sorted(int_array *in) { int i = 0; while(i < in->length - 1) { if (in->data[i] > in->data[i + 1]) @@ -47,7 +47,7 @@ int check_intArray_sorted(intArray *in) { } /* Convert an integer array into a combined integer */ -int intArray_to_int(intArray *in) { +int int_array_to_int(int_array *in) { int i; int out = 0; int current_digit; @@ -57,10 +57,10 @@ int intArray_to_int(intArray *in) { } /* Split an integer into an integer array */ -intArray int_to_intArray(int in) { +int_array int_to_int_array(int in) { int in_length = int_count_digits(in); int out_data[in_length]; - intArray out; + int_array out; out.data = out_data; out.length = in_length; int i; diff --git a/lib/array.h b/lib/array.h index ec9d733..37ccbba 100755 --- a/lib/array.h +++ b/lib/array.h @@ -1,21 +1,21 @@ #ifndef ARRAY_H #define ARRAY_H -typedef struct intArray { +typedef struct int_array { unsigned int length; int *data; -} intArray; +} int_array; int sizeof_str(char in[]); -void print_strArray(char in[]); +void print_str_array(char in[]); -void print_intArray(intArray *in); +void print_int_array(int_array *in); -int check_intArray_sorted(intArray *in); +int check_int_array_sorted(int_array *in); -intArray int_to_intArray(int in); +int_array int_to_int_array(int in); -int intArray_to_int(intArray *in); +int int_array_to_int(int_array *in); #endif diff --git a/lib/grapher.c b/lib/grapher.c new file mode 100644 index 0000000..2055cf5 --- /dev/null +++ b/lib/grapher.c @@ -0,0 +1,27 @@ +#include +#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; + + 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); + }; +}; diff --git a/lib/grapher.h b/lib/grapher.h new file mode 100644 index 0000000..e57663d --- /dev/null +++ b/lib/grapher.h @@ -0,0 +1,9 @@ +#ifndef GRAPHER_H +#define GRAPHER_H + +#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); + +#endif diff --git a/lib/math.c b/lib/math.c index a257354..88f83dd 100644 --- a/lib/math.c +++ b/lib/math.c @@ -1,8 +1,13 @@ #include "array.h" +#include -intArray find_divisors(int in) { +int32_t lerp(int32_t x_in, int32_t y_in, float position) { + return x_in + ((y_in - x_in) * position); +} + +int_array find_divisors(int in) { int i; - intArray out; + int_array out; for (i = 1; i <= in; ++i) { if (in % i == 0) { diff --git a/lib/math.h b/lib/math.h index 2865c09..39e10c1 100644 --- a/lib/math.h +++ b/lib/math.h @@ -1,8 +1,10 @@ #ifndef MATH_H #define MATH_H +#include #include "array.h" -intArray find_divisors(); +int32_t lerp(int32_t x_in, int32_t y_in, float position); +int_array find_divisors(); #endif diff --git a/lib/raytracer.c b/lib/raytracer.c new file mode 100644 index 0000000..0a58719 --- /dev/null +++ b/lib/raytracer.c @@ -0,0 +1,28 @@ +#include "raytracer.h" + +origin_3d multiply_origin_3d_by_scalar(origin_3d origin_3d_in, double scalar) { + origin_3d_in.x *= scalar; + origin_3d_in.y *= scalar; + origin_3d_in.z *= scalar; + return origin_3d_in; +} + +origin_3d add_origin_3d_together(origin_3d origin_3d_in_x, origin_3d *origin_3d_in_y) { + origin_3d_in_x.x += origin_3d_in_y->x; + origin_3d_in_x.y += origin_3d_in_y->y; + origin_3d_in_x.z += origin_3d_in_y->z; + return origin_3d_in_x; +} + +origin_3d get_point_along_vector_3d(vector_3d *vector_3d_in, double scalar) { + origin_3d scaled_direction = multiply_origin_3d_by_scalar(vector_3d_in->direction, scalar); + return add_origin_3d_together(vector_3d_in->origin, &scaled_direction); +}; + +camera init_camera(origin_3d origin, double distance_to_canvas, double width_canvas_pixel_spacing, double height_canvas_pixel_spacing) { + camera new_camera; + new_camera.origin = origin; + new_camera.distance_to_canvas = distance_to_canvas; + new_camera.width_canvas_pixel_spacing = width_canvas_pixel_spacing; + new_camera.height_canvas_pixel_spacing = height_canvas_pixel_spacing; +}; diff --git a/lib/raytracer.h b/lib/raytracer.h new file mode 100644 index 0000000..b1e1095 --- /dev/null +++ b/lib/raytracer.h @@ -0,0 +1,49 @@ +#ifndef RAYTRACER_H +#define RAYTRACER_H + +#include + +typedef struct origin_3d { + double x; + double y; + double z; +} origin_3d; + +typedef struct vector_3d { + origin_3d origin; + origin_3d direction; + double magnitude; +} vector_3d; + +typedef struct raytracer_pixel_color { + uint8_t blue; + uint8_t green; + uint8_t red; +} raytracer_pixel_color; + +typedef struct sphere_object { + origin_3d origin; + double radius; +} sphere_object; + +/* TODO: Probably better to have this allocated dynamically/make more advanced resizeable array datatype. Need to write decent memory allocator first. Also add more object types */ +typedef struct object_array { + unsigned int length; + sphere_object data[]; +} object_array; + +typedef struct camera { + origin_3d origin; + double distance_to_canvas; + double width_canvas_pixel_spacing; + double height_canvas_pixel_spacing; + double total_canvas_width; + double total_canvas_height; +} camera; + +origin_3d multiply_origin_3d_by_scalar(origin_3d origin_3d_in, double scalar); +origin_3d add_origin_3d_together(origin_3d origin_3d_in_x, origin_3d *origin_3d_in_y); +origin_3d get_point_along_vector_3d(vector_3d *vector_3d_in, double magnitude); +camera init_camera(origin_3d origin, double distance_to_canvas, double width_canvas_pixel_spacing, double height_canvas_pixel_spacing); + +#endif diff --git a/main.c b/main.c index e81a433..fe624da 100755 --- a/main.c +++ b/main.c @@ -2,13 +2,14 @@ #include #include #include -#include +/*#include #include "lib/temperature.h" #include "lib/array.h" #include "lib/base10.h" -// #include "lib/math.h" -#include "lib/alloc.h" +#include "lib/alloc.h"*/ +#include "lib/math.h" #include "lib/bmp.h" +#include "lib/grapher.h" void swap_int(int *pX, int *pY) { int temp = *pX; @@ -40,7 +41,7 @@ int main() { ss_alloc_free(reserved_memory); printf("Memory : %d/%d", ALLOC_SIZE, (int)ss_alloc_get_used_bytes());*/ - const bitmap_header test = init_bitmap_header(1920, 1080); + const bitmap_header test = init_bitmap_header(255, 255); const bitmap_file test_file = init_bitmap_file(&test, "test.bmp"); bitmap_pixel_color target_pixel; @@ -48,12 +49,24 @@ int main() { int32_t height; for (height = 1; height <= test_file.bitmap_metadata->image_height; ++height) { for (width = 1; width <= test_file.bitmap_metadata->image_width; ++width) { + /* Test hello world */ + /* target_pixel.red = ((float)width / (float)test_file.bitmap_metadata->image_width) * 255; target_pixel.green = ((float)height / (float)test_file.bitmap_metadata->image_height) * 255; target_pixel.blue = 100; + */ + + /* 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; + write_bitmap_pixel(&test_file, &target_pixel, width, height); } } + + grapher_draw_line(&test_file, 50, 50, 200, 50); + close(test_file.fd); }