zigrtiow: add fuzzy metal reflections.

This commit is contained in:
Bill Thiede 2022-08-06 08:17:26 -07:00
parent 6b4be0ed1e
commit ac73d13fb0
2 changed files with 6 additions and 3 deletions

View File

@ -51,8 +51,8 @@ pub fn main() anyerror!void {
const material_ground = Material{ .labertian = Labertian{ .albedo = Color.init(0.8, 0.8, 0.0) } }; const material_ground = Material{ .labertian = Labertian{ .albedo = Color.init(0.8, 0.8, 0.0) } };
const material_center = Material{ .labertian = Labertian{ .albedo = Color.init(0.7, 0.3, 0.3) } }; const material_center = Material{ .labertian = Labertian{ .albedo = Color.init(0.7, 0.3, 0.3) } };
const material_left = Material{ .metal = Metal{ .albedo = Color.init(0.8, 0.8, 0.8) } }; const material_left = Material{ .metal = Metal{ .albedo = Color.init(0.8, 0.8, 0.8), .fuzz = 0.3 } };
const material_right = Material{ .metal = Metal{ .albedo = Color.init(0.8, 0.6, 0.2) } }; const material_right = Material{ .metal = Metal{ .albedo = Color.init(0.8, 0.6, 0.2), .fuzz = 1.0 } };
try tmp_world.add(Hittable{ .sphere = Sphere.init(Point3.init(0, -100.5, -1), 100, material_ground) }); try tmp_world.add(Hittable{ .sphere = Sphere.init(Point3.init(0, -100.5, -1), 100, material_ground) });
try tmp_world.add(Hittable{ .sphere = Sphere.init(Point3.init(0, 0, -1), 0.5, material_center) }); try tmp_world.add(Hittable{ .sphere = Sphere.init(Point3.init(0, 0, -1), 0.5, material_center) });

View File

@ -5,6 +5,7 @@ const HitRecord = @import("./hittable.zig").HitRecord;
const Vec3 = vec.Vec3; const Vec3 = vec.Vec3;
const Color = vec.Color; const Color = vec.Color;
const random_unit_vector = vec.random_unit_vector; const random_unit_vector = vec.random_unit_vector;
const random_in_unit_sphere = vec.random_in_unit_sphere;
pub const Material = union(enum) { pub const Material = union(enum) {
labertian: Labertian, labertian: Labertian,
@ -37,9 +38,11 @@ pub const Labertian = struct {
pub const Metal = struct { pub const Metal = struct {
albedo: Vec3, albedo: Vec3,
fuzz: f32,
pub fn scatter(metal: Metal, r_in: Ray, rec: HitRecord) ?ScatterRec { pub fn scatter(metal: Metal, r_in: Ray, rec: HitRecord) ?ScatterRec {
const reflected = reflect(r_in.direction().unit(), rec.normal); const reflected = reflect(r_in.direction().unit(), rec.normal);
const scattered = Ray.init(rec.p, reflected); const scattered = Ray.init(rec.p, reflected.add(random_in_unit_sphere().scale(metal.fuzz)));
const attenuation = metal.albedo; const attenuation = metal.albedo;
if (scattered.direction().dot(rec.normal) > 0) { if (scattered.direction().dot(rec.normal) > 0) {
return ScatterRec{ return ScatterRec{