tangled
alpha
login
or
join now
altagos.dev
/
rayray
this repo has no description
0
fork
atom
overview
issues
pulls
pipelines
cleanup
altagos.dev
2 years ago
ec8102db
aa0fd13a
+29
-32
6 changed files
expand all
collapse all
unified
split
build.zig
src
hittable.zig
interval.zig
main.zig
rayray.zig
renderer.zig
+5
-17
build.zig
···
28
28
exe.root_module.addImport("spall", spall.module("spall"));
29
29
exe.root_module.addImport("rayray", rayray);
30
30
31
31
-
const alib = b.dependency("a", .{
32
32
-
.target = target,
33
33
-
.optimize = optimize,
34
34
-
.log_ignore_default = true,
35
35
-
});
36
36
-
37
37
-
exe.root_module.addImport("a", alib.module("a"));
38
38
-
39
31
b.installArtifact(exe);
40
32
41
33
const run_cmd = b.addRunArtifact(exe);
···
47
39
48
40
const run_step = b.step("run", "Run the app");
49
41
run_step.dependOn(&run_cmd.step);
50
50
-
51
51
-
const lib_unit_tests = b.addTest(.{
52
52
-
.root_source_file = .{ .path = "src/root.zig" },
53
53
-
.target = target,
54
54
-
.optimize = optimize,
55
55
-
});
56
56
-
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
57
57
-
const test_step = b.step("test", "Run unit tests");
58
58
-
test_step.dependOn(&run_lib_unit_tests.step);
59
42
}
60
43
61
44
fn addDeps(b: *std.Build, module: *std.Build.Module) void {
45
45
+
const alib = b.dependency("a", .{
46
46
+
.log_ignore_default = true,
47
47
+
});
48
48
+
module.addImport("a", alib.module("a"));
49
49
+
62
50
const zmath = b.dependency("zmath", .{
63
51
.enable_cross_platform_determinism = true,
64
52
});
+2
src/hittable.zig
···
4
4
5
5
const IntervalF32 = @import("interval.zig").IntervalF32;
6
6
const Ray = @import("ray.zig");
7
7
+
8
8
+
// Hittable Objects
7
9
pub const Sphere = @import("hittable/sphere.zig");
8
10
9
11
pub const HitRecord = struct {
+10
src/interval.zig
···
54
54
return self.current;
55
55
} else return null;
56
56
}
57
57
+
58
58
+
pub fn nextInc(self: *Iterator) ?T {
59
59
+
self.current += 1;
60
60
+
return if (self.current <= self.interval.max) self.current else null;
61
61
+
}
62
62
+
63
63
+
pub fn nextExc(self: *Iterator) ?T {
64
64
+
self.current += 1;
65
65
+
return if (self.current < self.interval.max) self.current else null;
66
66
+
}
57
67
};
58
68
59
69
min: T,
+5
-1
src/main.zig
···
25
25
spall.init_thread();
26
26
defer spall.deinit_thread();
27
27
28
28
+
// Setting up the world
28
29
var world = HittableList.init(allocator);
29
30
try world.add(Hittable.initSphere(Sphere{ .center = zm.f32x4(0, 0, -1, 0), .radius = 0.5 }));
30
31
try world.add(Hittable.initSphere(Sphere{ .center = zm.f32x4(0, -100.5, -1, 0), .radius = 100 }));
31
32
32
32
-
const s = spall.trace(@src(), "Main", .{});
33
33
+
const s = spall.trace(@src(), "Raytracer", .{});
33
34
35
35
+
// Raytracing part
34
36
var raytracer = try rayray.Raytracer.init(allocator, world);
35
37
defer raytracer.deinit();
36
38
···
39
41
40
42
s.end();
41
43
44
44
+
// Saving to file
42
45
const s_saving = spall.trace(@src(), "Write Image", .{});
43
46
defer s_saving.end();
47
47
+
44
48
try img.writeToFilePath("./out/out.png", .{ .png = .{} });
45
49
std.log.info("Image saved to: out/out.ong", .{});
46
50
}
-1
src/rayray.zig
···
3
3
const spall = @import("spall");
4
4
const zigimg = @import("zigimg");
5
5
const color = zigimg.color;
6
6
-
const zm = @import("zmath");
7
6
8
7
pub const Camera = @import("camera.zig");
9
8
pub const hittable = @import("hittable.zig");
+7
-13
src/renderer.zig
···
2
2
3
3
const spall = @import("spall");
4
4
const zigimg = @import("zigimg");
5
5
-
const color = zigimg.color;
6
5
const zm = @import("zmath");
7
6
8
7
pub const Camera = @import("camera.zig");
8
8
+
pub const hittable = @import("hittable.zig");
9
9
+
pub const Ray = @import("ray.zig");
10
10
+
9
11
pub const interval = @import("interval.zig");
10
12
pub const IntervalUsize = interval.IntervalUsize;
11
13
pub const IntervalF32 = interval.IntervalF32;
12
12
-
pub const hittable = @import("hittable.zig");
13
13
-
pub const Ray = @import("ray.zig");
14
14
15
15
const log = std.log.scoped(.renderer);
16
16
···
31
31
32
32
pub fn run(ctx: Context, height: IntervalUsize, width: IntervalUsize) void {
33
33
var height_iter = height.iter();
34
34
-
height_iter.upper_boundry = .inclusive;
35
35
-
36
36
-
while (height_iter.next()) |j| {
34
34
+
while (height_iter.nextInc()) |j| {
37
35
if (j >= ctx.cam.image_height) break;
38
36
39
37
var width_iter = width.iter();
40
40
-
height_iter.upper_boundry = .inclusive;
41
41
-
42
42
-
while (width_iter.next()) |i| inner: {
43
43
-
if (i >= ctx.cam.image_width) break :inner;
44
44
-
38
38
+
while (width_iter.nextExc()) |i| {
45
39
const pixel_center = ctx.cam.pixel00_loc + (zm.f32x4s(@as(f32, @floatFromInt(i))) * ctx.cam.pixel_delta_u) + (zm.f32x4s(@as(f32, @floatFromInt(j))) * ctx.cam.pixel_delta_v);
46
40
const ray_direction = pixel_center - ctx.cam.camera_center;
47
41
var ray = Ray.init(ctx.cam.camera_center, ray_direction);
···
69
63
done.store(true, .Release);
70
64
}
71
65
72
72
-
fn vecToRgba(v: zm.Vec) color.Rgba32 {
66
66
+
fn vecToRgba(v: zm.Vec) zigimg.color.Rgba32 {
73
67
const r: u8 = @intFromFloat(255.999 * v[0]);
74
68
const g: u8 = @intFromFloat(255.999 * v[1]);
75
69
const b: u8 = @intFromFloat(255.999 * v[2]);
76
70
const a: u8 = @intFromFloat(255.999 * v[3]);
77
71
78
78
-
return color.Rgba32.initRgba(r, g, b, a);
72
72
+
return zigimg.color.Rgba32.initRgba(r, g, b, a);
79
73
}