aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/rtw/texture.zig
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2022-12-07 22:37:14 +0900
committernsfisis <nsfisis@gmail.com>2022-12-07 22:54:47 +0900
commit86914985b77ddf5d6d3a3dd6c13deed9d906a471 (patch)
treea523883d6524d55aee713bfd6400f044f8715d52 /src/rtw/texture.zig
parente264078ed9d94b5091a6d78f8128496ac49fbde4 (diff)
downloadRayTracingInOneWeekend.zig-86914985b77ddf5d6d3a3dd6c13deed9d906a471.tar.gz
RayTracingInOneWeekend.zig-86914985b77ddf5d6d3a3dd6c13deed9d906a471.tar.zst
RayTracingInOneWeekend.zig-86914985b77ddf5d6d3a3dd6c13deed9d906a471.zip
refactor: separate single main.zig
Diffstat (limited to 'src/rtw/texture.zig')
-rw-r--r--src/rtw/texture.zig72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/rtw/texture.zig b/src/rtw/texture.zig
new file mode 100644
index 0000000..47c3c4f
--- /dev/null
+++ b/src/rtw/texture.zig
@@ -0,0 +1,72 @@
+const std = @import("std");
+
+const Color = @import("vec.zig").Color;
+const Vec3 = @import("vec.zig").Vec3;
+
+const TextureTag = enum {
+ solid,
+ checker,
+};
+
+pub const Texture = union(TextureTag) {
+ solid: SolidTexture,
+ checker: CheckerTexture,
+
+ pub fn makeSolid(color: Color) Texture {
+ return .{ .solid = .{ .color = color } };
+ }
+
+ pub fn makeChecker(allocator: std.mem.Allocator, odd: Color, even: Color) !Texture {
+ return .{ .checker = try CheckerTexture.init(
+ allocator,
+ Texture.makeSolid(odd),
+ Texture.makeSolid(even),
+ ) };
+ }
+
+ pub fn value(tx: Texture, u: f64, v: f64, p: Vec3) Color {
+ return switch (tx) {
+ TextureTag.solid => |solidTx| solidTx.value(u, v, p),
+ TextureTag.checker => |checkerTx| checkerTx.value(u, v, p),
+ };
+ }
+};
+
+pub const SolidTexture = struct {
+ color: Color,
+
+ fn value(tx: SolidTexture, u: f64, v: f64, p: Vec3) Color {
+ _ = u;
+ _ = v;
+ _ = p;
+ return tx.color;
+ }
+};
+
+pub const CheckerTexture = struct {
+ allocator: std.mem.Allocator,
+ odd: *const Texture,
+ even: *const Texture,
+
+ fn init(allocator: std.mem.Allocator, odd: Texture, even: Texture) !CheckerTexture {
+ var odd_ = try allocator.create(Texture);
+ var even_ = try allocator.create(Texture);
+ odd_.* = odd;
+ even_.* = even;
+ return .{
+ .allocator = allocator,
+ .odd = odd_,
+ .even = even_,
+ };
+ }
+
+ fn deinit(tx: CheckerTexture) void {
+ tx.allocator.destroy(tx.even);
+ tx.allocator.destroy(tx.odd);
+ }
+
+ fn value(tx: CheckerTexture, u: f64, v: f64, p: Vec3) Color {
+ const sines = @sin(10 * p.x) * @sin(10 * p.y) * @sin(10 * p.z);
+ return if (sines < 0) tx.odd.value(u, v, p) else tx.even.value(u, v, p);
+ }
+};