zigrtiow: shoot child rays for diffuse shading.
This commit is contained in:
parent
f4d3129d5a
commit
94b0f8355e
@ -15,18 +15,18 @@ const Point3 = vec.Point3;
|
|||||||
const Ray = ray.Ray;
|
const Ray = ray.Ray;
|
||||||
const Sphere = sphere.Sphere;
|
const Sphere = sphere.Sphere;
|
||||||
const Vec3 = vec.Vec3;
|
const Vec3 = vec.Vec3;
|
||||||
|
const random_in_unit_sphere = vec.random_in_unit_sphere;
|
||||||
const info = std.log.info;
|
const info = std.log.info;
|
||||||
const write_color = color.write_color;
|
const write_color = color.write_color;
|
||||||
|
|
||||||
fn ray_color(r: Ray, world: Hittable) Color {
|
fn ray_color(r: Ray, world: Hittable, depth: isize) Color {
|
||||||
|
// If we've exceeded the ray bounce limit, no more light is gathered.
|
||||||
|
if (depth <= 0) return Color.init(0, 0, 0);
|
||||||
|
|
||||||
var hit = world.hit(r, 0, std.math.inf(f32));
|
var hit = world.hit(r, 0, std.math.inf(f32));
|
||||||
if (hit) |rec| {
|
if (hit) |rec| {
|
||||||
const n = rec.normal;
|
const target = rec.p.add(rec.normal.add(random_in_unit_sphere()));
|
||||||
return Color.init(
|
return ray_color(Ray.init(rec.p, target.sub(rec.p)), world, depth - 1);
|
||||||
n.x() + 1,
|
|
||||||
n.y() + 1,
|
|
||||||
n.z() + 1,
|
|
||||||
).scale(0.5);
|
|
||||||
}
|
}
|
||||||
var unit_direction = r.direction().unit();
|
var unit_direction = r.direction().unit();
|
||||||
const t = 0.5 * (unit_direction.y() + 1);
|
const t = 0.5 * (unit_direction.y() + 1);
|
||||||
@ -39,11 +39,13 @@ pub fn main() anyerror!void {
|
|||||||
const image_width = 400;
|
const image_width = 400;
|
||||||
const image_height = @floatToInt(isize, @intToFloat(f32, image_width) / aspect_ratio);
|
const image_height = @floatToInt(isize, @intToFloat(f32, image_width) / aspect_ratio);
|
||||||
const samples_per_pixel = 100;
|
const samples_per_pixel = 100;
|
||||||
|
const max_depth = 50;
|
||||||
|
|
||||||
// World
|
// World
|
||||||
var world = HittableList.init();
|
var tmp_world = HittableList.init();
|
||||||
try world.add(Hittable{ .sphere = Sphere.init(Point3.init(0, 0, -1), 0.5) });
|
try tmp_world.add(Hittable{ .sphere = Sphere.init(Point3.init(0, 0, -1), 0.5) });
|
||||||
try world.add(Hittable{ .sphere = Sphere.init(Point3.init(0, -100.5, -1), 100) });
|
try tmp_world.add(Hittable{ .sphere = Sphere.init(Point3.init(0, -100.5, -1), 100) });
|
||||||
|
const world = Hittable{ .hittable_list = tmp_world };
|
||||||
|
|
||||||
// Camera
|
// Camera
|
||||||
const cam = Camera.init();
|
const cam = Camera.init();
|
||||||
@ -58,7 +60,7 @@ pub fn main() anyerror!void {
|
|||||||
const rand = prng.random();
|
const rand = prng.random();
|
||||||
var j: isize = image_height - 1;
|
var j: isize = image_height - 1;
|
||||||
while (j >= 0) : (j -= 1) {
|
while (j >= 0) : (j -= 1) {
|
||||||
//info("Scanlines remaining: {d}", .{j});
|
info("Scanlines remaining: {d}", .{j});
|
||||||
|
|
||||||
var i: isize = 0;
|
var i: isize = 0;
|
||||||
while (i < image_width) : (i += 1) {
|
while (i < image_width) : (i += 1) {
|
||||||
@ -68,8 +70,7 @@ pub fn main() anyerror!void {
|
|||||||
const u = (@intToFloat(f32, i) + rand.float(f32)) / @intToFloat(f32, image_width - 1);
|
const u = (@intToFloat(f32, i) + rand.float(f32)) / @intToFloat(f32, image_width - 1);
|
||||||
const v = (@intToFloat(f32, j) + rand.float(f32)) / @intToFloat(f32, image_height - 1);
|
const v = (@intToFloat(f32, j) + rand.float(f32)) / @intToFloat(f32, image_height - 1);
|
||||||
const r = cam.get_ray(u, v);
|
const r = cam.get_ray(u, v);
|
||||||
//Ray.init(origin, lower_left_corner.add(horizontal.scale(u)).add(vertical.scale(v)).sub(origin));
|
pixel_color = pixel_color.add(ray_color(r, world, max_depth));
|
||||||
pixel_color = pixel_color.add(ray_color(r, Hittable{ .hittable_list = world }));
|
|
||||||
}
|
}
|
||||||
try write_color(pixel_color, samples_per_pixel);
|
try write_color(pixel_color, samples_per_pixel);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,13 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
const info = std.log.info;
|
||||||
|
|
||||||
|
var prng = std.rand.DefaultPrng.init(0);
|
||||||
|
const rand = prng.random();
|
||||||
|
|
||||||
pub const Vec3 = struct {
|
pub const Vec3 = struct {
|
||||||
v: @Vector(3, f32),
|
v: @Vector(3, f32),
|
||||||
|
|
||||||
pub fn init(v0: f32, v1: f32, v2: f32) Vec3 {
|
pub fn init(v0: f32, v1: f32, v2: f32) Vec3 {
|
||||||
return Vec3{
|
return Vec3{
|
||||||
.v = .{ v0, v1, v2 },
|
.v = .{ v0, v1, v2 },
|
||||||
@ -36,6 +44,23 @@ pub const Vec3 = struct {
|
|||||||
const t = u.v * v.v;
|
const t = u.v * v.v;
|
||||||
return t[0] + t[1] + t[2];
|
return t[0] + t[1] + t[2];
|
||||||
}
|
}
|
||||||
|
/// Return value in unit cube from -1, 1
|
||||||
|
pub fn random() Vec3 {
|
||||||
|
return Vec3.init(
|
||||||
|
rand.float(f32) * 2 - 0.5,
|
||||||
|
rand.float(f32) * 2 - 0.5,
|
||||||
|
rand.float(f32) * 2 - 0.5,
|
||||||
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub fn random_in_unit_sphere() Vec3 {
|
||||||
|
while (true) {
|
||||||
|
const p = Vec3.random();
|
||||||
|
if (p.length_squared() > 1) continue;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub const Color = Vec3;
|
pub const Color = Vec3;
|
||||||
pub const Point3 = Vec3;
|
pub const Point3 = Vec3;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user