Compare commits

...

4 Commits

3 changed files with 52 additions and 1 deletions

10
zigrtiow/src/color.zig Normal file
View File

@ -0,0 +1,10 @@
const std = @import("std");
const Color = @import("./vec.zig").Color;
pub fn write_color(c: Color) anyerror!void {
var ir = @floatToInt(u8, 255.999 * c.x());
var ig = @floatToInt(u8, 255.999 * c.y());
var ib = @floatToInt(u8, 255.999 * c.z());
const stdout = std.io.getStdOut();
try stdout.writer().print("{d} {d} {d}\n", .{ ir, ig, ib });
}

View File

@ -1,7 +1,29 @@
const std = @import("std");
const info = std.log.info;
const vec = @import("./vec.zig");
const Vec3 = vec.Vec3;
const Color = vec.Color;
const write_color = @import("./color.zig").write_color;
pub fn main() anyerror!void {
std.log.info("All your codebase are belong to us.", .{});
const image_width: isize = 256;
const image_height: isize = 256;
const stdout = std.io.getStdOut();
info("Writing image {d}x{d}\n", .{ image_width, image_height });
try stdout.writer().print("P3\n{d} {d}\n255\n", .{ image_width, image_height });
var j: isize = image_height - 1;
while (j >= 0) : (j -= 1) {
info("Scanlines remaining: {d}", .{j});
var i: isize = 0;
while (i < image_width) : (i += 1) {
const pixel_color = Color.init(@intToFloat(f32, i) / @intToFloat(f32, image_width - 1), @intToFloat(f32, j) / @intToFloat(f32, image_height - 1), 0.25);
try write_color(pixel_color);
}
}
}
test "basic test" {

19
zigrtiow/src/vec.zig Normal file
View File

@ -0,0 +1,19 @@
pub const Vec3 = struct {
v: @Vector(3, f32),
pub fn init(v0: f32, v1: f32, v2: f32) Vec3 {
return Vec3{
.v = .{ v0, v1, v2 },
};
}
pub fn x(vec: Vec3) f32 {
return vec.v[0];
}
pub fn y(vec: Vec3) f32 {
return vec.v[1];
}
pub fn z(vec: Vec3) f32 {
return vec.v[2];
}
};
pub const Color = Vec3;
pub const Point3 = Vec3;