MOS
camera.hpp
1 #pragma once
2 
3 #include <array>
4 #include <glm/glm.hpp>
5 #include <map>
6 #include <string>
7 
8 namespace mos {
9 namespace gfx {
10 
12 class Camera final {
13 public:
14  using Planes = std::array<glm::vec4, 6>;
15  Camera(const glm::vec3 &position = glm::vec3(0.0f),
16  const glm::vec3 &center = glm::vec3(0.0f),
17  const glm::mat4 &projection = glm::mat4(1.0f),
18  const glm::vec3 &up = glm::vec3(0.0f, 0.0f, 1.0f));
19 
20  static auto load(const std::string &directory, const std::string &path,
21  const glm::mat4 &parent_transform = glm::mat4(1.0f))
22  -> Camera;
23 
24  auto position() const -> glm::vec3;
25 
27  auto position(const glm::vec3 &position,
28  const glm::vec3 &up = glm::vec3(0.0f, 0.0f, 1.0f)) -> void;
29 
31  auto center(const glm::vec3 &center,
32  const glm::vec3 &up = glm::vec3(0.0f, 0.0f, 1.0f)) -> void;
33 
35  auto direction() const -> glm::vec3;
36 
37  auto right() const -> glm::vec3;
38 
40  auto direction(const glm::vec3 &direction,
41  const glm::vec3 &up = glm::vec3(0.0f, 0.0f, 1.0f)) -> void;
42 
44  auto aspect_ratio() const -> float;
45 
47  auto in_frustum(const glm::vec3 &point, float radius) const -> bool;
48 
50  auto near_plane() const -> float;
51 
53  auto far_plane() const -> float;
54 
56  auto projection() const -> glm::mat4;
57 
59  auto projection(const glm::mat4 &proj) -> void;
60 
62  auto view() const -> glm::mat4;
63 
65  auto view(const glm::mat4 mat) -> void;
66 
68  auto field_of_view_vertical() const -> float;
69 
71  auto field_of_view_horizontal() const -> float;
72 
73 private:
74  auto calculate_view(const glm::vec3 &position, const glm::vec3 &center,
75  const glm::vec3 &up) -> void;
76  auto calculate_frustum() -> void;
77  auto calculate_near_far() -> void;
78  glm::mat4 projection_{};
79  glm::mat4 view_{};
80  Planes frustum_planes_;
81  float near_{0.1f};
82  float far_{100.0f};
83 };
84 } // namespace gfx
85 } // namespace mos
auto near_plane() const -> float
Get near clip plane.
auto in_frustum(const glm::vec3 &point, float radius) const -> bool
Check if sphere with a radius is within camera frustum.
auto far_plane() const -> float
Get far clip plane.
auto projection() const -> glm::mat4
Get projection matrix.
Rendering camera view.
Definition: camera.hpp:12
auto field_of_view_vertical() const -> float
Get the vertical field of view.
auto field_of_view_horizontal() const -> float
Get the horizonal field of view.
auto direction() const -> glm::vec3
Forward direction.
auto aspect_ratio() const -> float
Get the aspect ratio.
auto view() const -> glm::mat4
Get view matrix.
auto center(const glm::vec3 &center, const glm::vec3 &up=glm::vec3(0.0f, 0.0f, 1.0f)) -> void
Set center/focus point.