2 #include "Viewport.hpp"
4 #include "../creature/Creature.hpp"
5 #include "../math/const.hpp"
6 #include "../world/Body.hpp"
7 #include "../world/Planet.hpp"
11 #include <glm/gtx/euler_angles.hpp>
12 #include <glm/gtx/rotate_vector.hpp>
13 #include <glm/gtx/transform.hpp>
19 Camera::Camera(const world::Body &r) noexcept
24 , projection(glm::perspective(fov, aspect, near, far))
27 , track_orient(false) {
31 Camera::~Camera() noexcept {
34 Camera &Camera::FOV(float f) noexcept {
40 Camera &Camera::Aspect(float r) noexcept {
46 Camera &Camera::Aspect(float w, float h) noexcept {
51 Camera &Camera::Clip(float n, float f) noexcept {
58 Camera &Camera::Reference(const world::Body &r) noexcept {
63 Camera &Camera::FirstPerson(int srf, const glm::vec3 &pos, const glm::vec3 &at) noexcept {
66 float dir = srf < 3 ? 1.0f : -1.0f;
69 position[(srf + 0) % 3] = pos.x;
70 position[(srf + 1) % 3] = pos.y;
71 position[(srf + 2) % 3] = dir * (pos.z + Reference().Radius());
73 glm::vec3 up(world::Planet::SurfaceNormal(srf));
76 target[(srf + 0) % 3] = at.x;
77 target[(srf + 1) % 3] = at.y;
78 target[(srf + 2) % 3] = dir * (at.z + Reference().Radius());
80 view = glm::lookAt(position, target, up);
85 Camera &Camera::MapView(int srf, const glm::vec3 &pos, float roll) noexcept {
88 float dir = srf < 3 ? 1.0f : -1.0f;
91 up[(srf + 0) % 3] = std::sin(roll);
92 up[(srf + 1) % 3] = std::cos(roll);
93 up[(srf + 2) % 3] = 0.0f;
95 glm::vec3 target = pos;
96 target[(srf + 2) % 3] -= dir;
98 view = glm::lookAt(pos, target, up);
103 Camera &Camera::Orbital(const glm::vec3 &pos) noexcept {
104 track_orient = false;
105 view = glm::lookAt(pos, glm::vec3(0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
109 Camera &Camera::TopDown(const creature::Creature &c, double distance, double roll) {
110 const creature::Situation &s = c.GetSituation();
112 int srf = s.Surface();
113 glm::vec3 pos(s.Position() + (world::Planet::SurfaceNormal(srf) * distance));
114 Reference(s.GetPlanet());
115 return MapView(srf, pos, roll);
117 glm::vec3 pos(s.Position());
118 pos += glm::normalize(pos) * float(distance);
123 Camera &Camera::Radial(const creature::Creature &c, double distance, const glm::dvec3 &angle) {
124 const creature::Situation &s = c.GetSituation();
125 glm::dvec3 pos(s.Position());
127 glm::dvec3 dir(0.0, 0.0, -distance);
129 Reference(s.GetPlanet());
131 int srf = s.Surface();
132 up = world::Planet::SurfaceNormal(srf);
134 world::Planet::SurfaceOrientation(srf)
135 * glm::dmat3(glm::eulerAngleYX(-angle.y, -angle.x))
139 dir = glm::dmat3(glm::eulerAngleYX(-angle.y, -angle.x)) * dir;
141 pos += up * (c.Size() * 0.5);
142 up = glm::rotate(up, angle.z, glm::normalize(-dir));
143 view = glm::lookAt(pos - dir, pos, up);
147 glm::mat4 Camera::Model(const world::Body &b) const noexcept {
149 return track_orient ? glm::mat4(1.0f) : glm::mat4(ref->LocalTransform());
150 } else if (b.HasParent() && &b.Parent() == ref) {
152 ? ref->InverseTransform() * b.FromParent() * b.LocalTransform()
153 : b.FromParent() * b.LocalTransform();
154 } else if (ref->HasParent() && &ref->Parent() == &b) {
156 ? ref->InverseTransform() * ref->ToParent() * b.LocalTransform()
157 : ref->ToParent() * b.LocalTransform();
160 ? ref->InverseTransform() * ref->ToUniverse() * b.FromUniverse() * b.LocalTransform()
161 : ref->ToUniverse() * b.FromUniverse() * b.LocalTransform();
165 void Camera::UpdateProjection() noexcept {
166 projection = glm::perspective(fov, aspect, near, far);
170 Viewport::Viewport(int w, int h)
174 glClearColor(0.0, 0.0, 0.0, 1.0);
177 Viewport::~Viewport() {
181 void Viewport::Resize(int w, int h) {
184 glViewport(0, 0, w, h);
187 void Viewport::Clear() {
188 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
191 void Viewport::ClearDepth() {
192 glClear(GL_DEPTH_BUFFER_BIT);