VOX
A little voxel engine
Loading...
Searching...
No Matches
Camera.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "define.hpp"
4
5#include <glm/glm.hpp>
6#include <glm/gtc/matrix_transform.hpp>
7
8#include <mutex>
9#include "Tracy.hpp"
10
12{
13
14public:
15
17 const glm::dvec3 & pos, const glm::dvec3 & front, const glm::dvec3 & up,
18 const double fov, const double ratio, const double nearD, const double farD
19 );
20
21 bool sphereInFrustum(const glm::dvec3 & center, double radius) const;
22
23 glm::dvec3 nbr, nbl, ntr, ntl, fbr, fbl, ftr, ftl;
24
25private:
26
27 glm::dvec3 m_camera_position;
28 glm::dvec3 m_x, m_y, m_z;
29 double m_nearD, m_farD;
30 double m_ratio;
31 double m_fov, m_tang;
32 double m_nearH, m_nearW;
33 double m_farH, m_farW;
34 double m_sphereFactorX, m_sphereFactorY;
35};
36
40class Camera
41{
42
43public:
44
46 {
47 glm::mat4 view = glm::mat4(1.0f);
48 glm::mat4 projection = glm::mat4(1.0f);
49 glm::dvec3 position = glm::dvec3(0.0f, 0.0f, 0.0f);
50 double fov = 0.0f;
51 double near_plane = 0.0f;
52 double far_plane = 0.0f;
54 glm::dvec3(0.0f, 0.0f, 0.0f),
55 glm::dvec3(0.0f, 0.0f, 0.0f),
56 glm::dvec3(0.0f, 0.0f, 0.0f),
57 0.0f, 0.0f, 0.0f, 0.0f
58 );
59 };
60
61 Camera() = default;
62 ~Camera() = default;
63
65 const glm::dvec3 & position,
66 double pitch,
67 double yaw,
68 double far_plane
69 ):
70 position(position),
71 pitch(pitch),
72 yaw(yaw),
73 up(0.0f, 1.0f, 0.0f),
74 fov(80.0f),
75 near_plane(0.01f),
76 far_plane(far_plane)
77 {
78 }
79
80 Camera(const Camera & camera):
81 position(camera.position),
82 pitch(camera.pitch),
83 yaw(camera.yaw),
84 up(camera.up),
85 fov(camera.fov),
86 near_plane(camera.near_plane),
87 far_plane(camera.far_plane)
88 {
89 }
90
91 Camera & operator=(const Camera & camera)
92 {
93 std::lock_guard lock(m_mutex);
94 position = camera.position;
95 pitch = camera.pitch;
96 yaw = camera.yaw;
97 up = camera.up;
98 fov = camera.fov;
99 near_plane = camera.near_plane;
100 far_plane = camera.far_plane;
101
102 return *this;
103 }
104
105 Camera(Camera && camera):
106 position(camera.position),
107 pitch(camera.pitch),
108 yaw(camera.yaw),
109 up(camera.up),
110 fov(camera.fov),
111 near_plane(camera.near_plane),
112 far_plane(camera.far_plane)
113 {
114 }
115
117 {
118 if (this != &camera)
119 {
120 std::lock_guard lock(m_mutex);
121 position = camera.position;
122 pitch = camera.pitch;
123 yaw = camera.yaw;
124 up = camera.up;
125 fov = camera.fov;
126 near_plane = camera.near_plane;
127 far_plane = camera.far_plane;
128 }
129 return *this;
130 }
131
132 RenderInfo getRenderInfo(double aspect_ratio) const
133 {
134 std::lock_guard lock(m_mutex);
135 return {
136 getViewMatrix(),
137 getProjectionMatrix(aspect_ratio),
138 position,
139 glm::radians(fov),
140 near_plane,
141 far_plane,
142 getViewFrustum(aspect_ratio)
143 };
144 }
145
146private:
147
148 glm::dvec3 position;
149 double pitch;
150 double yaw;
151 glm::dvec3 up;
152 double fov;
153 double near_plane;
154 double far_plane;
155
156 mutable TracyLockableN(std::mutex, m_mutex, "Camera Mutex");
157
158 glm::dvec3 direction() const;
159
160 glm::mat4 getViewMatrix() const;
161
162 glm::mat4 getProjectionMatrix(double aspect_ratio) const;
163
164 ViewFrustum getViewFrustum(double aspect_ratio) const;
165};
A simple camera class.
Definition: Camera.hpp:41
Camera(const glm::dvec3 &position, double pitch, double yaw, double far_plane)
Definition: Camera.hpp:64
Camera(const Camera &camera)
Definition: Camera.hpp:80
RenderInfo getRenderInfo(double aspect_ratio) const
Definition: Camera.hpp:132
Camera & operator=(Camera &&camera)
Definition: Camera.hpp:116
Camera & operator=(const Camera &camera)
Definition: Camera.hpp:91
Camera(Camera &&camera)
Definition: Camera.hpp:105
~Camera()=default
Camera()=default
Definition: Camera.hpp:12
glm::dvec3 nbl
Definition: Camera.hpp:23
glm::dvec3 ftr
Definition: Camera.hpp:23
glm::dvec3 ntr
Definition: Camera.hpp:23
glm::dvec3 nbr
Definition: Camera.hpp:23
glm::dvec3 fbr
Definition: Camera.hpp:23
glm::dvec3 fbl
Definition: Camera.hpp:23
glm::dvec3 ftl
Definition: Camera.hpp:23
bool sphereInFrustum(const glm::dvec3 &center, double radius) const
Definition: Camera.cpp:52
glm::dvec3 ntl
Definition: Camera.hpp:23
Definition: Camera.hpp:46
glm::mat4 projection
Definition: Camera.hpp:48
glm::mat4 view
Definition: Camera.hpp:47
glm::dvec3 position
Definition: Camera.hpp:49
double near_plane
Definition: Camera.hpp:51
ViewFrustum view_frustum
Definition: Camera.hpp:53
double fov
Definition: Camera.hpp:50
double far_plane
Definition: Camera.hpp:52