Repository navigation
Expand file tree
/
Copy pathrasterizer.zig
More file actions
228 lines (213 loc) · 11 KB
/
Copy pathrasterizer.zig
File metadata and controls
228 lines (213 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//! Draws textured triangles into pixels with a depth buffer, nearer covering farther. A
//! triangle's corners are given in pixels, each with its depth and texture coordinates, which
//! are spread evenly between them: exact for a view without perspective, as the isometric
//! view is.
const std = @import("std");
const world = @import("world");
pub const Corner = struct {
x: f32,
y: f32,
/// Larger is nearer.
depth: f32,
/// On the texture, from 0 to 1 across and down.
u: f32,
v: f32,
};
/// Where triangles are drawn: each pixel's colour, its depth, and the tag of what drew it, row by
/// row.
pub const Target = struct {
width: usize,
height: usize,
colours: [][4]u8,
depths: []f32,
/// 0 where nothing is drawn.
tags: []u8,
/// Whether how opaque a texture is counts: what covers a pixel is layered with what's there,
/// as glass needs. Otherwise a texel is drawn or left out, by whether it's at least half
/// opaque.
keepsOpacity: bool = false,
/// The pixels triangles have reached since the last `clear`, so only those need clearing and
/// reading.
drawn: Area = .{},
/// From `left` and `top` up to but not including `right` and `bottom`; empty where they meet.
pub const Area = struct { left: usize = 0, top: usize = 0, right: usize = 0, bottom: usize = 0 };
pub fn init(allocator: std.mem.Allocator, width: usize, height: usize) !Target {
const colours = try allocator.alloc([4]u8, width * height);
errdefer allocator.free(colours);
const depths = try allocator.alloc(f32, width * height);
errdefer allocator.free(depths);
const tags = try allocator.alloc(u8, width * height);
var target: Target = .{ .width = width, .height = height, .colours = colours, .depths = depths, .tags = tags };
target.drawn = .{ .right = width, .bottom = height };
target.clear();
return target;
}
pub fn deinit(self: Target, allocator: std.mem.Allocator) void {
allocator.free(self.tags);
allocator.free(self.depths);
allocator.free(self.colours);
}
pub fn clear(self: *Target) void {
const area = self.drawn;
for (area.top..area.bottom) |row| {
const start = row * self.width + area.left;
const end = row * self.width + area.right;
@memset(self.colours[start..end], @splat(0));
@memset(self.depths[start..end], -std.math.inf(f32));
@memset(self.tags[start..end], 0);
}
self.drawn = .{};
}
fn include(self: *Target, area: Area) void {
if (area.left >= area.right or area.top >= area.bottom) return;
const drawn = self.drawn;
self.drawn = if (drawn.left >= drawn.right or drawn.top >= drawn.bottom) area else .{
.left = @min(drawn.left, area.left),
.top = @min(drawn.top, area.top),
.right = @max(drawn.right, area.right),
.bottom = @max(drawn.bottom, area.bottom),
};
}
};
/// Draws the triangle in `texture`, lit by `light` 256ths, on each pixel whose middle it covers
/// where its texel there is at least half opaque and it's nearer than what the pixel shows, or
/// with `Target.keepsOpacity`, layered with what's there.
pub fn triangle(target: *Target, corners: [3]Corner, texture: *const world.Image, light: u16, tag: u8) void {
const a = corners[0];
const b = corners[1];
const c = corners[2];
const area = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
if (area == 0) return;
const width: f32 = @floatFromInt(target.width);
const height: f32 = @floatFromInt(target.height);
const left: usize = @intFromFloat(std.math.clamp(@floor(@min(a.x, b.x, c.x)), 0, width));
const right: usize = @intFromFloat(std.math.clamp(@ceil(@max(a.x, b.x, c.x)), 0, width));
const top: usize = @intFromFloat(std.math.clamp(@floor(@min(a.y, b.y, c.y)), 0, height));
const bottom: usize = @intFromFloat(std.math.clamp(@ceil(@max(a.y, b.y, c.y)), 0, height));
target.include(.{ .left = left, .top = top, .right = right, .bottom = bottom });
for (top..bottom) |row| for (left..right) |column| {
const x = @as(f32, @floatFromInt(column)) + 0.5;
const y = @as(f32, @floatFromInt(row)) + 0.5;
// How much of each corner the point takes, from 0 to 1, all of them inside.
const wa = ((b.x - x) * (c.y - y) - (b.y - y) * (c.x - x)) / area;
const wb = ((c.x - x) * (a.y - y) - (c.y - y) * (a.x - x)) / area;
const wc = 1 - wa - wb;
if (wa < 0 or wb < 0 or wc < 0) continue;
const p = row * target.width + column;
const depth = wa * a.depth + wb * b.depth + wc * c.depth;
if (!target.keepsOpacity and depth <= target.depths[p]) continue;
const texel = texelAt(texture, wa * a.u + wb * b.u + wc * c.u, wa * a.v + wb * b.v + wc * c.v);
if (texel[3] < (if (target.keepsOpacity) @as(u8, 1) else 128)) continue;
var colour: [4]u8 = .{ 0, 0, 0, if (target.keepsOpacity) texel[3] else 255 };
for (0..3) |i| colour[i] = @intCast(@min(255, @as(u32, texel[i]) * light / 256));
if (target.keepsOpacity and target.tags[p] != 0) {
const nearer = depth > target.depths[p];
colour = if (nearer) over(colour, target.colours[p]) else over(target.colours[p], colour);
if (!nearer) {
target.colours[p] = colour;
continue;
}
}
target.depths[p] = depth;
target.tags[p] = tag;
target.colours[p] = colour;
};
}
/// `front` over `back`, each as opaque as its alpha says.
fn over(front: [4]u8, back: [4]u8) [4]u8 {
const frontWeight = @as(u32, front[3]) * 255;
const backWeight = @as(u32, back[3]) * (255 - @as(u32, front[3]));
const total = frontWeight + backWeight;
if (total == 0) return @splat(0);
var out: [4]u8 = undefined;
for (0..3) |i| out[i] = @intCast((front[i] * frontWeight + back[i] * backWeight) / total);
out[3] = @intCast(total / 255);
return out;
}
test over {
const glass: [4]u8 = .{ 100, 200, 100, 51 };
const frame: [4]u8 = .{ 10, 20, 30, 255 };
// An opaque front hides what's behind it.
try std.testing.expectEqual(frame, over(frame, glass));
// Over something opaque, the result is opaque.
try std.testing.expectEqual([4]u8{ 28, 56, 44, 255 }, over(glass, frame));
// Two layers of glass are more opaque than one, and keep their colour.
try std.testing.expectEqual([4]u8{ 100, 200, 100, 91 }, over(glass, glass));
}
/// The texel at (u, v), each from 0 to 1.
pub fn texelAt(image: *const world.Image, u: f32, v: f32) [4]u8 {
const x: usize = @min(image.width - 1, @as(usize, @intFromFloat(std.math.clamp(u, 0, 1) * @as(f32, @floatFromInt(image.width)))));
const y: usize = @min(image.height - 1, @as(usize, @intFromFloat(std.math.clamp(v, 0, 1) * @as(f32, @floatFromInt(image.height)))));
return image.pixels[y * image.width + x];
}
test triangle {
var target: Target = try .init(std.testing.allocator, 4, 4);
defer target.deinit(std.testing.allocator);
// A texture red on the left, and holes on the right.
const pixels = [_][4]u8{ .{ 200, 0, 0, 255 }, .{ 0, 0, 0, 0 } };
const texture: world.Image = .{ .width = 2, .height = 1, .pixels = &pixels };
// The top left half of the target, the texture across it; corners in either turn draw.
const corners = [3]Corner{
.{ .x = 0, .y = 0, .depth = 1, .u = 0, .v = 0 },
.{ .x = 4, .y = 0, .depth = 1, .u = 1, .v = 0 },
.{ .x = 0, .y = 4, .depth = 1, .u = 0, .v = 1 },
};
triangle(&target, .{ corners[0], corners[2], corners[1] }, &texture, 128, 1);
try std.testing.expectEqual([4]u8{ 100, 0, 0, 255 }, target.colours[0]);
try std.testing.expectEqual(1, target.tags[4]);
// Through the texture's hole, and outside the triangle, nothing is drawn.
try std.testing.expectEqual(0, target.tags[2]);
try std.testing.expectEqual(0, target.tags[15]);
// Farther, it's hidden; nearer, it covers.
triangle(&target, corners, &texture, 256, 2);
try std.testing.expectEqual(1, target.tags[0]);
var nearer = corners;
for (&nearer) |*corner| corner.depth = 2;
triangle(&target, nearer, &texture, 256, 2);
try std.testing.expectEqual([4]u8{ 200, 0, 0, 255 }, target.colours[0]);
try std.testing.expectEqual(2, target.tags[0]);
// Keeping opacity: glass drawn after what's in front of it goes under it, and glass drawn
// over something stays in front.
const glassy = [_][4]u8{.{ 100, 100, 200, 51 }};
const glass: world.Image = .{ .width = 1, .height = 1, .pixels = &glassy };
const opaque_ = [_][4]u8{.{ 200, 0, 0, 255 }};
const frame: world.Image = .{ .width = 1, .height = 1, .pixels = &opaque_ };
var order: [2]Target = .{ try .init(std.testing.allocator, 4, 4), try .init(std.testing.allocator, 4, 4) };
defer for (order) |one| one.deinit(std.testing.allocator);
for (&order) |*one| one.keepsOpacity = true;
triangle(&order[0], corners, &frame, 256, 1);
triangle(&order[0], nearer, &glass, 256, 1);
triangle(&order[1], nearer, &glass, 256, 1);
triangle(&order[1], corners, &frame, 256, 1);
try std.testing.expectEqual(order[0].colours[0], order[1].colours[0]);
try std.testing.expectEqual(255, order[0].colours[0][3]);
try std.testing.expectEqual(2, order[0].depths[0]);
try std.testing.expectEqual(2, order[1].depths[0]);
// Alone, glass keeps how opaque it is.
var alone: Target = try .init(std.testing.allocator, 4, 4);
defer alone.deinit(std.testing.allocator);
alone.keepsOpacity = true;
triangle(&alone, corners, &glass, 256, 1);
try std.testing.expectEqual(51, alone.colours[0][3]);
}
test "Target.clear clears what triangles reached" {
var target: Target = try .init(std.testing.allocator, 8, 8);
defer target.deinit(std.testing.allocator);
try std.testing.expectEqual(Target.Area{}, target.drawn);
const pixels = [_][4]u8{.{ 200, 0, 0, 255 }};
const texture: world.Image = .{ .width = 1, .height = 1, .pixels = &pixels };
// Two triangles apart: what's reached covers both, and what's between them.
triangle(&target, .{ .{ .x = 1, .y = 1, .depth = 1, .u = 0, .v = 0 }, .{ .x = 3, .y = 1, .depth = 1, .u = 0, .v = 0 }, .{ .x = 1, .y = 3, .depth = 1, .u = 0, .v = 0 } }, &texture, 256, 1);
triangle(&target, .{ .{ .x = 5, .y = 4, .depth = 1, .u = 0, .v = 0 }, .{ .x = 7, .y = 4, .depth = 1, .u = 0, .v = 0 }, .{ .x = 5, .y = 6, .depth = 1, .u = 0, .v = 0 } }, &texture, 256, 1);
try std.testing.expectEqual(Target.Area{ .left = 1, .top = 1, .right = 7, .bottom = 6 }, target.drawn);
try std.testing.expectEqual(1, target.tags[1 * 8 + 1]);
try std.testing.expectEqual(1, target.tags[4 * 8 + 5]);
// Cleared, the target is as new.
target.clear();
try std.testing.expectEqual(Target.Area{}, target.drawn);
for (target.tags, target.colours, target.depths) |tag, colour, depth| {
try std.testing.expectEqual(0, tag);
try std.testing.expectEqual([4]u8{ 0, 0, 0, 0 }, colour);
try std.testing.expectEqual(-std.math.inf(f32), depth);
}
}