Antkeeper  0.0.1
sky-pass.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_RENDER_SKY_PASS_HPP
21 #define ANTKEEPER_RENDER_SKY_PASS_HPP
22 
23 #include <engine/render/pass.hpp>
25 #include <engine/math/vector.hpp>
32 #include <engine/gl/texture.hpp>
33 #include <engine/math/se3.hpp>
34 #include <engine/scene/object.hpp>
36 
37 class resource_manager;
38 
39 namespace render {
40 
41 class material;
42 class model;
43 
49 class sky_pass: public pass
50 {
51 public:
53  virtual ~sky_pass() = default;
54  void render(render::context& ctx) override;
55 
58 
67  void set_transmittance_lut_sample_count(std::uint16_t count);
68 
77 
79  [[nodiscard]] inline std::uint16_t get_transmittance_lut_sample_count() const noexcept
80  {
81  return m_transmittance_lut_sample_count;
82  }
83 
85  [[nodiscard]] inline const math::vec2<std::uint16_t>& get_transmittance_lut_resolution() const noexcept
86  {
87  return m_transmittance_lut_resolution;
88  }
89 
93 
103 
113 
122 
124  [[nodiscard]] inline std::uint16_t get_multiscattering_lut_direction_sample_count() const noexcept
125  {
126  return m_multiscattering_lut_direction_sample_count;
127  }
128 
130  [[nodiscard]] inline std::uint16_t get_multiscattering_lut_scatter_sample_count() const noexcept
131  {
132  return m_multiscattering_lut_scatter_sample_count;
133  }
134 
136  [[nodiscard]] inline const math::vec2<std::uint16_t>& get_multiscattering_lut_resolution() const noexcept
137  {
138  return m_multiscattering_lut_resolution;
139  }
140 
144 
153  void set_luminance_lut_sample_count(std::uint16_t count);
154 
163 
165  [[nodiscard]] inline std::uint16_t get_luminance_lut_sample_count() const noexcept
166  {
167  return m_luminance_lut_sample_count;
168  }
169 
171  [[nodiscard]] inline const math::vec2<std::uint16_t>& get_luminance_lut_resolution() const noexcept
172  {
173  return m_luminance_lut_resolution;
174  }
175 
177 
178  void update_tweens();
179 
180  void set_magnification(float scale);
181 
182  void set_sky_model(std::shared_ptr<render::model> model);
183  void set_moon_model(std::shared_ptr<render::model> model);
184  void set_stars_model(std::shared_ptr<render::model> model);
185 
186  void set_icrf_to_eus(const math::se3<float>& transformation);
187 
189  void set_sun_luminance(const math::fvec3& luminance);
190  void set_sun_illuminance(const math::fvec3& illuminance, const math::fvec3& transmitted_illuminance);
191  void set_sun_angular_radius(float radius);
192  void set_planet_radius(float radius);
193  void set_atmosphere_upper_limit(float limit);
194  void set_observer_elevation(float elevation);
195  void set_rayleigh_parameters(float scale_height, const math::fvec3& scattering);
196  void set_mie_parameters(float scale_height, float scattering, float extinction, float anisotropy);
197  void set_ozone_parameters(float lower_limit, float upper_limit, float mode, const math::fvec3& absorption);
198  void set_airglow_luminance(const math::fvec3& luminance);
199  void set_ground_albedo(const math::fvec3& albedo);
200 
204  void set_moon_sunlight_direction(const math::fvec3& direction);
205  void set_moon_sunlight_illuminance(const math::fvec3& illuminance);
206  void set_moon_planetlight_direction(const math::fvec3& direction);
207  void set_moon_planetlight_illuminance(const math::fvec3& illuminance);
208  void set_moon_illuminance(const math::fvec3& illuminance, const math::fvec3& transmitted_illuminance);
209 
210 
211 
212  void set_sky_probe(std::shared_ptr<scene::light_probe> probe);
213 
219  inline constexpr void set_layer_mask(std::uint32_t mask) noexcept
220  {
221  m_layer_mask = mask;
222  }
223 
224 private:
225  void rebuild_transmittance_lut_framebuffer();
226  void rebuild_transmittance_lut_shader_program();
227  void rebuild_transmittance_lut_command_buffer();
228  void rebuild_multiscattering_lut_framebuffer();
229  void rebuild_multiscattering_lut_shader_program();
230  void rebuild_multiscattering_lut_command_buffer();
231  void rebuild_luminance_lut_framebuffer();
232  void rebuild_luminance_lut_shader_program();
233  void rebuild_luminance_lut_command_buffer();
234 
235  void rebuild_sky_lut_command_buffer();
236  void rebuild_sky_probe_command_buffer();
237 
238  std::shared_ptr<gl::sampler> m_lut_sampler;
239  std::unique_ptr<gl::vertex_array> m_vertex_array;
240 
241  // Transmittance
242  std::uint16_t m_transmittance_lut_sample_count{40};
243  math::vec2<std::uint16_t> m_transmittance_lut_resolution{256, 64};
244  std::shared_ptr<gl::texture_2d> m_transmittance_lut_texture;
245  std::shared_ptr<gl::framebuffer> m_transmittance_lut_framebuffer;
246  std::shared_ptr<gl::shader_template> m_transmittance_lut_shader_template;
247  std::unique_ptr<gl::shader_program> m_transmittance_lut_shader_program;
248  std::vector<std::function<void()>> m_transmittance_lut_command_buffer;
249  bool m_render_transmittance_lut{false};
250 
251  // Multiscattering
252  std::uint16_t m_multiscattering_lut_direction_sample_count{64};
253  std::uint16_t m_multiscattering_lut_scatter_sample_count{20};
254  math::vec2<std::uint16_t> m_multiscattering_lut_resolution{32, 32};
255  std::shared_ptr<gl::texture_2d> m_multiscattering_lut_texture;
256  std::shared_ptr<gl::framebuffer> m_multiscattering_lut_framebuffer;
257  std::shared_ptr<gl::shader_template> m_multiscattering_lut_shader_template;
258  std::unique_ptr<gl::shader_program> m_multiscattering_lut_shader_program;
259  std::vector<std::function<void()>> m_multiscattering_lut_command_buffer;
260  bool m_render_multiscattering_lut{false};
261 
262  // Luminance
263  std::uint16_t m_luminance_lut_sample_count{30};
264  math::vec2<std::uint16_t> m_luminance_lut_resolution{200, 100};
265  std::shared_ptr<gl::texture_2d> m_luminance_lut_texture;
266  std::shared_ptr<gl::framebuffer> m_luminance_lut_framebuffer;
267  std::shared_ptr<gl::shader_template> m_luminance_lut_shader_template;
268  std::unique_ptr<gl::shader_program> m_luminance_lut_shader_program;
269  std::vector<std::function<void()>> m_luminance_lut_command_buffer;
270  bool m_render_luminance_lut{false};
271 
272  // Sky probe
273  std::shared_ptr<scene::light_probe> m_sky_probe;
274  std::vector<std::unique_ptr<gl::framebuffer>> m_sky_probe_framebuffers;
275  std::shared_ptr<gl::shader_template> m_sky_probe_shader_template;
276  std::unique_ptr<gl::shader_program> m_sky_probe_shader_program;
277  std::vector<std::function<void()>> m_sky_probe_command_buffer;
278 
279  math::fvec3 dominant_light_direction;
280  math::fvec3 dominant_light_illuminance;
281  math::fvec3 observer_position;
282  float camera_exposure;
283 
284  std::shared_ptr<gl::shader_program> sky_shader_program;
285  const gl::shader_variable* model_view_projection_var;
286  const gl::shader_variable* view_var;
287  const gl::shader_variable* view_projection_var;
288  const gl::shader_variable* inv_view_projection_var;
289  const gl::shader_variable* camera_position_var;
290  const gl::shader_variable* mouse_var;
291  const gl::shader_variable* resolution_var;
292  const gl::shader_variable* light_direction_var;
293  const gl::shader_variable* sun_luminance_var;
294  const gl::shader_variable* sun_angular_radius_var;
295  const gl::shader_variable* atmosphere_radii_var;
296  const gl::shader_variable* observer_position_var;
297  const gl::shader_variable* sky_transmittance_lut_var;
298  const gl::shader_variable* sky_transmittance_lut_resolution_var;
299  const gl::shader_variable* sky_luminance_lut_var;
300  const gl::shader_variable* sky_luminance_lut_resolution_var;
301 
302  std::shared_ptr<gl::shader_program> moon_shader_program;
303  const gl::shader_variable* moon_model_var;
304  const gl::shader_variable* moon_view_projection_var;
305  const gl::shader_variable* moon_normal_model_var;
306  const gl::shader_variable* moon_camera_position_var;
307  const gl::shader_variable* moon_sunlight_direction_var;
308  const gl::shader_variable* moon_sunlight_illuminance_var;
309  const gl::shader_variable* moon_planetlight_direction_var;
310  const gl::shader_variable* moon_planetlight_illuminance_var;
311  const gl::shader_variable* moon_albedo_map_var;
312  const gl::shader_variable* moon_normal_map_var;
313  const gl::shader_variable* moon_observer_position_var;
314  const gl::shader_variable* moon_sky_transmittance_lut_var;
315  const gl::shader_variable* moon_atmosphere_radii_var;
316 
317  std::shared_ptr<render::model> sky_model;
318  const material* sky_material;
319  const gl::vertex_array* sky_model_vao;
320  const gl::vertex_buffer* sky_model_vbo;
321  gl::primitive_topology sky_model_primitive_topology;
322  std::size_t sky_model_vertex_offset{};
323  std::size_t sky_model_vertex_stride{};
324  std::uint32_t sky_model_first_vertex{};
325  std::uint32_t sky_model_vertex_count{};
326 
327  std::shared_ptr<render::model> moon_model;
328  const material* moon_material;
329  const gl::vertex_array* moon_model_vao;
330  const gl::vertex_buffer* moon_model_vbo;
331  gl::primitive_topology moon_model_primitive_topology;
332  std::size_t moon_model_vertex_offset{};
333  std::size_t moon_model_vertex_stride{};
334  std::uint32_t moon_model_first_vertex{};
335  std::uint32_t moon_model_vertex_count{};
336  std::shared_ptr<gl::texture_2d> m_moon_albedo_map;
337  std::shared_ptr<gl::texture_2d> m_moon_normal_map;
338 
339  std::shared_ptr<render::model> stars_model;
340  const material* star_material;
341  const gl::vertex_array* stars_model_vao;
342  const gl::vertex_buffer* stars_model_vbo;
343  gl::primitive_topology stars_model_primitive_topology;
344  std::size_t stars_model_vertex_offset{};
345  std::size_t stars_model_vertex_stride{};
346  std::uint32_t stars_model_first_vertex{};
347  std::uint32_t stars_model_vertex_count{};
348  std::unique_ptr<gl::shader_program> star_shader_program;
349  const gl::shader_variable* star_model_view_projection_var;
350  const gl::shader_variable* star_exposure_var;
351  const gl::shader_variable* star_inv_resolution_var;
352 
353  math::fvec2 mouse_position;
354 
355  tween<math::fvec3> sun_position_tween;
356  tween<math::fvec3> sun_luminance_tween;
357  tween<math::fvec3> sun_illuminance_tween;
358  math::fvec3 sun_transmitted_illuminance;
359  tween<math::fvec3> icrf_to_eus_translation;
360  tween<math::fquat> icrf_to_eus_rotation;
361 
362  tween<math::fvec3> moon_position_tween;
363  tween<math::fquat> moon_rotation_tween;
364  tween<float> moon_angular_radius_tween;
365  tween<math::fvec3> moon_sunlight_direction_tween;
366  tween<math::fvec3> moon_sunlight_illuminance_tween;
367  tween<math::fvec3> moon_planetlight_direction_tween;
368  tween<math::fvec3> moon_planetlight_illuminance_tween;
369  tween<math::fvec3> moon_illuminance_tween;
370  math::fvec3 moon_transmitted_illuminance;
371 
372  float sun_angular_radius;
373  float atmosphere_upper_limit;
374  math::fvec4 atmosphere_radii;
375  float observer_elevation;
376  tween<math::fvec3> observer_position_tween;
377  math::fvec4 rayleigh_parameters;
378  math::fvec4 mie_parameters;
379  math::fvec3 ozone_distribution;
380  math::fvec3 ozone_absorption;
381  math::fvec3 airglow_luminance;
382  math::fvec3 m_ground_albedo{};
383 
384  float magnification;
385 
386  std::uint32_t m_layer_mask{1};
387 };
388 
389 } // namespace render
390 
391 #endif // ANTKEEPER_RENDER_SKY_PASS_HPP
Graphics pipeline interface.
Definition: pipeline.hpp:48
Shader program variable.
Vertex arrays describes how vertex input attributes are stored in vertex buffers.
Vertex buffer object (VBO).
Render pass.
Definition: pass.hpp:34
void set_magnification(float scale)
Definition: sky-pass.cpp:705
void set_sky_probe(std::shared_ptr< scene::light_probe > probe)
Definition: sky-pass.cpp:867
void set_luminance_lut_sample_count(std::uint16_t count)
Sets the number of luminance integration samples.
Definition: sky-pass.cpp:506
void set_mie_parameters(float scale_height, float scattering, float extinction, float anisotropy)
Definition: sky-pass.cpp:783
void set_moon_planetlight_direction(const math::fvec3 &direction)
Definition: sky-pass.cpp:851
void set_multiscattering_lut_direction_sample_count(std::uint16_t count)
Sets the number of multiscattering directions to sample.
Definition: sky-pass.cpp:463
void set_sun_position(const math::fvec3 &position)
Definition: sky-pass.cpp:716
const math::vec2< std::uint16_t > & get_transmittance_lut_resolution() const noexcept
Returns the resolution of the transmittance LUT texture, in pixels.
Definition: sky-pass.hpp:85
void set_ground_albedo(const math::fvec3 &albedo)
Definition: sky-pass.cpp:818
void set_transmittance_lut_resolution(const math::vec2< std::uint16_t > &resolution)
Sets the resolution of the transmittance LUT.
Definition: sky-pass.cpp:450
void set_rayleigh_parameters(float scale_height, const math::fvec3 &scattering)
Definition: sky-pass.cpp:768
void set_moon_sunlight_direction(const math::fvec3 &direction)
Definition: sky-pass.cpp:841
void set_ozone_parameters(float lower_limit, float upper_limit, float mode, const math::fvec3 &absorption)
Definition: sky-pass.cpp:798
void set_multiscattering_lut_scatter_sample_count(std::uint16_t count)
Sets the number of multiscattering scatter events to sample per sample direction.
Definition: sky-pass.cpp:478
void set_transmittance_lut_sample_count(std::uint16_t count)
Sets the number of transmittance integration samples.
Definition: sky-pass.cpp:435
void set_airglow_luminance(const math::fvec3 &luminance)
Definition: sky-pass.cpp:813
void set_observer_elevation(float elevation)
Definition: sky-pass.cpp:762
void set_multiscattering_lut_resolution(const math::vec2< std::uint16_t > &resolution)
Sets the resolution of the multiscattering LUT.
Definition: sky-pass.cpp:493
void set_moon_sunlight_illuminance(const math::fvec3 &illuminance)
Definition: sky-pass.cpp:846
constexpr void set_layer_mask(std::uint32_t mask) noexcept
Sets the layer mask of the sky.
Definition: sky-pass.hpp:219
void set_moon_rotation(const math::fquat &rotation)
Definition: sky-pass.cpp:831
void update_tweens()
Definition: sky-pass.cpp:686
void set_icrf_to_eus(const math::se3< float > &transformation)
Definition: sky-pass.cpp:710
void set_moon_model(std::shared_ptr< render::model > model)
Definition: sky-pass.cpp:590
void set_atmosphere_upper_limit(float limit)
Definition: sky-pass.cpp:751
void set_luminance_lut_resolution(const math::vec2< std::uint16_t > &resolution)
Sets the resolution of the luminance LUT.
Definition: sky-pass.cpp:521
void render(render::context &ctx) override
Definition: sky-pass.cpp:148
void set_sun_illuminance(const math::fvec3 &illuminance, const math::fvec3 &transmitted_illuminance)
Definition: sky-pass.cpp:721
std::uint16_t get_luminance_lut_sample_count() const noexcept
Returns the number of luminance integration samples.
Definition: sky-pass.hpp:165
void set_moon_angular_radius(float angular_radius)
Definition: sky-pass.cpp:836
void set_moon_planetlight_illuminance(const math::fvec3 &illuminance)
Definition: sky-pass.cpp:856
void set_sun_angular_radius(float radius)
Definition: sky-pass.cpp:732
sky_pass(gl::pipeline *pipeline, const gl::framebuffer *framebuffer, resource_manager *resource_manager)
Definition: sky-pass.cpp:44
std::uint16_t get_multiscattering_lut_scatter_sample_count() const noexcept
Returns the number of multiscattering scatter samples per direction.
Definition: sky-pass.hpp:130
void set_moon_position(const math::fvec3 &position)
Definition: sky-pass.cpp:826
virtual ~sky_pass()=default
void set_moon_illuminance(const math::fvec3 &illuminance, const math::fvec3 &transmitted_illuminance)
Definition: sky-pass.cpp:861
void set_sky_model(std::shared_ptr< render::model > model)
Definition: sky-pass.cpp:534
const math::vec2< std::uint16_t > & get_luminance_lut_resolution() const noexcept
Returns the resolution of the luminance LUT texture, in pixels.
Definition: sky-pass.hpp:171
void set_sun_luminance(const math::fvec3 &luminance)
Definition: sky-pass.cpp:727
const math::vec2< std::uint16_t > & get_multiscattering_lut_resolution() const noexcept
Returns the resolution of the multiscattering LUT texture, in pixels.
Definition: sky-pass.hpp:136
void set_planet_radius(float radius)
Definition: sky-pass.cpp:737
std::uint16_t get_transmittance_lut_sample_count() const noexcept
Returns the number of transmittance integration samples.
Definition: sky-pass.hpp:79
void set_stars_model(std::shared_ptr< render::model > model)
Definition: sky-pass.cpp:643
std::uint16_t get_multiscattering_lut_direction_sample_count() const noexcept
Returns the number of multiscattering direction samples.
Definition: sky-pass.hpp:124
Manages the loading, caching, and saving of resources.
Container which stores two states along with an interpolator, for quick and easy tween<T,...
Definition: tween.hpp:36
T angular_radius(T radius, T distance)
Finds the angular radius of a celestial object, given its radius and distance.
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
primitive_topology
Primitive topologies.
quaternion< T > rotation(const vec3< T > &from, const vec3< T > &to, T tolerance=T{1e-6})
Constructs a quaternion representing the minimum rotation from one direction to another direction.
Definition: quaternion.hpp:691
constexpr mat4< T > scale(const vec3< T > &v)
Constructs a scale matrix.
T extinction(T scattering, T albedo)
Calculates an extinction coefficient.
Definition: atmosphere.hpp:121
T absorption(T scattering, T albedo)
Calculates an absorption coefficient.
Definition: atmosphere.hpp:105
T scattering(T density, T polarization, T wavelength)
Calculates a wavelength-dependent scattering coefficient.
Definition: atmosphere.hpp:69
High-level rendering.
Quaternion composed of a real scalar part and imaginary vector part.
Definition: quaternion.hpp:39
SE(3) proper rigid transformation (rototranslation).
Definition: se3.hpp:35
n-dimensional vector.
Definition: vector.hpp:44
Context of a renderer.
Definition: context.hpp:40