35 lines
844 B
Zig
35 lines
844 B
Zig
const std = @import("std");
|
|
const Random = std.rand.Random;
|
|
|
|
const vec3 = @import("vec3.zig");
|
|
const Color = @import("color.zig").Color;
|
|
const Ray = @import("ray.zig").Ray;
|
|
const HitRecord = @import("hittable.zig").HitRecord;
|
|
|
|
pub const MaterialType = enum {
|
|
Lambertian,
|
|
};
|
|
|
|
pub const Material = struct {
|
|
materialType: MaterialType,
|
|
color: Color,
|
|
};
|
|
|
|
pub const ScatteredRay = struct {
|
|
ray: Ray,
|
|
color: Color,
|
|
};
|
|
|
|
pub fn scatter(ray: Ray, hit: HitRecord, rng: *Random) ?ScatteredRay {
|
|
var scatterDirection = hit.normal.add(vec3.random_unit_vector(rng));
|
|
|
|
// Catch degenerate scatter direction
|
|
if (scatterDirection.nearZero()) {
|
|
scatterDirection = hit.normal;
|
|
}
|
|
|
|
return ScatteredRay{
|
|
.ray = Ray{ .origin = hit.p, .direction = scatterDirection},
|
|
.color = hit.material.color,
|
|
};
|
|
} |