]> git.localhorst.tv Git - tacos.git/blobdiff - src/graphics/camera.cpp
controllable camera
[tacos.git] / src / graphics / camera.cpp
index bc43e04292e233696e0a60b7c5339031f57db912..d9b8e848c779a5bb5ff1c889934e906295114b40 100644 (file)
@@ -5,21 +5,33 @@
 
 namespace tacos {
 
-Camera::Camera(const glm::vec3 &f)
-: focus(&f)
+Camera::Camera()
+: focus(0.0f)
 , distance(100.0f)
-, pitch(1.04719755119659774614f) // π/3
-, yaw(0.0f) {
+, orientation(0.52359877559829887307f, 0.0f) { // π/6
 
 }
 
-void Camera::Focus(const glm::vec3 &f) noexcept {
-       focus = &f;
+glm::mat4 Camera::View() const noexcept {
+       // zero yaw is into the screen, so -Z is the base view direction we're transforming from
+       // pitch needs to be inverted because we're projecting the distance back
+       const glm::vec3 position(focus - distance * rotateY(rotateX(glm::vec3(0.0f, 0.0f, -1.0f), -orientation.x), orientation.y));
+       return glm::lookAt(position, focus, glm::vec3(0.0f, 1.0f, 0.0f));
 }
 
-glm::mat4 Camera::View() const noexcept {
-       const glm::vec3 position(*focus - distance * rotateY(rotateX(glm::vec3(0.0f, 0.0f, 1.0f), pitch), yaw));
-       return glm::lookAt(position, *focus, glm::vec3(0.0f, 1.0f, 0.0f));
+void Camera::Move(const glm::vec3 &delta) noexcept {
+       focus += rotateY(delta, orientation.y);
+}
+
+void Camera::Rotate(const glm::vec2 &delta) noexcept {
+       orientation += delta;
+       orientation.x = glm::clamp(orientation.x, -1.55f, 1.55f);
+       while (orientation.y > 3.14159265358979323844f) { // π
+               orientation.y -= 6.28318530717958647688f; // 2π
+       }
+       while (orientation.y < -3.14159265358979323844f) {
+               orientation.y += 6.28318530717958647688f;
+       }
 }
 
 }