From 8fc3a2577a36989fe7b071b21bb3c5d5a314354f Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sat, 22 Jun 2024 16:46:55 +0100 Subject: [PATCH] Split scene definition to use it in both raytracer and rasteriser --- compile | 1 + include/raytracer/raytracer.h | 35 +----------------- include/scene/scene.h | 41 +++++++++++++++++++++ src/raytracer/main.c | 69 ++--------------------------------- src/scene/scene.c | 69 +++++++++++++++++++++++++++++++++++ 5 files changed, 117 insertions(+), 98 deletions(-) create mode 100644 include/scene/scene.h create mode 100644 src/scene/scene.c diff --git a/compile b/compile index 8fca7e2..465fedd 100755 --- a/compile +++ b/compile @@ -6,6 +6,7 @@ LIBS="$(pkg-config --libs sdl2) -lm" RAYTRACER_SRC="src/window/*.c \ src/vector/*.c \ + src/scene/*.c \ src/raytracer/*.c \ src/math/*.c \ " diff --git a/include/raytracer/raytracer.h b/include/raytracer/raytracer.h index ba68db2..02da08e 100644 --- a/include/raytracer/raytracer.h +++ b/include/raytracer/raytracer.h @@ -2,41 +2,10 @@ #define RAYTRACER_H #include "c_cpp_aliases/aliases.h" +#include "scene/scene.h" #include "vector/vec.h" #include "window/window.h" -typedef struct { - f32 radius; - vec3f_t centre; - colour_t colour; - f32 specular; - f32 reflective; -} sphere_t; - -typedef enum { - LIGHT_TYPE_POINT, - LIGHT_TYPE_DIRECTIONAL, - LIGHT_TYPE_AMBIENT, - - COUNT_LIGHT_TYPE, -} light_type_t; - -typedef struct { - light_type_t type; - f32 intensity; - union { - vec3f_t position; - vec3f_t direction; - }; -} light_t; - -typedef struct { - sphere_t *spheres; - light_t *lights; - u32 spheres_count; - u32 lights_count; -} scene_t; - typedef struct { f32 t1; f32 t2; @@ -44,7 +13,7 @@ typedef struct { typedef struct { f32 closest_t; - sphere_t *closest_sphere; + const sphere_t *closest_sphere; } intersection_t; colour_t trace_ray(vec3f_t origin, vec3f_t direction, f32 t_min, f32 t_max, diff --git a/include/scene/scene.h b/include/scene/scene.h new file mode 100644 index 0000000..b4f8477 --- /dev/null +++ b/include/scene/scene.h @@ -0,0 +1,41 @@ +#ifndef SCENE_H +#define SCENE_H + +#include "c_cpp_aliases/aliases.h" +#include "window/window.h" + +typedef struct { + f32 radius; + vec3f_t centre; + colour_t colour; + f32 specular; + f32 reflective; +} sphere_t; + +typedef enum { + LIGHT_TYPE_POINT, + LIGHT_TYPE_DIRECTIONAL, + LIGHT_TYPE_AMBIENT, + + COUNT_LIGHT_TYPE, +} light_type_t; + +typedef struct { + light_type_t type; + f32 intensity; + union { + vec3f_t position; + vec3f_t direction; + }; +} light_t; + +typedef struct { + const sphere_t *spheres; + const light_t *lights; + u32 spheres_count; + u32 lights_count; +} scene_t; + +const scene_t *build_test_scene(void); + +#endif // !SCENE_H diff --git a/src/raytracer/main.c b/src/raytracer/main.c index 81725eb..a2eb270 100644 --- a/src/raytracer/main.c +++ b/src/raytracer/main.c @@ -1,5 +1,6 @@ #include "c_cpp_aliases/aliases.h" #include "raytracer/raytracer.h" +#include "scene/scene.h" #include "vector/vec.h" #include "window/window.h" #include @@ -8,7 +9,6 @@ #include #include -#define ARR_LEN(ARR) sizeof(ARR) / sizeof(ARR[0]) #define RECURSION_DEPTH 3 typedef struct { @@ -31,72 +31,11 @@ i32 main(i32 argc, char *argv[]) { return EXIT_FAILURE; } + const scene_t *scene = build_test_scene(); + bool running = true; SDL_Event event = {0}; - sphere_t spheres[] = { - (sphere_t){ - .radius = 1.0f, - .centre = (vec3f_t){.x = 0.0f, .y = -1.0f, .z = 3.0f}, - .colour = - (colour_t){ - .rgba.r = 245, .rgba.g = 238, .rgba.b = 158, .rgba.a = 255}, - .specular = 500.0f, - .reflective = 0.3f, - }, - (sphere_t){ - .radius = 1.0f, - .centre = (vec3f_t){.x = -2.0f, .y = 0.0f, .z = 4.0f}, - .colour = - (colour_t){ - .rgba.r = 59, .rgba.g = 142, .rgba.b = 165, .rgba.a = 255}, - .specular = 10.0f, - .reflective = 0.1f, - }, - (sphere_t){ - .radius = 1.0f, - .centre = (vec3f_t){.x = 2.0f, .y = 0.0f, .z = 4.0f}, - .colour = - (colour_t){ - .rgba.r = 171, .rgba.g = 52, .rgba.b = 40, .rgba.a = 255}, - .specular = 500.0f, - .reflective = 0.4f, - }, - (sphere_t){ - .radius = 5000.0f, - .centre = (vec3f_t){.x = 0.0f, .y = -5001.0f, .z = 0.0f}, - .colour = - (colour_t){ - .rgba.r = 255, .rgba.g = 255, .rgba.b = 0, .rgba.a = 255}, - .specular = 1000.0f, - .reflective = 0.5f, - }, - }; - - light_t lights[] = { - (light_t){ - .type = LIGHT_TYPE_AMBIENT, - .intensity = 0.2f, - }, - (light_t){ - .type = LIGHT_TYPE_POINT, - .intensity = 0.6f, - .position = (vec3f_t){.x = 2.0f, .y = 1.0f, .z = 0.0f}, - }, - (light_t){ - .type = LIGHT_TYPE_DIRECTIONAL, - .intensity = 0.2f, - .direction = (vec3f_t){.x = 1.0f, .y = 4.0f, .z = 4.0f}, - }, - }; - - scene_t scene = { - .spheres = spheres, - .lights = lights, - .spheres_count = ARR_LEN(spheres), - .lights_count = ARR_LEN(lights), - }; - i32 w_min = ((i32)window.half_width) * -1; i32 w_max = (i32)window.half_width; i32 h_min = ((i32)window.half_height) * -1; @@ -118,7 +57,7 @@ i32 main(i32 argc, char *argv[]) { vec3f_t direction = window_to_viewport(&window, x, y, viewport); vec3f_t rotated = vec_rotate_vec3f_t(direction, camera.rotation); colour_t colour = trace_ray(camera.position, rotated, 1, INFINITY, - &scene, bg, RECURSION_DEPTH); + scene, bg, RECURSION_DEPTH); set_pixel(&window, x, y, colour); } } diff --git a/src/scene/scene.c b/src/scene/scene.c new file mode 100644 index 0000000..08e8d47 --- /dev/null +++ b/src/scene/scene.c @@ -0,0 +1,69 @@ +#include "scene/scene.h" +#include "c_cpp_aliases/aliases.h" + +#define ARR_LEN(ARR) sizeof(ARR) / sizeof(ARR[0]) + +internal const sphere_t spheres[] = { + (sphere_t){ + .radius = 1.0f, + .centre = (vec3f_t){.x = 0.0f, .y = -1.0f, .z = 3.0f}, + .colour = + (colour_t){ + .rgba.r = 245, .rgba.g = 238, .rgba.b = 158, .rgba.a = 255}, + .specular = 500.0f, + .reflective = 0.3f, + }, + (sphere_t){ + .radius = 1.0f, + .centre = (vec3f_t){.x = -2.0f, .y = 0.0f, .z = 4.0f}, + .colour = + (colour_t){ + .rgba.r = 59, .rgba.g = 142, .rgba.b = 165, .rgba.a = 255}, + .specular = 10.0f, + .reflective = 0.1f, + }, + (sphere_t){ + .radius = 1.0f, + .centre = (vec3f_t){.x = 2.0f, .y = 0.0f, .z = 4.0f}, + .colour = + (colour_t){ + .rgba.r = 171, .rgba.g = 52, .rgba.b = 40, .rgba.a = 255}, + .specular = 500.0f, + .reflective = 0.4f, + }, + (sphere_t){ + .radius = 5000.0f, + .centre = (vec3f_t){.x = 0.0f, .y = -5001.0f, .z = 0.0f}, + .colour = + (colour_t){ + .rgba.r = 255, .rgba.g = 255, .rgba.b = 0, .rgba.a = 255}, + .specular = 1000.0f, + .reflective = 0.5f, + }, +}; + +internal const light_t lights[] = { + (light_t){ + .type = LIGHT_TYPE_AMBIENT, + .intensity = 0.2f, + }, + (light_t){ + .type = LIGHT_TYPE_POINT, + .intensity = 0.6f, + .position = (vec3f_t){.x = 2.0f, .y = 1.0f, .z = 0.0f}, + }, + (light_t){ + .type = LIGHT_TYPE_DIRECTIONAL, + .intensity = 0.2f, + .direction = (vec3f_t){.x = 1.0f, .y = 4.0f, .z = 4.0f}, + }, +}; + +internal scene_t scene = (scene_t){ + .spheres = spheres, + .lights = lights, + .spheres_count = ARR_LEN(spheres), + .lights_count = ARR_LEN(lights), +}; + +const scene_t *build_test_scene(void) { return &scene; }