I'm trying to make a basic FPS camera view matrix, using position, pitch/yaw, and worldUp. Currently, my camera's view matrix is just a plain rotation matrix (made with yaw/pitch/roll) and the result is the view appears to be looking down the negative Z-axis, which in my case is my floor. Most tutorials I can find recommend making a view matrix by passing in an "eye" and "target" vector. But this isn't a desirable setup for an FPS camera where I only know the position and angles (pitch/yaw/roll) of the camera -- I don't have a specific point in mind that I want the camera to be focused on!
I'm using OpenGL as a renderer, but I don't want to use GLM as I'd wish/hope to understand how to implement this without helper libs. Also, my meshes and world matrices are made so that Z is my worldUp, X as forward, and Y as right/left. That might seem odd, but old Unreal engine games used this setup and I'm just accustomed to it for mapping/modding.
As mentioned my view matrix is currently looking down at the floor, but I'm not surprised that it's wrong because I've never incorporated or multiplied my worldUp vector into the matrix, and I don't understand how to.
So that's my question: How exactly do I incorporate my worldUp vector into my rotation matrix, to turn it into a proper view matrix -- or better yet, what is the proper method for constructing a view matrix when you have exactly these variables: 1.) Camera Position (XYZ) 2.) World Up vector ("0,0,1" for me) 3.) Camera Pitch/Yaw/Roll (Really only need Pitch/Yaw!)
I've been going through loads of tutorials and I can't find what I'm looking for. This is how I've started, but all it is is a normal rotation matrix, so honestly a full methodology would be really appreciated as I've been pulling my hair out over this for a week now. Thank you for any guidance on this.
MAT4X4F mrol;
MAT4X4F mpit;
MAT4X4F myaw;
float cr, cp, cy;
float sr, sp, sy;
cr = cos(_cam_roll); cp = cos(_cam_pitch); cy = cos(_cam_yaw);
sr = sin(_cam_roll); sp = sin(_cam_pitch); sy = sin(_cam_yaw);
// Yaw (about Z-axis)
myaw.m[0] = cy; myaw.m[1] = -sy; myaw.m[2] = 0.0f; myaw.m[3] = 0.0f;
myaw.m[4] = sy; myaw.m[5] = cy; myaw.m[6] = 0.0f; myaw.m[7] = 0.0f;
myaw.m[8] = 0.0f; myaw.m[9] = 0.0f; myaw.m[10] = 1.0f; myaw.m[11] = 0.0f;
myaw.m[12] = 0.0f; myaw.m[13] = 0.0f; myaw.m[14] = 0.0f; myaw.m[15] = 1.0f;
// Pitch (about Y-axis)
mpit.m[0] = cp; mpit.m[1] = 0.0f; mpit.m[2] = sp; mpit.m[3] = 0.0f;
mpit.m[4] = 0.0f; mpit.m[5] = 1.0f; mpit.m[6] = 0.0f; mpit.m[7] = 0.0f;
mpit.m[8] = -sp; mpit.m[9] = 0.0f; mpit.m[10] = cp; mpit.m[11] = 0.0f;
mpit.m[12] = 0.0f; mpit.m[13] = 0.0f; mpit.m[14] = 0.0f; mpit.m[15] = 1.0f;
// Roll (about X-axis)
mrol.m[0] = 1.0f; mrol.m[1] = 0.0f; mrol.m[2] = 0.0f; mrol.m[3] = 0.0f;
mrol.m[4] = 0.0f; mrol.m[5] = cr; mrol.m[6] = -sr; mrol.m[7] = 0.0f;
mrol.m[8] = 0.0f; mrol.m[9] = sr; mrol.m[10] = cr; mrol.m[11] = 0.0f;
mrol.m[12] = 0.0f; mrol.m[13] = 0.0f; mrol.m[14] = 0.0f; mrol.m[15] = 1.0f;
_cam_matrix = myaw * mpit * mrol;
Most tutorials I can find recommend making a view matrix by passing in an "eye" and "target" vector. But this isn't a desirable setup for an FPS camera where I only know the position and angles (pitch/yaw/roll) of the camera
But you do know what to look at. Its quite easy to create an forward vector from yaw and pitch, and position + forward is the position to look at. \$\endgroup\$