this repo has no description
0
fork

Configure Feed

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

updated zig + small changes

Altagos 4c306c5c 469ec404

+40 -32
+2
src/Camera.zig
··· 32 32 aspect_ratio: f32, 33 33 34 34 samples_per_pixel: usize, 35 + samples_per_pixel_v: zm.Vec, 35 36 max_depth: usize, 36 37 37 38 vfov: f32, ··· 117 118 .aspect_ratio = aspect_ratio, 118 119 119 120 .samples_per_pixel = opts.samples_per_pixel, 121 + .samples_per_pixel_v = zm.f32x4s(@as(f32, @floatFromInt(opts.samples_per_pixel))), 120 122 .max_depth = opts.max_depth, 121 123 122 124 .vfov = vfov,
+1 -1
src/hittable.zig
··· 28 28 pub const Hittable = union(enum) { 29 29 sphere: struct { Sphere, []const u8 }, 30 30 31 - pub fn sphere(name: []const u8, s: Sphere) Hittable { 31 + pub fn initSphere(name: []const u8, s: Sphere) Hittable { 32 32 // std.log.info("created sphere with mat: {}", .{s.mat}); 33 33 return .{ .sphere = .{ s, name } }; 34 34 }
+4 -4
src/material.zig
··· 12 12 metal: Metal, 13 13 dielectric: Dielectric, 14 14 15 - pub fn lambertian(tex: texture.Texture) Material { 15 + pub fn initLambertian(tex: texture.Texture) Material { 16 16 return .{ .lambertian = .{ .tex = tex } }; 17 17 } 18 18 19 - pub fn lambertianS(albedo: zm.Vec) Material { 19 + pub fn initLambertianS(albedo: zm.Vec) Material { 20 20 return .{ .lambertian = .{ .tex = .{ .solid_color = .{ .albedo = albedo } } } }; 21 21 } 22 22 23 - pub fn metal(albedo: zm.Vec, fuzz: f32) Material { 23 + pub fn initMetal(albedo: zm.Vec, fuzz: f32) Material { 24 24 return .{ .metal = .{ .albedo = albedo, .fuzz = if (fuzz < 1) fuzz else 1.0 } }; 25 25 } 26 26 27 - pub fn dielectric(refraction_index: f32) Material { 27 + pub fn initDielectric(refraction_index: f32) Material { 28 28 return .{ .dielectric = .{ .refraction_index = refraction_index } }; 29 29 } 30 30
+3 -3
src/scences/checker.zig
··· 22 22 c2.* = tex.Texture{ .solid_color = tex.SolidColor.rgb(0.9, 0.9, 0.9) }; 23 23 24 24 const checker = try allocator.create(Material); 25 - checker.* = Material.lambertian(tex.Texture{ .checker_texture = tex.CheckerTexture.init(0.32, c1, c2) }); 25 + checker.* = Material.initLambertian(tex.Texture{ .checker_texture = tex.CheckerTexture.init(0.32, c1, c2) }); 26 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))); 27 + try world.add(Hittable.initSphere("s1", Sphere.init(zm.f32x4(0, -10, 0, 0), 10, checker))); 28 + try world.add(Hittable.initSphere("s2", Sphere.init(zm.f32x4(0, 10, 0, 0), 10, checker))); 29 29 30 30 return .{ .allocator = allocator, .world = world, .camera = Camera.Options{ 31 31 .aspect_ratio = 16.0 / 9.0,
+15 -15
src/scences/in_one_weekend.zig
··· 16 16 var world = HittableList.init(allocator); 17 17 18 18 const material_ground = try allocator.create(Material); 19 - material_ground.* = Material.lambertianS(zm.f32x4(0.5, 0.5, 0.5, 1.0)); 20 - try world.add(Hittable.sphere("Ground", Sphere.init(zm.f32x4(0, -1000, 0, 0), 1000, material_ground))); 19 + material_ground.* = Material.initLambertianS(zm.f32x4(0.5, 0.5, 0.5, 1.0)); 20 + try world.add(Hittable.initSphere("Ground", Sphere.init(zm.f32x4(0, -1000, 0, 0), 1000, material_ground))); 21 21 22 22 const a_max = 30; 23 23 const b_max = 30; ··· 41 41 if (choose_mat < 0.8) { 42 42 // diffuse 43 43 const albedo = rayray.util.randomVec3() * rayray.util.randomVec3() + zm.f32x4(0, 0, 0, 1); 44 - material.* = Material.lambertianS(albedo); 44 + material.* = Material.initLambertianS(albedo); 45 45 const center2 = center + zm.f32x4(0, rayray.util.randomF32M(0, 0.5), 0, 0); 46 - try world.add(Hittable.sphere("Lambertian", Sphere.initMoving(center, center2, 0.2, material))); 46 + try world.add(Hittable.initSphere("Lambertian", Sphere.initMoving(center, center2, 0.2, material))); 47 47 } else if (choose_mat < 0.95) { 48 48 // metal 49 49 const albedo = rayray.util.randomVec3M(0.5, 1) + zm.f32x4(0, 0, 0, 1); 50 50 const fuzz = rayray.util.randomF32M(0, 0.5); 51 - material.* = Material.metal(albedo, fuzz); 52 - try world.add(Hittable.sphere("Metal", Sphere.init(center, 0.2, material))); 51 + material.* = Material.initMetal(albedo, fuzz); 52 + try world.add(Hittable.initSphere("Metal", Sphere.init(center, 0.2, material))); 53 53 } else { 54 54 // glass 55 - material.* = Material.dielectric(1.5); 56 - try world.add(Hittable.sphere("Dielectric", Sphere.init(center, 0.2, material))); 55 + material.* = Material.initDielectric(1.5); 56 + try world.add(Hittable.initSphere("Dielectric", Sphere.init(center, 0.2, material))); 57 57 } 58 58 } 59 59 } 60 60 } 61 61 62 62 const material1 = try allocator.create(Material); 63 - material1.* = Material.dielectric(1.5); 64 - // try world.add(Hittable.sphere("One: Dielectric", Sphere{ .center = zm.f32x4(0, 1, 0, 0), .radius = 1, .mat = material1 })); 63 + material1.* = Material.initDielectric(1.5); 64 + try world.add(Hittable.initSphere("One: Dielectric", Sphere.init(zm.f32x4(0, 1, 0, 0), 1, material1))); 65 65 66 66 const material2 = try allocator.create(Material); 67 - material2.* = Material.lambertianS(zm.f32x4(0.4, 0.2, 0.1, 1)); 68 - try world.add(Hittable.sphere("Two: Lambertian", Sphere.init(zm.f32x4(-4, 1, 0, 0), 1, material1))); 67 + material2.* = Material.initLambertianS(zm.f32x4(0.4, 0.2, 0.1, 1)); 68 + try world.add(Hittable.initSphere("Two: Lambertian", Sphere.init(zm.f32x4(-4, 1, 0, 0), 1, material2))); 69 69 70 70 const material3 = try allocator.create(Material); 71 - material3.* = Material.metal(zm.f32x4(0.7, 0.6, 0.5, 1), 0); 72 - try world.add(Hittable.sphere("Three: Metal", Sphere.init(zm.f32x4(4, 1, 0, 0), 1, material3))); 71 + material3.* = Material.initMetal(zm.f32x4(0.7, 0.6, 0.5, 1), 0); 72 + try world.add(Hittable.initSphere("Three: Metal", Sphere.init(zm.f32x4(4, 1, 0, 0), 1, material3))); 73 73 74 - try world.add(Hittable.sphere("One: Dielectric", Sphere.init(zm.f32x4(0, 1, 0, 0), 1, material2))); 74 + // try world.add(Hittable.sphere("One: Dielectric", Sphere.init(zm.f32x4(0, 1, 0, 0), 1, material2))); 75 75 76 76 return .{ .allocator = allocator, .world = world, .camera = .{ 77 77 .aspect_ratio = 16.0 / 9.0,
+15 -9
src/tracer.zig
··· 24 24 width: IntervalUsize, 25 25 }; 26 26 27 + const white = zm.f32x4s(1.0); 28 + const black = zm.f32x4(0, 0, 0, 1.0); 29 + 27 30 pub fn rayColor(r: *Ray, world: *BVH, depth: usize) zm.Vec { 28 31 @setFloatMode(.optimized); 29 - if (depth == 0) return zm.f32x4(0, 0, 0, 1.0); 32 + if (depth == 0) return black; 30 33 31 34 if (world.hit(r, .{ .min = 0.001, .max = std.math.inf(f32) })) |rec| { 32 - var attenuation = zm.f32x4s(1.0); 35 + var attenuation = white; 33 36 if (rec.mat.scatter(r, @constCast(&rec), &attenuation)) |new_r| { 34 37 return attenuation * rayColor(@constCast(&new_r), world, depth - 1); 35 38 } 36 39 37 - return zm.f32x4(0, 0, 0, 1.0); 40 + return black; 38 41 } 39 42 40 43 const unit_direction = zm.normalize3(r.dir); ··· 43 46 } 44 47 45 48 pub fn trace(ctx: Context) void { 46 - const spp = zm.f32x4s(@as(f32, @floatFromInt(ctx.cam.samples_per_pixel))); 47 - 48 49 var height_iter = ctx.height.iter(); 49 50 while (height_iter.nextInc()) |j| { 50 51 if (j >= ctx.cam.image_height) break; ··· 58 59 col += rayColor(&ray, ctx.world, ctx.cam.max_depth); 59 60 } 60 61 61 - ctx.cam.setPixel(i, j, vecToRgba(col, spp)) catch break; 62 + ctx.cam.setPixel(i, j, vecToRgba( 63 + col, 64 + ctx.cam.samples_per_pixel_v, 65 + )) catch break; 62 66 } 63 67 } 64 68 } ··· 68 72 const v256 = zm.f32x4s(256); 69 73 70 74 inline fn vecToRgba(v: zm.Vec, samples_per_pixel: zm.Vec) zigimg.color.Rgba32 { 71 - var rgba = zm.sqrt(v / samples_per_pixel); // linear to gamma 72 - rgba = zm.clampFast(rgba, zero, nearly_one); 73 - rgba = rgba * v256; 75 + const rgba = zm.clampFast( 76 + @sqrt(v / samples_per_pixel), 77 + zero, 78 + nearly_one, 79 + ) * v256; // linear to gamma 74 80 75 81 return zigimg.color.Rgba32.initRgba( 76 82 @intFromFloat(rgba[0]),