Botcraft 1.21.4
Loading...
Searching...
No Matches
Camera.cpp
Go to the documentation of this file.
2
3namespace Botcraft
4{
5 namespace Renderer
6 {
7 Camera::Camera(glm::vec3 position_, glm::vec3 up_, float yaw_, float pitch_)
8 {
9 position = position_;
10 worldUp = up_;
11 yaw = yaw_;
12 pitch = pitch_;
14 }
15
16 const glm::mat4& Camera::GetViewMatrix() const
17 {
18 return view_matrix;
19 }
20
21 float Camera::GetDistance(const float x_, const float y_, const float z_) const
22 {
23 float delta_x = position.x - x_;
24 float delta_y = position.y - y_;
25 float delta_z = position.z - z_;
26
27 return delta_x * delta_x + delta_y * delta_y + delta_z * delta_z;
28 }
29
30 void Camera::SetPosition(const float x, const float y, const float z)
31 {
32 glm::vec3 new_pos(x, y, z);
34 position = new_pos;
36 }
37
38 const glm::vec3& Camera::GetPosition() const
39 {
40 return position;
41 }
42
43 const glm::vec3& Camera::GetFront() const
44 {
45 return front;
46 }
47
48 const std::array<glm::vec4, 6>& Camera::GetFrustumPlanes() const
49 {
50 return frustum;
51 }
52
53 float Camera::GetYaw() const
54 {
55 return yaw;
56 }
57
58 float Camera::GetPitch() const
59 {
60 return pitch;
61 }
62
63 void Camera::SetRotation(const float pitch_, const float yaw_)
64 {
66 pitch = pitch_;
67 yaw = yaw_;
68 while (yaw >= 180.0f)
69 {
70 yaw -= 360.0f;
71 }
72 while (yaw < -180.0f)
73 {
74 yaw += 360.0f;
75 }
77 }
78
79 void Camera::SetProjection(const glm::mat4& projection)
80 {
81 projection_matrix = projection;
83 }
84
86 {
88 }
89
94
99
104
106 {
107 // Calculate the front, right and up vectors
108 front.x = -sin(glm::radians(yaw)) * cos(glm::radians(pitch));
109 front.y = -sin(glm::radians(pitch));
110 front.z = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
111 front = glm::normalize(front);
112
113 // Calculate up vector needed for view matrix
114 const glm::vec3 right = glm::normalize(glm::cross(front, worldUp));
115 const glm::vec3 up = glm::normalize(glm::cross(right, front));
116
117 view_matrix = glm::lookAt(position, position + front, up);
119 }
120
122 {
123 const glm::mat4 clip_matrix = glm::transpose(projection_matrix * view_matrix);
124
125 // Compute the planes
126 frustum[0] = clip_matrix[3] - clip_matrix[0]; // right
127 frustum[1] = clip_matrix[3] + clip_matrix[0]; // left
128 frustum[2] = clip_matrix[3] + clip_matrix[1]; // bottom
129 frustum[3] = clip_matrix[3] - clip_matrix[1]; // top
130 frustum[4] = clip_matrix[3] + clip_matrix[2]; // near
131 frustum[5] = clip_matrix[3] - clip_matrix[2]; // far
132
133 // We do not need normalization for our usecase
134 }
135 } // Renderer
136} // Botcraft
float GetPitch() const
Definition Camera.cpp:58
void SetPosition(const float x, const float y, const float z)
Definition Camera.cpp:30
bool GetHasChangedPosition() const
Definition Camera.cpp:85
const std::array< glm::vec4, 6 > & GetFrustumPlanes() const
Definition Camera.cpp:48
const glm::vec3 & GetFront() const
Definition Camera.cpp:43
Camera(glm::vec3 position_=glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up_=glm::vec3(0.0f, 1.0f, 0.0f), float yaw_=0.0f, float pitch_=0.0f)
Definition Camera.cpp:7
void SetRotation(const float pitch_, const float yaw_)
Definition Camera.cpp:63
const glm::vec3 & GetPosition() const
Definition Camera.cpp:38
const glm::mat4 & GetViewMatrix() const
Definition Camera.cpp:16
float GetDistance(const float x_, const float y_, const float z_) const
Definition Camera.cpp:21
std::array< glm::vec4, 6 > frustum
Definition Camera.hpp:65
void SetProjection(const glm::mat4 &projection)
Definition Camera.cpp:79
bool GetHasChangedOrientation() const
Definition Camera.cpp:90