add Metal material
This commit is contained in:
parent
b3c47a914f
commit
bc3fb54991
3 changed files with 45 additions and 11 deletions
|
@ -8,6 +8,7 @@ const HitRecord = @import("hittable.zig").HitRecord;
|
|||
|
||||
pub const MaterialType = enum {
|
||||
Lambertian,
|
||||
Metal,
|
||||
};
|
||||
|
||||
pub const Material = struct {
|
||||
|
@ -21,15 +22,34 @@ pub const ScatteredRay = struct {
|
|||
};
|
||||
|
||||
pub fn scatter(ray: Ray, hit: HitRecord, rng: *Random) ?ScatteredRay {
|
||||
var scatterDirection = hit.normal.add(vec3.random_unit_vector(rng));
|
||||
const scatteredDirection = switch (hit.material.materialType) {
|
||||
|
||||
// Catch degenerate scatter direction
|
||||
if (scatterDirection.nearZero()) {
|
||||
scatterDirection = hit.normal;
|
||||
.Lambertian => blk: {
|
||||
var scatterDirection = hit.normal.add(vec3.random_unit_vector(rng));
|
||||
// Catch degenerate scatter direction
|
||||
if (scatterDirection.nearZero()) {
|
||||
scatterDirection = hit.normal;
|
||||
}
|
||||
break :blk scatterDirection;
|
||||
},
|
||||
|
||||
.Metal => blk: {
|
||||
const reflected = ray.direction.unit().reflect(hit.normal);
|
||||
const scattered = Ray{ .origin = hit.p, .direction = reflected };
|
||||
if (scattered.direction.dot(hit.normal) <= 0) {
|
||||
break :blk null;
|
||||
}
|
||||
break :blk scattered.direction;
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
if (scatteredDirection) |dir| {
|
||||
return ScatteredRay{
|
||||
.ray = Ray{ .origin = hit.p, .direction = dir},
|
||||
.color = hit.material.color,
|
||||
};
|
||||
}
|
||||
|
||||
return ScatteredRay{
|
||||
.ray = Ray{ .origin = hit.p, .direction = scatterDirection},
|
||||
.color = hit.material.color,
|
||||
};
|
||||
return null;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue