Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
ClusterSelection.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
13#pragma once
14
15#include "../../core/Types.hpp"
16#include "../../math/Math.hpp"
17
18#include <algorithm>
19#include <cmath>
20#include <cstring>
21#include <limits>
22#include <vector>
23
24namespace esengine::lod {
25
32struct Cluster {
35 f32 centre[3]{};
36 f32 radius = 0.0f;
37 f32 error = 0.0f;
41};
42static_assert(sizeof(Cluster) == 48, "Cluster is the record an .esmesh stores");
43
51 std::vector<Cluster> clusters;
52 std::vector<u32> indices;
53};
54
62struct ClusterEye {
63 glm::vec3 position{0.0f};
64 f32 scale = 1.0f;
65 f32 threshold = 1.0f;
67 bool flat = false;
68};
69
71inline constexpr f32 kClusterNearGap = 1e-6f;
72
74inline f32 clusterErrorOnScreen(const ClusterEye& eye, const f32* centre, f32 radius, f32 error) {
75 if (!(error < std::numeric_limits<f32>::infinity())) return std::numeric_limits<f32>::infinity();
76 if (eye.flat) return error * eye.scale;
77 const glm::vec3 to = glm::vec3(centre[0], centre[1], centre[2]) - eye.position;
78 return error * eye.scale / std::max(glm::length(to) - radius, kClusterNearGap);
79}
80
82inline bool clusterDrawn(const ClusterEye& eye, const Cluster& cluster) {
83 if (clusterErrorOnScreen(eye, cluster.centre, cluster.radius, cluster.error) > eye.threshold) return false;
84 return clusterErrorOnScreen(eye, cluster.groupCentre, cluster.groupRadius, cluster.groupError) > eye.threshold;
85}
86
88inline u32 selectClusters(const ClusterHierarchy& hierarchy, const ClusterEye& eye, std::vector<u32>& out) {
89 out.clear();
90 u32 indices = 0;
91 const u32 count = static_cast<u32>(hierarchy.clusters.size());
92 for (u32 i = 0; i < count; ++i) {
93 const Cluster& cluster = hierarchy.clusters[i];
94 if (!clusterDrawn(eye, cluster)) continue;
95 out.push_back(i);
96 indices += cluster.indexCount;
97 }
98 return indices;
99}
100
102inline u64 cutSignature(const std::vector<u32>& picked) {
103 u64 hash = 0xcbf29ce484222325ull;
104 for (const u32 id : picked) {
105 hash ^= id;
106 hash *= 0x100000001b3ull;
107 }
108 return hash ^ (static_cast<u64>(picked.size()) << 40);
109}
110
117inline void assembleCut(const ClusterHierarchy& hierarchy, const std::vector<u32>& picked, u32 vertexBase,
118 std::vector<u32>& out) {
119 usize total = 0;
120 for (const u32 id : picked) total += hierarchy.clusters[id].indexCount;
121 out.resize(total);
122 usize at = 0;
123 for (const u32 id : picked) {
124 const Cluster& cluster = hierarchy.clusters[id];
125 const u32* from = hierarchy.indices.data() + cluster.firstIndex;
126 if (vertexBase == 0) {
127 std::memcpy(out.data() + at, from, static_cast<usize>(cluster.indexCount) * sizeof(u32));
128 } else {
129 for (u32 i = 0; i < cluster.indexCount; ++i) out[at + i] = from[i] + vertexBase;
130 }
131 at += cluster.indexCount;
132 }
133}
134
140inline bool clusterHierarchyValid(const ClusterHierarchy& hierarchy, u32 vertexCount) {
141 const usize total = hierarchy.indices.size();
142 for (const Cluster& cluster : hierarchy.clusters) {
143 if (cluster.indexCount % 3 != 0) return false;
144 if (cluster.firstIndex > total || cluster.indexCount > total - cluster.firstIndex) return false;
145 if (!(cluster.groupError >= cluster.error)) return false;
146 }
147 for (const u32 index : hierarchy.indices) if (index >= vertexCount) return false;
148 return !hierarchy.clusters.empty();
149}
150
151} // namespace esengine::lod
Math utilities and GLM configuration for ESEngine.
Core type definitions and utilities for ESEngine.
constexpr f32 kClusterNearGap
The nearest a sphere's surface is taken to be: an eye inside one sees its error as large as it can be...
bool clusterHierarchyValid(const ClusterHierarchy &hierarchy, u32 vertexCount)
Whether hierarchy is one a cut can be assembled from: every run inside the indices,...
u32 selectClusters(const ClusterHierarchy &hierarchy, const ClusterEye &eye, std::vector< u32 > &out)
The clusters eye draws, in the hierarchy's order, into out. Returns the indices they hold together.
f32 clusterErrorOnScreen(const ClusterEye &eye, const f32 *centre, f32 radius, f32 error)
The size on screen of error, read at the nearest point of the sphere it belongs to.
u64 cutSignature(const std::vector< u32 > &picked)
A number that changes when the picked clusters do: two cuts with the same one are drawn from the same...
bool clusterDrawn(const ClusterEye &eye, const Cluster &cluster)
Whether cluster is the level of its surface that eye draws.
void assembleCut(const ClusterHierarchy &hierarchy, const std::vector< u32 > &picked, u32 vertexBase, std::vector< u32 > &out)
The picked clusters' indices, one run after another, into out.
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
float f32
32-bit floating point
Definition Types.hpp:59
An eye as a hierarchy measures from it.
bool flat
An orthographic view: an error is the same size at every distance.
A mesh's clusters and the indices they name, every level together.
One cluster: its run of the hierarchy's indices, its own bounds and its group's.