summaryrefslogtreecommitdiff
path: root/gfx/gl/camera.h
blob: 8d5326140828a7f295938a3f7166e8ebff1db0b5 (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
78
79
80
81
82
83
84
85
86
87
#pragma once

#include "config/types.h"
#include <glm/glm.hpp>
#include <maths.h>
#include <ray.h>

class Camera {
public:
	Camera(GlobalPosition3D, Angle fov, Angle aspect, GlobalDistance zNear, GlobalDistance zFar);

	[[nodiscard]] glm::mat4
	getViewProjection() const
	{
		return viewProjection;
	}

	[[nodiscard]] Ray<GlobalPosition3D> unProject(const ScreenRelCoord &) const;

	void
	setPosition(const GlobalPosition3D & p)
	{
		position = p;
		updateView();
	}

	void
	setForward(const Direction3D & f)
	{
		setForward(f, upFromForward(f));
	}

	void
	setForward(const Direction3D & f, const Direction3D & u)
	{
		forward = f;
		up = u;
		updateView();
	}

	void
	setView(const GlobalPosition3D & p, const Direction3D & f)
	{
		position = p;
		setForward(f);
	}

	void
	setView(const GlobalPosition3D & p, const Direction3D & f, const Direction3D & u)
	{
		position = p;
		setView(f, u);
	}

	void
	lookAt(const GlobalPosition3D & target)
	{
		setForward(glm::normalize(RelativePosition3D(target - position)));
	}

	[[nodiscard]] auto
	getForward() const
	{
		return forward;
	}

	[[nodiscard]] auto
	getPosition() const
	{
		return position;
	}

	[[nodiscard]] std::array<GlobalPosition4D, 4> extentsAtDist(GlobalDistance) const;

	[[nodiscard]] static Direction3D upFromForward(const Direction3D & forward);

private:
	void updateView();

	GlobalPosition3D position;
	Direction3D forward;
	Direction3D up;

	GlobalDistance near, far;
	glm::mat4 projection;
	glm::mat4 viewProjection, inverseViewProjection;
};