implement Material as tagged union

This commit is contained in:
Fabien Freling 2021-05-27 21:19:31 +02:00
parent bc3fb54991
commit c842fcf713
2 changed files with 53 additions and 44 deletions

View file

@ -14,8 +14,6 @@ const World = @import("world.zig").World;
const Camera = @import("camera.zig").Camera;
const material = @import("material.zig");
const Material = material.Material;
const MaterialType = material.MaterialType;
const scatter = @import("material.zig").scatter;
// From: https://github.com/Nelarius/weekend-raytracer-zig/blob/master/src/main.zig
// See https://github.com/zig-lang/zig/issues/565
@ -46,7 +44,7 @@ fn rayColor(ray: Ray, world: World, rng: *Random, depth: i32) Color {
}
if (world.hit(ray, 0.001, 99999)) |hit| {
if (scatter(ray, hit, rng)) |sRay| {
if (material.scatter(ray, hit, rng)) |sRay| {
return sRay.color.mul(rayColor(sRay.ray, world, rng, depth - 1));
}
return Color{ .x = 0, .y = 0, .z = 0 };
@ -94,20 +92,24 @@ pub fn main() anyerror!void {
// World
const materialGround = Material{
.materialType = .Lambertian,
.color = Color{ .x = 0.8, .y = 0.8, .z = 0.0 },
.lambertian = material.Lambertian{
.color = Color{ .x = 0.8, .y = 0.8, .z = 0.0 }
}
};
const materialCenter = Material{
.materialType = .Lambertian,
.color = Color{ .x = 0.7, .y = 0.3, .z = 0.3 },
.lambertian = material.Lambertian{
.color = Color{ .x = 0.7, .y = 0.3, .z = 0.3 }
}
};
const materialLeft = Material{
.materialType = .Metal,
.color = Color{ .x = 0.8, .y = 0.8, .z = 0.8 },
.metal = material.Metal{
.color = Color{ .x = 0.8, .y = 0.8, .z = 0.8 }
}
};
const materialRight = Material{
.materialType = .Metal,
.color = Color{ .x = 0.8, .y = 0.6, .z = 0.2 },
.metal = material.Metal{
.color = Color{ .x = 0.8, .y = 0.6, .z = 0.2 }
}
};
const spheres = [_]Sphere{