this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

implemented a checker texture

Altagos 06ac077e 8a358211

+142 -41
-17
src/Texture.zig
··· 1 - const zm = @import("zmath"); 2 - 3 - pub const SolidColor = struct { 4 - albedo: zm.Vec, 5 - 6 - pub fn init(albedo: zm.Vec) SolidColor { 7 - return .{ .albedo = albedo }; 8 - } 9 - 10 - pub fn rgb(r: f32, g: f32, b: f32) SolidColor { 11 - return init(zm.f32x4(r, g, b, 1.0)); 12 - } 13 - 14 - pub fn value(self: *SolidColor, _: f32, _: f32, _: zm.Vec) zm.Vec { 15 - return self.albedo; 16 - } 17 - };
+2 -2
src/hittable.zig
··· 15 15 normal: zm.Vec = zm.f32x4s(1.0), 16 16 mat: *Material, 17 17 t: f32, 18 - u: f32, 19 - v: f32, 18 + u: f32 = 0, 19 + v: f32 = 0, 20 20 front_face: bool = true, 21 21 22 22 pub fn setFaceNormal(self: *HitRecord, r: *Ray, outward_normal: zm.Vec) void {
+11
src/hittable/sphere.zig
··· 1 + const math = @import("std").math; 2 + 1 3 const zm = @import("zmath"); 2 4 3 5 const AABB = @import("../AABB.zig"); ··· 71 73 72 74 const outward_normal = (rec.p - self.center) / zm.f32x4s(self.radius); 73 75 rec.setFaceNormal(r, outward_normal); 76 + setSphereUV(&rec, outward_normal); 74 77 75 78 return rec; 76 79 } ··· 79 82 if (!self.is_moving) return self.center; 80 83 return self.center + zm.f32x4s(time) * self.center_vec; 81 84 } 85 + 86 + fn setSphereUV(rec: *HitRecord, p: zm.Vec) void { 87 + const theta = math.acos(-p[1]); 88 + const phi = math.atan2(-p[2], p[0]) + math.pi; 89 + 90 + rec.u = phi / (2 * math.pi); 91 + rec.v = theta / phi; 92 + }
+2 -14
src/main.zig
··· 22 22 const allocator = arena.allocator(); 23 23 24 24 // Setting up the world 25 - var scence = try scences.inOneWeekend(allocator); 25 + var scence = try scences.checker(allocator); 26 26 defer scence.deinit(); 27 27 28 28 std.log.info("World created", .{}); 29 29 30 30 // Raytracing part 31 - var raytracer = try rayray.Raytracer.init(allocator, scence.world, .{ 32 - .aspect_ratio = 16.0 / 9.0, 33 - .image_width = 400, 34 - .samples_per_pixel = 50, 35 - .max_depth = 50, 36 - 37 - .vfov = 20, 38 - .look_from = zm.f32x4(20, 6, 6, 0), 39 - .look_at = zm.f32x4(0, 0, 0, 0), 40 - 41 - .defocus_angle = 0.6, 42 - .focus_dist = 18, 43 - }); 31 + var raytracer = try rayray.Raytracer.init(allocator, scence.world, scence.camera); 44 32 defer raytracer.deinit(); 45 33 46 34 var timer = try std.time.Timer.start();
+9 -4
src/material.zig
··· 5 5 const hittable = @import("hittable.zig"); 6 6 const Ray = @import("Ray.zig"); 7 7 const util = @import("util.zig"); 8 + const texture = @import("texture.zig"); 8 9 9 10 pub const Material = union(enum) { 10 11 lambertian: Lambertian, 11 12 metal: Metal, 12 13 dielectric: Dielectric, 13 14 14 - pub fn lambertian(albedo: zm.Vec) Material { 15 - return .{ .lambertian = .{ .albedo = albedo } }; 15 + pub fn lambertian(tex: texture.Texture) Material { 16 + return .{ .lambertian = .{ .tex = tex } }; 17 + } 18 + 19 + pub fn lambertianS(albedo: zm.Vec) Material { 20 + return .{ .lambertian = .{ .tex = .{ .solid_color = .{ .albedo = albedo } } } }; 16 21 } 17 22 18 23 pub fn metal(albedo: zm.Vec, fuzz: f32) Material { ··· 33 38 }; 34 39 35 40 pub const Lambertian = struct { 36 - albedo: zm.Vec, 41 + tex: texture.Texture, 37 42 38 43 pub inline fn scatter(self: *Lambertian, r: *Ray, rec: *hittable.HitRecord, attenuation: *zm.Vec) ?Ray { 39 44 var scatter_dir = rec.normal + util.randomUnitVec(); 40 45 41 46 if (util.nearZero(scatter_dir)) scatter_dir = rec.normal; 42 47 43 - attenuation.* = self.albedo; 48 + attenuation.* = self.tex.value(rec.u, rec.v, rec.p); 44 49 // return Ray.initT(rec.p, scatter_dir, r.tm); 45 50 return Ray{ .orig = rec.p, .dir = scatter_dir, .tm = r.tm }; 46 51 }
+1
src/rayray.zig
··· 12 12 pub const interval = @import("interval.zig"); 13 13 const IntervalUsize = interval.IntervalUsize; 14 14 pub const material = @import("material.zig"); 15 + pub const texture = @import("texture.zig"); 15 16 pub const tracer = @import("tracer.zig"); 16 17 pub const util = @import("util.zig"); 17 18
+1
src/scences.zig
··· 1 + pub const checker = @import("scences/checker.zig").scene; 1 2 pub const inOneWeekend = @import("scences/in_one_weekend.zig").scene;
+46
src/scences/checker.zig
··· 1 + const std = @import("std"); 2 + 3 + const rayray = @import("rayray"); 4 + const Camera = rayray.Camera; 5 + const Hittable = rayray.hittable.Hittable; 6 + const HittableList = rayray.hittable.HittableList; 7 + const Material = rayray.material.Material; 8 + const Sphere = rayray.hittable.Sphere; 9 + const tex = rayray.texture; 10 + const zm = rayray.zmath; 11 + 12 + camera: Camera.Options, 13 + world: HittableList, 14 + allocator: std.mem.Allocator, 15 + 16 + pub fn scene(allocator: std.mem.Allocator) !@This() { 17 + var world = HittableList.init(allocator); 18 + 19 + const c1 = try allocator.create(tex.Texture); 20 + c1.* = tex.Texture{ .solid_color = tex.SolidColor.rgb(0.2, 0.3, 0.1) }; 21 + const c2 = try allocator.create(tex.Texture); 22 + c2.* = tex.Texture{ .solid_color = tex.SolidColor.rgb(0.9, 0.9, 0.9) }; 23 + 24 + const checker = try allocator.create(Material); 25 + checker.* = Material.lambertian(tex.Texture{ .checker_texture = tex.CheckerTexture.init(0.32, c1, c2) }); 26 + 27 + try world.add(Hittable.sphere("s1", Sphere.init(zm.f32x4(0, -10, 0, 0), 10, checker))); 28 + try world.add(Hittable.sphere("s2", Sphere.init(zm.f32x4(0, 10, 0, 0), 10, checker))); 29 + 30 + return .{ .allocator = allocator, .world = world, .camera = Camera.Options{ 31 + .aspect_ratio = 16.0 / 9.0, 32 + .image_width = 400, 33 + .samples_per_pixel = 100, 34 + .max_depth = 50, 35 + 36 + .vfov = 20, 37 + .look_from = zm.f32x4(13, 2, 3, 0), 38 + .look_at = zm.f32x4(0, 0, 0, 0), 39 + 40 + .defocus_angle = 0, 41 + } }; 42 + } 43 + 44 + pub fn deinit(self: *@This()) void { 45 + self.world.deinit(); 46 + }
+18 -4
src/scences/in_one_weekend.zig
··· 1 1 const std = @import("std"); 2 2 3 3 const rayray = @import("rayray"); 4 + const Camera = rayray.Camera; 4 5 const Hittable = rayray.hittable.Hittable; 5 6 const HittableList = rayray.hittable.HittableList; 6 7 const Material = rayray.material.Material; 7 8 const Sphere = rayray.hittable.Sphere; 8 9 const zm = rayray.zmath; 9 10 11 + camera: Camera.Options, 10 12 world: HittableList, 11 13 allocator: std.mem.Allocator, 12 14 ··· 14 16 var world = HittableList.init(allocator); 15 17 16 18 const material_ground = try allocator.create(Material); 17 - material_ground.* = Material.lambertian(zm.f32x4(0.5, 0.5, 0.5, 1.0)); 19 + material_ground.* = Material.lambertianS(zm.f32x4(0.5, 0.5, 0.5, 1.0)); 18 20 try world.add(Hittable.sphere("Ground", Sphere.init(zm.f32x4(0, -1000, 0, 0), 1000, material_ground))); 19 21 20 22 const a_max = 30; ··· 39 41 if (choose_mat < 0.8) { 40 42 // diffuse 41 43 const albedo = rayray.util.randomVec3() * rayray.util.randomVec3() + zm.f32x4(0, 0, 0, 1); 42 - material.* = Material.lambertian(albedo); 44 + material.* = Material.lambertianS(albedo); 43 45 const center2 = center + zm.f32x4(0, rayray.util.randomF32M(0, 0.5), 0, 0); 44 46 try world.add(Hittable.sphere("Lambertian", Sphere.initMoving(center, center2, 0.2, material))); 45 47 } else if (choose_mat < 0.95) { ··· 62 64 // try world.add(Hittable.sphere("One: Dielectric", Sphere{ .center = zm.f32x4(0, 1, 0, 0), .radius = 1, .mat = material1 })); 63 65 64 66 const material2 = try allocator.create(Material); 65 - material2.* = Material.lambertian(zm.f32x4(0.4, 0.2, 0.1, 1)); 67 + material2.* = Material.lambertianS(zm.f32x4(0.4, 0.2, 0.1, 1)); 66 68 try world.add(Hittable.sphere("Two: Lambertian", Sphere.init(zm.f32x4(-4, 1, 0, 0), 1, material1))); 67 69 68 70 const material3 = try allocator.create(Material); ··· 71 73 72 74 try world.add(Hittable.sphere("One: Dielectric", Sphere.init(zm.f32x4(0, 1, 0, 0), 1, material2))); 73 75 74 - return .{ .allocator = allocator, .world = world }; 76 + return .{ .allocator = allocator, .world = world, .camera = .{ 77 + .aspect_ratio = 16.0 / 9.0, 78 + .image_width = 400, 79 + .samples_per_pixel = 50, 80 + .max_depth = 50, 81 + 82 + .vfov = 20, 83 + .look_from = zm.f32x4(20, 6, 6, 0), 84 + .look_at = zm.f32x4(0, 0, 0, 0), 85 + 86 + .defocus_angle = 0.6, 87 + .focus_dist = 18, 88 + } }; 75 89 } 76 90 77 91 pub fn deinit(self: *@This()) void {
+52
src/texture.zig
··· 1 + const zm = @import("zmath"); 2 + 3 + pub 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 + 14 + pub 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 + 30 + pub 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 + };