Antkeeper  0.0.1
camera.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_CAMERA_HPP
21 #define ANTKEEPER_SCENE_CAMERA_HPP
22 
23 #include <engine/scene/object.hpp>
26 #include <engine/math/vector.hpp>
28 #include <engine/math/numbers.hpp>
29 
30 namespace scene {
31 
35 class camera: public object<camera>
36 {
37 public:
40 
48  [[nodiscard]] geom::ray<float, 3> pick(const math::fvec2& ndc) const;
49 
57  [[nodiscard]] math::fvec3 project(const math::fvec3& object, const math::fvec4& viewport) const;
58 
66  [[nodiscard]] math::fvec3 unproject(const math::fvec3& window, const math::fvec4& viewport) const;
67 
76  void set_perspective(float vertical_fov, float aspect_ratio, float near, float far = std::numeric_limits<float>::infinity());
77 
83  void set_vertical_fov(float vertical_fov);
84 
90  void set_aspect_ratio(float aspect_ratio);
91 
102  void set_orthographic(float clip_left, float clip_right, float clip_bottom, float clip_top, float clip_near, float clip_far);
103 
109  void set_exposure_value(float ev100);
110 
116  inline void set_compositor(render::compositor* compositor) noexcept
117  {
118  m_compositor = compositor;
119  }
120 
126  inline void set_composite_index(int index) noexcept
127  {
128  m_composite_index = index;
129  }
130 
135  [[nodiscard]] inline constexpr const render::compositor* get_compositor() const noexcept
136  {
137  return m_compositor;
138  }
139  [[nodiscard]] inline constexpr render::compositor* get_compositor() noexcept
140  {
141  return m_compositor;
142  }
144 
146  [[nodiscard]] inline constexpr int get_composite_index() const noexcept
147  {
148  return m_composite_index;
149  }
150 
151  [[nodiscard]] inline constexpr const aabb_type& get_bounds() const noexcept override
152  {
153  return m_bounds;
154  }
155 
157  [[nodiscard]] inline constexpr bool is_orthographic() const noexcept
158  {
159  return m_orthographic;
160  }
161 
163  [[nodiscard]] inline constexpr float get_clip_left() const noexcept
164  {
165  return m_clip_left;
166  }
167 
169  [[nodiscard]] inline constexpr float get_clip_right() const noexcept
170  {
171  return m_clip_right;
172  }
173 
175  [[nodiscard]] inline constexpr float get_clip_bottom() const noexcept
176  {
177  return m_clip_bottom;
178  }
179 
181  [[nodiscard]] inline constexpr float get_clip_top() const noexcept
182  {
183  return m_clip_top;
184  }
185 
187  [[nodiscard]] inline constexpr float get_clip_near() const noexcept
188  {
189  return m_clip_near;
190  }
191 
193  [[nodiscard]] inline constexpr float get_clip_far() const noexcept
194  {
195  return m_clip_far;
196  }
197 
199  [[nodiscard]] inline constexpr float get_vertical_fov() const noexcept
200  {
201  return m_vertical_fov;
202  }
203 
205  [[nodiscard]] inline constexpr float get_aspect_ratio() const noexcept
206  {
207  return m_aspect_ratio;
208  }
209 
211  [[nodiscard]] inline constexpr float get_exposure_value() const noexcept
212  {
213  return m_exposure_value;
214  }
215 
217  [[nodiscard]] inline constexpr float get_exposure_normalization() const noexcept
218  {
219  return m_exposure_normalization;
220  }
221 
223  [[nodiscard]] inline constexpr const math::fmat4& get_view() const noexcept
224  {
225  return m_view;
226  }
227 
229  [[nodiscard]] inline constexpr const math::fmat4& get_inv_view() const noexcept
230  {
231  return m_inv_view;
232  }
233 
235  [[nodiscard]] inline constexpr const math::fmat4& get_projection() const noexcept
236  {
237  return m_projection;
238  }
239 
241  [[nodiscard]] inline constexpr const math::fmat4& get_inv_projection() const noexcept
242  {
243  return m_inv_projection;
244  }
245 
247  [[nodiscard]] inline constexpr const math::fmat4& get_view_projection() const noexcept
248  {
249  return m_view_projection;
250  }
251 
253  [[nodiscard]] inline constexpr const math::fmat4& get_inv_view_projection() const noexcept
254  {
255  return m_inv_view_projection;
256  }
257 
259  [[nodiscard]] inline constexpr const math::fvec3& get_forward() const noexcept
260  {
261  return m_forward;
262  }
263 
265  [[nodiscard]] inline constexpr const math::fvec3& get_up() const noexcept
266  {
267  return m_up;
268  }
269 
271  [[nodiscard]] inline constexpr const view_frustum_type& get_view_frustum() const noexcept
272  {
273  return m_view_frustum;
274  }
275 
276 private:
277  void transformed() override;
278  void update_frustum();
279 
280  render::compositor* m_compositor{nullptr};
281  int m_composite_index{0};
282 
283  bool m_orthographic{true};
284 
285  float m_clip_left{-1.0f};
286  float m_clip_right{1.0f};
287  float m_clip_bottom{-1.0f};
288  float m_clip_top{1.0f};
289  float m_clip_near{-1.0f};
290  float m_clip_far{1.0f};
291  float m_vertical_fov{math::half_pi<float>};
292  float m_aspect_ratio{1.0f};
293  float m_exposure_value{0.0f};
294  float m_exposure_normalization{1.0f};
295 
296  math::fvec3 m_forward{0.0f, 0.0f, -1.0f};
297  math::fvec3 m_up{0.0f, 1.0f, 0.0f};
298 
300  math::fmat4 m_inv_view{math::fmat4::identity()};
301  math::fmat4 m_projection{math::fmat4::identity()};
302  math::fmat4 m_inv_projection{math::fmat4::identity()};
303  math::fmat4 m_view_projection{math::fmat4::identity()};
304  math::fmat4 m_inv_view_projection{math::fmat4::identity()};
305 
306  view_frustum_type m_view_frustum;
307  aabb_type m_bounds{{}, {}};
308 };
309 
310 } // namespace scene
311 
312 #endif // ANTKEEPER_SCENE_CAMERA_HPP
constexpr const math::fmat4 & get_projection() const noexcept
Returns the camera's projection matrix.
Definition: camera.hpp:235
constexpr const math::fvec3 & get_up() const noexcept
Returns the camera's up vector.
Definition: camera.hpp:265
constexpr const aabb_type & get_bounds() const noexcept override
Returns the bounds of the object.
Definition: camera.hpp:151
constexpr float get_clip_right() const noexcept
Returns the signed distance to the camera's right clipping plane.
Definition: camera.hpp:169
constexpr float get_exposure_value() const noexcept
Returns the camera's ISO 100 exposure value.
Definition: camera.hpp:211
constexpr const math::fmat4 & get_view() const noexcept
Returns the camera's view matrix.
Definition: camera.hpp:223
constexpr bool is_orthographic() const noexcept
Returns true if the camera uses an orthographic projection matrix, false otherwise.
Definition: camera.hpp:157
constexpr float get_aspect_ratio() const noexcept
Returns the camera's aspect ratio.
Definition: camera.hpp:205
constexpr render::compositor * get_compositor() noexcept
Returns the camera's compositor.
Definition: camera.hpp:139
void set_aspect_ratio(float aspect_ratio)
Sets the camera's aspect ratio.
Definition: camera.cpp:98
constexpr const math::fmat4 & get_view_projection() const noexcept
Returns the camera's view-projection matrix.
Definition: camera.hpp:247
constexpr const math::fvec3 & get_forward() const noexcept
Returns the camera's forward vector.
Definition: camera.hpp:259
constexpr float get_clip_top() const noexcept
Returns the signed distance to the camera's top clipping plane.
Definition: camera.hpp:181
void set_vertical_fov(float vertical_fov)
Sets the camera's vertical field of view.
Definition: camera.cpp:90
constexpr int get_composite_index() const noexcept
Returns the composite index of the camera.
Definition: camera.hpp:146
math::fvec3 project(const math::fvec3 &object, const math::fvec4 &viewport) const
Maps object coordinates to window coordinates.
Definition: camera.cpp:35
math::fvec3 unproject(const math::fvec3 &window, const math::fvec4 &viewport) const
Maps window coordinates to object coordinates.
Definition: camera.cpp:46
void set_orthographic(float clip_left, float clip_right, float clip_bottom, float clip_top, float clip_near, float clip_far)
Sets the camera's projection matrix using orthographic projection.
Definition: camera.cpp:106
geom::ray< float, 3 > pick(const math::fvec2 &ndc) const
Constructs a picking ray from normalized device coordinates (NDC).
Definition: camera.cpp:26
constexpr float get_clip_near() const noexcept
Returns the signed distance to the camera's near clipping plane.
Definition: camera.hpp:187
constexpr const math::fmat4 & get_inv_view_projection() const noexcept
Returns the inverse of the camera's view-projection matrix.
Definition: camera.hpp:253
constexpr float get_exposure_normalization() const noexcept
Returns the camera's exposure normalization factor.
Definition: camera.hpp:217
constexpr float get_vertical_fov() const noexcept
Returns the camera's vertical field of view, in radians.
Definition: camera.hpp:199
constexpr const math::fmat4 & get_inv_view() const noexcept
Returns the inverse of the camera's view matrix.
Definition: camera.hpp:229
constexpr const math::fmat4 & get_inv_projection() const noexcept
Returns the inverse of the camera's projection matrix.
Definition: camera.hpp:241
constexpr float get_clip_left() const noexcept
Returns the signed distance to the camera's left clipping plane.
Definition: camera.hpp:163
constexpr const view_frustum_type & get_view_frustum() const noexcept
Returns the camera's view frustum.
Definition: camera.hpp:271
void set_exposure_value(float ev100)
Sets the camera's ISO 100 exposure value.
Definition: camera.cpp:130
void set_compositor(render::compositor *compositor) noexcept
Sets the camera's compositor.
Definition: camera.hpp:116
geom::view_frustum< float > view_frustum_type
Camera view frustum type.
Definition: camera.hpp:39
constexpr float get_clip_far() const noexcept
Returns the signed distance to the camera's far clipping plane.
Definition: camera.hpp:193
constexpr float get_clip_bottom() const noexcept
Returns the signed distance to the camera's bottom clipping plane.
Definition: camera.hpp:175
void set_perspective(float vertical_fov, float aspect_ratio, float near, float far=std::numeric_limits< float >::infinity())
Sets the camera's projection matrix using perspective projection.
Definition: camera.cpp:61
void set_composite_index(int index) noexcept
Sets the composite index of the camera.
Definition: camera.hpp:126
constexpr const render::compositor * get_compositor() const noexcept
Returns the camera's compositor.
Definition: camera.hpp:135
geom::box< float > aabb_type
Definition: object.hpp:42
Abstract base class for lights, cameras, model instances, and other scene objects.
Definition: object.hpp:172
T vertical_fov(T h, T r)
Calculates a vertical FoV given a horizontal FoV and aspect ratio.
3D scene.
Definition: context.hpp:28
n-dimensional axis-aligned rectangle.
Half of a line proceeding from an initial point.
Definition: ray.hpp:38
n by m column-major matrix.
Definition: math/matrix.hpp:44
static constexpr matrix identity() noexcept
Returns an identity matrix, with ones on the main diagonal and zeros elsewhere.
n-dimensional vector.
Definition: vector.hpp:44