add hittable spheres

This commit is contained in:
Fabien Freling 2021-04-24 19:24:28 +02:00
parent 4c271103b3
commit 79772c0df4
4 changed files with 101 additions and 7 deletions

View file

@ -0,0 +1,21 @@
const Sphere = @import("sphere.zig").Sphere;
const HitRecord = @import("hittable.zig").HitRecord;
const Ray = @import("ray.zig").Ray;
pub const World = struct {
spheres: []const Sphere,
pub fn hit(self: World, ray: Ray, t_min: f32, t_max: f32) ?HitRecord {
var hitRecord: ?HitRecord = null;
var closest = t_max;
for (self.spheres) |sphere| {
if (sphere.hit(ray, t_min, closest)) |localHit| {
closest = localHit.t;
hitRecord = localHit;
}
}
return hitRecord;
}
};