Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
ReflectionStore.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
17#pragma once
18
19#include "../../core/Types.hpp"
20#include "../../core/Hash.hpp"
22#include "./LightConstants.hpp"
23
24#include <glm/glm.hpp>
25#include <vector>
26
27namespace esengine {
28
31public:
33 struct Probe {
34 glm::vec3 min{0.0f};
35 glm::vec3 max{0.0f};
36 u32 slot = 0;
37 };
38
39 void clear() {
40 probes_.clear();
41 environment_ = 0;
42 }
43
51 bool add(const glm::vec3& min, const glm::vec3& max, u32 slot, u32 environment) {
52 if (environment == 0 || slot == 0 || slot >= MAX_REFLECTION_PROBES) return false;
53 if (max.x <= min.x || max.y <= min.y || max.z <= min.z) return false;
54 // One bake for the frame: the first taken decides it, and a probe naming
55 // another is from a bake this scene is no longer showing.
56 if (environment_ == 0) environment_ = environment;
57 else if (environment_ != environment) return false;
58 probes_.push_back({min, max, slot});
59 return true;
60 }
61
62 bool empty() const { return probes_.empty(); }
63 u32 count() const { return static_cast<u32>(probes_.size()); }
64
69 u64 revision() const {
71 const auto mix = [&h](const void* data, usize bytes) { h = hash::fnv1aBytes(h, data, bytes); };
72 mix(&environment_, sizeof(environment_));
73 for (const Probe& p : probes_) {
74 mix(&p.min, sizeof(p.min));
75 mix(&p.max, sizeof(p.max));
76 mix(&p.slot, sizeof(p.slot));
77 }
78 if (revision_ == 0 || h != revision_hash_) {
79 revision_hash_ = h;
80 revision_ = nextRevision();
81 }
82 return revision_;
83 }
84
85
87 u32 environment() const { return environment_; }
88
96 u32 slotAt(const glm::vec3& p) const {
97 const Probe* best = nullptr;
98 f32 bestSize = 0.0f;
99 for (const Probe& v : probes_) {
100 if (p.x < v.min.x || p.x > v.max.x || p.y < v.min.y || p.y > v.max.y
101 || p.z < v.min.z || p.z > v.max.z) continue;
102 const glm::vec3 e = v.max - v.min;
103 const f32 size = e.x * e.y * e.z;
104 if (!best || size < bestSize) { best = &v; bestSize = size; }
105 }
106 return best ? best->slot : 0u;
107 }
108
114 u32 slotForBox(const glm::vec3& centre, const glm::vec3& half) const {
115 if (const u32 at = slotAt(centre); at != 0) return at;
116 const Probe* best = nullptr;
117 f32 bestSize = 0.0f;
118 for (const Probe& v : probes_) {
119 const glm::vec3 vc = (v.min + v.max) * 0.5f;
120 const glm::vec3 vh = (v.max - v.min) * 0.5f;
121 const glm::vec3 gap = glm::abs(centre - vc) - (half + vh);
122 if (gap.x > 0.0f || gap.y > 0.0f || gap.z > 0.0f) continue;
123 const f32 size = vh.x * vh.y * vh.z;
124 if (!best || size < bestSize) { best = &v; bestSize = size; }
125 }
126 return best ? best->slot : 0u;
127 }
128
130 const std::vector<Probe>& probes() const { return probes_; }
131
132private:
133 std::vector<Probe> probes_;
134 u32 environment_ = 0;
135 mutable u64 revision_ = 0;
136 mutable u64 revision_hash_ = 0;
137};
138
139} // namespace esengine
Per-frame 2D lighting constants — the third tier of the engine's constant-buffer layering (after per-...
What a resource's content is, as a number that changes whenever it does.
Core type definitions and utilities for ESEngine.
The frame's reflection probes, and the lookup that picks one.
const std::vector< Probe > & probes() const
The probes themselves, for the frame block that carries their boxes.
u64 revision() const
What the frame's reflection probes say, as a number that changes whenever it does (see core/Revision....
u32 slotForBox(const glm::vec3 &centre, const glm::vec3 &half) const
The column a whole object reflects, by its world box rather than its origin (ProbeStore::sampleBox's ...
u32 environment() const
The baked environment every probe of this frame reads, or 0.
bool add(const glm::vec3 &min, const glm::vec3 &max, u32 slot, u32 environment)
Takes a probe, or refuses one nothing can read.
u32 slotAt(const glm::vec3 &p) const
The column a point at p reflects, or 0 for the environment.
u64 fnv1aBytes(u64 h, const void *data, usize bytes)
Definition Hash.hpp:21
constexpr u64 kFnv64Offset
Definition Hash.hpp:12
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
constexpr u32 MAX_REFLECTION_PROBES
Columns of the reflection atlas one frame can address — column 0 the environment's,...
u64 nextRevision()
A revision no content has had before. Never 0, which reads as "none".
Definition Revision.hpp:24
float f32
32-bit floating point
Definition Types.hpp:59
One probe: the world box it answers inside, and its atlas column.