this repo has no description
at 1cd5e1ab441ef6a246a03a423071f2efe5d8cb20 52 lines 1.4 kB view raw
1const zm = @import("zmath"); 2 3pub const Texture = union(enum) { 4 solid_color: SolidColor, 5 checker_texture: CheckerTexture, 6 7 pub fn value(self: *Texture, u: f32, v: f32, p: zm.Vec) zm.Vec { 8 switch (self.*) { 9 inline else => |*n| return n.value(u, v, p), 10 } 11 } 12}; 13 14pub const SolidColor = struct { 15 albedo: zm.Vec, 16 17 pub fn init(albedo: zm.Vec) SolidColor { 18 return SolidColor{ .albedo = albedo }; 19 } 20 21 pub fn rgb(r: f32, g: f32, b: f32) SolidColor { 22 return init(zm.f32x4(r, g, b, 1.0)); 23 } 24 25 pub fn value(self: *SolidColor, _: f32, _: f32, _: zm.Vec) zm.Vec { 26 return self.albedo; 27 } 28}; 29 30pub const CheckerTexture = struct { 31 inv_scale: f32, 32 even: *Texture, 33 odd: *Texture, 34 35 pub fn init(scale: f32, even: *Texture, odd: *Texture) CheckerTexture { 36 return CheckerTexture{ .inv_scale = 1 / scale, .even = even, .odd = odd }; 37 } 38 39 pub fn value(self: *CheckerTexture, u: f32, v: f32, p: zm.Vec) zm.Vec { 40 const x = @as(i32, @intFromFloat(@floor(self.inv_scale * p[0]))); 41 const y = @as(i32, @intFromFloat(@floor(self.inv_scale * p[1]))); 42 const z = @as(i32, @intFromFloat(@floor(self.inv_scale * p[2]))); 43 44 const is_even = @rem(x + y + z, 2) == 0; 45 46 if (is_even) { 47 return self.even.value(u, v, p); 48 } else { 49 return self.odd.value(u, v, p); 50 } 51 } 52};