Skip to content

Commit

Permalink
Merge pull request #21 from Miou-zora/specular
Browse files Browse the repository at this point in the history
Add specular
  • Loading branch information
Miou-zora authored Apr 26, 2024
2 parents ddf0b8b + c4bad76 commit 0590808
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 44 deletions.
51 changes: 43 additions & 8 deletions src/Cylinder.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ const Ray = @import("Ray.zig").Ray;
const std = @import("std");
const HitRecord = @import("HitRecord.zig").HitRecord;
const Vec3 = @import("Vec3.zig").Vec3;
const Material = @import("Material.zig").Material;

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

radius: f32,
origin: Pt3,
material: Material,

pub fn hits(self: *const Self, ray: Ray) HitRecord {
const rx_minus_cx = ray.origin.x - self.origin.x;
Expand All @@ -26,7 +28,13 @@ pub const Cylinder = struct {
if (t < 0.0) {
return HitRecord.nil();
} else {
return HitRecord{ .hit = true, .normal = Vec3{ .x = intersection_point.x - self.origin.x, .y = intersection_point.y - self.origin.y, .z = 0 }, .intersection_point = intersection_point, .t = t };
return HitRecord{
.hit = true,
.normal = Vec3{ .x = intersection_point.x - self.origin.x, .y = intersection_point.y - self.origin.y, .z = 0 },
.intersection_point = intersection_point,
.t = t,
.material = self.material,
};
}
} else {
const t1 = (-b + @sqrt(delta)) / (2.0 * a);
Expand All @@ -36,14 +44,27 @@ pub const Cylinder = struct {
}
const t = if (t1 < t2 and t1 > 0) t1 else t2;
const intersection_point = ray.origin.addVec3(ray.direction.mulf32(t));
return HitRecord{ .hit = true, .normal = Vec3{ .x = intersection_point.x - self.origin.x, .y = intersection_point.y - self.origin.y, .z = 0 }, .intersection_point = intersection_point, .t = t };
return HitRecord{
.hit = true,
.normal = Vec3{ .x = intersection_point.x - self.origin.x, .y = intersection_point.y - self.origin.y, .z = 0 },
.intersection_point = intersection_point,
.t = t,
.material = self.material,
};
}
}
};

test "hit" {
const cylinder = Cylinder{ .radius = 1.0, .origin = Pt3{ .x = 0.0, .y = 0.0, .z = 0.0 } };
const ray = Ray{ .origin = Pt3{ .x = 0.0, .y = 2.0, .z = 0.0 }, .direction = Vec3{ .x = 0.0, .y = -1.0, .z = 0.0 } };
const cylinder = Cylinder{
.radius = 1.0,
.origin = Pt3{ .x = 0.0, .y = 0.0, .z = 0.0 },
.material = Material.nil(),
};
const ray = Ray{
.origin = Pt3{ .x = 0.0, .y = 2.0, .z = 0.0 },
.direction = Vec3{ .x = 0.0, .y = -1.0, .z = 0.0 },
};
const hit_record = cylinder.hits(ray);
try std.testing.expect(hit_record.hit);
try std.testing.expect(hit_record.intersection_point.x == 0.0);
Expand All @@ -55,15 +76,29 @@ test "hit" {
}

test "dontHit" {
const cylinder = Cylinder{ .radius = 1.0, .origin = Pt3{ .x = 0.0, .y = 0.0, .z = 0.0 } };
const ray = Ray{ .origin = Pt3{ .x = 1.0, .y = 1.0, .z = 0.0 }, .direction = Vec3{ .x = 1.0, .y = 1.0, .z = 0.0 } };
const cylinder = Cylinder{
.radius = 1.0,
.origin = Pt3{ .x = 0.0, .y = 0.0, .z = 0.0 },
.material = Material.nil(),
};
const ray = Ray{
.origin = Pt3{ .x = 1.0, .y = 1.0, .z = 0.0 },
.direction = Vec3{ .x = 1.0, .y = 1.0, .z = 0.0 },
};
const hit_record = cylinder.hits(ray);
try std.testing.expect(!hit_record.hit);
}

test "limit" {
const cylinder = Cylinder{ .radius = 1.0, .origin = Pt3{ .x = 0.0, .y = 0.0, .z = 0.0 } };
const ray = Ray{ .origin = Pt3{ .x = 0.0, .y = 2.0, .z = 0.0 }, .direction = Vec3{ .x = 0.0, .y = -1.0, .z = 0.0 } };
const cylinder = Cylinder{
.radius = 1.0,
.origin = Pt3{ .x = 0.0, .y = 0.0, .z = 0.0 },
.material = Material.nil(),
};
const ray = Ray{
.origin = Pt3{ .x = 0.0, .y = 2.0, .z = 0.0 },
.direction = Vec3{ .x = 0.0, .y = -1.0, .z = 0.0 },
};
const hit_record = cylinder.hits(ray);
try std.testing.expect(hit_record.hit);
try std.testing.expect(hit_record.intersection_point.x == 0.0);
Expand Down
10 changes: 9 additions & 1 deletion src/HitRecord.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Vec3 = @import("Vec3.zig").Vec3;
const Pt3 = @import("Pt3.zig").Pt3;
const Material = @import("Material.zig").Material;

pub const HitRecord = struct {
const Self = @This();
Expand All @@ -8,8 +9,15 @@ pub const HitRecord = struct {
normal: Vec3,
intersection_point: Pt3,
t: f32,
material: Material,

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

pub const Material = struct {
const Self = @This();
color: Color,
specular: f32,

pub fn nil() Self {
return Material{
.color = .{ .red = 0.0, .green = 0.0, .blue = 0.0 },
.specular = 0.0,
};
}
};

test {
std.testing.refAllDecls(@This());
}
6 changes: 6 additions & 0 deletions src/Plane.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ const Ray = @import("Ray.zig").Ray;
const std = @import("std");
const HitRecord = @import("HitRecord.zig").HitRecord;
const Vec3 = @import("Vec3.zig").Vec3;
const Material = @import("Material.zig").Material;

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

normal: Vec3,
origin: Pt3,
material: Material,

pub fn hits(self: *const Self, ray: Ray) HitRecord {
const denom = self.normal.dot(ray.direction);
Expand All @@ -29,6 +31,7 @@ pub const Plane = struct {
.intersection_point = hit_point,
.normal = self.normal,
.t = t,
.material = self.material,
};
}
};
Expand All @@ -37,6 +40,7 @@ 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 },
.material = Material.nil(),
};

const ray = Ray{
Expand All @@ -59,6 +63,7 @@ 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 },
.material = Material.nil(),
};

