#pragma once #include "lights.h" #include "spheres.h" #include "transformations.h" #include "intersections.h" #include "rays.h" #include "materials.h" #define WORLD_MAX_OBJECTS 128 struct world { struct light light; int object_count; struct sphere objects[WORLD_MAX_OBJECTS]; }; inline static struct world world() { return (struct world){ .light = (struct light){{{{0}}}}, .object_count = 0, }; } inline static struct world default_world() { struct sphere s1 = sphere(); s1.material.color = color(0.8f, 1.0f, 0.6f); s1.material.diffuse = 0.7f; s1.material.specular = 0.2f; struct sphere s2 = sphere(); s2.transform = scaling(0.5f, 0.5f, 0.5f); return (struct world){ .light = point_light(point(-10.0f, 10.0f, -10.0f), color(1.0f, 1.0f, 1.0f)), .object_count = 2, .objects = { s1, s2 } }; } inline static void intersect_world(struct world * world, struct ray ray, struct intersections * intersections) { intersections->count = 0; for (int i = 0; i < world->object_count; i++) { intersect(&world->objects[i], ray, intersections); } intersections_sort(intersections); } inline static struct tuple shade_hit(struct world * world, struct computations * computations) { struct tuple color = lighting(computations->object->material, world->light, computations->point, computations->eyev, computations->normalv); return color; } inline static struct tuple color_at(struct world * world, struct ray ray) { struct intersections xs; intersect_world(world, ray, &xs); struct intersection * i = hit(&xs); if (i != NULL) { struct computations computations = prepare_computations(*i, ray); struct tuple color = shade_hit(world, &computations); return color; } else { return color(0.0f, 0.0f, 0.0f); } }