Compare commits

..

2 Commits

7 changed files with 133 additions and 114 deletions

View File

@ -6,6 +6,7 @@ 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 \
" "

@ -1 +1 @@
Subproject commit f95f3aa499910286876c1f2cdceea8146ebcf7b1 Subproject commit 9f2e22e6cfd3e90b155110f1914f46494a3b0e7c

View File

@ -2,41 +2,10 @@
#define RAYTRACER_H #define RAYTRACER_H
#include "c_cpp_aliases/aliases.h" #include "c_cpp_aliases/aliases.h"
#include "scene/scene.h"
#include "vector/vec.h" #include "vector/vec.h"
#include "window/window.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 { typedef struct {
f32 t1; f32 t1;
f32 t2; f32 t2;
@ -44,7 +13,7 @@ typedef struct {
typedef struct { typedef struct {
f32 closest_t; f32 closest_t;
sphere_t *closest_sphere; const sphere_t *closest_sphere;
} intersection_t; } intersection_t;
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,

41
include/scene/scene.h Normal file
View File

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

View File

@ -1,5 +1,6 @@
#include "c_cpp_aliases/aliases.h" #include "c_cpp_aliases/aliases.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>
@ -8,7 +9,6 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#define ARR_LEN(ARR) sizeof(ARR) / sizeof(ARR[0])
#define RECURSION_DEPTH 3 #define RECURSION_DEPTH 3
typedef struct { typedef struct {
@ -31,72 +31,11 @@ i32 main(i32 argc, char *argv[]) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
const scene_t *scene = build_test_scene();
bool running = true; bool running = true;
SDL_Event event = {0}; 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_min = ((i32)window.half_width) * -1;
i32 w_max = (i32)window.half_width; i32 w_max = (i32)window.half_width;
i32 h_min = ((i32)window.half_height) * -1; 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 direction = window_to_viewport(&window, x, y, viewport);
vec3f_t rotated = vec_rotate_vec3f_t(direction, camera.rotation); vec3f_t rotated = vec_rotate_vec3f_t(direction, camera.rotation);
colour_t colour = trace_ray(camera.position, rotated, 1, INFINITY, colour_t colour = trace_ray(camera.position, rotated, 1, INFINITY,
&scene, bg, RECURSION_DEPTH); scene, bg, RECURSION_DEPTH);
set_pixel(&window, x, y, colour); set_pixel(&window, x, y, colour);
} }
} }

View File

@ -7,22 +7,22 @@
#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 find_closest_intersection(vec3f_t origin,
vec3f_t direction, f32 t_min, vec3f_t direction, f32 t_min,
f32 t_max, f32 t_max,
const 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 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,
vec3f_t surface_normal, vec3f_t view_vector, vec3f_t surface_normal, vec3f_t view_vector,
f32 specular_exponent); f32 specular_exponent);
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);
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 scene_t *scene, colour_t default_colour,
@ -67,12 +67,12 @@ 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 find_closest_intersection(vec3f_t origin,
vec3f_t direction, f32 t_min, vec3f_t direction, f32 t_min,
f32 t_max, f32 t_max,
const scene_t *scene) { const scene_t *scene) {
f32 closest_t = INFINITY; f32 closest_t = INFINITY;
sphere_t *closest_sphere = NULL; const sphere_t *closest_sphere = NULL;
for (u32 i = 0; i < scene->spheres_count; ++i) { for (u32 i = 0; i < scene->spheres_count; ++i) {
solutions_t solutions = solutions_t solutions =
@ -94,7 +94,7 @@ INTERNAL intersection_t find_closest_intersection(vec3f_t origin,
return (intersection_t){closest_t, closest_sphere}; return (intersection_t){closest_t, closest_sphere};
} }
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) {
f32 r = sphere.radius; f32 r = sphere.radius;
vec3f_t CO = vec_sub(vec3f_t, origin, sphere.centre); vec3f_t CO = vec_sub(vec3f_t, origin, sphere.centre);
@ -114,7 +114,7 @@ INTERNAL solutions_t ray_intersects_sphere(vec3f_t origin, vec3f_t direction,
return (solutions_t){t1, t2}; return (solutions_t){t1, t2};
} }
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 scene_t *scene) {
f32 I = 0.0f; f32 I = 0.0f;
@ -160,13 +160,13 @@ INTERNAL f32 compute_lighting(vec3f_t position, vec3f_t surface_normal,
return I; return I;
} }
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) {
return light_intensity * return light_intensity *
cos_angle_between_vectors(light_direction, surface_normal); cos_angle_between_vectors(light_direction, surface_normal);
} }
INTERNAL f32 light_specular(f32 light_intensity, vec3f_t light_direction, internal f32 light_specular(f32 light_intensity, vec3f_t light_direction,
vec3f_t surface_normal, vec3f_t view_vector, vec3f_t surface_normal, vec3f_t view_vector,
f32 specular_exponent) { f32 specular_exponent) {
vec3f_t R = reflect_ray(light_direction, surface_normal); vec3f_t R = reflect_ray(light_direction, surface_normal);
@ -175,7 +175,7 @@ INTERNAL f32 light_specular(f32 light_intensity, vec3f_t light_direction,
powf(cos_angle_between_vectors(R, view_vector), specular_exponent); powf(cos_angle_between_vectors(R, view_vector), specular_exponent);
} }
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) {
vec3f_t _2N = vec_mul_num(vec3f_t, surface_normal, 2.0f); vec3f_t _2N = vec_mul_num(vec3f_t, surface_normal, 2.0f);
f32 dot_product = vec_dot(vec3f_t, light_direction, surface_normal); f32 dot_product = vec_dot(vec3f_t, light_direction, surface_normal);
@ -184,7 +184,7 @@ INTERNAL vec3f_t reflect_ray(vec3f_t light_direction, vec3f_t surface_normal) {
return vec_sub(vec3f_t, _2N_mul_dot, light_direction); return vec_sub(vec3f_t, _2N_mul_dot, light_direction);
} }
INTERNAL f32 cos_angle_between_vectors(vec3f_t v1, vec3f_t v2) { internal f32 cos_angle_between_vectors(vec3f_t v1, vec3f_t v2) {
f32 dot_product = vec_dot(vec3f_t, v1, v2); f32 dot_product = vec_dot(vec3f_t, v1, v2);
if (dot_product < 0.0f) { if (dot_product < 0.0f) {

69
src/scene/scene.c Normal file
View File

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