aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main.zig
blob: 7373cb64dadaf56983a0694eecbf863626bef99a (plain)
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
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);
}

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) {
            const r = @intToFloat(f64, i) / (image_width - 1);
            const g = @intToFloat(f64, j) / (image_height - 1);
            const b = 0.25;

            const ir = @floatToInt(u8, 255.999 * r);
            const ig = @floatToInt(u8, 255.999 * g);
            const ib = @floatToInt(u8, 255.999 * b);

            try stdout.print("{} {} {}\n", .{ ir, ig, ib });
            i += 1;
        }
        j -= 1;
    }

    try bw.flush();
}