]> git.localhorst.tv Git - tacos.git/blob - src/graphics/camera.cpp
mouse cursor mockup
[tacos.git] / src / graphics / camera.cpp
1 #include "camera.hpp"
2
3 #include <glm/gtx/rotate_vector.hpp>
4
5
6 namespace tacos {
7
8 Camera::Camera()
9 : focus(0.0f)
10 , distance(100.0f)
11 , orientation(0.52359877559829887307f, 0.0f) { // π/6
12
13 }
14
15 glm::mat4 Camera::View() const noexcept {
16         // zero yaw is into the screen, so -Z is the base view direction we're transforming from
17         // pitch needs to be inverted because we're projecting the distance back
18         const glm::vec3 position(focus - distance * rotateY(rotateX(glm::vec3(0.0f, 0.0f, -1.0f), -orientation.x), orientation.y));
19         return glm::lookAt(position, focus, glm::vec3(0.0f, 1.0f, 0.0f));
20 }
21
22 void Camera::Move(const glm::vec3 &delta) noexcept {
23         focus += rotateY(delta, orientation.y);
24 }
25
26 void Camera::Rotate(const glm::vec2 &delta) noexcept {
27         orientation += delta;
28         orientation.x = glm::clamp(orientation.x, 0.0f, 1.55f);
29         while (orientation.y > 3.14159265358979323844f) { // π
30                 orientation.y -= 6.28318530717958647688f; // 2π
31         }
32         while (orientation.y < -3.14159265358979323844f) {
33                 orientation.y += 6.28318530717958647688f;
34         }
35 }
36
37 }