2 #include "Viewport.hpp"
4 #include "../const.hpp"
5 #include "../world/Body.hpp"
9 #include <glm/gtx/transform.hpp>
15 Camera::Camera(const world::Body &r) noexcept
20 , projection(glm::perspective(fov, aspect, near, far))
23 , track_orient(false) {
27 Camera::~Camera() noexcept {
30 Camera &Camera::FOV(float f) noexcept {
36 Camera &Camera::Aspect(float r) noexcept {
42 Camera &Camera::Aspect(float w, float h) noexcept {
47 Camera &Camera::Clip(float n, float f) noexcept {
54 Camera &Camera::Reference(const world::Body &r) noexcept {
59 Camera &Camera::FirstPerson(int srf, const glm::vec3 &pos, const glm::vec3 &at) noexcept {
62 float dir = srf < 3 ? 1.0f : -1.0f;
65 position[(srf + 0) % 3] = pos.x;
66 position[(srf + 1) % 3] = pos.y;
67 position[(srf + 2) % 3] = dir * (pos.z + Reference().Radius());
70 up[(srf + 2) % 3] = dir;
73 target[(srf + 0) % 3] = at.x;
74 target[(srf + 1) % 3] = at.y;
75 target[(srf + 2) % 3] = dir * (at.z + Reference().Radius());
77 view = glm::lookAt(position, target, up);
82 Camera &Camera::MapView(int srf, const glm::vec3 &pos, float roll) noexcept {
85 float dir = srf < 3 ? 1.0f : -1.0f;
88 position[(srf + 0) % 3] = pos.x;
89 position[(srf + 1) % 3] = pos.y;
90 position[(srf + 2) % 3] = dir * (pos.z + Reference().Radius());
93 up[(srf + 0) % 3] = std::cos(roll);
94 up[(srf + 1) % 3] = std::sin(roll);
95 up[(srf + 2) % 3] = 0.0f;
97 glm::vec3 target = position;
98 target[(srf + 2) % 3] -= dir;
100 view = glm::lookAt(position, target, up);
105 Camera &Camera::Orbital(const glm::vec3 &pos) noexcept {
106 track_orient = false;
107 view = glm::lookAt(pos, glm::vec3(0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
111 glm::mat4 Camera::Model(const world::Body &b) const noexcept {
113 return track_orient ? glm::mat4(1.0f) : glm::mat4(ref->LocalTransform());
114 } else if (b.HasParent() && &b.Parent() == ref) {
116 ? ref->InverseTransform() * b.FromParent() * b.LocalTransform()
117 : b.FromParent() * b.LocalTransform();
118 } else if (ref->HasParent() && &ref->Parent() == &b) {
120 ? ref->InverseTransform() * ref->ToParent() * b.LocalTransform()
121 : ref->ToParent() * b.LocalTransform();
124 ? ref->InverseTransform() * ref->ToUniverse() * b.FromUniverse() * b.LocalTransform()
125 : ref->ToUniverse() * b.FromUniverse() * b.LocalTransform();
129 void Camera::UpdateProjection() noexcept {
130 projection = glm::perspective(fov, aspect, near, far);
134 Viewport::Viewport(int w, int h)
138 glClearColor(0.0, 0.0, 0.0, 1.0);
141 Viewport::~Viewport() {
145 void Viewport::Resize(int w, int h) {
148 glViewport(0, 0, w, h);
151 void Viewport::Clear() {
152 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
155 void Viewport::ClearDepth() {
156 glClear(GL_DEPTH_BUFFER_BIT);