add Lambertian material
This commit is contained in:
parent
3296ea3e81
commit
b3c47a914f
7 changed files with 85 additions and 19 deletions
35
in_one_weekend/src/material.zig
Normal file
35
in_one_weekend/src/material.zig
Normal file
|
@ -0,0 +1,35 @@
|
|||
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,
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue