Antkeeper  0.0.1
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
directional-light.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_SCENE_DIRECTIONAL_LIGHT_HPP
21 #define ANTKEEPER_SCENE_DIRECTIONAL_LIGHT_HPP
22 
23 #include <engine/scene/light.hpp>
24 #include <engine/gl/texture.hpp>
25 #include <engine/math/vector.hpp>
26 #include <memory>
27 #include <span>
28 
29 namespace scene {
30 
34 class directional_light: public light
35 {
36 public:
39 
41  [[nodiscard]] inline light_type get_light_type() const noexcept override
42  {
44  }
45 
48 
54  void set_direction(const math::fvec3& direction);
55 
61  inline void set_color(const math::fvec3& color) noexcept
62  {
63  m_color = color;
64  color_updated();
65  }
66 
72  inline void set_illuminance(float illuminance) noexcept
73  {
74  m_illuminance = illuminance;
75  illuminance_updated();
76  }
77 
79  [[nodiscard]] inline constexpr const math::fvec3& get_direction() const noexcept
80  {
81  return m_direction;
82  }
83 
85  [[nodiscard]] inline constexpr const math::fvec3& get_color() const noexcept
86  {
87  return m_color;
88  }
89 
91  [[nodiscard]] inline constexpr float get_illuminance() const noexcept
92  {
93  return m_illuminance;
94  }
95 
97  [[nodiscard]] inline constexpr const math::fvec3& get_colored_illuminance() const noexcept
98  {
99  return m_colored_illuminance;
100  }
101 
103 
106 
112  void set_shadow_caster(bool caster) noexcept;
113 
119  void set_shadow_framebuffer(std::shared_ptr<gl::framebuffer> framebuffer) noexcept;
120 
126  void set_shadow_bias(float bias) noexcept;
127 
135  void set_shadow_cascade_count(unsigned int count) noexcept;
136 
142  void set_shadow_max_distance(float distance) noexcept;
143 
149  void set_shadow_fade_range(float range) noexcept;
150 
156  void set_shadow_cascade_distribution(float weight) noexcept;
157 
159  [[nodiscard]] inline constexpr bool is_shadow_caster() const noexcept
160  {
161  return m_shadow_caster;
162  }
163 
165  [[nodiscard]] inline constexpr const std::shared_ptr<gl::framebuffer>& get_shadow_framebuffer() const noexcept
166  {
167  return m_shadow_framebuffer;
168  }
169 
171  [[nodiscard]] inline constexpr const std::shared_ptr<gl::texture_2d>& get_shadow_texture() const noexcept
172  {
173  return m_shadow_texture;
174  }
175 
177  [[nodiscard]] inline constexpr float get_shadow_bias() const noexcept
178  {
179  return m_shadow_bias;
180  }
181 
183  [[nodiscard]] inline constexpr unsigned int get_shadow_cascade_count() const noexcept
184  {
185  return m_shadow_cascade_count;
186  }
187 
189  [[nodiscard]] inline constexpr float get_shadow_max_distance() const noexcept
190  {
191  return m_shadow_max_distance;
192  }
193 
195  [[nodiscard]] inline constexpr float get_shadow_fade_range() const noexcept
196  {
197  return m_shadow_fade_range;
198  }
199 
201  [[nodiscard]] inline constexpr float get_shadow_cascade_distribution() const noexcept
202  {
203  return m_shadow_cascade_distribution;
204  }
205 
208  [[nodiscard]] inline constexpr const math::fvec4& get_shadow_cascade_distances() const noexcept
209  {
210  return m_shadow_cascade_distances;
211  }
212  [[nodiscard]] inline constexpr math::fvec4& get_shadow_cascade_distances() noexcept
213  {
214  return m_shadow_cascade_distances;
215  }
217 
219  [[nodiscard]] inline constexpr std::span<const math::fmat4> get_shadow_scale_bias_matrices() const noexcept
220  {
221  return m_shadow_scale_bias_matrices;
222  }
223 
226  [[nodiscard]] inline constexpr std::span<const math::fmat4> get_shadow_cascade_matrices() const noexcept
227  {
228  return m_shadow_cascade_matrices;
229  }
230  [[nodiscard]] inline constexpr std::span<math::fmat4> get_shadow_cascade_matrices() noexcept
231  {
232  return m_shadow_cascade_matrices;
233  }
235 
237 
238 private:
239  void transformed() override;
240  void color_updated();
241  void illuminance_updated();
242  void update_shadow_scale_bias_matrices();
243  void update_shadow_cascade_distances();
244 
245  math::fvec3 m_direction{0.0f, 0.0f, -1.0f};
246  math::fvec3 m_color{1.0f, 1.0f, 1.0f};
247  float m_illuminance{};
248  math::fvec3 m_colored_illuminance{};
249 
250  bool m_shadow_caster{false};
251  std::shared_ptr<gl::framebuffer> m_shadow_framebuffer;
252  std::shared_ptr<gl::texture_2d> m_shadow_texture;
253  float m_shadow_bias{0.005f};
254  unsigned int m_shadow_cascade_count{4};
255  float m_shadow_max_distance{100.0f};
256  float m_shadow_fade_range{0.0f};
257  float m_shadow_cascade_distribution{0.8f};
258  math::fvec4 m_shadow_cascade_distances;
259  math::fmat4 m_shadow_cascade_matrices[4];
260  math::fmat4 m_shadow_scale_bias_matrices[4];
261 };
262 
263 } // namespace scene
264 
265 #endif // ANTKEEPER_SCENE_DIRECTIONAL_LIGHT_HPP
Light source with parallel rays and constant intensity.
void set_shadow_fade_range(float range) noexcept
Sets the distance from the maximum shadow distance at which shadows will begin to fade out.
void set_direction(const math::fvec3 &direction)
Sets the direction of the directional light.
light_type get_light_type() const noexcept override
Returns light_type::directional.
constexpr float get_shadow_fade_range() const noexcept
Returns the distance from the maximum shadow distance at which shadows will begin to fade out.
constexpr bool is_shadow_caster() const noexcept
Returns true if the light casts shadows, false otherwise.
void set_shadow_framebuffer(std::shared_ptr< gl::framebuffer > framebuffer) noexcept
Sets the shadow map framebuffer.
constexpr const std::shared_ptr< gl::framebuffer > & get_shadow_framebuffer() const noexcept
Returns the shadow map framebuffer, of nullptr if no shadow map framebuffer is set.
void set_shadow_cascade_count(unsigned int count) noexcept
Sets the number of shadow cascades.
constexpr const math::fvec3 & get_colored_illuminance() const noexcept
Returns the color-modulated illuminance of the light on a surface perpendicular to the light directio...
void set_shadow_max_distance(float distance) noexcept
Sets the maximum distance from a camera's near clipping plane up to which shadows are visible.
constexpr const math::fvec4 & get_shadow_cascade_distances() const noexcept
Returns the array of shadow cascade far clipping plane distances.
constexpr float get_shadow_bias() const noexcept
Returns the shadow bias factor.
void set_illuminance(float illuminance) noexcept
Sets the illuminance of the light on a surface perpendicular to the light direction.
directional_light()
Creates a directional light.
constexpr const std::shared_ptr< gl::texture_2d > & get_shadow_texture() const noexcept
Returns the shadow map texture.
constexpr float get_shadow_max_distance() const noexcept
Returns the maximum distance from a camera's near clipping plane up to which shadows are visible.
void set_color(const math::fvec3 &color) noexcept
Sets the color of the light.
constexpr const math::fvec3 & get_color() const noexcept
Returns the color of the light.
constexpr std::span< const math::fmat4 > get_shadow_cascade_matrices() const noexcept
Returns the array of world-space to cascade texture-space transformation matrices.
void set_shadow_bias(float bias) noexcept
Sets the shadow bias factor for reducing self-shadowing.
constexpr const math::fvec3 & get_direction() const noexcept
Returns a unit vector pointing in the light direction.
constexpr unsigned int get_shadow_cascade_count() const noexcept
Returns the number of shadow cascades.
constexpr math::fvec4 & get_shadow_cascade_distances() noexcept
Enables or disables shadow casting.
void set_shadow_caster(bool caster) noexcept
Enables or disables shadow casting.
void set_shadow_cascade_distribution(float weight) noexcept
Sets the shadow cascade distribution.
constexpr std::span< math::fmat4 > get_shadow_cascade_matrices() noexcept
Returns the array of world-space to cascade texture-space transformation matrices.
constexpr float get_shadow_cascade_distribution() const noexcept
Returns the shadow cascade distribution weight.
constexpr float get_illuminance() const noexcept
Returns the illuminance of the light on a surface perpendicular to the light direction.
constexpr std::span< const math::fmat4 > get_shadow_scale_bias_matrices() const noexcept
Returns the array of shadow cascade scale-bias matrices.
Abstract base class for light objects.
Definition: scene/light.hpp:32
constexpr int count(T x) noexcept
Returns the number of set bits in a value, known as a population count or Hamming weight.
Definition: bit-math.hpp:211
Color science.
Definition: aces.hpp:27
T distance(const vector< T, N > &p0, const vector< T, N > &p1)
Calculates the distance between two points.
Definition: vector.hpp:1106
3D scene.
Definition: context.hpp:28
light_type
Light types.
Definition: light-type.hpp:29
@ directional
Directional light.
n by m column-major matrix.
Definition: math/matrix.hpp:44
n-dimensional vector.
Definition: vector.hpp:44