raytracing/in_one_weekend/src/world.zig

34 lines
865 B
Zig

const std = @import("std");
const ArrayList = std.ArrayList;
const Sphere = @import("sphere.zig").Sphere;
const HitRecord = @import("hittable.zig").HitRecord;
const Ray = @import("ray.zig").Ray;
pub const World = struct {
spheres: ArrayList(Sphere),
pub fn init() World {
return World {
.spheres = ArrayList(Sphere).init(std.testing.allocator)
};
}
pub fn deinit(self: *World) void {
self.spheres.deinit();
}
pub fn hit(self: *const World, ray: Ray, t_min: f32, t_max: f32) ?HitRecord {
var hitRecord: ?HitRecord = null;
var closest = t_max;
for (self.spheres.items) |sphere| {
if (sphere.hit(ray, t_min, closest)) |localHit| {
closest = localHit.t;
hitRecord = localHit;
}
}
return hitRecord;
}
};