Switched to quaternion rotations to avoid drift

master
Sean Hickey 2014-07-19 23:29:10 -04:00
parent 7aad0929a6
commit ec02330acc
3 changed files with 33 additions and 3 deletions

View File

@ -49,8 +49,33 @@ void Camera::translate(vec3 v) {
this->target += v; // keep the view direction constant
}
// axis-angle rotation
void Camera::rotate(vec3 v) {
this->rotateQuat(v);
//this->rotateAxisAngle(v); // to change easily
}
// quaternion rotation
void Camera::rotateQuat(vec3 v) {
v = -v;
float angle = length(v);
vec3 w = normalize(v);
glm::quat viewDir (0, normalize(this->getViewDir()));
viewDir = glm::rotate(viewDir, angle, w);
glm::quat upDir (0, normalize(this->getUpDir()));
upDir = glm::rotate(upDir, angle, w);
vec3 u = axis(viewDir);
this->up = axis(upDir);
vec3 view = this->target - this->position;
this->target = this->position + u*length(view);
}
// axis-angle (exponential map) rotation
// doesn't have SLERP, so a more noticeable drift occurs
void Camera::rotateAxisAngle(vec3 v) {
float angle = length(v);
vec3 w = normalize(v);
vec3 a = this->getViewDir();

View File

@ -7,7 +7,9 @@
#ifndef CAMERA_H
#define CAMERA_H
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
using namespace glm;
@ -24,6 +26,9 @@ class Camera {
void translate(vec3 v);
void rotate(vec3 v);
void rotateAxisAngle(vec3 v);
void rotateQuat(vec3 v);
private:
vec3 position;
vec3 target;

View File

@ -52,7 +52,7 @@ void initGlobals() {
// globals
globals.translate_step = 0.05f;
globals.rotate_step = 0.01f;
globals.rotate_step = 0.03f;
globals.mousex = viewport.width/2.0;
globals.mousey = viewport.height/2.0;
}
@ -116,7 +116,7 @@ void moveCamera() {
glm::vec3 right = glm::cross(forward,up);
float epsilon = 0.001f;
float scale = 2000.0f;
float scale = 1000.0f;
// mouse
double xpos, ypos;