tangled
alpha
login
or
join now
altagos.dev
/
rayray
0
fork
atom
this repo has no description
0
fork
atom
overview
issues
pulls
pipelines
small stuff
altagos.dev
2 years ago
3f665895
8b67f636
+41
-41
8 changed files
expand all
collapse all
unified
split
build.zig
src
BVH.zig
hittable
sphere.zig
hittable.zig
main.zig
rayray.zig
scences
in_one_weekend.zig
tracer.zig
+12
-9
build.zig
···
9
9
.enable = enable_spall,
10
10
});
11
11
12
12
-
const strip = b.option(bool, "strip", "") orelse false;
12
12
+
const strip = b.option(bool, "strip", "") orelse (optimize != .Debug);
13
13
14
14
const rayray = b.addModule("rayray", .{
15
15
.root_source_file = .{ .path = "src/rayray.zig" },
···
30
30
});
31
31
exe.root_module.strip = strip;
32
32
33
33
-
addDeps(b, &exe.root_module);
33
33
+
// addDeps(b, &exe.root_module);
34
34
exe.root_module.addImport("spall", spall.module("spall"));
35
35
exe.root_module.addImport("rayray", rayray);
36
36
37
37
+
const alib = b.dependency("a", .{
38
38
+
.log_ignore_default = true,
39
39
+
});
40
40
+
exe.root_module.addImport("a", alib.module("a"));
41
41
+
37
42
b.installArtifact(exe);
38
43
39
44
const run_cmd = b.addRunArtifact(exe);
···
48
53
}
49
54
50
55
fn addDeps(b: *std.Build, module: *std.Build.Module) void {
51
51
-
const alib = b.dependency("a", .{
52
52
-
.log_ignore_default = true,
53
53
-
});
54
54
-
module.addImport("a", alib.module("a"));
55
55
-
56
56
const zmath = b.dependency("zmath", .{
57
57
-
.enable_cross_platform_determinism = true,
57
57
+
.optimize = .ReleaseFast,
58
58
+
.enable_cross_platform_determinism = false,
58
59
});
59
60
module.addImport("zmath", zmath.module("root"));
60
61
61
61
-
const zigimg = b.dependency("zigimg", .{});
62
62
+
const zigimg = b.dependency("zigimg", .{
63
63
+
.optimize = .ReleaseFast,
64
64
+
});
62
65
module.addImport("zigimg", zigimg.module("zigimg"));
63
66
}
+1
-2
src/BVH.zig
···
109
109
}
110
110
111
111
switch (self.*) {
112
112
-
.ast => |*a| return a.hit(r, ray_t),
113
113
-
.leaf => |*l| return l.hit(r, ray_t),
112
112
+
inline else => |*n| return n.hit(r, ray_t),
114
113
}
115
114
}
116
115
+2
-8
src/hittable.zig
···
33
33
34
34
pub fn boundingBox(self: *Hittable) AABB {
35
35
switch (self.*) {
36
36
-
.sphere => |*s| return s[0].boundingBox(),
36
36
+
inline else => |*n| return n[0].boundingBox(),
37
37
}
38
38
}
39
39
···
45
45
46
46
pub inline fn hit(self: *Hittable, r: *Ray, ray_t: IntervalF32) ?HitRecord {
47
47
switch (self.*) {
48
48
-
.sphere => |*s| {
49
49
-
// std.log.debug("try to hit Sphere: {}", .{s});
50
50
-
// std.log.info("hitting sphere with mat: {}", .{s.mat});
51
51
-
return s[0].hit(r, ray_t);
52
52
-
},
48
48
+
inline else => |*n| return n[0].hit(r, ray_t),
53
49
}
54
54
-
55
55
-
return null;
56
50
}
57
51
};
58
52
+1
-1
src/hittable/sphere.zig
···
56
56
const discriminant = half_b * half_b - a * c;
57
57
if (discriminant < 0) return null;
58
58
59
59
-
const sqrtd = @sqrt(discriminant);
59
59
+
const sqrtd = zm.sqrt(discriminant);
60
60
61
61
// Find the nearest root that lies in the acceptable range
62
62
var root = (-half_b - sqrtd) / a;
+6
-6
src/main.zig
···
2
2
3
3
const aa = @import("a");
4
4
const spall = @import("spall");
5
5
-
const zm = @import("zmath");
6
5
7
6
const rayray = @import("rayray");
8
7
const Hittable = rayray.hittable.Hittable;
9
8
const HittableList = rayray.hittable.HittableList;
10
9
const Material = rayray.material.Material;
11
10
const Sphere = rayray.hittable.Sphere;
11
11
+
const zm = rayray.zmath;
12
12
13
13
const scences = @import("scences.zig");
14
14
···
39
39
// Raytracing part
40
40
var raytracer = try rayray.Raytracer.init(allocator, scence.world, .{
41
41
.aspect_ratio = 16.0 / 9.0,
42
42
-
.image_width = 400,
43
43
-
.samples_per_pixel = 100,
44
44
-
.max_depth = 50,
42
42
+
.image_width = 4000,
43
43
+
.samples_per_pixel = 500,
44
44
+
.max_depth = 100,
45
45
46
46
.vfov = 20,
47
47
-
.look_from = zm.f32x4(13, 2, 3, 0),
47
47
+
.look_from = zm.f32x4(20, 6, 6, 0),
48
48
.look_at = zm.f32x4(0, 0, 0, 0),
49
49
50
50
.defocus_angle = 0.6,
51
51
-
.focus_dist = 10,
51
51
+
.focus_dist = 18,
52
52
});
53
53
defer raytracer.deinit();
54
54
+8
-3
src/rayray.zig
···
1
1
const std = @import("std");
2
2
3
3
+
pub const zmath = @import("zmath");
4
4
+
3
5
const spall = @import("spall");
4
6
const zigimg = @import("zigimg");
5
7
const color = zigimg.color;
···
7
9
pub const BVH = @import("BVH.zig");
8
10
pub const Camera = @import("Camera.zig");
9
11
pub const hittable = @import("hittable.zig");
10
10
-
const IntervalUsize = @import("interval.zig").IntervalUsize;
12
12
+
pub const interval = @import("interval.zig");
13
13
+
const IntervalUsize = interval.IntervalUsize;
11
14
pub const material = @import("material.zig");
12
15
pub const tracer = @import("tracer.zig");
13
16
pub const util = @import("util.zig");
···
105
108
.terminal = stderr,
106
109
.supports_ansi_escape_codes = true,
107
110
};
108
108
-
109
111
var node = progress.start("Rendered Chunks", num_chunks);
110
112
node.setCompletedItems(0);
111
113
node.context.refresh();
114
114
+
115
115
+
var completed_chunks: u64 = 0;
112
116
113
117
while (true) {
114
118
var done = true;
···
119
123
if (task_done and !t.marked_as_done) {
120
124
t.marked_as_done = true;
121
125
node.completeOne();
122
122
-
try self.camera.image.writeToFilePath("./out/out.png", .{ .png = .{} });
126
126
+
completed_chunks += 1;
127
127
+
if (completed_chunks % self.thread_pool.threads.len == 0) try self.camera.image.writeToFilePath("./out/out.png", .{ .png = .{} });
123
128
} else if (!task_done) {
124
129
done = false;
125
130
}
+4
-5
src/scences/in_one_weekend.zig
···
1
1
const std = @import("std");
2
2
3
3
-
const zm = @import("zmath");
4
4
-
5
3
const rayray = @import("rayray");
6
4
const Hittable = rayray.hittable.Hittable;
7
5
const HittableList = rayray.hittable.HittableList;
8
6
const Material = rayray.material.Material;
9
7
const Sphere = rayray.hittable.Sphere;
8
8
+
const zm = rayray.zmath;
10
9
11
10
world: HittableList,
12
11
allocator: std.mem.Allocator,
···
18
17
material_ground.* = Material.lambertian(zm.f32x4(0.5, 0.5, 0.5, 1.0));
19
18
try world.add(Hittable.sphere("Ground", Sphere{ .center = zm.f32x4(0, -1000, 0, 0), .radius = 1000, .mat = material_ground }));
20
19
21
21
-
var a: isize = -22;
22
22
-
while (a < 22) : (a += 1) {
20
20
+
var a: isize = -33;
21
21
+
while (a < 11) : (a += 1) {
23
22
var b: isize = -22;
24
24
-
while (b < 22) : (b += 1) {
23
23
+
while (b < 7) : (b += 1) {
25
24
const choose_mat = rayray.util.randomF32();
26
25
const center = zm.f32x4(
27
26
@as(f32, @floatFromInt(a)) + 0.9 * rayray.util.randomF32(),
+7
-7
src/tracer.zig
···
60
60
}
61
61
}
62
62
63
63
+
const zero = zm.f32x4s(0.0);
64
64
+
const nearly_one = zm.f32x4s(0.999);
65
65
+
const v256 = zm.f32x4s(256);
66
66
+
63
67
inline fn vecToRgba(v: zm.Vec, samples_per_pixel: usize) zigimg.color.Rgba32 {
64
64
-
var rgba = linearToGamma(zm.Vec, v / zm.f32x4s(@as(f32, @floatFromInt(samples_per_pixel))));
65
65
-
rgba = zm.clampFast(rgba, zm.f32x4s(0.0), zm.f32x4s(0.999));
66
66
-
rgba = rgba * zm.f32x4s(256);
68
68
+
var rgba = zm.sqrt(v / zm.f32x4s(@as(f32, @floatFromInt(samples_per_pixel)))); // linear to gamma
69
69
+
rgba = zm.clampFast(rgba, zero, nearly_one);
70
70
+
rgba = rgba * v256;
67
71
68
72
return zigimg.color.Rgba32.initRgba(
69
73
@intFromFloat(rgba[0]),
···
72
76
@intFromFloat(rgba[3]),
73
77
);
74
78
}
75
75
-
76
76
-
inline fn linearToGamma(comptime T: type, linear_component: T) T {
77
77
-
return @sqrt(linear_component);
78
78
-
}