Unravel Engine C++ Reference
Loading...
Searching...
No Matches
reflection_probe_component.cpp
Go to the documentation of this file.
2
3namespace unravel
4{
5
7{
8 if(probe_.type == probe_type::sphere)
9 {
10 auto sphere = math::bsphere(math::vec3(0.0f, 0.0f, 0.0f), probe_.sphere_data.range);
11 math::bbox result;
12 result.from_sphere(sphere.position, sphere.radius);
13 return result;
14 }
15 else if(probe_.type == probe_type::box)
16 {
17 math::bbox result;
18 result.min = -probe_.box_data.extents;
19 result.max = probe_.box_data.extents;
20 return result;
21 }
22
23 return {};
24}
25
27 const math::vec3& position,
28 const math::vec3& scale,
29 const math::vec3& view_origin,
30 const math::transform& view,
31 const math::transform& proj) const -> int
32{
33 if(probe_.type == probe_type::sphere)
34 {
36 rect.right,
37 rect.top,
39 position,
40 probe_.sphere_data.range * math::max(scale.x, math::max(scale.y, scale.z)),
41 view_origin,
42 view,
43 proj);
44 }
45 else if(probe_.type == probe_type::box)
46 {
47 float w2 = math::pow(scale.x * probe_.box_data.extents.x * 2.0f, 2.0f);
48 float h2 = math::pow(scale.y * probe_.box_data.extents.y * 2.0f, 2.0f);
49 float l2 = math::pow(scale.z * probe_.box_data.extents.z * 2.0f, 2.0f);
50 float d2 = w2 + h2 + l2;
51 float d = math::sqrt(d2);
52
54 rect.right,
55 rect.top,
57 position,
58 d,
59 view_origin,
60 view,
61 proj);
62 }
63 else
64 {
65 return 1;
66 }
67}
68
70{
71 return rview_[idx];
72}
73
75{
76 auto& tex = rview_[0].tex_get_or_emplace("CUBEMAP");
77 if(!tex)
78 {
79 constexpr uint16_t size = 256;
80 tex = std::make_shared<gfx::texture>(size,
81 true,
82 1,
83 gfx::texture_format::RGBA8,
84 BGFX_TEXTURE_COMPUTE_WRITE |BGFX_TEXTURE_BLIT_DST | BGFX_TEXTURE_RT);
85 }
86
87 return tex;
88}
89
91{
92 auto& tex = rview_[0].tex_get_or_emplace("CUBEMAP_PREFILTERED");
93 if(!tex)
94 {
95 constexpr uint16_t size = 256;
96 tex = std::make_shared<gfx::texture>(size,
97 true,
98 1,
99 gfx::texture_format::RGBA8,
100 BGFX_TEXTURE_COMPUTE_WRITE | BGFX_TEXTURE_BLIT_DST | BGFX_TEXTURE_RT);
101 }
102
103 return tex;
104}
105
107{
108 auto& fbo = rview_[face].fbo_get_or_emplace("CUBEMAP");
109 if(!fbo)
110 {
111 auto& tex = rview_[face].tex_get_or_emplace("CUBEMAP_FACE");
112 if(!tex)
113 {
114 constexpr uint16_t size = 256;
115 tex = std::make_shared<gfx::texture>(size,
116 size,
117 true,
118 1,
119 gfx::texture_format::RGBA8,
120 BGFX_TEXTURE_BLIT_DST | BGFX_TEXTURE_RT);
121 }
122
124 att.layer = 0;
125 att.texture = tex;
126 att.generate_mips = true;
127
128 fbo = std::make_shared<gfx::frame_buffer>();
129 fbo->populate({att});
130 }
131
132
133 return fbo;
134}
135
137{
138
139 // Check if all faces have been generated; if so, reset the state
140 bool fully_generated = true;
141 for(auto& frame : generated_frame_)
142 {
143 fully_generated &= frame != uint64_t(-1);
144 }
145
146
147 if(fully_generated)
148 {
149 for(auto& frame : generated_frame_)
150 {
151 frame = uint64_t(-1); // Reset to an initial invalid state
152 }
153 }
154
155 if(fully_generated)
156 {
157 first_generation_ = false;
158 }
159 generated_faces_count_ = 0; // Reset the count of generated faces
160}
161
163{
164 return probe_;
165}
166
168{
169 if(probe == probe_)
170 {
171 return;
172 }
173
174 touch();
175
176 probe_ = probe;
177}
178
180{
181 bool generated = true;
182 for(size_t i = 0; i < generated_frame_.size(); ++i)
183 {
184 generated &= already_generated(i);
185 }
186 // Check if all faces have been generated in the current cycle
187 return generated;
188}
189
191{
192 if(!first_generation_)
193 {
194 if(generated_faces_count_ == faces_per_frame_)
195 {
196 return true;
197 }
198 }
199
200 // Return true if the face has been generated in the current cycle
201 return generated_frame_[face] != uint64_t(-1);
202}
204{
205 generated_frame_[face] = frame;
206 generated_faces_count_++;
207}
208} // namespace unravel
Provides storage for common representation of spherical bounding volume, and wraps up common function...
Definition bsphere.h:18
General purpose transformation class designed to maintain each component of the transformation separa...
Definition transform.hpp:27
auto compute_projected_sphere_rect(irect32_t &rect, const math::vec3 &position, const math::vec3 &scale, const math::vec3 &view_origin, const math::transform &view, const math::transform &proj) const -> int
Computes the projected sphere rectangle.
auto get_cubemap_fbo(size_t face) -> const gfx::frame_buffer::ptr &
Gets the cubemap frame buffer object (FBO).
auto get_bounds() const -> math::bbox
Gets the bounding box of the probe object.
auto get_probe() const -> const reflection_probe &
Gets the reflection probe object.
void update()
Updates the reflection probe component.
auto get_cubemap_prefiltered() -> const gfx::texture::ptr &
Gets the cubemap texture.
void set_generation_frame(size_t face, uint64_t frame)
marks the genrerated face this frame
auto get_render_view(size_t idx) -> gfx::render_view &
Gets the render view.
auto already_generated() const -> bool
Check if the cubemap was generated this frame.
auto get_cubemap() -> const gfx::texture::ptr &
void set_probe(const reflection_probe &probe)
Sets the reflection probe object.
uint32_t frame
Definition graphics.cpp:21
float scale
Definition hub.cpp:25
Definition bbox.cpp:5
std::uint32_t compute_projected_sphere_rect(std::int32_t &left, std::int32_t &right, std::int32_t &top, std::int32_t &bottom, const glm::vec3 &sphere_center, float radius, const glm::vec3 &view_origin, const glm::mat4 &view, const glm::mat4 &proj)
Definition math.h:223
@ sphere
Sphere type reflection probe.
@ box
Box type reflection probe.
std::shared_ptr< gfx::texture > texture
Texture handle.
std::uint16_t layer
Cubemap side or depth layer/slice.
Storage for box vector values and wraps up common functionality.
Definition bbox.h:21
vec3 max
The maximum vector value of the bounding box.
Definition bbox.h:311
bbox & from_sphere(const vec3 &center, float radius)
Calculates the bounding box based on the sphere specified.
Definition bbox.cpp:213
vec3 min
The minimum vector value of the bounding box.
Definition bbox.h:306
void touch()
Marks the component as 'touched'.
math::vec3 extents
Extents of the box projection.
float range
Range of the sphere projection.
Structure representing a reflection probe.
probe_type type
Type of the reflection probe.
box box_data
Data describing box projection.
sphere sphere_data
Data describing sphere projection.