Antkeeper  0.0.1
light-probe.cpp
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 
21 
22 namespace scene {
23 
25 {
26  // Allocate illuminance texture
27  m_illuminance_texture = std::make_shared<gl::texture_1d>
28  (
29  std::make_shared<gl::image_view_1d>
30  (
31  std::make_shared<gl::image_1d>
32  (
34  12
35  )
36  ),
37  std::make_shared<gl::sampler>
38  (
43  )
44  );
45 
46  // Allocate and init illuminance framebuffer
47  const gl::framebuffer_attachment attachments[1] =
48  {
49  {
51  m_illuminance_texture->get_image_view(),
52  0
53  }
54  };
55  m_illuminance_framebuffer = std::make_shared<gl::framebuffer>(attachments, 12, 1);
56 
57  // Init illuminance matrices
58  m_illuminance_matrices[0] = {};
59  m_illuminance_matrices[1] = {};
60  m_illuminance_matrices[2] = {};
61 }
62 
64 {
65  m_illuminance_texture->get_image_view()->get_image()->read
66  (
67  0,
68  0,
69  0,
70  0,
71  12,
72  1,
73  1,
75  std::as_writable_bytes(std::span{m_illuminance_matrices})
76  );
77 }
78 
79 void light_probe::set_luminance_texture(std::shared_ptr<gl::texture_cube> texture)
80 {
81  if (m_luminance_texture != texture)
82  {
83  m_luminance_texture = texture;
84 
85  // Update luminance framebuffers
86  if (m_luminance_texture)
87  {
88  const auto face_size = texture->get_image_view()->get_image()->get_dimensions()[0];
89  const auto mip_count = static_cast<std::uint32_t>(std::bit_width(face_size));
90 
91  m_luminance_framebuffers.resize(mip_count);
92 
93  for (std::uint32_t i = 0; i < mip_count; ++i)
94  {
95  const gl::framebuffer_attachment attachments[1] =
96  {
97  {
99  m_luminance_texture->get_image_view(),
100  i
101  }
102  };
103  m_luminance_framebuffers[i] = std::make_shared<gl::framebuffer>(attachments, face_size >> i, face_size >> i);
104  }
105  }
106  else
107  {
108  m_luminance_framebuffers.clear();
109  }
110 
113  }
114 }
115 
117 {
118  m_luminance_outdated = outdated;
119 }
120 
122 {
123  m_illuminance_outdated = outdated;
124 }
125 
126 void light_probe::transformed()
127 {
128  m_bounds = {get_translation(), get_translation()};
129 }
130 
131 } // namespace scene
void set_luminance_texture(std::shared_ptr< gl::texture_cube > texture)
Sets the light probe's luminance texture.
Definition: light-probe.cpp:79
void update_illuminance_matrices()
Updates the light probe's illuminance matrices from its illuminance texture.
Definition: light-probe.cpp:63
void set_illuminance_outdated(bool outdated)
Marks the light probe's illuminance as either outdated or current.
light_probe()
Constructs a light probe.
Definition: light-probe.cpp:24
void set_luminance_outdated(bool outdated)
Marks the light probe's luminance as either outdated or current.
constexpr const vector_type & get_translation() const noexcept
Returns the translation of the object.
Definition: object.hpp:133
@ r32g32b32a32_sfloat
@ clamp_to_edge
Clamp to edge wrap mode.
@ color_attachment_bit
Framebuffer color attachment.
@ nearest
Nearest filtering.
@ nearest
Nearest filtering.
3D scene.
Definition: context.hpp:28