1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
const std = @import("std");
const Vec3f = @Vector(3, f64);
const Point3 = Vec3f;
const Color = Vec3f;
fn vecNorm(v: Vec3f) f64 {
return @sqrt(vecNormSquared(v));
}
fn vecNormSquared(v: Vec3f) f64 {
return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
}
fn vecDot(u: Vec3f, v: Vec3f) f64 {
return u[0] * v[0] + u[1] * v[1] + u[2] * v[2];
}
fn vecCross(u: Vec3f, v: Vec3f) Vec3f {
return Vec3f(
u[1] * v[2] - u[2] * v[1],
u[2] * v[0] - u[0] * v[2],
u[0] * v[1] - u[1] * v[0],
);
}
fn vecNormalized(v: Vec3f) Vec3f {
return v / vecNorm(v);
}
fn writeColor(out: anytype, c: Color) !void {
try out.print("{} {} {}\n", .{
@floatToInt(u8, 255.999 * c[0]),
@floatToInt(u8, 255.999 * c[1]),
@floatToInt(u8, 255.999 * c[2]),
});
}
pub fn main() !void {
// Image
const image_width = 256;
const image_height = 256;
// Render
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();
try stdout.print("P3\n{} {}\n255\n", .{ image_width, image_height });
var j: i32 = image_height - 1;
while (j >= 0) {
std.debug.print("\rScanlines remaining: {}", .{j});
var i: i32 = 0;
while (i < image_width) {
try writeColor(stdout, Color{
@intToFloat(f64, i) / (image_width - 1),
@intToFloat(f64, j) / (image_height - 1),
0.25,
});
i += 1;
}
j -= 1;
}
try bw.flush();
}
|