raytracing/in_one_weekend/src/main.zig

215 lines
7.2 KiB
Zig

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;
const Point3 = vec3.Point3;
const color = @import("color.zig");
const Color = color.Color;
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;
// 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
const SDL_WINDOWPOS_UNDEFINED = @bitCast(c_int, sdl.c.SDL_WINDOWPOS_UNDEFINED_MASK);
const fps = 60;
fn hitSphere(center: Point3, radius: f32, ray: Ray) f32 {
const ssr = ray.origin.sub(center); // Sphere-space ray, (A - C) in book
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;
if (discriminant < 0) {
return -1;
} else {
return (-half_b - std.math.sqrt(discriminant)) / a;
}
}
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| {
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 };
}
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_s(1.0 - t).add(blue.mul_s(t));
}
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;
}
}
pub fn main() anyerror!void {
if (sdl.c.SDL_Init(sdl.c.SDL_INIT_VIDEO) != 0) {
std.log.err("Unable to initialize SDL: {}", .{sdl.c.SDL_GetError()});
return error.SDLInitializationFailed;
}
defer sdl.c.SDL_Quit();
// Image
const aspectRatio: f32 = 16.0 / 9.0;
const imageWidth = 600;
const imageHeight = @floatToInt(usize, imageWidth / aspectRatio);
const samplesPerPixel = 100;
const maxDepth = 5;
// World
const materialGround = Material{
.lambertian = material.Lambertian{
.color = Color{ .x = 0.8, .y = 0.8, .z = 0.0 },
}
};
const materialCenter = Material{
.lambertian = material.Lambertian{
.color = Color{ .x = 0.1, .y = 0.2, .z = 0.5 },
}
};
const materialLeft = Material{
.dielectric = material.Dielectric{
.refraction_index = 1.5,
}
};
const materialRight = Material{
.metal = material.Metal{
.color = Color{ .x = 0.8, .y = 0.6, .z = 0.2 },
.fuzz = 1.0,
}
};
const spheres = [_]Sphere{
Sphere{ .center = Point3{ .x = 0, .y = -100.5, .z = -1 }, .radius = 100, .material = materialGround },
Sphere{ .center = Point3{ .x = 0, .y = 0, .z = -1 }, .radius = 0.5, .material = materialCenter },
Sphere{ .center = Point3{ .x = -1, .y = 0, .z = -1 }, .radius = 0.5, .material = materialLeft },
Sphere{ .center = Point3{ .x = -1, .y = 0, .z = -1 }, .radius = -0.45, .material = materialLeft },
Sphere{ .center = Point3{ .x = 1, .y = 0, .z = -1 }, .radius = 0.5, .material = materialRight },
};
const world = World{ .spheres = spheres[0..spheres.len] };
// Camera
const camera = Camera.init(
Point3{ .x = -2, .y = 2, .z = 1 },
Point3{ .x = 0, .y = 0, .z = -1 },
Vec3{ .x = 0, .y = 1, .z = 0 },
20.0,
aspectRatio
);
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()});
return error.SDLInitializationFailed;
};
const surface = sdl.c.SDL_GetWindowSurface(window) orelse {
std.log.err("Unable to get window surface: {}", .{sdl.c.SDL_GetError()});
return error.SDLInitializationFailed;
};
var prng = std.rand.DefaultPrng.init(42);
var pixelAccu: [imageWidth * imageHeight]Color = undefined;
for (pixelAccu) |*pixel| {
pixel.* = Color{};
}
if (sdl.c.SDL_UpdateWindowSurface(window) != 0) {
std.log.err("Error updating window surface: {}", .{sdl.c.SDL_GetError()});
return error.SDLUpdateWindowFailed;
}
var running = true;
var k: usize = 0;
while (running) {
var event: sdl.c.SDL_Event = undefined;
while (sdl.c.SDL_PollEvent(&event) != 0) {
switch (event.@"type") {
sdl.c.SDL_QUIT => {
running = false;
},
else => {},
}
}
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];
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));
const r = camera.getRay(u, v);
const sample = rayColor(r, world, &prng.random, maxDepth);
pixelColor.* = pixelColor.*.add(sample);
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;
}
setProgress(surface, imageWidth, imageHeight, @intToFloat(f32, k + 1) / @intToFloat(f32, samplesPerPixel));
sdl.c.SDL_UnlockSurface(surface);
_ = sdl.c.SDL_UpdateWindowSurface(window);
k += 1;
} else {
sdl.c.SDL_Delay(1000 / fps);
}
}
}