]> git.localhorst.tv Git - blobs.git/blob - src/graphics/viewport.cpp
track creature with camera
[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_0p25)
19 , aspect(1.0f)
20 , near(0.1f)
21 , far(12560.0f)
22 , projection(glm::perspective(fov, aspect, near, far))
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::FirstPerson(int srf, const glm::vec3 &pos, const glm::vec3 &at) noexcept {
62         track_orient = true;
63
64         float dir = srf < 3 ? 1.0f : -1.0f;
65
66         glm::vec3 position;
67         position[(srf + 0) % 3] = pos.x;
68         position[(srf + 1) % 3] = pos.y;
69         position[(srf + 2) % 3] = dir * (pos.z + Reference().Radius());
70
71         glm::vec3 up(0.0f);
72         up[(srf + 2) % 3] = dir;
73
74         glm::vec3 target;
75         target[(srf + 0) % 3] = at.x;
76         target[(srf + 1) % 3] = at.y;
77         target[(srf + 2) % 3] = dir * (at.z + Reference().Radius());
78
79         view = glm::lookAt(position, target, up);
80
81         return *this;
82 }
83
84 Camera &Camera::MapView(int srf, const glm::vec3 &pos, float roll) noexcept {
85         track_orient = true;
86
87         float dir = srf < 3 ? 1.0f : -1.0f;
88
89         glm::vec3 up(0.0f);
90         up[(srf + 0) % 3] = std::sin(roll);
91         up[(srf + 1) % 3] = std::cos(roll);
92         up[(srf + 2) % 3] = 0.0f;
93
94         glm::vec3 target = pos;
95         target[(srf + 2) % 3] -= dir;
96
97         view = glm::lookAt(pos, target, up);
98
99         return *this;
100 }
101
102 Camera &Camera::TopDown(const creature::Creature &c, float distance, float roll) {
103         const creature::Situation &s = c.GetSituation();
104         if (s.OnPlanet()) {
105                 int srf = s.Surface();
106                 glm::vec3 pos(s.Position());
107                 pos[(srf + 2) % 3] += srf < 3 ? distance : -distance;
108                 Reference(s.GetPlanet());
109                 return MapView(srf, pos, roll);
110         } else {
111                 glm::vec3 pos(s.Position());
112                 pos += glm::normalize(pos) * distance;
113                 return Orbital(pos);
114         }
115 }
116
117 Camera &Camera::Orbital(const glm::vec3 &pos) noexcept {
118         track_orient = false;
119         view = glm::lookAt(pos, glm::vec3(0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
120         return *this;
121 }
122
123 glm::mat4 Camera::Model(const world::Body &b) const noexcept {
124         if (&b == ref) {
125                 return track_orient ? glm::mat4(1.0f) : glm::mat4(ref->LocalTransform());
126         } else if (b.HasParent() && &b.Parent() == ref) {
127                 return track_orient
128                         ? ref->InverseTransform() * b.FromParent() * b.LocalTransform()
129                         : b.FromParent() * b.LocalTransform();
130         } else if (ref->HasParent() && &ref->Parent() == &b) {
131                 return track_orient
132                         ? ref->InverseTransform() * ref->ToParent() * b.LocalTransform()
133                         : ref->ToParent() * b.LocalTransform();
134         } else {
135                 return track_orient
136                         ? ref->InverseTransform() * ref->ToUniverse() * b.FromUniverse() * b.LocalTransform()
137                         : ref->ToUniverse() * b.FromUniverse() * b.LocalTransform();
138         }
139 }
140
141 void Camera::UpdateProjection() noexcept {
142         projection = glm::perspective(fov, aspect, near, far);
143 }
144
145
146 Viewport::Viewport(int w, int h)
147 : width(w)
148 , height(h) {
149         Resize(w, h);
150         glClearColor(0.0, 0.0, 0.0, 1.0);
151 }
152
153 Viewport::~Viewport() {
154 }
155
156
157 void Viewport::Resize(int w, int h) {
158         width = w;
159         height = h;
160         glViewport(0, 0, w, h);
161 }
162
163 void Viewport::Clear() {
164         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
165 }
166
167 void Viewport::ClearDepth() {
168         glClear(GL_DEPTH_BUFFER_BIT);
169 }
170
171 }
172 }