summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2021-06-19 23:17:49 +0200
committerAdrian Kummerlaender2021-06-19 23:17:49 +0200
commit0f0a35339723e5dc67d2e519f606e5854cfb9e96 (patch)
tree0729ce5669e8f560f0b4eca6f71f4997e4bc4656
parentb8d9e36baf889f93aff5761f49fa7eb30874d283 (diff)
downloadLiterateLB-0f0a35339723e5dc67d2e519f606e5854cfb9e96.tar
LiterateLB-0f0a35339723e5dc67d2e519f606e5854cfb9e96.tar.gz
LiterateLB-0f0a35339723e5dc67d2e519f606e5854cfb9e96.tar.bz2
LiterateLB-0f0a35339723e5dc67d2e519f606e5854cfb9e96.tar.lz
LiterateLB-0f0a35339723e5dc67d2e519f606e5854cfb9e96.tar.xz
LiterateLB-0f0a35339723e5dc67d2e519f606e5854cfb9e96.tar.zst
LiterateLB-0f0a35339723e5dc67d2e519f606e5854cfb9e96.zip
Tidy up camera rotation
-rw-r--r--lbm.org65
-rw-r--r--tangle/util/camera.h59
2 files changed, 48 insertions, 76 deletions
diff --git a/lbm.org b/lbm.org
index c953031..ac99324 100644
--- a/lbm.org
+++ b/lbm.org
@@ -4491,13 +4491,11 @@ a orbiting camera. This type of camera can be rotated around a central target po
While translation is realized easily by adding a shift vector to the current camera position, rotation
is more complex. We are going to accumulate all rotation operations in a single quaternion variable
-=_rotation=. This 4D vector is easily converted into a rotation matrix for mapping the screen space
-pinhole parameters into world space.
+=_rotation=. Given 3D vectors $v$ are easily rotated by a quaternion $q$ using $v^\prime = q v \overline{q}$.
#+NAME: camera-spherical-coordinates
#+BEGIN_SRC cpp
glm::quat _rotation;
-glm::mat3 _matrix;
#+END_SRC
Using the rotation matrix we can compute the pinhole camera parameters. As they only change
@@ -4505,27 +4503,22 @@ when a rotation is performed we store them in a set of variables.
#+NAME: camera-cartesian-coordinates
#+BEGIN_SRC cpp
-float3 _target;
-float3 _position;
-float3 _forward;
-float3 _right;
-float3 _up;
+glm::vec3 _target;
+glm::vec3 _position;
+glm::vec3 _forward;
+glm::vec3 _right;
+glm::vec3 _up;
float _distance;
#+END_SRC
-The =update= function projects the screen space forward, right and up vectors into world space
-by simply multiplying them by the rotation matrix.
+The =update= function projects the screen space forward, right and up vectors into world space.
#+NAME: camera-projection
#+BEGIN_SRC cpp :eval no :main no
-glm::vec3 position = _matrix * glm::vec3(0, _distance, 0);
-_position = _target + make_float3(position[0], position[1], position[2]);
-_forward = normalize(_target - _position);
-
-glm::vec3 right = _matrix * glm::vec4(-1, 0, 0, 0);
-_right = make_float3(right[0], right[1], right[2]);
-glm::vec3 up = _matrix * glm::vec4(glm::cross(glm::vec3(0, 1, 0), glm::vec3(-1, 0, 0)), 0);
-_up = make_float3(up[0], up[1], up[2]);
+_position = _target + glm::axis(_rotation * glm::quat(0, 0, _distance, 0) * glm::conjugate(_rotation));
+_forward = glm::normalize(_target - _position);
+_right = glm::axis(_rotation * glm::quat(0, -1, 0, 0) * glm::conjugate(_rotation));
+_up = glm::axis(_rotation * glm::quat(0, glm::cross(glm::vec3(0, 1, 0), glm::vec3(-1, 0, 0))) * glm::conjugate(_rotation));
#+END_SRC
Finally we need to handle user input events to change the translation vector
@@ -4554,22 +4547,18 @@ case sf::Event::MouseButtonReleased:
}
break;
case sf::Event::MouseMoved:
+ float2 mouse = make_float2(event.mouseMove.x, event.mouseMove.y);
if (_dragging) {
- float2 mouse = make_float2(event.mouseMove.x, event.mouseMove.y);
- float2 delta = mouse - _lastMouse;
- _lastMouse = mouse;
-
- glm::quat rotation_z = glm::conjugate(_rotation) * glm::vec3(0,0,0.01*delta.x);
- glm::quat rotation_x = glm::conjugate(_rotation) * glm::vec3(0.01*delta.y,0,0);
-
- _matrix = _matrix * glm::mat3_cast(_rotation * glm::cross(rotation_x, rotation_z));
+ float2 delta = 0.005 * (mouse - _lastMouse);
+ glm::quat rotation_z = glm::vec3(0,0,delta.x);
+ glm::quat rotation_x = glm::vec3(delta.y,0,0);
+ _rotation *= glm::cross(rotation_x, rotation_z);
}
if (_moving) {
- float2 mouse = make_float2(event.mouseMove.x, event.mouseMove.y);
- float2 delta = mouse - _lastMouse;
- _lastMouse = mouse;
- _target += 0.4*_right*delta.x + 0.4*_up*delta.y;
+ float2 delta = 0.04 * (mouse - _lastMouse);
+ _target += _right*delta.x + _up*delta.y;
}
+ _lastMouse = mouse;
break;
}
#+END_SRC
@@ -4587,7 +4576,7 @@ private:
public:
Camera(float3 target, float distance):
_distance(distance),
- _target(target),
+ _target(target.x, target.y, target.z),
_dragging(false),
_moving(false) {
update();
@@ -4603,28 +4592,24 @@ public:
}
float3 getPosition() const {
- return _position;
+ return make_float3(_position.x, _position.y, _position.z);
}
float3 getForward() const {
- return _forward;
+ return make_float3(_forward.x, _forward.y, _forward.z);
}
float3 getRight() const {
- return _right;
+ return make_float3(_right.x, _right.y, _right.z);
}
float3 getUp() const {
- return _up;
+ return make_float3(_up.x, _up.y, _up.z);
}
};
#+END_SRC
#+BEGIN_SRC cpp :tangle tangle/util/camera.h :eval no :main no
#include <cuda-samples/Common/helper_math.h>
-#include "SFML/Window/Event.hpp"
-
-#include <glm/glm.hpp>
-#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/quaternion.hpp>
-#include <glm/common.hpp>
+#include "SFML/Window/Event.hpp"
<<camera>>
#+END_SRC
diff --git a/tangle/util/camera.h b/tangle/util/camera.h
index 2b2c9ef..4f31489 100644
--- a/tangle/util/camera.h
+++ b/tangle/util/camera.h
@@ -1,20 +1,15 @@
#include <cuda-samples/Common/helper_math.h>
-#include "SFML/Window/Event.hpp"
-
-#include <glm/glm.hpp>
-#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/quaternion.hpp>
-#include <glm/common.hpp>
+#include "SFML/Window/Event.hpp"
class Camera {
private:
glm::quat _rotation;
- glm::mat3 _matrix;
- float3 _target;
- float3 _position;
- float3 _forward;
- float3 _right;
- float3 _up;
+ glm::vec3 _target;
+ glm::vec3 _position;
+ glm::vec3 _forward;
+ glm::vec3 _right;
+ glm::vec3 _up;
float _distance;
bool _dragging;
bool _moving;
@@ -23,21 +18,17 @@ private:
public:
Camera(float3 target, float distance):
_distance(distance),
- _target(target),
+ _target(target.x, target.y, target.z),
_dragging(false),
_moving(false) {
update();
}
void update() {
- glm::vec3 position = _matrix * glm::vec3(0, _distance, 0);
- _position = _target + make_float3(position[0], position[1], position[2]);
- _forward = normalize(_target - _position);
-
- glm::vec3 right = _matrix * glm::vec4(-1, 0, 0, 0);
- _right = make_float3(right[0], right[1], right[2]);
- glm::vec3 up = _matrix * glm::vec4(glm::cross(glm::vec3(0, 1, 0), glm::vec3(-1, 0, 0)), 0);
- _up = make_float3(up[0], up[1], up[2]);
+ _position = _target + glm::axis(_rotation * glm::quat(0, 0, _distance, 0) * glm::conjugate(_rotation));
+ _forward = glm::normalize(_target - _position);
+ _right = glm::axis(_rotation * glm::quat(0, -1, 0, 0) * glm::conjugate(_rotation));
+ _up = glm::axis(_rotation * glm::quat(0, glm::cross(glm::vec3(0, 1, 0), glm::vec3(-1, 0, 0))) * glm::conjugate(_rotation));
}
void handle(sf::Event& event) {
@@ -62,37 +53,33 @@ public:
}
break;
case sf::Event::MouseMoved:
+ float2 mouse = make_float2(event.mouseMove.x, event.mouseMove.y);
if (_dragging) {
- float2 mouse = make_float2(event.mouseMove.x, event.mouseMove.y);
- float2 delta = mouse - _lastMouse;
- _lastMouse = mouse;
-
- glm::quat rotation_z = glm::conjugate(_rotation) * glm::vec3(0,0,0.01*delta.x);
- glm::quat rotation_x = glm::conjugate(_rotation) * glm::vec3(0.01*delta.y,0,0);
-
- _matrix = _matrix * glm::mat3_cast(_rotation * glm::cross(rotation_x, rotation_z));
+ float2 delta = 0.005 * (mouse - _lastMouse);
+ glm::quat rotation_z = glm::vec3(0,0,delta.x);
+ glm::quat rotation_x = glm::vec3(delta.y,0,0);
+ _rotation *= glm::cross(rotation_x, rotation_z);
}
if (_moving) {
- float2 mouse = make_float2(event.mouseMove.x, event.mouseMove.y);
- float2 delta = mouse - _lastMouse;
- _lastMouse = mouse;
- _target += 0.4*_right*delta.x + 0.4*_up*delta.y;
+ float2 delta = 0.04 * (mouse - _lastMouse);
+ _target += _right*delta.x + _up*delta.y;
}
+ _lastMouse = mouse;
break;
}
update();
}
float3 getPosition() const {
- return _position;
+ return make_float3(_position.x, _position.y, _position.z);
}
float3 getForward() const {
- return _forward;
+ return make_float3(_forward.x, _forward.y, _forward.z);
}
float3 getRight() const {
- return _right;
+ return make_float3(_right.x, _right.y, _right.z);
}
float3 getUp() const {
- return _up;
+ return make_float3(_up.x, _up.y, _up.z);
}
};