Use stub Vec3 / Color to implement gradient image.
This commit is contained in:
parent
386daf5876
commit
f0da916a22
10
zigrtiow/src/color.zig
Normal file
10
zigrtiow/src/color.zig
Normal 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 });
|
||||
}
|
||||
@ -1,5 +1,9 @@
|
||||
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 {
|
||||
const image_width: isize = 256;
|
||||
@ -16,15 +20,8 @@ pub fn main() anyerror!void {
|
||||
|
||||
var i: isize = 0;
|
||||
while (i < image_width) : (i += 1) {
|
||||
var r = @intToFloat(f32, i) / @intToFloat(f32, image_width - 1);
|
||||
var g = @intToFloat(f32, j) / @intToFloat(f32, image_height - 1);
|
||||
var b: f32 = 0.25;
|
||||
|
||||
var ir = @floatToInt(u8, 255.999 * r);
|
||||
var ig = @floatToInt(u8, 255.999 * g);
|
||||
var ib = @floatToInt(u8, 255.999 * b);
|
||||
|
||||
try stdout.writer().print("{d} {d} {d}\n", .{ ir, ig, ib });
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
19
zigrtiow/src/vec.zig
Normal file
19
zigrtiow/src/vec.zig
Normal 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;
|
||||
Loading…
x
Reference in New Issue
Block a user