From f2118614da629bd7fa98cad7702ef846758418c0 Mon Sep 17 00:00:00 2001
From: Brad Davis <bdavis@saintandreas.org>
Date: Wed, 23 Nov 2016 10:30:52 -0800
Subject: [PATCH] Fix camera in render perf test

---
 tests/render-perf/src/Camera.hpp | 27 ++++++++++++++-------------
 tests/render-perf/src/main.cpp   |  5 +++--
 2 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/tests/render-perf/src/Camera.hpp b/tests/render-perf/src/Camera.hpp
index 6e1d95bfe1..a3b33ceb14 100644
--- a/tests/render-perf/src/Camera.hpp
+++ b/tests/render-perf/src/Camera.hpp
@@ -13,11 +13,11 @@ protected:
     }
 
 public:
-    glm::quat getOrientation() const {
-        return glm::angleAxis(yawPitch.x, Vectors::UP) * glm::angleAxis(yawPitch.y, Vectors::RIGHT);
+    const glm::quat& getOrientation() const {
+        return orientation;
     }
 
-    vec2 yawPitch { 0 };
+    glm::quat orientation;
     glm::vec3 position;
     float rotationSpeed { 1.0f };
     float movementSpeed { 1.0f };
@@ -77,24 +77,25 @@ public:
     };
 
     void rotate(const float delta) {
-        yawPitch.x += delta;
+        orientation = glm::angleAxis(delta, Vectors::UP) * orientation;
         updateViewMatrix();
     }
 
     void rotate(const glm::vec2& delta) {
-        yawPitch += delta;
+
+        // create orientation vectors
+        auto lookat = orientation * Vectors::UNIT_NEG_Z;
+        auto forward = glm::normalize(vec3(lookat.x, 0, lookat.z));
+        auto side = glm::cross(Vectors::UP, forward);
+
+        // rotate camera with quaternions created from axis and angle
+        orientation = glm::angleAxis(delta.x, Vectors::UP) * orientation;
+        orientation = glm::angleAxis(-delta.y, side) * orientation;
         updateViewMatrix();
     }
 
     void setRotation(const glm::quat& rotation) {
-        glm::vec3 f = rotation * Vectors::UNIT_NEG_Z;
-        f.y = 0;
-        f = glm::normalize(f);
-        yawPitch.x = angleBetween(Vectors::UNIT_NEG_Z, f);
-        f = rotation * Vectors::UNIT_NEG_Z;
-        f.x = 0;
-        f = glm::normalize(f);
-        yawPitch.y = angleBetween(Vectors::UNIT_NEG_Z, f);
+        orientation = rotation;
         updateViewMatrix();
     }
 
diff --git a/tests/render-perf/src/main.cpp b/tests/render-perf/src/main.cpp
index b152c4b4bf..3be1a92af1 100644
--- a/tests/render-perf/src/main.cpp
+++ b/tests/render-perf/src/main.cpp
@@ -946,7 +946,7 @@ private:
                         orientationRegex.cap(3).toFloat(),
                         orientationRegex.cap(4).toFloat());
                     if (!glm::any(glm::isnan(v))) {
-                        _camera.setRotation(glm::normalize(glm::quat(v.w, v.x, v.y, v.z)));
+                        _camera.setRotation(glm::quat(v.w, v.x, v.y, v.z));
                     }
                 }
             }
@@ -1008,6 +1008,7 @@ private:
             arg(v.x).arg(v.y).arg(v.z).
             arg(q.x).arg(q.y).arg(q.z).arg(q.w);
         _settings.setValue(LAST_LOCATION_KEY, viewpoint);
+        _camera.setRotation(q);
     }
 
     void restorePosition() {
@@ -1019,7 +1020,7 @@ private:
     }
 
     void resetPosition() {
-        _camera.yawPitch = vec3(0);
+        _camera.setRotation(quat());
         _camera.setPosition(vec3());
     }