summaryrefslogtreecommitdiff
path: root/gfx/gl/camera.h
blob: 2d937a0eca0b66549a08f3d14223e720637414fb (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma once

#include <glm/glm.hpp>
#include <maths.h>
#include <ray.hpp>

class Camera {
public:
	Camera(glm::vec3 pos, float fov, float aspect, float zNear, float zFar);

	[[nodiscard]] glm::mat4
	getViewProjection() const
	{
		return viewProjection;
	}
	[[nodiscard]] Ray unProject(const glm::vec2 &) const;

	void
	setPosition(const glm::vec3 & p)
	{
		position = p;
		updateView();
	}
	void
	setForward(const glm::vec3 & f)
	{
		setForward(f, upFromForward(f));
	}
	void
	setForward(const glm::vec3 & f, const glm::vec3 & u)
	{
		forward = f;
		up = u;
		updateView();
	}
	void
	setView(const glm::vec3 & p, const glm::vec3 & f)
	{
		position = p;
		setForward(f);
	}
	void
	setView(const glm::vec3 & p, const glm::vec3 & f, const glm::vec3 & u)
	{
		position = p;
		setView(f, u);
	}
	void
	lookAt(const glm::vec3 & target)
	{
		setForward(glm::normalize(target - position));
	}
	[[nodiscard]] auto
	getForward() const
	{
		return forward;
	}
	[[nodiscard]] auto
	getPosition() const
	{
		return position;
	}

	static glm::vec3 upFromForward(const glm::vec3 & forward);

private:
	void updateView();

	glm::vec3 position;
	glm::vec3 forward;
	glm::vec3 up;

	float fov, aspect, near, far;
	glm::mat4 projection;
	glm::mat4 view, unView;
	glm::mat4 viewProjection, inverseViewProjection;
};