summaryrefslogtreecommitdiff
path: root/gfx/gl/camera.cpp
blob: b4a76d0514ea758b3b80ae52650ec9863abc2805 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "camera.h"
#include <glm/gtx/transform.hpp>

Camera::Camera(glm::vec3 pos, float fov, float aspect, float zNear, float zFar) :
	projection {glm::perspective(fov, aspect, zNear, zFar)}, pos {pos}, forward {0.0F, 0.0F, 1.0F}, up {0.0F, 1.0F,
																											0.0F}
{
}

glm::mat4
Camera::GetViewProjection() const
{
	return projection * glm::lookAt(pos, pos + forward, up);
}

void
Camera::MoveForward(float amt)
{
	pos += forward * amt;
}

void
Camera::MoveRight(float amt)
{
	pos += glm::cross(up, forward) * amt;
}

void
Camera::Pitch(float angle)
{
	const auto right = glm::normalize(glm::cross(up, forward));

	forward = glm::vec3(glm::normalize(glm::rotate(angle, right) * glm::vec4(forward, 0.0)));
	up = glm::normalize(glm::cross(forward, right));
}

void
Camera::RotateY(float angle)
{
	static constexpr glm::vec3 UP {0.0F, 1.0F, 0.0F};

	const auto rotation = glm::rotate(angle, UP);

	forward = glm::vec3(glm::normalize(rotation * glm::vec4(forward, 0.0)));
	up = glm::vec3(glm::normalize(rotation * glm::vec4(up, 0.0)));
}