Engine
Frameworkcreatedbymeusableforthecreationofsimplegames.CurrentlysupportsOpenGL(Verysimple)andVulkan.
Camera.cpp
Go to the documentation of this file.
1 #define GLM_FORCE_SWIZZLE 1
2 #define GLM_FORCE_RADIANS 1
3 
5 #include "Engine/Utility/defines.hpp"
6 #include <ThirdParty/glm/glm/gtc/matrix_transform.hpp>
7 #include <ThirdParty/glm/glm/gtx/euler_angles.hpp>
8 
9 namespace Engine
10 {
11  Camera::Camera(glm::vec3 position, glm::vec3 rotation, float fov, float aspectRatio, float zNear, float zFar) :
12  fov(fov), aspectRatio(aspectRatio), verticalAngle(0), clippingPlanes(zNear, zFar), position(position),
13  rotation(rotation)
14  {
15  horizontalAngle = glm::pi<float>();
16  glm::vec3 target = glm::normalize(position + rotation);
17  view = glm::lookAt(position, position - target, glm::normalize(GetUp()));
18  projection = glm::perspective(glm::radians(fov), aspectRatio, clippingPlanes.x, clippingPlanes.y);
20  }
21 
22  float Camera::GetFoV() const
23  {
24  return fov;
25  }
26 
27  float Camera::GetAspectRatio() const
28  {
29  return aspectRatio;
30  }
31 
32  glm::vec2 Camera::GetClippingPlanes() const
33  {
34  return clippingPlanes;
35  }
36 
37  glm::vec3 Camera::GetUp() const
38  {
39  return glm::cross(GetRight(), rotation);
40  }
41 
42  glm::vec3 Camera::GetRight() const
43  {
44 #ifdef USING_OPENGL
45  return glm::vec3(
46  sin(horizontalAngle - glm::half_pi<float>()),
47  0,
48  cos(horizontalAngle - glm::half_pi<float>()));
49 #endif
50 #ifdef USING_VULKAN
51  return glm::vec3(
52  sin(horizontalAngle - glm::half_pi<float>()),
53  0,
54  cos(horizontalAngle - glm::half_pi<float>()));
55 #endif
56  }
57 
58  glm::vec3 Camera::GetPosition() const
59  {
60  return position;
61  }
62 
63  glm::vec3 Camera::GetRotation() const
64  {
65  return rotation;
66  }
67 
68  glm::mat4x4 Camera::GetView() const
69  {
70  glm::vec4 target(0.f, 0.f, -1.f, 1.f);
71  target = glm::eulerAngleYXZ(rotation.y, rotation.x, rotation.z)*target;
72  return glm::lookAt(position, position + glm::vec3(target.x, target.y, target.z), glm::vec3(0.f, 1.f, 0.f));
73  }
74 
75  glm::mat4x4 Camera::GetProjection() const
76  {
77  return projection;
78  }
79 
80  glm::mat4x4 Camera::GetViewProjection() const
81  {
82  return GetView() * GetProjection();
83  }
84 
86  {
87  return frustum;
88  }
89 
90  void Camera::SetProjection(float fov, float aspectRatio, float zNear, float zFar)
91  {
92  this->fov = fov;
93  this->aspectRatio = aspectRatio;
94  clippingPlanes.x = zNear;
95  clippingPlanes.y = zFar;
96 
97  projection = glm::perspective(fov, aspectRatio, zNear, zFar);
98  }
99 
100  void Camera::SetView(glm::mat4x4 view)
101  {
102  this->view = view;
103  }
104 
105  void Camera::SetViewProjection(glm::mat4x4 view, float fov, float aspectRatio, float zNear, float zFar)
106  {
107  SetView(view);
108  SetProjection(fov, aspectRatio, zNear, zFar);
109  }
110 
111  void Camera::UpdateFrustum(glm::vec3 right, glm::vec3 up)
112  {
113  frustum = Frustum(*this, right, up);
114  }
115 
116  void Camera::UpdateRotationOffset(glm::vec2 rotationOffset)
117  {
118  horizontalAngle += rotationOffset.x;
119  verticalAngle += rotationOffset.y;
120 
121  rotation = glm::vec3(
122  cos(verticalAngle) * sin(horizontalAngle),
123  sin(verticalAngle),
124  cos(verticalAngle) * cos(horizontalAngle));
125  }
126 
127  void Camera::MoveLeft(float distanceToMove)
128  {
129  glm::vec4 translation = glm::eulerAngleYXZ(rotation.y, rotation.x, rotation.z) * -glm::vec4(distanceToMove, 0.f, 0.f, 0.f);
130  position += glm::vec3(translation.x, translation.y, translation.z);
131  }
132 
133  void Camera::MoveRight(float distanceToMove)
134  {
135  glm::vec4 translation = glm::eulerAngleYXZ(rotation.y, rotation.x, rotation.z) * glm::vec4(distanceToMove, 0.f, 0.f, 0.f);
136  position += glm::vec3(translation.x, translation.y, translation.z);
137  }
138 
139  void Camera::MoveForwards(float distanceToMove)
140  {
141  glm::vec4 translation = glm::eulerAngleYXZ(rotation.y, rotation.x, rotation.z) * -glm::vec4(0.f, 0.f, distanceToMove, 0.f);
142  position += glm::vec3(translation.x, translation.y, translation.z);
143  }
144 
145  void Camera::MoveBackwards(float distanceToMove)
146  {
147  glm::vec4 translation = glm::eulerAngleYXZ(rotation.y, rotation.x, rotation.z) * glm::vec4(0.f, 0.f, distanceToMove, 0.f);
148  position += glm::vec3(translation.x, translation.y, translation.z);
149  }
150 
151  void Camera::SetPostionAndRotation(glm::vec3 position, glm::vec3 rotation)
152  {
153  this->position = position;
154  this->rotation = rotation;
155  }
156 
157  void Camera::SetPosition(glm::vec3 position)
158  {
159  SetPostionAndRotation(position, rotation);
160  }
161 
162  void Camera::SetRotation(glm::vec3 rotation)
163  {
164  SetPostionAndRotation(position, rotation);
165  }
166 } // namespace Engine
glm::vec3 GetRight() const
Definition: Camera.cpp:42
float GetAspectRatio() const
Definition: Camera.cpp:27
void SetView(glm::mat4x4 view)
Use this method to set the view matrix yourself.
Definition: Camera.cpp:100
glm::vec2 GetClippingPlanes() const
Definition: Camera.cpp:32
glm::mat4x4 GetViewProjection() const
Definition: Camera.cpp:80
void SetPosition(glm::vec3 position)
This method allows you to set the position of the camera.
Definition: Camera.cpp:157
Frustum GetFrustum() const
Definition: Camera.cpp:85
glm::mat4x4 GetProjection() const
Definition: Camera.cpp:75
void UpdateRotationOffset(glm::vec2 rotationOffset)
This method can be used to update the rotation of the camera based on the defined offset...
Definition: Camera.cpp:116
void SetPostionAndRotation(glm::vec3 position, glm::vec3 rotation)
Use this method to set the position and the rotation of the camera.
Definition: Camera.cpp:151
Camera()=delete
glm::vec3 GetPosition() const
Definition: Camera.cpp:58
void UpdateFrustum(glm::vec3 right, glm::vec3 up)
This method will create a new frustum for you based on the new right and up values.
Definition: Camera.cpp:111
void MoveForwards(float distanceToMove)
This method allows you to move X distance to the forward axis of the camera.
Definition: Camera.cpp:139
void SetViewProjection(glm::mat4x4 view, float fov, float aspectRatio, float zNear, float zFar)
This method allows you to set the view and projection matrices of the camera. This method will simply...
Definition: Camera.cpp:105
glm::vec3 GetUp() const
Definition: Camera.cpp:37
void MoveRight(float distanceToMove)
This method allows you to move X distance to the right axis of the camera.
Definition: Camera.cpp:133
glm::vec3 GetRotation() const
Definition: Camera.cpp:63
void MoveBackwards(float distanceToMove)
This method allows you to move X distance to the backward axis of the camera.
Definition: Camera.cpp:145
void SetRotation(glm::vec3 rotation)
This method allows you to set the rotation of the camera.
Definition: Camera.cpp:162
void MoveLeft(float distanceToMove)
This method allows you to move X distance to the left axis of the camera.
Definition: Camera.cpp:127
This object is used to keep track of the frustum area of the view and projection matrix. NOTE: only the Camera class is allowed to create this object.
Definition: Frustum.hpp:12
glm::mat4x4 GetView() const
Definition: Camera.cpp:68
float GetFoV() const
Definition: Camera.cpp:22
void SetProjection(float fov, float aspectRatio, float zNear, float zFar)
This function allows you to set the projection matrix of the camera. Automatically creates a projecti...
Definition: Camera.cpp:90