💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
7.0 KiB

  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. #ifndef ANTKEEPER_SCENE_DIRECTIONAL_LIGHT_HPP
  20. #define ANTKEEPER_SCENE_DIRECTIONAL_LIGHT_HPP
  21. #include "scene/light.hpp"
  22. #include "gl/texture-2d.hpp"
  23. #include "utility/fundamental-types.hpp"
  24. #include <vector>
  25. namespace scene {
  26. /**
  27. * Light source with parallel rays and constant intensity.
  28. */
  29. class directional_light: public light
  30. {
  31. public:
  32. /// Creates a directional light.
  33. directional_light();
  34. /**
  35. * Enables or disables shadow casting.
  36. *
  37. * @param caster `true` if the light should cast shadows, `false` otherwise.
  38. */
  39. void set_shadow_caster(bool caster) noexcept;
  40. /**
  41. * Sets the shadow map framebuffer.
  42. *
  43. * @param framebuffer Pointer to a shadow map framebuffer.
  44. */
  45. void set_shadow_framebuffer(const gl::framebuffer* framebuffer) noexcept;
  46. /**
  47. * Sets the shadow bias factor for reducing self-shadowing.
  48. *
  49. * @param bias Shadow bias factor.
  50. */
  51. void set_shadow_bias(float bias) noexcept;
  52. /**
  53. * Sets the number of shadow cascades.
  54. *
  55. * @param count Number of shadow cascades.
  56. */
  57. void set_shadow_cascade_count(unsigned int count) noexcept;
  58. /**
  59. * Sets the shadow cascade coverage factor.
  60. *
  61. * @param factor Percentage of the view frustum clipping range covered by shadow cascades. A value of `1.0` results in full coverage of the view frustum clipping range, `0.5` results in coverage of half of the clipping range, etc.
  62. */
  63. void set_shadow_cascade_coverage(float factor) noexcept;
  64. /**
  65. * Sets the shadow cascade distribution.
  66. *
  67. * @param weight Linear interpolation weight between uniform and logarithmic cascade distributions. A weight of `0.0` results in a uniform cascade distribution, while `1.0` results in a logarithmic distribution.
  68. */
  69. void set_shadow_cascade_distribution(float weight) noexcept;
  70. /**
  71. * Sets the light texture, also known as a gobo, cucoloris, or cookie.
  72. *
  73. * @param texture Light texture.
  74. */
  75. void set_light_texture(const gl::texture_2d* texture);
  76. /**
  77. * Sets the opacity of the light texture.
  78. *
  79. * @param opacity Light texture opacity, on `[0, 1]`.
  80. */
  81. void set_light_texture_opacity(float opacity);
  82. /**
  83. * Sets the scale of the light texture.
  84. *
  85. * @param scale Scale of the light texture.
  86. */
  87. void set_light_texture_scale(const float2& scale);
  88. /// Returns light_type::directional.
  89. virtual light_type get_light_type() const;
  90. /// Returns the normalized direction vector of the light.
  91. const float3& get_direction() const;
  92. /// Returns `true` if the light casts shadows, `false` otherwise.
  93. bool is_shadow_caster() const noexcept;
  94. /// Returns the shadow map framebuffer, of `nullptr` if no shadow map framebuffer is set.
  95. const gl::framebuffer* get_shadow_framebuffer() const noexcept;
  96. /// Returns the shadow bias factor.
  97. float get_shadow_bias() const noexcept;
  98. /// Returns the number of shadow cascades.
  99. unsigned int get_shadow_cascade_count() const noexcept;
  100. /// Returns the shadow cascade coverage factor.
  101. float get_shadow_cascade_coverage() const noexcept;
  102. /// Returns the shadow cascade distribution weight.
  103. float get_shadow_cascade_distribution() const noexcept;
  104. /// Returns the array of shadow cascade far clipping plane distances.
  105. float* get_shadow_cascade_distances() const noexcept;
  106. /// Returns the array of world-space to cascade texture-space transformation matrices.
  107. float4x4* get_shadow_cascade_matrices() const noexcept;
  108. /// Returns the light texture for this light, or `nullptr` if no light texture is set.
  109. const gl::texture_2d* get_light_texture() const;
  110. /// Returns the light texture opacity.
  111. float get_light_texture_opacity() const;
  112. /// Returns the light texture scale.
  113. const float2& get_light_texture_scale() const;
  114. /// Returns the light direction tween.
  115. const tween<float3>& get_direction_tween() const;
  116. /// Returns the light texture opacity tween.
  117. const tween<float>& get_light_texture_opacity_tween() const;
  118. /// Returns the light texture scale tween.
  119. const tween<float2>& get_light_texture_scale_tween() const;
  120. /// @copydoc object_base::update_tweens();
  121. virtual void update_tweens();
  122. private:
  123. virtual void transformed();
  124. tween<float3> direction;
  125. bool shadow_caster;
  126. const gl::framebuffer* shadow_framebuffer;
  127. float shadow_bias;
  128. unsigned int shadow_cascade_count;
  129. float shadow_cascade_coverage;
  130. float shadow_cascade_distribution;
  131. mutable std::vector<float> shadow_cascade_distances;
  132. mutable std::vector<float4x4> shadow_cascade_matrices;
  133. const gl::texture_2d* light_texture;
  134. tween<float> light_texture_opacity;
  135. tween<float2> light_texture_scale;
  136. };
  137. inline light_type directional_light::get_light_type() const
  138. {
  139. return light_type::directional;
  140. }
  141. inline const float3& directional_light::get_direction() const
  142. {
  143. return direction[1];
  144. }
  145. inline bool directional_light::is_shadow_caster() const noexcept
  146. {
  147. return shadow_caster;
  148. }
  149. inline const gl::framebuffer* directional_light::get_shadow_framebuffer() const noexcept
  150. {
  151. return shadow_framebuffer;
  152. }
  153. inline float directional_light::get_shadow_bias() const noexcept
  154. {
  155. return shadow_bias;
  156. }
  157. inline unsigned int directional_light::get_shadow_cascade_count() const noexcept
  158. {
  159. return shadow_cascade_count;
  160. }
  161. inline float directional_light::get_shadow_cascade_coverage() const noexcept
  162. {
  163. return shadow_cascade_coverage;
  164. }
  165. inline float directional_light::get_shadow_cascade_distribution() const noexcept
  166. {
  167. return shadow_cascade_distribution;
  168. }
  169. inline float* directional_light::get_shadow_cascade_distances() const noexcept
  170. {
  171. return shadow_cascade_distances.data();
  172. }
  173. inline float4x4* directional_light::get_shadow_cascade_matrices() const noexcept
  174. {
  175. return shadow_cascade_matrices.data();
  176. }
  177. inline const gl::texture_2d* directional_light::get_light_texture() const
  178. {
  179. return light_texture;
  180. }
  181. inline float directional_light::get_light_texture_opacity() const
  182. {
  183. return light_texture_opacity[1];
  184. }
  185. inline const float2& directional_light::get_light_texture_scale() const
  186. {
  187. return light_texture_scale[1];
  188. }
  189. inline const tween<float3>& directional_light::get_direction_tween() const
  190. {
  191. return direction;
  192. }
  193. inline const tween<float>& directional_light::get_light_texture_opacity_tween() const
  194. {
  195. return light_texture_opacity;
  196. }
  197. inline const tween<float2>& directional_light::get_light_texture_scale_tween() const
  198. {
  199. return light_texture_scale;
  200. }
  201. } // namespace scene
  202. #endif // ANTKEEPER_SCENE_DIRECTIONAL_LIGHT_HPP