aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.zig55
1 files changed, 24 insertions, 31 deletions
diff --git a/src/main.zig b/src/main.zig
index f72756a..d44bd09 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -328,17 +328,20 @@ const Camera = struct {
vertical: Vec3,
lower_left_corner: Point3,
- fn init(vfov: f64, aspect_ratio: f64) Camera {
+ fn init(lookFrom: Point3, lookAt: Point3, vup: Vec3, vfov: f64, aspect_ratio: f64) Camera {
const theta = deg2rad(vfov);
const h = @tan(theta / 2);
const viewport_height = 2.0 * h;
const viewport_width = aspect_ratio * viewport_height;
- const focal_length = 1.0;
- const origin = Point3{ .x = 0.0, .y = 0.0, .z = 0.0 };
- const horizontal = Vec3{ .x = viewport_width, .y = 0.0, .z = 0.0 };
- const vertical = Vec3{ .x = 0.0, .y = viewport_height, .z = 0.0 };
- const lower_left_corner = origin.sub(horizontal.div(2.0)).sub(vertical.div(2.0)).sub(Vec3{ .x = 0.0, .y = 0.0, .z = focal_length });
+ const w = lookFrom.sub(lookAt).normalized();
+ const u = vup.cross(w).normalized();
+ const v = w.cross(u);
+
+ const origin = lookFrom;
+ const horizontal = u.mul(viewport_width);
+ const vertical = v.mul(viewport_height);
+ const lower_left_corner = origin.sub(horizontal.div(2.0)).sub(vertical.div(2.0)).sub(w);
return Camera{
.origin = origin,
@@ -348,8 +351,8 @@ const Camera = struct {
};
}
- fn getRay(camera: Camera, u: f64, v: f64) Ray {
- const dir = camera.lower_left_corner.add(camera.horizontal.mul(u)).add(camera.vertical.mul(v)).sub(camera.origin);
+ fn getRay(camera: Camera, s: f64, t: f64) Ray {
+ const dir = camera.lower_left_corner.add(camera.horizontal.mul(s)).add(camera.vertical.mul(t)).sub(camera.origin);
return Ray{
.origin = camera.origin,
.dir = dir,
@@ -401,36 +404,26 @@ pub fn main() !void {
const samples_per_pixel = 100;
const max_depth = 50;
- // World
- // const material_ground = Material{ .diffuse = DiffuseMaterial{ .albedo = Color{ .x = 0.8, .y = 0.8, .z = 0.0 } } };
- // const material_center = Material{ .diffuse = DiffuseMaterial{ .albedo = Color{ .x = 0.1, .y = 0.2, .z = 0.5 } } };
- // const material_left = Material{ .dielectric = DielectricMaterial{ .ir = 1.5 } };
- // const material_right = Material{ .metal = MetalMaterial{ .albedo = Color{ .x = 0.8, .y = 0.6, .z = 0.2 }, .fuzz = 0.0 } };
- // const sphere1 = Hittable{ .sphere = Sphere{ .center = Point3{ .x = 0.0, .y = -100.5, .z = -1.0 }, .radius = 100.0, .material = &material_ground } };
- // const sphere2 = Hittable{ .sphere = Sphere{ .center = Point3{ .x = 0.0, .y = 0.0, .z = -1.0 }, .radius = 0.5, .material = &material_center } };
- // const sphere3 = Hittable{ .sphere = Sphere{ .center = Point3{ .x = -1.0, .y = 0.0, .z = -1.0 }, .radius = 0.5, .material = &material_left } };
- // const sphere4 = Hittable{ .sphere = Sphere{ .center = Point3{ .x = 1.0, .y = 0.0, .z = -1.0 }, .radius = 0.5, .material = &material_right } };
- // var hittable_objects = ArrayList(*const Hittable).init(allocator);
- // try hittable_objects.append(&sphere1);
- // try hittable_objects.append(&sphere2);
- // try hittable_objects.append(&sphere3);
- // try hittable_objects.append(&sphere4);
- // const world = Hittable{ .list = HittableList{ .objects = hittable_objects } };
- // defer hittable_objects.deinit();
-
- const R = @cos(pi / 4.0);
- const material_left = Material{ .diffuse = DiffuseMaterial{ .albedo = Color{ .x = 0.0, .y = 0.0, .z = 1.0 } } };
- const material_right = Material{ .diffuse = DiffuseMaterial{ .albedo = Color{ .x = 1.0, .y = 0.0, .z = 0.0 } } };
- const sphere1 = Hittable{ .sphere = Sphere{ .center = Point3{ .x = -R, .y = 0.0, .z = -1.0 }, .radius = R, .material = &material_left } };
- const sphere2 = Hittable{ .sphere = Sphere{ .center = Point3{ .x = R, .y = 0.0, .z = -1.0 }, .radius = R, .material = &material_right } };
+ const material_ground = Material{ .diffuse = DiffuseMaterial{ .albedo = Color{ .x = 0.8, .y = 0.8, .z = 0.0 } } };
+ const material_center = Material{ .diffuse = DiffuseMaterial{ .albedo = Color{ .x = 0.1, .y = 0.2, .z = 0.5 } } };
+ const material_left = Material{ .dielectric = DielectricMaterial{ .ir = 1.5 } };
+ const material_right = Material{ .metal = MetalMaterial{ .albedo = Color{ .x = 0.8, .y = 0.6, .z = 0.2 }, .fuzz = 0.0 } };
+ const sphere1 = Hittable{ .sphere = Sphere{ .center = Point3{ .x = 0.0, .y = -100.5, .z = -1.0 }, .radius = 100.0, .material = &material_ground } };
+ const sphere2 = Hittable{ .sphere = Sphere{ .center = Point3{ .x = 0.0, .y = 0.0, .z = -1.0 }, .radius = 0.5, .material = &material_center } };
+ const sphere3 = Hittable{ .sphere = Sphere{ .center = Point3{ .x = -1.0, .y = 0.0, .z = -1.0 }, .radius = 0.5, .material = &material_left } };
+ const sphere4 = Hittable{ .sphere = Sphere{ .center = Point3{ .x = -1.0, .y = 0.0, .z = -1.0 }, .radius = -0.45, .material = &material_left } };
+ const sphere5 = Hittable{ .sphere = Sphere{ .center = Point3{ .x = 1.0, .y = 0.0, .z = -1.0 }, .radius = 0.5, .material = &material_right } };
var hittable_objects = ArrayList(*const Hittable).init(allocator);
try hittable_objects.append(&sphere1);
try hittable_objects.append(&sphere2);
+ try hittable_objects.append(&sphere3);
+ try hittable_objects.append(&sphere4);
+ try hittable_objects.append(&sphere5);
const world = Hittable{ .list = HittableList{ .objects = hittable_objects } };
defer hittable_objects.deinit();
// Camera
- const camera = Camera.init(90.0, aspect_ratio);
+ const camera = Camera.init(Point3{ .x = -2.0, .y = 2.0, .z = 1.0 }, Point3{ .x = 0.0, .y = 0.0, .z = -1.0 }, Vec3{ .x = 0.0, .y = 1.0, .z = 0.0 }, 20.0, aspect_ratio);
// Render
const stdout_file = std.io.getStdOut().writer();