aboutsummaryrefslogtreecommitdiff
path: root/source/Game/Camera.cpp
blob: 39f0369e9fd5854bd193d34312f12c4e6b817dbb (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.hpp"

#include "AppConfig.hpp"

#include <glm/gtc/matrix_transform.hpp>

Camera::Camera()
	: eye(0.0f, 0.0f, 0.0f)
	, target(0.0, 0.0f, -2.0f)
	, pixelsPerMeter{ 50.0f } // Basic default
	, fov{ M_PI / 4 } // 45deg is the convention
	, perspective{ false } //
{
}

void Camera::SetEyePos(glm::vec3 pos) {
	auto lookVector = this->target - /*Old pos*/ this->eye;
	this->eye = pos;
	this->target = pos + lookVector;
}

void Camera::SetTargetPos(glm::vec3 pos) {
	this->target = pos;
}

void Camera::SetTargetDirection(glm::vec3 lookVector) {
	this->target = this->eye + lookVector;
}

void Camera::SetHasPerspective(bool perspective) {
	this->perspective = perspective;
}

glm::mat4 Camera::CalcViewMatrix() const {
	return glm::lookAt(eye, target, glm::vec3(0, 1, 0));
}

glm::mat4 Camera::CalcProjectionMatrix() const {
	if (perspective) {
		return glm::perspective(fov, AppConfig::mainWindowAspectRatio, 0.1f, 1000.0f);
	} else {
		float widthMeters = AppConfig::mainWindowWidth / pixelsPerMeter;
		float heightMeters = AppConfig::mainWindowHeight / pixelsPerMeter;
		return glm::ortho(-widthMeters / 2, +widthMeters / 2, -heightMeters / 2, +heightMeters / 2);
	}
}