Move scene to raytracer

This commit is contained in:
Abdelrahman Said 2024-07-09 19:44:19 +01:00
parent 4430ea872c
commit c3e1aac779
6 changed files with 110 additions and 129 deletions

View File

@ -9,7 +9,6 @@ LIBS="$(pkg-config --libs sdl2) -lm"
RAYTRACER_SRC="src/window/*.c \ RAYTRACER_SRC="src/window/*.c \
src/vector/*.c \ src/vector/*.c \
src/scene/*.c \
src/raytracer/*.c \ src/raytracer/*.c \
src/math/*.c \ src/math/*.c \
src/camera/*.c \ src/camera/*.c \
@ -17,7 +16,6 @@ RAYTRACER_SRC="src/window/*.c \
RASTERISER_SRC="src/window/*.c \ RASTERISER_SRC="src/window/*.c \
src/vector/*.c \ src/vector/*.c \
src/scene/*.c \
src/list/*.c \ src/list/*.c \
src/rasteriser/*.c \ src/rasteriser/*.c \
src/math/*.c \ src/math/*.c \

View File

@ -2,7 +2,6 @@
#define RAYTRACER_H #define RAYTRACER_H
#include "aliases.h" #include "aliases.h"
#include "scene/scene.h"
#include "vector/vec.h" #include "vector/vec.h"
#include "window/window.h" #include "window/window.h"
@ -11,13 +10,46 @@ typedef struct {
f32 t2; f32 t2;
} solutions_t; } solutions_t;
typedef struct {
f32 radius;
vec3f_t centre;
colour_t colour;
f32 specular;
f32 reflective;
} sphere_t;
typedef struct { typedef struct {
f32 closest_t; f32 closest_t;
const sphere_t *closest_sphere; const sphere_t *closest_sphere;
} intersection_t; } intersection_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;
} raytracer_scene_t;
const raytracer_scene_t *build_test_scene(void);
colour_t trace_ray(vec3f_t origin, vec3f_t direction, f32 t_min, f32 t_max, colour_t trace_ray(vec3f_t origin, vec3f_t direction, f32 t_min, f32 t_max,
const scene_t *scene, colour_t default_colour, const raytracer_scene_t *scene, colour_t default_colour,
u32 recursion_depth); u32 recursion_depth);
#endif // !RAYTRACER_H #endif // !RAYTRACER_H

View File

@ -1,42 +0,0 @@
#ifndef SCENE_H
#define SCENE_H
#include "aliases.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 {
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

View File

@ -1,7 +1,6 @@
#include "aliases.h" #include "aliases.h"
#include "camera/camera.h" #include "camera/camera.h"
#include "raytracer/raytracer.h" #include "raytracer/raytracer.h"
#include "scene/scene.h"
#include "vector/vec.h" #include "vector/vec.h"
#include "window/window.h" #include "window/window.h"
#include <SDL2/SDL_events.h> #include <SDL2/SDL_events.h>
@ -27,7 +26,7 @@ i32 main(i32 argc, char *argv[]) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
const scene_t *scene = build_test_scene(); const raytracer_scene_t *scene = build_test_scene();
bool running = true; bool running = true;
SDL_Event event = {0}; SDL_Event event = {0};

View File

@ -1,5 +1,6 @@
#include "raytracer/raytracer.h" #include "raytracer/raytracer.h"
#include "aliases.h" #include "aliases.h"
#include "misc/misc_utils.h"
#include "vector/vec.h" #include "vector/vec.h"
#include "window/window.h" #include "window/window.h"
#include <math.h> #include <math.h>
@ -7,15 +8,14 @@
#define SPECULAR_EPSILON 0.001f #define SPECULAR_EPSILON 0.001f
#define REFLECTIVE_EPSILON 0.1f #define REFLECTIVE_EPSILON 0.1f
internal intersection_t find_closest_intersection(vec3f_t origin, internal intersection_t
vec3f_t direction, f32 t_min, find_closest_intersection(vec3f_t origin, vec3f_t direction, f32 t_min,
f32 t_max, f32 t_max, const raytracer_scene_t *scene);
const scene_t *scene);
internal solutions_t ray_intersects_sphere(vec3f_t origin, vec3f_t direction, internal solutions_t ray_intersects_sphere(vec3f_t origin, vec3f_t direction,
sphere_t sphere); sphere_t sphere);
internal f32 compute_lighting(vec3f_t position, vec3f_t surface_normal, internal f32 compute_lighting(vec3f_t position, vec3f_t surface_normal,
vec3f_t view_vector, f32 specular_exponent, vec3f_t view_vector, f32 specular_exponent,
const scene_t *scene); const raytracer_scene_t *scene);
internal f32 light_diffuse(f32 light_intensity, vec3f_t light_direction, internal f32 light_diffuse(f32 light_intensity, vec3f_t light_direction,
vec3f_t surface_normal); vec3f_t surface_normal);
internal f32 light_specular(f32 light_intensity, vec3f_t light_direction, internal f32 light_specular(f32 light_intensity, vec3f_t light_direction,
@ -24,8 +24,73 @@ internal f32 light_specular(f32 light_intensity, vec3f_t light_direction,
internal vec3f_t reflect_ray(vec3f_t light_direction, vec3f_t surface_normal); internal vec3f_t reflect_ray(vec3f_t light_direction, vec3f_t surface_normal);
internal f32 cos_angle_between_vectors(vec3f_t v1, vec3f_t v2); internal f32 cos_angle_between_vectors(vec3f_t v1, vec3f_t v2);
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 raytracer_scene_t scene = (raytracer_scene_t){
.spheres = spheres,
.lights = lights,
.spheres_count = ARR_LEN(spheres),
.lights_count = ARR_LEN(lights),
};
const raytracer_scene_t *build_test_scene(void) { return &scene; }
colour_t trace_ray(vec3f_t origin, vec3f_t direction, f32 t_min, f32 t_max, colour_t trace_ray(vec3f_t origin, vec3f_t direction, f32 t_min, f32 t_max,
const scene_t *scene, colour_t default_colour, const raytracer_scene_t *scene, colour_t default_colour,
u32 recursion_depth) { u32 recursion_depth) {
intersection_t intersection = intersection_t intersection =
find_closest_intersection(origin, direction, t_min, t_max, scene); find_closest_intersection(origin, direction, t_min, t_max, scene);
@ -67,10 +132,9 @@ colour_t trace_ray(vec3f_t origin, vec3f_t direction, f32 t_min, f32 t_max,
return colour_add_colour(local_colour, reflected_colour); return colour_add_colour(local_colour, reflected_colour);
} }
internal intersection_t find_closest_intersection(vec3f_t origin, internal intersection_t
vec3f_t direction, f32 t_min, find_closest_intersection(vec3f_t origin, vec3f_t direction, f32 t_min,
f32 t_max, f32 t_max, const raytracer_scene_t *scene) {
const scene_t *scene) {
f32 closest_t = INFINITY; f32 closest_t = INFINITY;
const sphere_t *closest_sphere = NULL; const sphere_t *closest_sphere = NULL;
@ -116,7 +180,7 @@ internal solutions_t ray_intersects_sphere(vec3f_t origin, vec3f_t direction,
internal f32 compute_lighting(vec3f_t position, vec3f_t surface_normal, internal f32 compute_lighting(vec3f_t position, vec3f_t surface_normal,
vec3f_t view_vector, f32 specular_exponent, vec3f_t view_vector, f32 specular_exponent,
const scene_t *scene) { const raytracer_scene_t *scene) {
f32 I = 0.0f; f32 I = 0.0f;
light_t light = {0}; light_t light = {0};

View File

@ -1,70 +0,0 @@
#include "scene/scene.h"
#include "aliases.h"
#include "misc/misc_utils.h"
#include "vector/vec.h"
#include "window/window.h"
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; }