1 #define GLM_FORCE_SWIZZLE 1 2 #define GLM_FORCE_RADIANS 1 5 #include "Engine/Utility/defines.hpp" 6 #include <ThirdParty/glm/glm/gtc/matrix_transform.hpp> 7 #include <ThirdParty/glm/glm/gtx/euler_angles.hpp> 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),
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);
34 return clippingPlanes;
39 return glm::cross(
GetRight(), rotation);
46 sin(horizontalAngle - glm::half_pi<float>()),
48 cos(horizontalAngle - glm::half_pi<float>()));
52 sin(horizontalAngle - glm::half_pi<float>()),
54 cos(horizontalAngle - glm::half_pi<float>()));
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));
93 this->aspectRatio = aspectRatio;
94 clippingPlanes.x = zNear;
95 clippingPlanes.y = zFar;
97 projection = glm::perspective(fov, aspectRatio, zNear, zFar);
113 frustum =
Frustum(*
this, right, up);
118 horizontalAngle += rotationOffset.x;
119 verticalAngle += rotationOffset.y;
121 rotation = glm::vec3(
122 cos(verticalAngle) * sin(horizontalAngle),
124 cos(verticalAngle) * cos(horizontalAngle));
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);
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);
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);
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);
153 this->position = position;
154 this->rotation = rotation;
glm::vec3 GetRight() const
float GetAspectRatio() const
void SetView(glm::mat4x4 view)
Use this method to set the view matrix yourself.
glm::vec2 GetClippingPlanes() const
glm::mat4x4 GetViewProjection() const
void SetPosition(glm::vec3 position)
This method allows you to set the position of the camera.
Frustum GetFrustum() const
glm::mat4x4 GetProjection() const
void UpdateRotationOffset(glm::vec2 rotationOffset)
This method can be used to update the rotation of the camera based on the defined offset...
void SetPostionAndRotation(glm::vec3 position, glm::vec3 rotation)
Use this method to set the position and the rotation of the camera.
glm::vec3 GetPosition() const
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.
void MoveForwards(float distanceToMove)
This method allows you to move X distance to the forward axis of the camera.
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...
void MoveRight(float distanceToMove)
This method allows you to move X distance to the right axis of the camera.
glm::vec3 GetRotation() const
void MoveBackwards(float distanceToMove)
This method allows you to move X distance to the backward axis of the camera.
void SetRotation(glm::vec3 rotation)
This method allows you to set the rotation of the camera.
void MoveLeft(float distanceToMove)
This method allows you to move X distance to the left axis of the camera.
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.
glm::mat4x4 GetView() const
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...