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;
|
|
|
|
const sdl = @import("sdl.zig");
|
|
|
|
const vec3 = @import("vec3.zig");
|
|
|
|
const Vec3 = vec3.Vec3;
|
|
|
|
const Color = vec3.Color;
|
|
|
|
const Point3 = vec3.Point3;
|
|
|
|
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;
|
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-04-24 19:24:28 +02:00
|
|
|
fn rayColor(ray: Ray, world: World) Color {
|
|
|
|
if (world.hit(ray, 0, 99999)) |hit| {
|
|
|
|
return hit.normal.add(Vec3{ .x = 1, .y = 1, .z = 1}).div(2);
|
2020-09-18 13:55:48 +02:00
|
|
|
}
|
2020-09-05 17:25:27 +02:00
|
|
|
const unitDirection = vec3.unitVector(ray.direction);
|
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 };
|
|
|
|
return white.mul(1.0 - t).add(blue.mul(t));
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
// Image
|
|
|
|
const aspectRatio: f32 = 16.0 / 9.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-04-24 19:24:28 +02:00
|
|
|
// World
|
|
|
|
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 },
|
|
|
|
};
|
|
|
|
const world = World{ .spheres = spheres[0..spheres.len] };
|
|
|
|
|
2020-09-05 17:25:27 +02:00
|
|
|
// Camera
|
|
|
|
const viewportHeight = 2.0;
|
2021-04-24 17:53:11 +02:00
|
|
|
const viewportWidth = viewportHeight * aspectRatio;
|
2020-09-05 17:25:27 +02:00
|
|
|
const focalLength = 1.0;
|
|
|
|
|
|
|
|
const origin = Vec3{};
|
|
|
|
const horizontal = Vec3{ .x = viewportWidth };
|
|
|
|
const vertical = Vec3{ .y = viewportHeight };
|
|
|
|
const lowerLeftCorner = origin.sub(horizontal.div(2)).sub(vertical.div(2)).sub(Vec3{ .z = focalLength });
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
2020-09-05 17:25:27 +02:00
|
|
|
_ = sdl.c.SDL_LockSurface(surface);
|
2020-08-30 20:05:00 +02:00
|
|
|
|
2020-09-05 17:25:27 +02:00
|
|
|
var j: usize = 0;
|
|
|
|
while (j < imageHeight) {
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < imageWidth) {
|
|
|
|
const u = @intToFloat(f32, i) / @intToFloat(f32, (imageWidth - 1));
|
|
|
|
// SDL coordinate system is flipped compared to the raytracer
|
|
|
|
const v = @intToFloat(f32, (imageHeight - 1) - j) / @intToFloat(f32, (imageHeight - 1));
|
|
|
|
const r = Ray{
|
|
|
|
.origin = origin,
|
2021-04-24 17:45:08 +02:00
|
|
|
.direction = lowerLeftCorner.add(horizontal.mul(u)).add(vertical.mul(v)).sub(origin),
|
2020-09-05 17:25:27 +02:00
|
|
|
};
|
2021-04-24 19:24:28 +02:00
|
|
|
const pixelColor = rayColor(r, world);
|
2020-09-05 17:25:27 +02:00
|
|
|
sdl.setSurfacePixel(surface, i, j, pixelColor);
|
|
|
|
i += 1;
|
2020-08-30 20:05:00 +02:00
|
|
|
}
|
2020-09-05 17:25:27 +02:00
|
|
|
j += 1;
|
2020-08-30 20:05:00 +02:00
|
|
|
}
|
2020-09-05 17:25:27 +02:00
|
|
|
defer sdl.c.SDL_UnlockSurface(surface);
|
2020-08-30 20:05:00 +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;
|
|
|
|
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 => {},
|
|
|
|
}
|
|
|
|
}
|
2020-09-05 17:25:27 +02:00
|
|
|
sdl.c.SDL_Delay(1000 / fps);
|
2020-08-30 20:05:00 +02:00
|
|
|
}
|
2020-08-29 23:50:38 +02:00
|
|
|
}
|