add Lambertian material
This commit is contained in:
parent
3296ea3e81
commit
b3c47a914f
7 changed files with 85 additions and 19 deletions
|
@ -1,6 +1,7 @@
|
|||
const std = @import("std");
|
||||
const print = std.debug.print;
|
||||
const Random = std.rand.Random;
|
||||
|
||||
const sdl = @import("sdl.zig");
|
||||
const vec3 = @import("vec3.zig");
|
||||
const Vec3 = vec3.Vec3;
|
||||
|
@ -11,6 +12,10 @@ const Ray = @import("ray.zig").Ray;
|
|||
const Sphere = @import("sphere.zig").Sphere;
|
||||
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
|
||||
|
@ -41,15 +46,16 @@ fn rayColor(ray: Ray, world: World, rng: *Random, depth: i32) Color {
|
|||
}
|
||||
|
||||
if (world.hit(ray, 0.001, 99999)) |hit| {
|
||||
const target = hit.p.add(vec3.random_in_hemisphere(rng, hit.normal));
|
||||
const newRay = Ray{ .origin = hit.p, .direction = target.sub(hit.p)};
|
||||
return rayColor(newRay, world, rng, depth - 1).div(2);
|
||||
if (scatter(ray, hit, rng)) |sRay| {
|
||||
return sRay.color.mul(rayColor(sRay.ray, world, rng, depth - 1));
|
||||
}
|
||||
return Color{ .x = 0, .y = 0, .z = 0 };
|
||||
}
|
||||
const unitDirection = vec3.unitVector(ray.direction);
|
||||
const unitDirection = ray.direction.unit();
|
||||
const t = 0.5 * (unitDirection.y + 1.0);
|
||||
const white = Color{ .x = 1.0, .y = 1.0, .z = 1.0 };
|
||||
const blue = Color{ .x = 0.5, .y = 0.7, .z = 1.0 };
|
||||
return white.mul(1.0 - t).add(blue.mul(t));
|
||||
return white.mul_s(1.0 - t).add(blue.mul_s(t));
|
||||
}
|
||||
|
||||
fn setProgress(surface: *sdl.c.SDL_Surface, width: usize, height: usize, percent: f32) void {
|
||||
|
@ -87,9 +93,18 @@ pub fn main() anyerror!void {
|
|||
const maxDepth = 5;
|
||||
|
||||
// World
|
||||
const materialGround = Material{
|
||||
.materialType = MaterialType.Lambertian,
|
||||
.color = Color{ .x = 0.8, .y = 0.8, .z = 0.0 },
|
||||
};
|
||||
const materialCenter = Material{
|
||||
.materialType = MaterialType.Lambertian,
|
||||
.color = Color{ .x = 0.7, .y = 0.3, .z = 0.3 },
|
||||
};
|
||||
|
||||
const spheres = [_]Sphere{
|
||||
Sphere{ .center = Point3{ .x = 0, .y = 0, .z = -1 }, .radius = 0.5 },
|
||||
Sphere{ .center = Point3{ .x = 0, .y = -100.5, .z = -1 }, .radius = 100 },
|
||||
Sphere{ .center = Point3{ .x = 0, .y = 0, .z = -1 }, .radius = 0.5, .material = materialCenter },
|
||||
Sphere{ .center = Point3{ .x = 0, .y = -100.5, .z = -1 }, .radius = 100, .material = materialGround },
|
||||
};
|
||||
const world = World{ .spheres = spheres[0..spheres.len] };
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue