63 static constexpr f32 kOpen = std::numeric_limits<f32>::infinity();
73 while (side * 2 <= std::min(std::max(
size, 1u),
kSize)) side *= 2;
74 if (side == size_)
return;
77 while ((side >>= 1) > 0) ++levels_;
86 void begin(
const glm::mat4& viewProjection) {
87 view_projection_ = viewProjection;
100 bool addBox(
const glm::vec3& centre,
const glm::quat& rotation,
const glm::vec3& half) {
101 glm::vec3 corners[8];
102 if (!projectBox(centre, rotation, half, corners))
return false;
107 glm::vec2 lo(
static_cast<f32>(size_)), hi(0.0f);
108 for (
const glm::vec3& c : corners) {
109 lo = glm::min(lo, glm::vec2(c));
110 hi = glm::max(hi, glm::vec2(c));
115 const i32 x0 = std::max(0,
static_cast<i32>(std::floor(lo.x)));
116 const i32 y0 = std::max(0,
static_cast<i32>(std::floor(lo.y)));
117 const i32 x1 = std::min(
static_cast<i32>(size_) - 1,
static_cast<i32>(std::floor(hi.x)));
118 const i32 y1 = std::min(
static_cast<i32>(size_) - 1,
static_cast<i32>(std::floor(hi.y)));
121 if (x1 < x0 || y1 < y0)
return false;
125 for (
const auto& quad : kFaces) {
127 if (buildFace(corners, quad, face)) faces[faceCount++] = face;
130 if (faceCount == 0)
return false;
132 if (boxes_ == 0) base_.fill(
kOpen);
135 for (
i32 y = y0; y <= y1; ++y) {
136 for (
i32 x = x0; x <= x1; ++x) {
137 const glm::vec2 p(
static_cast<f32>(x) + 0.5f,
static_cast<f32>(y) + 0.5f);
139 for (
u32 f = 0; f < faceCount; ++f) {
140 const Face& face = faces[f];
141 if (!face.covers(p))
continue;
145 blocked = std::min(blocked, face.maxDepthOver(p));
147 if (blocked <
kOpen) {
148 f32& cell = base_[
static_cast<usize>(y) * size_ +
static_cast<usize>(x)];
152 cell = std::min(cell, blocked);
164 if (boxes_ == 0)
return;
165 for (
u32 level = 1; level < levels_; ++level) {
166 const u32 size = size_ >> level;
167 const u32 src = size_ >> (level - 1);
168 f32* out = levelData(level);
169 const f32* in = levelData(level - 1);
170 for (
u32 y = 0; y <
size; ++y) {
171 for (
u32 x = 0; x <
size; ++x) {
172 const f32 a = in[(2 * y) * src + 2 * x];
173 const f32 b = in[(2 * y) * src + 2 * x + 1];
174 const f32 c = in[(2 * y + 1) * src + 2 * x];
175 const f32 d = in[(2 * y + 1) * src + 2 * x + 1];
176 out[y *
size + x] = std::max(std::max(a, b), std::max(c, d));
184 bool hidden(
const glm::vec3& centre,
const glm::vec3& half)
const {
185 if (boxes_ == 0)
return false;
187 glm::vec3 corners[8];
188 if (!projectBox(centre, glm::quat(1.0f, 0.0f, 0.0f, 0.0f), half, corners))
return false;
190 glm::vec2 lo(
static_cast<f32>(size_)), hi(0.0f);
192 for (
const glm::vec3& c : corners) {
193 lo = glm::min(lo, glm::vec2(c));
194 hi = glm::max(hi, glm::vec2(c));
195 nearest = std::min(nearest, c.z);
201 const i32 x0 = std::max(0,
static_cast<i32>(std::floor(lo.x)));
202 const i32 y0 = std::max(0,
static_cast<i32>(std::floor(lo.y)));
203 const i32 x1 = std::min(
static_cast<i32>(size_) - 1,
static_cast<i32>(std::floor(hi.x)));
204 const i32 y1 = std::min(
static_cast<i32>(size_) - 1,
static_cast<i32>(std::floor(hi.y)));
205 if (x1 < x0 || y1 < y0)
return false;
211 while (level + 1 < levels_
212 && ((x1 >> level) - (x0 >> level) >=
static_cast<i32>(
kTestSpan)
213 || (y1 >> level) - (y0 >> level) >=
static_cast<i32>(
kTestSpan))) {
216 const u32 size = size_ >> level;
217 const f32* cells = levelData(level);
219 for (
i32 y = y0 >> level; y <= (y1 >> level); ++y) {
220 for (
i32 x = x0 >> level; x <= (x1 >> level); ++x) {
221 farthest = std::max(farthest, cells[
static_cast<usize>(y) *
size
222 +
static_cast<usize>(x)]);
227 return farthest < nearest;
231 bool empty()
const {
return boxes_ == 0; }
245 f32 zx = 0.0f, zy = 0.0f, z0 = 0.0f;
247 bool covers(
const glm::vec2& p)
const {
248 for (
u32 i = 0; i < 4; ++i) {
249 if (edges[i].x * p.x + edges[i].y * p.y + edges[i].z < bias[i])
return false;
254 f32 maxDepthOver(
const glm::vec2& p)
const {
255 return zx * p.x + zy * p.y + z0 + 0.5f * (std::fabs(zx) + std::fabs(zy));
262 static constexpr u32 kFaces[6][4] = {
263 {0, 1, 3, 2}, {4, 6, 7, 5}, {0, 4, 5, 1}, {2, 3, 7, 6}, {0, 2, 6, 4}, {1, 5, 7, 3},
275 bool projectBox(
const glm::vec3& centre,
const glm::quat& rotation,
const glm::vec3& half,
276 glm::vec3 (&out)[8])
const {
277 const glm::mat3 basis = glm::mat3_cast(rotation);
278 for (
u32 i = 0; i < 8; ++i) {
279 const glm::vec3 local((i & 1u) ? half.x : -half.x,
280 (i & 2u) ? half.y : -half.y,
281 (i & 4u) ? half.z : -half.z);
282 const glm::vec4 clip = view_projection_ * glm::vec4(centre + basis * local, 1.0f);
283 if (clip.w <=
kMinW)
return false;
284 const glm::vec3 ndc = glm::vec3(clip) / clip.w;
288 if (ndc.z < -1.0f)
return false;
289 out[i] = {(ndc.x + 1.0f) * 0.5f *
static_cast<f32>(size_),
290 (ndc.y + 1.0f) * 0.5f *
static_cast<f32>(size_), ndc.z};
297 static bool buildFace(
const glm::vec3 (&corners)[8],
const u32 (&quad)[4], Face& out) {
298 const glm::vec3 v[4] = {corners[quad[0]], corners[quad[1]],
299 corners[quad[2]], corners[quad[3]]};
303 for (
u32 i = 0; i < 4; ++i) {
304 const glm::vec3& a = v[i];
305 const glm::vec3& b = v[(i + 1) % 4];
306 area += a.x * b.y - b.x * a.y;
308 if (std::fabs(area) < 1e-3f)
return false;
309 const f32 sign = area > 0.0f ? 1.0f : -1.0f;
311 for (
u32 i = 0; i < 4; ++i) {
312 const glm::vec3& a = v[i];
313 const glm::vec3& b = v[(i + 1) % 4];
314 const f32 ex = -(b.y - a.y) * sign;
315 const f32 ey = (b.x - a.x) * sign;
316 out.edges[i] = {ex, ey, -(ex * a.x + ey * a.y)};
320 out.bias[i] = 0.5f * (std::fabs(ex) + std::fabs(ey));
323 const glm::vec2 d1(v[1].x - v[0].x, v[1].y - v[0].y);
324 const glm::vec2 d2(v[2].x - v[0].x, v[2].y - v[0].y);
325 const f32 det = d1.x * d2.y - d1.y * d2.x;
326 if (std::fabs(det) < 1e-6f)
return false;
327 const f32 dz1 = v[1].z - v[0].z;
328 const f32 dz2 = v[2].z - v[0].z;
329 out.zx = (dz1 * d2.y - dz2 * d1.y) / det;
330 out.zy = (d1.x * dz2 - d2.x * dz1) / det;
331 out.z0 = v[0].z - out.zx * v[0].x - out.zy * v[0].y;
338 f32* levelData(
u32 level) {
return base_.data() + offsetOf(level); }
339 const f32* levelData(
u32 level)
const {
return base_.data() + offsetOf(level); }
343 for (
u32 i = 0; i < level; ++i) offset += static_cast<usize>(size_ >> i) * (size_ >> i);
347 glm::mat4 view_projection_{1.0f};
348 std::array<f32, kCells> base_{};
static constexpr u32 kLevels
64, 32, 16, 8, 4, 2, 1 — down to one texel, so an object covering the whole screen still takes a boun...