const ray = Ray{
Expand All @@ -75,6 +80,7 @@ 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 },
.material = Material.nil(),
};

const ray = Ray{
Expand Down
21 changes: 19 additions & 2 deletions src/Sphere.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ const Ray = @import("Ray.zig").Ray;
const std = @import("std");
const HitRecord = @import("HitRecord.zig").HitRecord;
const Vec3 = @import("Vec3.zig").Vec3;
const Material = @import("Material.zig").Material;

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

center: Pt3,
radius: f32,
material: Material,

pub fn hits(self: *const Self, ray: Ray) HitRecord {
const a: f32 = ray.direction.dot(ray.direction);
Expand All @@ -27,7 +29,13 @@ pub const Sphere = struct {
if (t < 0) {
return HitRecord.nil();
} else {
return HitRecord{ .hit = true, .normal = intersection_point.subVec3(self.center), .intersection_point = intersection_point, .t = t };
return HitRecord{
.hit = true,
.normal = intersection_point.subVec3(self.center),
.intersection_point = intersection_point,
.t = t,
.material = self.material,
};
}
} else {
const t1 = (-b + @sqrt(delta)) / (2 * a);
Expand All @@ -40,7 +48,13 @@ pub const Sphere = struct {
if (t < 0) {
return HitRecord.nil();
} else {
return HitRecord{ .hit = true, .normal = intersection_point.subVec3(self.center), .intersection_point = intersection_point, .t = t };
return HitRecord{
.hit = true,
.normal = intersection_point.subVec3(self.center),
.intersection_point = intersection_point,
.t = t,
.material = self.material,
};
}
}
}
Expand All @@ -50,6 +64,7 @@ test "hit" {
const sphere = Sphere{
.center = Pt3{ .x = 0, .y = 0, .z = 0 },
.radius = 1,
.material = Material.nil(),
};
const ray = Ray{
.origin = Pt3{ .x = 0, .y = 0, .z = 2 },
Expand All @@ -70,6 +85,7 @@ test "dontHit" {
const sphere = Sphere{
.center = Pt3{ .x = 100, .y = 100, .z = 100 },
.radius = 1,
.material = Material.nil(),
};
const ray = Ray{
.origin = Pt3{ .x = 0, .y = 0, .z = 0 },
Expand All @@ -84,6 +100,7 @@ test "limit" {
const sphere = Sphere{
.center = Pt3{ .x = 0, .y = 0, .z = 0 },
.radius = 1,
.material = Material.nil(),
};
const ray = Ray{
.origin = Pt3{ .x = 0, .y = -1, .z = -1 },
Expand Down
9 changes: 9 additions & 0 deletions src/Vec3.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ pub const Vec3 = struct {
};
}

pub fn normalized(self: *const Self) Vec3 {
const vec_length = self.length();
return Vec3{
.x = self.x / vec_length,
.y = self.y / vec_length,
.z = self.z / vec_length,
};
}

pub fn mulVec3(self: *const Self, other: Self) Vec3 {
return Vec3{
.x = self.x * other.x,
Expand Down
Loading

0 comments on commit 0590808

Please sign in to comment.