]> git.localhorst.tv Git - blobs.git/blob - src/graphics/viewport.cpp
better camera behaviour
[blobs.git] / src / graphics / viewport.cpp
1 #include "Camera.hpp"
2 #include "Viewport.hpp"
3
4 #include "../creature/Creature.hpp"
5 #include "../math/const.hpp"
6 #include "../world/Body.hpp"
7 #include "../world/Planet.hpp"
8
9 #include <cmath>
10 #include <GL/glew.h>
11 #include <glm/gtx/transform.hpp>
12
13
14 namespace blobs {
15 namespace graphics {
16
17 Camera::Camera(const world::Body &r) noexcept
18 : fov(PI * 0.25)
19 , aspect(1.0f)
20 , near(0.1f)
21 , far(12560.0f)
22 , projection(glm::infinitePerspective(fov, aspect, near))
23 , view(1.0f)
24 , ref(&r)
25 , track_orient(false) {
26
27 }
28
29 Camera::~Camera() noexcept {
30 }
31
32 Camera &Camera::FOV(float f) noexcept {
33         fov = f;
34         UpdateProjection();
35         return *this;
36 }
37
38 Camera &Camera::Aspect(float r) noexcept {
39         aspect = r;
40         UpdateProjection();
41         return *this;
42 }
43
44 Camera &Camera::Aspect(float w, float h) noexcept {
45         Aspect(w / h);
46         return *this;
47 }
48
49 Camera &Camera::Clip(float n, float f) noexcept {
50         near = n;
51         far = f;
52         UpdateProjection();
53         return *this;
54 }
55
56 Camera &Camera::Reference(const world::Body &r) noexcept {
57         ref = &r;
58         return *this;
59 }
60
61 Camera &Camera::LookAt(const glm::vec3 &pos, const glm::vec3 &tgt, const glm::vec3 &up) noexcept {
62         view = glm::lookAt(pos, tgt, up);
63         return *this;
64 }
65
66 glm::mat4 Camera::Model(const world::Body &b) const noexcept {
67         if (&b == ref) {
68                 return track_orient ? glm::mat4(1.0f) : glm::mat4(ref->LocalTransform());
69         } else if (b.HasParent() && &b.Parent() == ref) {
70                 return glm::mat4(track_orient
71                         ? ref->InverseTransform() * b.FromParent() * b.LocalTransform()
72                         : b.FromParent() * b.LocalTransform());
73         } else if (ref->HasParent() && &ref->Parent() == &b) {
74                 return glm::mat4(track_orient
75                         ? ref->InverseTransform() * ref->ToParent() * b.LocalTransform()
76                         : ref->ToParent() * b.LocalTransform());
77         } else {
78                 return glm::mat4(track_orient
79                         ? ref->InverseTransform() * ref->ToUniverse() * b.FromUniverse() * b.LocalTransform()
80                         : ref->ToUniverse() * b.FromUniverse() * b.LocalTransform());
81         }
82 }
83
84 void Camera::UpdateProjection() noexcept {
85         projection = glm::infinitePerspective(fov, aspect, near);
86 }
87
88
89 Viewport::Viewport(int w, int h)
90 : width(w)
91 , height(h) {
92         Resize(w, h);
93         glClearColor(0.0, 0.0, 0.0, 1.0);
94 }
95
96 Viewport::~Viewport() {
97 }
98
99
100 void Viewport::Resize(int w, int h) {
101         width = w;
102         height = h;
103         glViewport(0, 0, w, h);
104 }
105
106 void Viewport::Clear() {
107         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
108 }
109
110 void Viewport::ClearDepth() {
111         glClear(GL_DEPTH_BUFFER_BIT);
112 }
113
114 }
115 }