raytracing/in_one_weekend/src/main.zig

285 lines
9.4 KiB
Zig
Raw Normal View History

2020-08-29 23:50:38 +02:00
const std = @import("std");
2020-09-05 17:25:27 +02:00
const print = std.debug.print;
2021-05-10 23:30:34 +02:00
const Random = std.rand.Random;
2021-05-11 22:15:09 +02:00
2020-09-05 17:25:27 +02:00
const sdl = @import("sdl.zig");
const vec3 = @import("vec3.zig");
const Vec3 = vec3.Vec3;
const Point3 = vec3.Point3;
2021-04-26 22:51:49 +02:00
const color = @import("color.zig");
const Color = color.Color;
2020-09-05 17:25:27 +02:00
const Ray = @import("ray.zig").Ray;
2021-04-24 19:24:28 +02:00
const Sphere = @import("sphere.zig").Sphere;
const World = @import("world.zig").World;
2021-04-26 22:51:49 +02:00
const Camera = @import("camera.zig").Camera;
2021-05-11 22:15:09 +02:00
const material = @import("material.zig");
const Material = material.Material;
2020-08-30 20:05:00 +02:00
// From: https://github.com/Nelarius/weekend-raytracer-zig/blob/master/src/main.zig
// See https://github.com/zig-lang/zig/issues/565
// SDL_video.h:#define SDL_WINDOWPOS_UNDEFINED SDL_WINDOWPOS_UNDEFINED_DISPLAY(0)
// SDL_video.h:#define SDL_WINDOWPOS_UNDEFINED_DISPLAY(X) (SDL_WINDOWPOS_UNDEFINED_MASK|(X))
// SDL_video.h:#define SDL_WINDOWPOS_UNDEFINED_MASK 0x1FFF0000u
2020-09-05 17:25:27 +02:00
const SDL_WINDOWPOS_UNDEFINED = @bitCast(c_int, sdl.c.SDL_WINDOWPOS_UNDEFINED_MASK);
2020-08-30 20:05:00 +02:00
const fps = 60;
2020-08-29 23:50:38 +02:00
2021-04-24 17:45:08 +02:00
fn hitSphere(center: Point3, radius: f32, ray: Ray) f32 {
2020-09-18 13:55:48 +02:00
const ssr = ray.origin.sub(center); // Sphere-space ray, (A - C) in book
2021-04-24 17:59:17 +02:00
const a = ray.direction.length_squared();
const half_b = Vec3.dot(ssr, ray.direction);
const c = ssr.length_squared() - (radius * radius);
const discriminant = half_b * half_b - a * c;
2021-04-24 17:45:08 +02:00
if (discriminant < 0) {
return -1;
} else {
2021-04-24 17:59:17 +02:00
return (-half_b - std.math.sqrt(discriminant)) / a;
2021-04-24 17:45:08 +02:00
}
2020-09-18 13:55:48 +02:00
}
2021-05-10 23:30:34 +02:00
fn rayColor(ray: Ray, world: World, rng: *Random, depth: i32) Color {
// If we've exceeded the ray bounce limit, no more light is gathered.
if (depth <= 0) {
return Color{ .x = 0, .y = 0, .z = 0 };
}
if (world.hit(ray, 0.001, 99999)) |hit| {
2021-05-27 21:19:31 +02:00
if (material.scatter(ray, hit, rng)) |sRay| {
2021-05-11 22:15:09 +02:00
return sRay.color.mul(rayColor(sRay.ray, world, rng, depth - 1));
}
return Color{ .x = 0, .y = 0, .z = 0 };
2020-09-18 13:55:48 +02:00
}
2021-05-11 22:15:09 +02:00
const unitDirection = ray.direction.unit();
2021-04-24 19:24:28 +02:00
const t = 0.5 * (unitDirection.y + 1.0);
2020-09-05 17:25:27 +02:00
const white = Color{ .x = 1.0, .y = 1.0, .z = 1.0 };
const blue = Color{ .x = 0.5, .y = 0.7, .z = 1.0 };
2021-05-11 22:15:09 +02:00
return white.mul_s(1.0 - t).add(blue.mul_s(t));
2020-09-05 17:25:27 +02:00
}
2021-04-27 08:30:24 +02:00
fn setProgress(surface: *sdl.c.SDL_Surface, width: usize, height: usize, percent: f32) void {
if (percent == 1.0) {
return;
}
const progressBarHeight = 3;
const progressWidth = @floatToInt(usize, @intToFloat(f32, width) * percent);
const progressColor = Color{ .x = 1.0 };
var j: usize = 0;
while (j < progressBarHeight) {
var i: usize = 0;
while (i < progressWidth) {
sdl.setSurfacePixel(surface, i, j, progressColor);
i += 1;
}
j += 1;
}
}
2021-05-28 19:51:05 +02:00
fn setupWorld(world: *World, rng: *Random) !void {
try world.spheres.append(Sphere{
.center = Point3{ .x = 0, .y = -1000, .z = 0 },
.radius = 1000,
.material = Material{ .lambertian = material.Lambertian{
.color = Color{ .x = 0.5, .y = 0.5, .z = 0.5 }
}}
});
var a: f32 = -11;
while (a < 11) : (a += 1) {
var b: f32 = -11;
while (b < 11) : (b += 1) {
const chooseMat = rng.float(f32);
const center = Point3{
.x = a + 0.9 * rng.float(f32),
.y = 0.2,
.z = b + 0.9 * rng.float(f32),
};
if (center.sub(Point3{ .x = 4, .y = 0.2 }).length() > 0.9) {
if (chooseMat < 0.8) {
// diffuse
try world.spheres.append(Sphere{
.center = center,
.radius = 0.2,
.material = Material{ .lambertian = material.Lambertian{
.color = Color{
.x = rng.float(f32) * rng.float(f32),
.y = rng.float(f32) * rng.float(f32),
.z = rng.float(f32) * rng.float(f32),
}
}},
});
} else if (chooseMat < 0.95) {
// metal
try world.spheres.append(Sphere{
.center = center,
.radius = 0.2,
.material = Material{ .metal = material.Metal{
.color = Color{
.x = 0.5 + rng.float(f32) / 2,
.y = 0.5 + rng.float(f32) / 2,
.z = 0.5 + rng.float(f32) / 2,
},
.fuzz = rng.float(f32) / 2,
}},
});
} else {
// glass
try world.spheres.append(Sphere{
.center = center,
.radius = 0.2,
.material = Material{ .dielectric = material.Dielectric{
.refraction_index = 1.5,
}},
});
}
}
}
}
try world.spheres.append(Sphere{
.center = Point3{ .x = 0, .y = 1, .z = 0 },
.radius = 1,
.material = Material{ .dielectric = material.Dielectric{
.refraction_index = 1.5
}},
});
try world.spheres.append(Sphere{
.center = Point3{ .x = -4, .y = 1, .z = 0 },
.radius = 1,
.material = Material{ .lambertian = material.Lambertian{
.color = Color{ .x = 0.4, .y = 0.2, .z = 0.1 }
}},
});
try world.spheres.append(Sphere{
.center = Point3{ .x = 4, .y = 1, .z = 0 },
.radius = 1,
.material = Material{ .metal = material.Metal{
.color = Color{ .x = 0.7, .y = 0.6, .z = 0.5 },
.fuzz = 0.0
}},
});
}
2020-08-29 23:50:38 +02:00
pub fn main() anyerror!void {
2020-09-05 17:25:27 +02:00
if (sdl.c.SDL_Init(sdl.c.SDL_INIT_VIDEO) != 0) {
std.log.err("Unable to initialize SDL: {}", .{sdl.c.SDL_GetError()});
2020-08-30 20:05:00 +02:00
return error.SDLInitializationFailed;
}
2020-09-05 17:25:27 +02:00
defer sdl.c.SDL_Quit();
2021-05-28 19:51:05 +02:00
var prng = std.rand.DefaultPrng.init(42);
2020-09-05 17:25:27 +02:00
// Image
2021-05-28 19:59:32 +02:00
const aspectRatio: f32 = 3.0 / 2.0;
2021-04-24 17:45:08 +02:00
const imageWidth = 600;
2020-09-05 17:25:27 +02:00
const imageHeight = @floatToInt(usize, imageWidth / aspectRatio);
2021-05-28 19:59:32 +02:00
const samplesPerPixel = 128;
const maxDepth = 16;
2020-09-05 17:25:27 +02:00
2021-04-24 19:24:28 +02:00
// World
2021-05-28 19:51:05 +02:00
var world = World.init();
defer world.deinit();
try setupWorld(&world, &prng.random);
2021-04-24 19:24:28 +02:00
2020-09-05 17:25:27 +02:00
// Camera
2021-05-28 19:51:05 +02:00
const lookFrom = Point3{ .x = 13, .y = 2, .z = 3 };
const lookAt = Point3{ .x = 0, .y = 0, .z = 0 };
2021-05-27 23:02:42 +02:00
const vup = Vec3{ .x = 0, .y = 1, .z = 0 };
2021-05-28 19:51:05 +02:00
const distToFocus = 10;
const aperture = 0.1;
2021-05-27 22:44:29 +02:00
const camera = Camera.init(
2021-05-27 23:02:42 +02:00
lookFrom,
lookAt,
vup,
2021-05-27 22:44:29 +02:00
20.0,
2021-05-27 23:02:42 +02:00
aspectRatio,
aperture,
distToFocus
2021-05-27 22:44:29 +02:00
);
2020-08-30 20:05:00 +02:00
2020-09-05 17:25:27 +02:00
const window = sdl.c.SDL_CreateWindow(
"Raytracing in One Weekend",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
imageWidth,
imageHeight,
sdl.c.SDL_WINDOW_OPENGL
) orelse {
std.log.err("Unable to create window: {}", .{sdl.c.SDL_GetError()});
2020-08-30 20:05:00 +02:00
return error.SDLInitializationFailed;
};
2020-09-05 17:25:27 +02:00
const surface = sdl.c.SDL_GetWindowSurface(window) orelse {
std.log.err("Unable to get window surface: {}", .{sdl.c.SDL_GetError()});
2020-08-30 20:05:00 +02:00
return error.SDLInitializationFailed;
};
var pixelAccu: [imageWidth * imageHeight]Color = undefined;
for (pixelAccu) |*pixel| {
pixel.* = Color{};
}
2021-04-26 22:51:49 +02:00
2020-09-05 17:25:27 +02:00
if (sdl.c.SDL_UpdateWindowSurface(window) != 0) {
std.log.err("Error updating window surface: {}", .{sdl.c.SDL_GetError()});
2020-08-30 20:05:00 +02:00
return error.SDLUpdateWindowFailed;
}
var running = true;
2021-04-27 08:30:24 +02:00
var k: usize = 0;
2020-08-30 20:05:00 +02:00
while (running) {
2020-09-05 17:25:27 +02:00
var event: sdl.c.SDL_Event = undefined;
while (sdl.c.SDL_PollEvent(&event) != 0) {
2020-08-30 20:05:00 +02:00
switch (event.@"type") {
2020-09-05 17:25:27 +02:00
sdl.c.SDL_QUIT => {
2020-08-30 20:05:00 +02:00
running = false;
},
else => {},
}
}
2021-04-27 08:30:24 +02:00
2021-04-27 09:05:46 +02:00
if (k < samplesPerPixel) {
// Render
_ = sdl.c.SDL_LockSurface(surface);
var j: usize = 0;
while (j < imageHeight) {
var i: usize = 0;
while (i < imageWidth) {
var imageIndex = i + j * imageWidth;
var pixelColor = &pixelAccu[imageIndex];
2021-04-27 08:30:24 +02:00
2021-04-27 09:05:46 +02:00
const u = (@intToFloat(f32, i) + prng.random.float(f32)) / @intToFloat(f32, (imageWidth - 1));
const v = (@intToFloat(f32, j) + prng.random.float(f32)) / @intToFloat(f32, (imageHeight - 1));
2021-05-27 23:02:42 +02:00
const r = camera.getRay(u, v, &prng.random);
2021-05-10 23:30:34 +02:00
const sample = rayColor(r, world, &prng.random, maxDepth);
2021-04-27 09:05:46 +02:00
pixelColor.* = pixelColor.*.add(sample);
2021-04-27 08:30:24 +02:00
2021-04-27 09:05:46 +02:00
const averageColor = color.averageColor(pixelColor, @intCast(i32, k));
// SDL coordinate system is flipped compared to the raytracer
sdl.setSurfacePixel(surface, i, imageHeight - 1 - j, averageColor);
i += 1;
}
j += 1;
2021-04-27 08:30:24 +02:00
}
2021-04-27 09:05:46 +02:00
setProgress(surface, imageWidth, imageHeight, @intToFloat(f32, k + 1) / @intToFloat(f32, samplesPerPixel));
sdl.c.SDL_UnlockSurface(surface);
_ = sdl.c.SDL_UpdateWindowSurface(window);
k += 1;
} else {
2021-04-27 08:30:24 +02:00
sdl.c.SDL_Delay(1000 / fps);
2021-04-27 09:05:46 +02:00
2021-04-27 08:30:24 +02:00
}
2020-08-30 20:05:00 +02:00
}
2020-08-29 23:50:38 +02:00
}