diff --git a/CMakeLists.txt b/CMakeLists.txt index ab771af..77fab29 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,9 +12,11 @@ add_executable(raytracer main.c perlin.c point3.c ray.c + scenes.c texture.c utils.c vec3.c external/stb_image.c) -target_link_libraries(raytracer m) +target_link_libraries(raytracer PRIVATE m) +target_compile_options(raytracer PRIVATE -march=native) diff --git a/main.c b/main.c index 054d48b..ffac1e8 100644 --- a/main.c +++ b/main.c @@ -10,23 +10,14 @@ #include "color.h" #include "hittable.h" #include "material.h" -#include "perlin.h" #include "point3.h" #include "ray.h" -#include "texture.h" +#include "scenes.h" #include "utils.h" #include "vec3.h" -#define SCENE_RANDOM 0 -#define SCENE_TWO_SPHERES 1 -#define SCENE_TWO_PERLIN_SPHERES 2 -#define SCENE_EARTH 3 -#define SCENE_SIMPLE_LIGHT 4 -#define SCENE_CORNELL_BOX 5 -#define SCENE_CORNELL_BOX_SMOKE 6 - #ifndef SCENE_SELECT -#define SCENE_SELECT SCENE_CORNELL_BOX_SMOKE +#define SCENE_SELECT SCENE_COMPLEX #endif static Color ray_color(Ray r, Color background_color, const Hittable *world, @@ -51,243 +42,6 @@ static Color ray_color(Ray r, Color background_color, const Hittable *world, world, depth - 1))); } -static const Hittable *generate_random_scene(Arena *arena) { - Hittable *world = hittable_create_hittable_list(arena); - - const Texture *checker = texture_create_checker_solid_color( - (Color){0.2, 0.3, 0.1}, (Color){0.9, 0.9, 0.9}, 10.0, arena); - const Material *ground_material = material_create_lambertian(checker, arena); - const Hittable *ground_sphere = hittable_create_sphere( - (Point3){0.0, -1000.0, 0.0}, 1000.0, ground_material, arena); - hittable_list_add(&world->list, ground_sphere, arena); - - const Material *glass = material_create_dielectric(1.5, arena); - - for (int a = -11; a < 11; ++a) { - for (int b = -11; b < 11; ++b) { - double choose_material = random_double(); - Point3 center = {a + 0.9 * random_double(), 0.2, - b + 0.9 * random_double()}; - - if (vec3_length(point3_sub(center, (Point3){4.0, 0.2, 0.0})) > 0.9) { - if (choose_material < 0.8) { - const Material *material = material_create_lambertian_color( - color_mul(color_random(), color_random()), arena); - const Hittable *sphere = - hittable_create_sphere(center, 0.2, material, arena); - hittable_list_add(&world->list, sphere, arena); - } else if (choose_material < 0.95) { - const Material *material = material_create_metal_color( - color_random_in_range(0.5, 1), random_double_in_range(0.5, 1.0), - arena); - const Hittable *sphere = - hittable_create_sphere(center, 0.2, material, arena); - hittable_list_add(&world->list, sphere, arena); - } else { - const Hittable *sphere = - hittable_create_sphere(center, 0.2, glass, arena); - hittable_list_add(&world->list, sphere, arena); - } - } - } - } - - const Material *lambertian = - material_create_lambertian_color((Color){0.4, 0.2, 0.1}, arena); - const Material *metal = - material_create_metal_color((Color){0.7, 0.6, 0.5}, 0.0, arena); - - const Hittable *sphere1 = - hittable_create_sphere((Point3){0.0, 1.0, 0.0}, 1.0, glass, arena); - hittable_list_add(&world->list, sphere1, arena); - const Hittable *sphere2 = - hittable_create_sphere((Point3){-4.0, 1.0, 0.0}, 1.0, lambertian, arena); - hittable_list_add(&world->list, sphere2, arena); - const Hittable *sphere3 = - hittable_create_sphere((Point3){4.0, 1.0, 0.0}, 1.0, metal, arena); - hittable_list_add(&world->list, sphere3, arena); - - Hittable *bvh_root = hittable_create_bvh_node( - world->list.objects, 0, world->list.size, 0.0, 1.0, arena); - return bvh_root; -} - -static Hittable *two_spheres(Arena *arena) { - Hittable *world = hittable_create_hittable_list(arena); - - Texture *checker = texture_create_checker_solid_color( - (Color){0.2, 0.3, 0.1}, (Color){0.9, 0.9, 0.9}, 10.0, arena); - hittable_list_add( - &world->list, - hittable_create_sphere((Point3){0.0, -10.0, 0.0}, 10.0, - material_create_lambertian(checker, arena), arena), - arena); - hittable_list_add( - &world->list, - hittable_create_sphere((Point3){0.0, 10.0, 0.0}, 10.0, - material_create_lambertian(checker, arena), arena), - arena); - - return world; -} - -static Hittable *two_perlin_spheres(Arena *arena) { - Hittable *world = hittable_create_hittable_list(arena); - - Texture *perlin_texture = - texture_create_perlin_noise(4.0, PERLIN_DEFAULT_POINT_COUNT, arena); - hittable_list_add( - &world->list, - hittable_create_sphere((Point3){0.0, -1000.0, 0.0}, 1000.0, - material_create_lambertian(perlin_texture, arena), - arena), - arena); - hittable_list_add( - &world->list, - hittable_create_sphere((Point3){0.0, 2.0, 0.0}, 2.0, - material_create_lambertian(perlin_texture, arena), - arena), - arena); - - return world; -} - -static Hittable *earth(Arena *arena) { - Texture *earth_texture = texture_create_image("assets/earthmap.jpg", arena); - Material *earth_surface = material_create_lambertian(earth_texture, arena); - Hittable *globe = hittable_create_sphere((Point3){0.0, 0.0, 0.0}, 2.0, - earth_surface, arena); - return globe; -} - -static Hittable *simple_light(Arena *arena) { - Hittable *world = hittable_create_hittable_list(arena); - - Texture *perlin_texture = - texture_create_perlin_noise(4.0, PERLIN_DEFAULT_POINT_COUNT, arena); - hittable_list_add( - &world->list, - hittable_create_sphere((Point3){0.0, -1000.0, 0.0}, 1000.0, - material_create_lambertian(perlin_texture, arena), - arena), - arena); - hittable_list_add( - &world->list, - hittable_create_sphere((Point3){0.0, 2.0, 0.0}, 2.0, - material_create_lambertian(perlin_texture, arena), - arena), - arena); - - Material *diffuse_light = - material_create_diffuse_light_color((Color){4.0, 4.0, 4.0}, arena); - hittable_list_add(&world->list, - hittable_create_xy_rectangle(3.0, 5.0, 1.0, 3.0, -2.0, - diffuse_light, arena), - arena); - - return world; -} - -static Hittable *cornell_box(Arena *arena) { - Hittable *world = hittable_create_hittable_list(arena); - - Material *red = - material_create_lambertian_color((Color){0.65, 0.05, 0.05}, arena); - Material *white = - material_create_lambertian_color((Color){0.73, 0.73, 0.73}, arena); - Material *green = - material_create_lambertian_color((Color){0.12, 0.45, 0.15}, arena); - Material *light = - material_create_diffuse_light_color((Color){15.0, 15.0, 15.0}, arena); - - hittable_list_add( - &world->list, - hittable_create_yz_rectangle(0, 555, 0, 555, 555, green, arena), arena); - hittable_list_add(&world->list, - hittable_create_yz_rectangle(0, 555, 0, 555, 0, red, arena), - arena); - hittable_list_add( - &world->list, - hittable_create_xz_rectangle(213, 343, 227, 332, 554, light, arena), - arena); - hittable_list_add( - &world->list, - hittable_create_xz_rectangle(0, 555, 0, 555, 0, white, arena), arena); - hittable_list_add( - &world->list, - hittable_create_xz_rectangle(0, 555, 0, 555, 555, white, arena), arena); - hittable_list_add( - &world->list, - hittable_create_xy_rectangle(0, 555, 0, 555, 555, white, arena), arena); - - Hittable *box1 = hittable_create_box((Point3){0, 0, 0}, - (Point3){165, 330, 165}, white, arena); - box1 = hittable_create_y_rotation(box1, 15, arena); - box1 = hittable_create_translation(box1, (Vec3){265, 0, 295}, arena); - hittable_list_add(&world->list, box1, arena); - - Hittable *box2 = hittable_create_box((Point3){0, 0, 0}, - (Point3){165, 165, 165}, white, arena); - box2 = hittable_create_y_rotation(box2, -18, arena); - box2 = hittable_create_translation(box2, (Vec3){130, 0, 65}, arena); - hittable_list_add(&world->list, box2, arena); - - return world; -} - -static Hittable *cornell_box_smoke(Arena *arena) { - Hittable *world = hittable_create_hittable_list(arena); - - Material *red = - material_create_lambertian_color((Color){0.65, 0.05, 0.05}, arena); - Material *white = - material_create_lambertian_color((Color){0.73, 0.73, 0.73}, arena); - Material *green = - material_create_lambertian_color((Color){0.12, 0.45, 0.15}, arena); - Material *light = - material_create_diffuse_light_color((Color){7.0, 7.0, 7.0}, arena); - - hittable_list_add( - &world->list, - hittable_create_yz_rectangle(0, 555, 0, 555, 555, green, arena), arena); - hittable_list_add(&world->list, - hittable_create_yz_rectangle(0, 555, 0, 555, 0, red, arena), - arena); - hittable_list_add( - &world->list, - hittable_create_xz_rectangle(113, 443, 127, 432, 554, light, arena), - arena); - hittable_list_add( - &world->list, - hittable_create_xz_rectangle(0, 555, 0, 555, 0, white, arena), arena); - hittable_list_add( - &world->list, - hittable_create_xz_rectangle(0, 555, 0, 555, 555, white, arena), arena); - hittable_list_add( - &world->list, - hittable_create_xy_rectangle(0, 555, 0, 555, 555, white, arena), arena); - - Hittable *box1 = hittable_create_box((Point3){0, 0, 0}, - (Point3){165, 330, 165}, white, arena); - box1 = hittable_create_y_rotation(box1, 15, arena); - box1 = hittable_create_translation(box1, (Vec3){265, 0, 295}, arena); - hittable_list_add(&world->list, - hittable_create_constant_medium_color( - box1, 0.01, (Color){0.0, 0.0, 0.0}, arena), - arena); - - Hittable *box2 = hittable_create_box((Point3){0, 0, 0}, - (Point3){165, 165, 165}, white, arena); - box2 = hittable_create_y_rotation(box2, -18, arena); - box2 = hittable_create_translation(box2, (Vec3){130, 0, 65}, arena); - hittable_list_add(&world->list, - hittable_create_constant_medium_color( - box2, 0.01, (Color){1.0, 1.0, 1.0}, arena), - arena); - - return world; -} - int main(int argc, char *argv[]) { srand(time(0)); @@ -331,35 +85,35 @@ int main(int argc, char *argv[]) { const Hittable *world = 0; #if SCENE_SELECT == SCENE_RANDOM - world = generate_random_scene(&arena); + world = random_scene(&arena); background_color = (Color){0.7, 0.8, 1.0}; - look_from = (Point3){13.0, 2.0, 3.0}; - look_at = (Point3){0.0, 0.0, 0.0}; + look_from = (Point3){13, 2, 3}; + look_at = (Point3){0, 0, 0}; vfov = 20.0; aperture = 0.1; #elif SCENE_SELECT == SCENE_TWO_SPHERES world = two_spheres(&arena); background_color = (Color){0.7, 0.8, 1.0}; - look_from = (Point3){13.0, 2.0, 3.0}; - look_at = (Point3){0.0, 0.0, 0.0}; + look_from = (Point3){13, 2, 3}; + look_at = (Point3){0, 0, 0}; vfov = 20.0; #elif SCENE_SELECT == SCENE_TWO_PERLIN_SPHERES world = two_perlin_spheres(&arena); background_color = (Color){0.7, 0.8, 1.0}; - look_from = (Point3){13.0, 2.0, 3.0}; - look_at = (Point3){0.0, 0.0, 0.0}; + look_from = (Point3){13, 2, 3}; + look_at = (Point3){0, 0, 0}; vfov = 20.0; #elif SCENE_SELECT == SCENE_EARTH world = earth(&arena); background_color = (Color){0.7, 0.8, 1.0}; - look_from = (Point3){13.0, 2.0, 3.0}; - look_at = (Point3){0.0, 0.0, 0.0}; + look_from = (Point3){13, 2, 3}; + look_at = (Point3){0, 0, 0}; vfov = 20.0; #elif SCENE_SELECT == SCENE_SIMPLE_LIGHT world = simple_light(&arena); background_color = (Color){0.0, 0.0, 0.0}; - look_from = (Point3){26.0, 3.0, 6.0}; - look_at = (Point3){0.0, 2.0, 0.0}; + look_from = (Point3){26, 3, 6}; + look_at = (Point3){0, 2, 0}; vfov = 20.0; #elif SCENE_SELECT == SCENE_CORNELL_BOX world = cornell_box(&arena); @@ -367,16 +121,25 @@ int main(int argc, char *argv[]) { image_width = 600; samples_per_pixel = 1000; background_color = (Color){0.0, 0.0, 0.0}; - look_from = (Point3){278.0, 278.0, -800.0}; - look_at = (Point3){278.0, 278.0, 0.0}; + look_from = (Point3){278, 278, -800}; + look_at = (Point3){278, 278, 0}; vfov = 40.0; #elif SCENE_SELECT == SCENE_CORNELL_BOX_SMOKE world = cornell_box_smoke(&arena); aspect_ratio = 1.0; image_width = 600; samples_per_pixel = 200; - look_from = (Point3){278.0, 278.0, -800.0}; - look_at = (Point3){278.0, 278.0, 0.0}; + look_from = (Point3){278, 278, -800}; + look_at = (Point3){278, 278, 0}; + vfov = 40.0; +#elif SCENE_SELECT == SCENE_COMPLEX + world = complex_scene(&arena); + aspect_ratio = 1.0; + image_width = 800; + samples_per_pixel = 10000; + background_color = (Color){0.0, 0.0, 0.0}; + look_from = (Point3){478, 278, -600}; + look_at = (Point3){278, 278, 0}; vfov = 40.0; #else #error Unknown scene selected diff --git a/point3.c b/point3.c index 9999f7c..c3ff5bd 100644 --- a/point3.c +++ b/point3.c @@ -1,4 +1,5 @@ #include "point3.h" +#include "utils.h" Point3 point3_add(Point3 p, Vec3 v) { return (Point3){p.x + v.x, p.y + v.y, p.z + v.z}; @@ -7,3 +8,9 @@ Point3 point3_add(Point3 p, Vec3 v) { Vec3 point3_sub(Point3 p1, Point3 p2) { return (Vec3){p1.x - p2.x, p1.y - p2.y, p1.z - p2.z}; } + +Point3 point3_random_in_range(double min, double max) { + return (Point3){random_double_in_range(min, max), + random_double_in_range(min, max), + random_double_in_range(min, max)}; +} diff --git a/point3.h b/point3.h index 69bdc63..d095e68 100644 --- a/point3.h +++ b/point3.h @@ -10,4 +10,6 @@ typedef struct Point3 { Point3 point3_add(Point3 p, Vec3 v); Vec3 point3_sub(Point3 p1, Point3 p2); +Point3 point3_random_in_range(double min, double max); + #endif /* INCLUDED_POINT3_H */ diff --git a/scenes.c b/scenes.c new file mode 100644 index 0000000..60ce061 --- /dev/null +++ b/scenes.c @@ -0,0 +1,353 @@ +#include "scenes.h" +#include "utils.h" + +const Hittable *random_scene(Arena *arena) { + Hittable *world = hittable_create_hittable_list(arena); + + const Texture *checker = texture_create_checker_solid_color( + (Color){0.2, 0.3, 0.1}, (Color){0.9, 0.9, 0.9}, 10.0, arena); + const Material *ground_material = material_create_lambertian(checker, arena); + const Hittable *ground_sphere = hittable_create_sphere( + (Point3){0.0, -1000.0, 0.0}, 1000.0, ground_material, arena); + hittable_list_add(&world->list, ground_sphere, arena); + + const Material *glass = material_create_dielectric(1.5, arena); + + for (int a = -11; a < 11; ++a) { + for (int b = -11; b < 11; ++b) { + double choose_material = random_double(); + Point3 center = {a + 0.9 * random_double(), 0.2, + b + 0.9 * random_double()}; + + if (vec3_length(point3_sub(center, (Point3){4.0, 0.2, 0.0})) > 0.9) { + if (choose_material < 0.8) { + const Material *material = material_create_lambertian_color( + color_mul(color_random(), color_random()), arena); + const Hittable *sphere = + hittable_create_sphere(center, 0.2, material, arena); + hittable_list_add(&world->list, sphere, arena); + } else if (choose_material < 0.95) { + const Material *material = material_create_metal_color( + color_random_in_range(0.5, 1), random_double_in_range(0.5, 1.0), + arena); + const Hittable *sphere = + hittable_create_sphere(center, 0.2, material, arena); + hittable_list_add(&world->list, sphere, arena); + } else { + const Hittable *sphere = + hittable_create_sphere(center, 0.2, glass, arena); + hittable_list_add(&world->list, sphere, arena); + } + } + } + } + + const Material *lambertian = + material_create_lambertian_color((Color){0.4, 0.2, 0.1}, arena); + const Material *metal = + material_create_metal_color((Color){0.7, 0.6, 0.5}, 0.0, arena); + + const Hittable *sphere1 = + hittable_create_sphere((Point3){0.0, 1.0, 0.0}, 1.0, glass, arena); + hittable_list_add(&world->list, sphere1, arena); + const Hittable *sphere2 = + hittable_create_sphere((Point3){-4.0, 1.0, 0.0}, 1.0, lambertian, arena); + hittable_list_add(&world->list, sphere2, arena); + const Hittable *sphere3 = + hittable_create_sphere((Point3){4.0, 1.0, 0.0}, 1.0, metal, arena); + hittable_list_add(&world->list, sphere3, arena); + + Hittable *bvh_root = hittable_create_bvh_node( + world->list.objects, 0, world->list.size, 0.0, 1.0, arena); + return bvh_root; +} + +const Hittable *two_spheres(Arena *arena) { + Hittable *world = hittable_create_hittable_list(arena); + + Texture *checker = texture_create_checker_solid_color( + (Color){0.2, 0.3, 0.1}, (Color){0.9, 0.9, 0.9}, 10.0, arena); + hittable_list_add( + &world->list, + hittable_create_sphere((Point3){0.0, -10.0, 0.0}, 10.0, + material_create_lambertian(checker, arena), arena), + arena); + hittable_list_add( + &world->list, + hittable_create_sphere((Point3){0.0, 10.0, 0.0}, 10.0, + material_create_lambertian(checker, arena), arena), + arena); + + return world; +} + +const Hittable *two_perlin_spheres(Arena *arena) { + Hittable *world = hittable_create_hittable_list(arena); + + Texture *perlin_texture = + texture_create_perlin_noise(4.0, PERLIN_DEFAULT_POINT_COUNT, arena); + hittable_list_add( + &world->list, + hittable_create_sphere((Point3){0.0, -1000.0, 0.0}, 1000.0, + material_create_lambertian(perlin_texture, arena), + arena), + arena); + hittable_list_add( + &world->list, + hittable_create_sphere((Point3){0.0, 2.0, 0.0}, 2.0, + material_create_lambertian(perlin_texture, arena), + arena), + arena); + + return world; +} + +const Hittable *earth(Arena *arena) { + Texture *earth_texture = texture_create_image("assets/earthmap.jpg", arena); + Material *earth_surface = material_create_lambertian(earth_texture, arena); + Hittable *globe = hittable_create_sphere((Point3){0.0, 0.0, 0.0}, 2.0, + earth_surface, arena); + return globe; +} + +const Hittable *simple_light(Arena *arena) { + Hittable *world = hittable_create_hittable_list(arena); + + Texture *perlin_texture = + texture_create_perlin_noise(4.0, PERLIN_DEFAULT_POINT_COUNT, arena); + hittable_list_add( + &world->list, + hittable_create_sphere((Point3){0.0, -1000.0, 0.0}, 1000.0, + material_create_lambertian(perlin_texture, arena), + arena), + arena); + hittable_list_add( + &world->list, + hittable_create_sphere((Point3){0.0, 2.0, 0.0}, 2.0, + material_create_lambertian(perlin_texture, arena), + arena), + arena); + + Material *diffuse_light = + material_create_diffuse_light_color((Color){4.0, 4.0, 4.0}, arena); + hittable_list_add(&world->list, + hittable_create_xy_rectangle(3.0, 5.0, 1.0, 3.0, -2.0, + diffuse_light, arena), + arena); + + return world; +} + +const Hittable *cornell_box(Arena *arena) { + Hittable *world = hittable_create_hittable_list(arena); + + Material *red = + material_create_lambertian_color((Color){0.65, 0.05, 0.05}, arena); + Material *white = + material_create_lambertian_color((Color){0.73, 0.73, 0.73}, arena); + Material *green = + material_create_lambertian_color((Color){0.12, 0.45, 0.15}, arena); + Material *light = + material_create_diffuse_light_color((Color){15.0, 15.0, 15.0}, arena); + + hittable_list_add( + &world->list, + hittable_create_yz_rectangle(0, 555, 0, 555, 555, green, arena), arena); + hittable_list_add(&world->list, + hittable_create_yz_rectangle(0, 555, 0, 555, 0, red, arena), + arena); + hittable_list_add( + &world->list, + hittable_create_xz_rectangle(213, 343, 227, 332, 554, light, arena), + arena); + hittable_list_add( + &world->list, + hittable_create_xz_rectangle(0, 555, 0, 555, 0, white, arena), arena); + hittable_list_add( + &world->list, + hittable_create_xz_rectangle(0, 555, 0, 555, 555, white, arena), arena); + hittable_list_add( + &world->list, + hittable_create_xy_rectangle(0, 555, 0, 555, 555, white, arena), arena); + + Hittable *box1 = hittable_create_box((Point3){0, 0, 0}, + (Point3){165, 330, 165}, white, arena); + box1 = hittable_create_y_rotation(box1, 15, arena); + box1 = hittable_create_translation(box1, (Vec3){265, 0, 295}, arena); + hittable_list_add(&world->list, box1, arena); + + Hittable *box2 = hittable_create_box((Point3){0, 0, 0}, + (Point3){165, 165, 165}, white, arena); + box2 = hittable_create_y_rotation(box2, -18, arena); + box2 = hittable_create_translation(box2, (Vec3){130, 0, 65}, arena); + hittable_list_add(&world->list, box2, arena); + + return world; +} + +const Hittable *cornell_box_smoke(Arena *arena) { + Hittable *world = hittable_create_hittable_list(arena); + + Material *red = + material_create_lambertian_color((Color){0.65, 0.05, 0.05}, arena); + Material *white = + material_create_lambertian_color((Color){0.73, 0.73, 0.73}, arena); + Material *green = + material_create_lambertian_color((Color){0.12, 0.45, 0.15}, arena); + Material *light = + material_create_diffuse_light_color((Color){7.0, 7.0, 7.0}, arena); + + hittable_list_add( + &world->list, + hittable_create_yz_rectangle(0, 555, 0, 555, 555, green, arena), arena); + hittable_list_add(&world->list, + hittable_create_yz_rectangle(0, 555, 0, 555, 0, red, arena), + arena); + hittable_list_add( + &world->list, + hittable_create_xz_rectangle(113, 443, 127, 432, 554, light, arena), + arena); + hittable_list_add( + &world->list, + hittable_create_xz_rectangle(0, 555, 0, 555, 0, white, arena), arena); + hittable_list_add( + &world->list, + hittable_create_xz_rectangle(0, 555, 0, 555, 555, white, arena), arena); + hittable_list_add( + &world->list, + hittable_create_xy_rectangle(0, 555, 0, 555, 555, white, arena), arena); + + Hittable *box1 = hittable_create_box((Point3){0, 0, 0}, + (Point3){165, 330, 165}, white, arena); + box1 = hittable_create_y_rotation(box1, 15, arena); + box1 = hittable_create_translation(box1, (Vec3){265, 0, 295}, arena); + hittable_list_add(&world->list, + hittable_create_constant_medium_color( + box1, 0.01, (Color){0.0, 0.0, 0.0}, arena), + arena); + + Hittable *box2 = hittable_create_box((Point3){0, 0, 0}, + (Point3){165, 165, 165}, white, arena); + box2 = hittable_create_y_rotation(box2, -18, arena); + box2 = hittable_create_translation(box2, (Vec3){130, 0, 65}, arena); + hittable_list_add(&world->list, + hittable_create_constant_medium_color( + box2, 0.01, (Color){1.0, 1.0, 1.0}, arena), + arena); + + return world; +} + +const Hittable *complex_scene(Arena *arena) { + Hittable *boxes1 = hittable_create_hittable_list(arena); + + Material *ground = + material_create_lambertian_color((Color){0.48, 0.83, 0.53}, arena); + + const int boxes_per_side = 20; + for (int i = 0; i < boxes_per_side; ++i) { + for (int j = 0; j < boxes_per_side; ++j) { + double w = 100.0; + double x0 = -1000.0 + i * w; + double y0 = 0.0; + double z0 = -1000.0 + j * w; + double x1 = x0 + w; + double y1 = random_double_in_range(1, 101); + double z1 = z0 + w; + hittable_list_add(&boxes1->list, + hittable_create_box((Point3){x0, y0, z0}, + (Point3){x1, y1, z1}, ground, + arena), + arena); + } + } + + Hittable *objects = hittable_create_hittable_list(arena); + hittable_list_add(&objects->list, + hittable_create_bvh_node(boxes1->list.objects, 0, + boxes1->list.size, 0, 1, arena), + arena); + + Material *light = + material_create_diffuse_light_color((Color){7, 7, 7}, arena); + hittable_list_add( + &objects->list, + hittable_create_xz_rectangle(123, 423, 147, 412, 554, light, arena), + arena); + + hittable_list_add(&objects->list, + hittable_create_sphere((Point3){400, 400, 200}, 50, + material_create_lambertian_color( + (Color){0.7, 0.3, 0.1}, arena), + arena), + arena); + hittable_list_add( + &objects->list, + hittable_create_sphere((Point3){260, 150, 45}, 50, + material_create_dielectric(1.5, arena), arena), + arena); + hittable_list_add( + &objects->list, + hittable_create_sphere( + (Point3){0, 150, 145}, 50, + material_create_metal_color((Color){0.8, 0.8, 0.9}, 1.0, arena), + arena), + arena); + + Hittable *boundary = + hittable_create_sphere((Point3){360, 150, 145}, 70, + material_create_dielectric(1.5, arena), arena); + hittable_list_add(&objects->list, boundary, arena); + hittable_list_add(&objects->list, + hittable_create_constant_medium_color( + boundary, 0.2, (Color){0.2, 0.4, 0.9}, arena), + arena); + /* Ambient fog */ + boundary = hittable_create_sphere( + (Point3){0, 0, 0}, 5000, material_create_dielectric(1.5, arena), arena); + hittable_list_add(&objects->list, + hittable_create_constant_medium_color( + boundary, 0.0001, (Color){1.0, 1.0, 1.0}, arena), + arena); + + hittable_list_add( + &objects->list, + hittable_create_sphere( + (Point3){400, 200, 400}, 100, + material_create_lambertian( + texture_create_image("assets/earthmap.jpg", arena), arena), + arena), + arena); + hittable_list_add( + &objects->list, + hittable_create_sphere((Point3){220, 280, 300}, 80, + material_create_lambertian( + texture_create_perlin_noise( + 0.1, PERLIN_DEFAULT_POINT_COUNT, arena), + arena), + arena), + arena); + + Hittable *boxes2 = hittable_create_hittable_list(arena); + Material *white = + material_create_lambertian_color((Color){0.73, 0.73, 0.73}, arena); + int ns = 1000; + for (int i = 0; i < ns; ++i) { + hittable_list_add(&boxes2->list, + hittable_create_sphere(point3_random_in_range(0, 165), 10, + white, arena), + arena); + } + + hittable_list_add( + &objects->list, + hittable_create_translation( + hittable_create_y_rotation( + hittable_create_bvh_node(boxes2->list.objects, 0, + boxes2->list.size, 0, 1, arena), + 15, arena), + (Vec3){-100, 270, 395}, arena), + arena); + + return objects; +} diff --git a/scenes.h b/scenes.h new file mode 100644 index 0000000..8bc17c2 --- /dev/null +++ b/scenes.h @@ -0,0 +1,25 @@ +#ifndef INCLUDED_SCENES_H +#define INCLUDED_SCENES_H + +#include "arena.h" +#include "hittable.h" + +#define SCENE_RANDOM 0 +#define SCENE_TWO_SPHERES 1 +#define SCENE_TWO_PERLIN_SPHERES 2 +#define SCENE_EARTH 3 +#define SCENE_SIMPLE_LIGHT 4 +#define SCENE_CORNELL_BOX 5 +#define SCENE_CORNELL_BOX_SMOKE 6 +#define SCENE_COMPLEX 7 + +const Hittable *random_scene(Arena *arena); +const Hittable *two_spheres(Arena *arena); +const Hittable *two_perlin_spheres(Arena *arena); +const Hittable *earth(Arena *arena); +const Hittable *simple_light(Arena *arena); +const Hittable *cornell_box(Arena *arena); +const Hittable *cornell_box_smoke(Arena *arena); +const Hittable *complex_scene(Arena *arena); + +#endif /* INCLUDED_SCENES_H */