Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
CameraUtils.hpp
Go to the documentation of this file.
1#pragma once
2
3#define GLM_ENABLE_EXPERIMENTAL
4
5#include <glm/glm.hpp>
6#include <glm/gtc/quaternion.hpp>
7#include <glm/gtx/quaternion.hpp>
8
9namespace CameraMovement::Utils {
10
19inline glm::quat ComputeLookAtQuaternion(const glm::vec3 &eye, const glm::vec3 &target, const glm::vec3 &up)
20{
21 glm::vec3 direction = glm::normalize(target - eye);
22 glm::vec3 right = glm::normalize(glm::cross(up, direction));
23 glm::vec3 newUp = glm::cross(direction, right);
24
25 glm::mat3 rotationMatrix;
26 rotationMatrix[0] = right;
27 rotationMatrix[1] = newUp;
28 rotationMatrix[2] = direction;
29
30 return glm::quat_cast(rotationMatrix);
31}
32
41inline glm::quat RotateQuaternion(const glm::quat &current, float pitch, float yaw)
42{
43 glm::quat pitchQuat = glm::angleAxis(pitch, glm::vec3(1.0f, 0.0f, 0.0f));
44 glm::quat yawQuat = glm::angleAxis(yaw, glm::vec3(0.0f, 1.0f, 0.0f));
45
46 return glm::normalize(yawQuat * current * pitchQuat);
47}
48
55inline glm::vec3 GetRightVector(const glm::quat &rotation)
56{
57 return glm::normalize(rotation * glm::vec3(1.0f, 0.0f, 0.0f));
58}
59
66inline glm::vec3 GetUpVector(const glm::quat &rotation)
67{
68 return glm::normalize(rotation * glm::vec3(0.0f, 1.0f, 0.0f));
69}
70
77inline glm::vec3 GetForwardVector(const glm::quat &rotation)
78{
79 return glm::normalize(rotation * glm::vec3(0.0f, 0.0f, 1.0f));
80}
81
82} // namespace CameraMovement::Utils
Definition CameraManager.hpp:12
glm::vec3 GetUpVector(const glm::quat &rotation)
Get the up vector from a quaternion rotation.
Definition CameraUtils.hpp:66
glm::quat RotateQuaternion(const glm::quat &current, float pitch, float yaw)
Apply pitch and yaw rotation to a quaternion.
Definition CameraUtils.hpp:41
glm::quat ComputeLookAtQuaternion(const glm::vec3 &eye, const glm::vec3 &target, const glm::vec3 &up)
Compute a quaternion that represents a "look at" rotation.
Definition CameraUtils.hpp:19
glm::vec3 GetRightVector(const glm::quat &rotation)
Get the right vector from a quaternion rotation.
Definition CameraUtils.hpp:55
glm::vec3 GetForwardVector(const glm::quat &rotation)
Get the forward vector from a quaternion rotation.
Definition CameraUtils.hpp:77