#include "Camera.hpp"
#include "Viewport.hpp"
-#include "../const.hpp"
+#include "../creature/Creature.hpp"
+#include "../math/const.hpp"
#include "../world/Body.hpp"
+#include "../world/Planet.hpp"
#include <cmath>
#include <GL/glew.h>
+#include <glm/gtx/euler_angles.hpp>
+#include <glm/gtx/rotate_vector.hpp>
#include <glm/gtx/transform.hpp>
return *this;
}
-Camera &Camera::FirstPerson(int srf, const glm::vec3 &pos, const glm::vec3 &at) noexcept {
- track_orient = true;
-
- float dir = srf < 3 ? 1.0f : -1.0f;
-
- glm::vec3 position;
- position[(srf + 0) % 3] = pos.x;
- position[(srf + 1) % 3] = pos.y;
- position[(srf + 2) % 3] = dir * (pos.z + Reference().Radius());
-
- glm::vec3 up(0.0f);
- up[(srf + 2) % 3] = dir;
-
- glm::vec3 target;
- target[(srf + 0) % 3] = at.x;
- target[(srf + 1) % 3] = at.y;
- target[(srf + 2) % 3] = dir * (at.z + Reference().Radius());
-
- view = glm::lookAt(position, target, up);
-
- return *this;
-}
-
-Camera &Camera::MapView(int srf, const glm::vec3 &pos, float roll) noexcept {
- track_orient = true;
-
- float dir = srf < 3 ? 1.0f : -1.0f;
-
- glm::vec3 position;
- position[(srf + 0) % 3] = pos.x;
- position[(srf + 1) % 3] = pos.y;
- position[(srf + 2) % 3] = dir * (pos.z + Reference().Radius());
-
- glm::vec3 up(0.0f);
- up[(srf + 0) % 3] = std::cos(roll);
- up[(srf + 1) % 3] = std::sin(roll);
- up[(srf + 2) % 3] = 0.0f;
-
- glm::vec3 target = position;
- target[(srf + 2) % 3] -= dir;
-
- view = glm::lookAt(position, target, up);
-
- return *this;
-}
-
Camera &Camera::Orbital(const glm::vec3 &pos) noexcept {
track_orient = false;
view = glm::lookAt(pos, glm::vec3(0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
return *this;
}
+Camera &Camera::Radial(const creature::Creature &c, double distance, const glm::dvec3 &angle) {
+ const creature::Situation &s = c.GetSituation();
+ glm::dvec3 pos(s.Position());
+ glm::dvec3 up(0.0);
+ glm::dvec3 dir(0.0, 0.0, -distance);
+ if (s.OnSurface()) {
+ Reference(s.GetPlanet());
+ track_orient = true;
+ up = s.GetPlanet().NormalAt(s.Position());
+ glm::dvec3 ref(normalize(cross(up, glm::dvec3(up.z, up.x, up.y))));
+ dir =
+ glm::dmat3(ref, up, cross(ref, up))
+ * glm::dmat3(glm::eulerAngleYX(-angle.y, -angle.x))
+ * dir;
+ } else {
+ up.y = 1.0;
+ dir = glm::dmat3(glm::eulerAngleYX(-angle.y, -angle.x)) * dir;
+ }
+ pos += up * (c.Size() * 0.5);
+ up = glm::rotate(up, angle.z, glm::normalize(-dir));
+ view = glm::lookAt(pos - dir, pos, up);
+ return *this;
+}
+
glm::mat4 Camera::Model(const world::Body &b) const noexcept {
if (&b == ref) {
return track_orient ? glm::mat4(1.0f) : glm::mat4(ref->LocalTransform());
? ref->InverseTransform() * ref->ToParent() * b.LocalTransform()
: ref->ToParent() * b.LocalTransform();
} else {
- // TODO: model matrices for path distances > 1
- return track_orient ? glm::mat4(1.0f) : glm::mat4(ref->LocalTransform());
+ return track_orient
+ ? ref->InverseTransform() * ref->ToUniverse() * b.FromUniverse() * b.LocalTransform()
+ : ref->ToUniverse() * b.FromUniverse() * b.LocalTransform();
}
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
+void Viewport::ClearDepth() {
+ glClear(GL_DEPTH_BUFFER_BIT);
+}
+
}
}