I recently went back to look at some old OpenGL(3.x+) programs I did and checked my camera class. In my camera class I have a function which calculates the view matrix. The calculations are based on OpenGL's gluLookAt function
Here's what the class looks like (I've omitted several identical and irrelevant functions for less code):
#include "camera.h"
//Create view matrix
glm::mat4 Camera::createViewMatrix()
{
glm::vec3 lookDirection = glm::normalize(cameraTargetPos); //Look direction of camera
static glm::vec3 upDirection = glm::normalize(glm::vec3(0.0f, 1.0f, 0.0f)); //Up direction of camera in the world, aligned with world's y-axis.
glm::vec3 rightDirection = glm::normalize(glm::cross(lookDirection, upDirection)); //Right direction of camera
glm::vec3 perpUpDirection = glm::cross(rightDirection, lookDirection); //Re-calculate up direction, basis vectors may not be orthonormal
static glm::mat4 viewMatrixAxes;
//Create view matrix, [0] is first column, [1] is second etc.
viewMatrixAxes[0] = glm::vec4(rightDirection, 0.0f);
viewMatrixAxes[1] = glm::vec4(perpUpDirection, 0.0f);
viewMatrixAxes[2] = glm::vec4(-lookDirection, 0.0f);
viewMatrixAxes = glm::transpose(viewMatrixAxes); //Transpose for inverse
static glm::mat4 camPosTranslation; //Translate to position of camera
camPosTranslation[3] = glm::vec4(-cameraPosition, 1.0f);
viewMatrix = viewMatrixAxes*camPosTranslation;
return viewMatrix;
}
void Camera::yawRotation(GLfloat radian)
{
glm::vec3 spherCameraTarget(cartesianToSpherical(cameraTargetPos)); //Convert camera target to spherical coordinates
spherCameraTarget.y += radian; //Add radian units to camera target (in spherical coordinates)
cameraTargetPos = sphericalToCartesian(spherCameraTarget); //Convert camera target to cartesian coordinates
}
void Camera::pitchRotation(GLfloat radian)
{
glm::vec3 spherCameraTarget(cartesianToSpherical(cameraTargetPos)); //Convert camera target to spherical coordinates
spherCameraTarget.z += radian; //Add radian units to camera target (in spherical coordinates)
spherCameraTarget.z = glm::clamp(spherCameraTarget.z, 0.0001f, PI); //Clamp the pitch rotation between [0 PI] radians
cameraTargetPos = sphericalToCartesian(spherCameraTarget); //Convert camera target to cartesian coordinates
}
void Camera::moveBackward(GLfloat moveSpeed)
{
cameraPosition.x += (viewMatrix[0][2]*moveSpeed);
cameraPosition.y += (viewMatrix[1][2]*moveSpeed);
cameraPosition.z += (viewMatrix[2][2]*moveSpeed);
}
//Note: Change from standard convention. Z-axis is Y-axis, Y-axis is Z-axis and Z-axis should be negative
//Change this in formula below
glm::vec3 Camera::cartesianToSpherical(glm::vec3 cartesianCoordinate)
{
GLfloat r = (sqrt(pow(cartesianCoordinate.x, 2) + pow(cartesianCoordinate.y, 2) + pow(cartesianCoordinate.z, 2)));
GLfloat theta = atan2(-cartesianCoordinate.z, cartesianCoordinate.x);
GLfloat phi = acos(cartesianCoordinate.y/r);
glm::vec3 sphericalCoordinate(r, theta, phi);
return sphericalCoordinate;
}
//Note: See notes for cartesianToSpherical() function
glm::vec3 Camera::sphericalToCartesian(glm::vec3 sphericalCoordinate)
{
GLfloat theta = sphericalCoordinate.y;
GLfloat phi = sphericalCoordinate.z;
glm::vec3 cartesianCoordinate(cos(theta)*sin(phi), cos(phi), -sin(theta)*sin(phi));
return cartesianCoordinate * sphericalCoordinate.x;
} This worked as expected. I could strafe left/right, up/down, forward/backwards and I could rotate around my up vector 360 degrees and rotate around my right vector +-90 degrees. However I realized that according to the OpenGL documentation I'm calculating my view matrix wrong. If you look the first line of code in the createViewMatrix() function then you can see that my look direction is only a normalized camera look-at point. According to the documentation it should be (cameraTargetPos-cameraPosition). When I corrected it the entire camera system broke down, both the strafing and rotation. I fixed the strafing so it works now as it should, here's the new code (note that the view matrix is calculated correctly now and that all strafe methods update the camera look-at point):
//Create view matrix
glm::mat4 Camera::createViewMatrix()
{
glm::vec3 lookDirection = glm::normalize(cameraTargetPos-cameraPosition); //Look direction of camera
static glm::vec3 upDirection = glm::normalize(glm::vec3(0.0f, 1.0f, 0.0f)); //Up direction of camera in the world, aligned with world's y-axis.
glm::vec3 rightDirection = glm::normalize(glm::cross(lookDirection, upDirection)); //Right direction of camera
glm::vec3 perpUpDirection = glm::cross(rightDirection, lookDirection); //Re-calculate up direction, basis vectors may not be orthonormal
static glm::mat4 viewMatrixAxes;
//Create view matrix, [0] is first column, [1] is second etc.
viewMatrixAxes[0] = glm::vec4(rightDirection, 0.0f);
viewMatrixAxes[1] = glm::vec4(perpUpDirection, 0.0f);
viewMatrixAxes[2] = glm::vec4(-lookDirection, 0.0f);
viewMatrixAxes = glm::transpose(viewMatrixAxes); //Transpose for inverse
static glm::mat4 camPosTranslation; //Translate to position of camera
camPosTranslation[3] = glm::vec4(-cameraPosition, 1.0f);
viewMatrix = viewMatrixAxes*camPosTranslation;
return viewMatrix;
}
void Camera::moveBackward(GLfloat moveSpeed)
{
cameraTargetPos.x += (viewMatrix[0][2]*moveSpeed);
cameraTargetPos.y += (viewMatrix[1][2]*moveSpeed);
cameraTargetPos.z += (viewMatrix[2][2]*moveSpeed);
cameraPosition.x += (viewMatrix[0][2]*moveSpeed);
cameraPosition.y += (viewMatrix[1][2]*moveSpeed);
cameraPosition.z += (viewMatrix[2][2]*moveSpeed);
}
The rest of the functions in the old code (cartesianToSpherical(), yawRotation etc.) remain unchanged.
The rotation still remains broken. If I'm close enough to my object it works as it should. It's hard to describe how it's broken. If, for example I move my camera far back enough that the camera look-at point has a positive value (it starts with a negative value a first) one of the spherical coordinates (theta) ends up being negative so when I rotate around the up vector to the left I end up rotating right instead, not only that but I never complete a whole revolution. It's as if I rotate CW 45 degrees and then CCW 45 degrees, it just keeps going on like that. There's some other weird behaviour that goes on as well.
I'm quite certain it has to do with how I go back and forth between cartesian/spherical coordinates and the formulas I use but I'm lost in how to solve it. That I got it to work properly with wrong code is a miracle in itself already.
If you want to try and compile the code and run it to see the effect yourself let me know and I'll attach the source code here.
Any help is appreciated, thanks!