Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "la planche a pain" #14

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/HitRecord.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ const Vec3 = @import("Vec3.zig").Vec3;
const Pt3 = @import("Pt3.zig").Pt3;

pub const HitRecord = struct {
const Self = @This();

hit: bool,
normal: Vec3,
intersection_point: Pt3,

pub fn nil() Self {
return Self{ .hit = false, .normal = Vec3.nil(), .intersection_point = Pt3.nil() };
}
};
91 changes: 91 additions & 0 deletions src/Plane.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const Pt3 = @import("Pt3.zig").Pt3;
const Ray = @import("Ray.zig").Ray;
const std = @import("std");
const HitRecord = @import("HitRecord.zig").HitRecord;
const Vec3 = @import("Vec3.zig").Vec3;

pub const Plane = struct {
const Self = @This();

normal: Vec3,
origin: Pt3,

pub fn hits(self: *const Self, ray: Ray) HitRecord {
const denom = self.normal.dot(ray.direction);

if (denom == 0.0) {
return HitRecord.nil();
}

const t = (self.origin.subVec3(ray.origin)).dot(self.normal) / denom;

if (t < 0.0) {
return HitRecord.nil();
}

const hit_point = ray.at(t);
return HitRecord{
.hit = true,
.intersection_point = hit_point,
.normal = self.normal,
};
}
};

test "hit" {
const plane = Plane{
.normal = Vec3{ .x = 0.0, .y = 1.0, .z = 0.0 },
.origin = Pt3{ .x = 0.0, .y = 0.0, .z = 0.0 },
};

const ray = Ray{
.origin = Pt3{ .x = 0.0, .y = 1.0, .z = 0.0 },
.direction = Vec3{ .x = 0.0, .y = -1.0, .z = 0.0 },
};

const hit_record = plane.hits(ray);

try std.testing.expect(hit_record.hit);
try std.testing.expect(hit_record.intersection_point.x == 0.0);
try std.testing.expect(hit_record.intersection_point.y == 0.0);
try std.testing.expect(hit_record.intersection_point.z == 0.0);
try std.testing.expect(hit_record.normal.x == 0.0);
try std.testing.expect(hit_record.normal.y == 1.0);
try std.testing.expect(hit_record.normal.z == 0.0);
}

test "dontHit" {
const plane = Plane{
.normal = Vec3{ .x = 0.0, .y = 1.0, .z = 0.0 },
.origin = Pt3{ .x = 0.0, .y = 0.0, .z = 0.0 },
};

const ray = Ray{
.origin = Pt3{ .x = 0.0, .y = 1.0, .z = 0.0 },
.direction = Vec3{ .x = 0.0, .y = 1.0, .z = 0.0 },
};

const hit_record = plane.hits(ray);

try std.testing.expect(!hit_record.hit);
}

test "parallel" {
const plane = Plane{
.normal = Vec3{ .x = 0.0, .y = 1.0, .z = 0.0 },
.origin = Pt3{ .x = 0.0, .y = 0.0, .z = 0.0 },
};

const ray = Ray{
.origin = Pt3{ .x = 0.0, .y = 1.0, .z = 0.0 },
.direction = Vec3{ .x = 0.0, .y = 1.0, .z = 0.0 },
};

const hit_record = plane.hits(ray);

try std.testing.expect(!hit_record.hit);
}

test {
std.testing.refAllDecls(@This());
}
10 changes: 8 additions & 2 deletions src/Ray.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const std = @import("std");
const Pt3 = @import("Pt3.zig").Pt3;
const Vec3 = @import("Vec3.zig").Vec3;
pub const Ray = struct {
const Self = @This();

origin: @import("Pt3.zig").Pt3,
direction: @import("Vec3.zig").Vec3,
origin: Pt3,
direction: Vec3,

pub fn at(self: Self, t: f32) Pt3 {
return self.origin.addVec3(self.direction.mulf32(t));
}
};

test {
Expand Down
12 changes: 12 additions & 0 deletions src/Vec3.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ pub const Vec3 = struct {
};
}

pub fn collapse(self: *const Self) f32 {
return self.x + self.y + self.z;
}

pub fn inv(self: *const Self) Vec3 {
return Vec3{
.x = -self.x,
.y = -self.y,
.z = -self.z,
};
}

pub fn addVec3(self: *const Self, other: Self) Vec3 {
return Vec3{
.x = self.x + other.x,
Expand Down
1 change: 1 addition & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Sphere = @import("Sphere.zig").Sphere;
const Camera = @import("Camera.zig").Camera;
const qoi = @import("qoi.zig");
const Light = @import("Light.zig").Light;
pub const Plane = @import("Plane.zig").Plane;

fn compute_lighting(intersection: Vec3, normal: Vec3, light: Pt3, ambient: f32) f32 {
const L = intersection.to(light);
Expand Down