tangled
alpha
login
or
join now
altagos.dev
/
rayray
0
fork
atom
this repo has no description
0
fork
atom
overview
issues
pulls
pipelines
added logging
altagos.dev
2 years ago
d3334d20
99db8021
+47
-22
4 changed files
expand all
collapse all
unified
split
build.zig
build.zig.zon
src
main.zig
rayray.zig
+8
build.zig
···
27
27
exe.root_module.addImport("spall", spall.module("spall"));
28
28
exe.root_module.addImport("rayray", rayray);
29
29
30
30
+
const alib = b.dependency("a", .{
31
31
+
.target = target,
32
32
+
.optimize = optimize,
33
33
+
.log_ignore_default = true,
34
34
+
});
35
35
+
36
36
+
exe.root_module.addImport("a", alib.module("a"));
37
37
+
30
38
b.installArtifact(exe);
31
39
32
40
const run_cmd = b.addRunArtifact(exe);
+4
build.zig.zon
···
4
4
.minimum_zig_version = "0.12.0-dev.2631+3069669bc",
5
5
.dependencies = .{
6
6
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
7
7
+
.a = .{
8
8
+
.url = "https://git.sr.ht/~altagos/a/archive/790275535ae4ea236406dc198413c4d4f31919e7.tar.gz",
9
9
+
.hash = "12204b052e9dda5b17e6271afcf4bf0112d4f5012b73201cec4fbb6cc85e1d400ec5",
10
10
+
},
7
11
.zigimg = .{
8
12
.url = "https://github.com/rockorager/zigimg/archive/19a49a7e44fb4b1c22341dfbd6566019de742055.tar.gz",
9
13
.hash = "1220ebfa8587cfd644995fc08e218dbb3ebd7344fb8e129ff02bc5a6d52a2325370d",
+11
src/main.zig
···
1
1
const std = @import("std");
2
2
3
3
+
const a = @import("a");
3
4
const spall = @import("spall");
4
5
5
6
const Raytracer = @import("rayray").Raytracer;
7
7
+
8
8
+
pub const std_options = .{
9
9
+
.log_level = .debug,
10
10
+
.logFn = a.log.logFn,
11
11
+
};
6
12
7
13
pub fn main() !void {
8
14
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
···
21
27
defer raytracer.deinit();
22
28
23
29
const img = try raytracer.render();
30
30
+
std.log.info("Image rendered", .{});
24
31
25
32
s.end();
33
33
+
34
34
+
const s_saving = spall.trace(@src(), "Write Image", .{});
35
35
+
defer s_saving.end();
26
36
try img.writeToFilePath("./out/out.png", .{ .png = .{} });
37
37
+
std.log.info("Image saved to: out/out.ong", .{});
27
38
}
+24
-22
src/rayray.zig
···
4
4
const zigimg = @import("zigimg");
5
5
const color = zigimg.color;
6
6
7
7
+
const log = std.log.scoped(.rayray);
8
8
+
7
9
pub const Raytracer = struct {
8
10
const Self = @This();
9
11
···
22
24
_ = self;
23
25
}
24
26
27
27
+
// TODO: Render in cubes not in rows
25
28
pub fn render(self: *Self) !zigimg.Image {
26
26
-
const s = spall.trace(@src(), "render", .{});
29
29
+
const s = spall.trace(@src(), "Render", .{});
27
30
defer s.end();
28
31
29
32
const rows: usize = try std.Thread.getCpuCount();
···
34
37
}
35
38
break :blk rows + 1;
36
39
};
37
37
-
std.debug.print("rows: {}, row_height: {}, num_threads: {}", .{ rows, row_height, num_threads });
40
40
+
41
41
+
log.debug("rows: {}, row_height: {}, num_threads: {}", .{ rows, row_height, num_threads });
38
42
39
43
const threads = try self.allocator.alloc(std.Thread, num_threads);
40
44
defer self.allocator.free(threads);
···
51
55
return self.camera.image;
52
56
}
53
57
54
54
-
fn r(camera: *Camera, row: usize, height: usize) void {
58
58
+
fn r(cam: *Camera, row: usize, height: usize) void {
55
59
spall.init_thread();
56
60
defer spall.deinit_thread();
57
61
58
58
-
const s = spall.trace(@src(), "thread {}", .{row});
62
62
+
const s = spall.trace(@src(), "Render Thread {}", .{row});
59
63
defer s.end();
60
64
61
65
for (0..height) |iy| {
62
66
const y = iy + height * row;
63
63
-
if (y >= camera.height) break;
67
67
+
if (y >= cam.height) break;
64
68
65
65
-
for (0..camera.width) |x| {
66
66
-
@setRuntimeSafety(false);
67
67
-
if (iy <= height - 5) {
68
68
-
camera.setPixel(x, y, color.Rgba32.initRgba(
69
69
-
@intCast(x),
70
70
-
@intCast(y),
71
71
-
0,
72
72
-
255,
73
73
-
)) catch break;
74
74
-
} else {
75
75
-
camera.setPixel(x, y, color.Rgba32.initRgba(
76
76
-
0,
77
77
-
0,
78
78
-
255,
79
79
-
255,
80
80
-
)) catch break;
81
81
-
}
69
69
+
for (0..cam.width) |x| {
70
70
+
const col = blk: {
71
71
+
if (iy <= height - 5) {
72
72
+
@setRuntimeSafety(false);
73
73
+
break :blk color.Rgba32.initRgba(
74
74
+
@intCast(x),
75
75
+
@intCast(y),
76
76
+
0,
77
77
+
255,
78
78
+
);
79
79
+
}
80
80
+
break :blk color.Rgba32.initRgba(0, 0, 255, 255);
81
81
+
};
82
82
+
83
83
+
cam.setPixel(x, y, col) catch break;
82
84
}
83
85
}
84
86
}