36 lines
1.1 KiB
Zig
36 lines
1.1 KiB
Zig
const Point3 = @import("vec3.zig").Point3;
|
|
const Vec3 = @import("vec3.zig").Vec3;
|
|
const Ray = @import("ray.zig").Ray;
|
|
|
|
pub const Camera = struct {
|
|
origin: Point3,
|
|
lowerLeftCorner: Point3,
|
|
horizontal: Vec3,
|
|
vertical: Vec3,
|
|
|
|
pub fn init() Camera {
|
|
const aspectRatio = 16.0 / 9.0;
|
|
const viewportHeight = 2.0;
|
|
const viewportWidth = aspectRatio * viewportHeight;
|
|
const focalLength = 1.0;
|
|
|
|
const origin = Vec3{};
|
|
const horizontal = Vec3{ .x = viewportWidth };
|
|
const vertical = Vec3{ .y = viewportHeight };
|
|
const lowerLeftCorner = origin.sub(horizontal.div(2)).sub(vertical.div(2)).sub(Vec3{ .z = focalLength });
|
|
|
|
return Camera {
|
|
.origin = origin,
|
|
.horizontal = horizontal,
|
|
.vertical = vertical,
|
|
.lowerLeftCorner = lowerLeftCorner,
|
|
};
|
|
}
|
|
|
|
pub fn getRay(self: Camera, u: f32, v: f32) Ray {
|
|
return Ray{
|
|
.origin = self.origin,
|
|
.direction = self.lowerLeftCorner.add(self.horizontal.mul_s(u)).add(self.vertical.mul_s(v)).sub(self.origin),
|
|
};
|
|
}
|
|
}; |