Unravel Engine C++ Reference
Loading...
Searching...
No Matches
light_component.cpp
Go to the documentation of this file.
1#include "light_component.h"
2namespace unravel
3{
5{
6 return light_;
7}
8
10{
11 light_ = l;
12}
13
14auto light_component::get_bounds_sphere_impl(const math::vec3* light_direction) const -> math::bsphere
15{
16 math::bsphere result;
17
18 if(light_.type == light_type::point)
19 {
20 result = math::bsphere(math::vec3(0.0f, 0.0f, 0.0f), light_.point_data.range);
21 }
22 else if(light_.type == light_type::spot)
23 {
24 float range = light_.spot_data.get_range();
25
26 if(light_direction)
27 {
28 float clamped_inner_cone_angle =
29 math::radians(math::clamp(light_.spot_data.get_inner_angle(), 0.0f, 89.0f));
30 float clamped_outer_cone_angle = math::clamp(math::radians(light_.spot_data.get_outer_angle()),
31 clamped_inner_cone_angle + 0.001f,
32 math::radians(89.0f) + 0.001f);
33 float cos_outer_cone = math::cos(clamped_outer_cone_angle);
34 // Use the law of cosines to find the distance to the furthest edge of the
35 // spotlight cone from a
36 // position that is halfway down the spotlight direction
37 const float radius = math::sqrt(1.25f * range * range - range * range * cos_outer_cone);
38 math::vec3 center = math::vec3(0.0f, 0.0f, 0.0f) + 0.5f * (*light_direction) * range;
39
40 result = math::bsphere(center, radius);
41 }
42 else
43 {
44 result = math::bsphere(math::vec3(0.0f, 0.0f, 0.0f), range);
45 }
46 }
47 else
48 {
49 result = math::bsphere(math::vec3(0.0f, 0.0f, 0.0f), 999999999.0f);
50 }
51
52 return result;
53}
54
56{
57 return get_bounds_sphere_impl(nullptr);
58}
59
60auto light_component::get_bounds_sphere_precise(const math::vec3& light_direction) const -> math::bsphere
61{
62 return get_bounds_sphere_impl(&light_direction);
63}
64
65auto light_component::get_bounds() const -> math::bbox
66{
68 math::bbox result;
69 result.from_sphere(sphere.position, sphere.radius);
70 return result;
71}
72
73auto light_component::get_bounds_precise(const math::vec3& light_direction) const -> math::bbox
74{
75 auto sphere = get_bounds_sphere_precise(light_direction);
76 math::bbox result;
77 result.from_sphere(sphere.position, sphere.radius);
78 return result;
79}
80
82 const math::vec3& light_position,
83 const math::vec3& light_direction,
84 const math::vec3& view_origin,
85 const math::transform& view,
86 const math::transform& proj)
87{
88 if(light_.type == light_type::point)
89 {
91 rect.right,
92 rect.top,
94 light_position,
95 light_.point_data.range,
96 view_origin,
97 view,
98 proj);
99 }
100 else if(light_.type == light_type::spot)
101 {
102 float range = light_.spot_data.get_range();
103 float clamped_inner_cone_angle = math::radians(math::clamp(light_.spot_data.get_inner_angle(), 0.0f, 89.0f));
104 float clamped_outer_cone_angle = math::clamp(math::radians(light_.spot_data.get_outer_angle()),
105 clamped_inner_cone_angle + 0.001f,
106 math::radians(89.0f) + 0.001f);
107 float cos_outer_cone = math::cos(clamped_outer_cone_angle);
108 // Use the law of cosines to find the distance to the furthest edge of the
109 // spotlight cone from a
110 // position that is halfway down the spotlight direction
111 const float radius = math::sqrt(1.25f * range * range - range * range * cos_outer_cone);
112 math::vec3 center = light_position + 0.5f * light_direction * range;
113
115 rect.right,
116 rect.top,
117 rect.bottom,
118 center,
119 radius,
120 view_origin,
121 view,
122 proj);
123 }
124 else
125 {
126 return 1;
127 }
128}
129
131{
132 return *shadowmap_generator_;
133}
134
135auto skylight_component::get_mode() const noexcept -> const sky_mode&
136{
137 return mode_;
138}
140{
141 mode_ = mode;
142}
143
144auto skylight_component::get_turbidity() const noexcept -> float
145{
146 return turbidity_;
147}
148
150{
151 turbidity_ = math::clamp(turbidity, 1.9f, 10.0f);
152}
153
155{
156 return cloud_mode_;
157}
158
160{
161 cloud_mode_ = mode;
162}
163
164auto skylight_component::get_cloud_coverage() const noexcept -> float
165{
166 return cloud_coverage_;
167}
168
170{
171 cloud_coverage_ = math::clamp(coverage, 0.0f, 1.0f);
172}
173
175{
176 return cloud_base_altitude_;
177}
178
180{
181 cloud_base_altitude_ = math::max(altitude, 100.0f);
182}
183
184auto skylight_component::get_cloud_top_altitude() const noexcept -> float
185{
186 return cloud_top_altitude_;
187}
188
190{
191 cloud_top_altitude_ = math::max(altitude, cloud_base_altitude_ + 100.0f);
192}
193
194auto skylight_component::get_cloud_speed() const noexcept -> float
195{
196 return cloud_speed_;
197}
198
200{
201 cloud_speed_ = math::clamp(speed, 0.0f, 200.0f);
202}
203
204auto skylight_component::get_cloud_density() const noexcept -> float
205{
206 return cloud_density_;
207}
208
210{
211 cloud_density_ = math::max(density, 0.0f);
212}
213
214auto skylight_component::get_cloud_absorption() const noexcept -> float
215{
216 return cloud_absorption_;
217}
218
220{
221 cloud_absorption_ = math::clamp(absorption, 0.01f, 0.5f);
222}
223
225{
226 return cloud_light_absorption_;
227}
228
230{
231 cloud_light_absorption_ = math::clamp(absorption, 0.01f, 0.5f);
232}
233
234auto skylight_component::get_cloud_vol_uv_scale() const noexcept -> float
235{
236 return cloud_vol_uv_scale_;
237}
238
240{
241 cloud_vol_uv_scale_ = math::clamp(scale, 1.0e-6f, 0.01f);
242}
243
245{
246 return cloud_vol_edge_width_;
247}
248
250{
251 cloud_vol_edge_width_ = math::clamp(width, 0.01f, 0.8f);
252}
253
255{
256 return cloud_vol_shape_power_;
257}
258
260{
261 cloud_vol_shape_power_ = math::clamp(power, 0.5f, 4.0f);
262}
263
265{
266 return cloud_vol_detail_erode_;
267}
268
270{
271 cloud_vol_detail_erode_ = math::clamp(erode, 0.0f, 2.0f);
272}
273
275{
276 return cloud_vol_macro_strength_;
277}
278
280{
281 cloud_vol_macro_strength_ = math::clamp(strength, 0.0f, 1.5f);
282}
283
285{
286 return cloud_vol_coarse_scale_;
287}
288
290{
291 cloud_vol_coarse_scale_ = math::clamp(scale, 0.05f, 1.0f);
292}
293
294auto skylight_component::get_cloud_vol_base_mix() const noexcept -> float
295{
296 return cloud_vol_base_mix_;
297}
298
300{
301 cloud_vol_base_mix_ = math::clamp(mix, 0.0f, 1.0f);
302}
303
305{
306 return cloud_vol_sun_intensity_;
307}
308
310{
311 cloud_vol_sun_intensity_ = math::clamp(intensity, 0.0f, 200.0f);
312}
313
315{
316 return irradiance_intensity_;
317}
318
320{
321 irradiance_intensity_ = math::max(intensity, 0.0f);
322}
323
324auto skylight_component::get_irradiance_tint() const noexcept -> const math::color&
325{
326 return irradiance_tint_;
327}
328
330{
331 irradiance_tint_ = color;
332}
333
335{
336 return irradiance_quality_;
337}
338
340{
341 irradiance_quality_ = quality;
342}
343
345{
346 return irradiance_use_sky_;
347}
348
350{
351 irradiance_use_sky_ = use_sky;
352}
353
354auto skylight_component::get_sky_brightness() const noexcept -> float
355{
356 return sky_brightness_;
357}
358
360{
361 sky_brightness_ = math::max(brightness, 0.0f);
362}
363
364} // namespace unravel
uint32_t width
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 get_bounds() const -> math::bbox
Gets the bounding box of the light object.
void set_light(const light &l)
Sets the light object.
auto get_bounds_sphere() const -> math::bsphere
auto get_light() const -> const light &
Gets the light object.
auto compute_projected_sphere_rect(irect32_t &rect, const math::vec3 &light_position, const math::vec3 &light_direction, const math::vec3 &view_origin, const math::transform &view, const math::transform &proj) -> int
Computes the projected sphere rectangle.
auto get_shadowmap_generator() -> shadow::shadowmap_generator &
Gets the shadow map generator.
auto get_bounds_precise(const math::vec3 &light_direction) const -> math::bbox
Gets the bounding box of the light object.
auto get_bounds_sphere_precise(const math::vec3 &light_direction) const -> math::bsphere
Shadow mapping generator with improved algorithms for high-altitude cameras.
Definition shadow.h:654
Class that contains sky light data.
void set_mode(const sky_mode &mode)
Sets the sky mode.
auto get_cloud_top_altitude() const noexcept -> float
Gets the cloud top altitude in world units (volumetric slab top).
void set_cloud_vol_shape_power(float power)
auto get_irradiance_intensity() const noexcept -> float
Gets the irradiance intensity (strength of indirect diffuse).
void set_irradiance_tint(const math::color &color)
Sets the irradiance tint override.
void set_irradiance_intensity(float intensity)
Sets the irradiance intensity.
auto get_irradiance_tint() const noexcept -> const math::color &
Gets the optional irradiance tint override (default from sky when white).
void set_irradiance_use_sky(bool use_sky)
Sets whether the sky color contributes to the ambient irradiance.
void set_cloud_vol_macro_strength(float strength)
void set_sky_brightness(float brightness)
Sets the sky brightness multiplier.
void set_cloud_light_absorption(float absorption)
Sets the cloud light absorption (self-shadow strength).
void set_cloud_vol_edge_width(float width)
auto get_cloud_density() const noexcept -> float
Gets the cloud density multiplier.
void set_cloud_vol_detail_erode(float erode)
auto get_sky_brightness() const noexcept -> float
Gets the sky brightness multiplier (affects atmospheric pass and irradiance).
auto get_cloud_vol_coarse_scale() const noexcept -> float
Volumetric cloud shape: coarse noise sample scale (larger masses).
auto get_irradiance_quality() const noexcept -> irradiance_quality
Gets the irradiance quality for indirect diffuse.
auto get_cloud_absorption() const noexcept -> float
Gets the cloud absorption coefficient (Beer-Lambert extinction).
void set_cloud_mode(cloud_mode mode)
Sets the cloud rendering mode.
auto get_cloud_vol_detail_erode() const noexcept -> float
Volumetric cloud shape: Worley detail erosion strength.
auto get_cloud_vol_edge_width() const noexcept -> float
Volumetric cloud shape: smoothstep edge width for the density mask.
auto get_cloud_coverage() const noexcept -> float
Gets the cloud coverage value.
void set_cloud_vol_uv_scale(float scale)
auto get_cloud_vol_macro_strength() const noexcept -> float
Volumetric cloud shape: macro Worley threshold jitter (clearings vs sheet).
auto get_turbidity() const noexcept -> float
Gets the current turbidity value.
auto get_mode() const noexcept -> const sky_mode &
Gets the current sky mode.
auto get_cloud_vol_shape_power() const noexcept -> float
Volumetric cloud shape: contrast on the density mask (pow exponent).
auto get_cloud_vol_uv_scale() const noexcept -> float
Volumetric cloud shape: world-to-noise UV scale (smaller = larger features).
void set_cloud_absorption(float absorption)
Sets the cloud absorption coefficient.
auto get_irradiance_use_sky() const noexcept -> bool
Gets whether the sky color contributes to the ambient irradiance.
void set_cloud_vol_sun_intensity(float intensity)
auto get_cloud_vol_sun_intensity() const noexcept -> float
Volumetric cloud lighting: sun contribution multiplier in the cloud integrator.
void set_turbidity(float turbidity)
Sets the turbidity value.
irradiance_quality
Directional variation of the indirect diffuse ambient.
auto get_cloud_vol_base_mix() const noexcept -> float
Volumetric cloud shape: blend between coarse and fine base noise.
void set_cloud_coverage(float coverage)
Sets the cloud coverage value.
auto get_cloud_light_absorption() const noexcept -> float
Gets the cloud light absorption (self-shadow strength).
void set_irradiance_quality(irradiance_quality quality)
Sets the irradiance quality.
auto get_cloud_mode() const noexcept -> cloud_mode
Gets the cloud rendering mode.
void set_cloud_vol_coarse_scale(float scale)
void set_cloud_top_altitude(float altitude)
Sets the cloud top altitude. Must be greater than base altitude.
void set_cloud_base_altitude(float altitude)
Sets the cloud base altitude.
sky_mode
Enumeration for sky modes.
auto get_cloud_base_altitude() const noexcept -> float
Gets the cloud base altitude in world units.
void set_cloud_speed(float speed)
Sets the cloud speed multiplier.
auto get_cloud_speed() const noexcept -> float
Gets the cloud speed multiplier.
void set_cloud_density(float density)
Sets the cloud density multiplier.
uint16_t view
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:227
@ sphere
Sphere type reflection probe.
std::vector< math::color > color
std::vector< float > scale
Storage for box vector values and wraps up common functionality.
Definition bbox.h:21
bbox & from_sphere(const vec3 &center, float radius)
Calculates the bounding box based on the sphere specified.
Definition bbox.cpp:213
float range
The range of the point light.
Definition light.h:162
float get_range() const
Gets the range of the spot light.
Definition light.h:106
float get_outer_angle() const
Gets the outer angle of the spot light.
Definition light.h:121
float get_inner_angle() const
Gets the inner angle of the spot light.
Definition light.h:136
Struct representing a light.
Definition light.h:87
light_type type
The type of the light.
Definition light.h:89
point point_data
Data specific to point lights.
Definition light.h:203
spot spot_data
Data specific to spot lights.
Definition light.h:201