I've been working on an OpenGL project and currently I'm attempting to rotate the camera by using the input from the cursor movement. To rotate the orientation of the camera I need to edit a 3 length vector by multiplying it with a 3x3 rotation matrix but instead of making a separate rotation matrix for each axis I have to calculate all the rotations simultaneously with one 3x3 matrix. I am not using GLM to do the rotation but instead I'm using my own function. Currently, I have made a function which will rotate the camera on the X axis which won't work due to the fact it only rotates on a singular axis. The original glm function redirects to a quaternion rotate function that takes a 4x4 matrix, an angle and a vector as prerequisites. GLM Does anyone have a solution?

What I have so far: $$ \begin{bmatrix} cos(angle) & -sin(angle) & 0 \\ sin(angle) & cos(angle) & 0 \\ 0 & 0 & 1 \ \end{bmatrix}* \begin{bmatrix} v.a & v.b & v.c \end{bmatrix} $$

What the rotation is supposed to look like: https://drive.google.com/file/d/1MJKgO_3tQPFxVLlO007csKyyyM8ih_J8/view?usp=sharing

Currently it is only able to rotate on a single axis.


1 Answer 1


Yep, this is quite doable! To be precise, you wouldn't be rotating about "all axes", you'd be representing your rotation as a rotation about a single axis that would be a generic unit vector not necessarily pointing along a Cartesian axis. The details are a bit messy, but easy enough to plug into and you can find them on Wikipedia's "Rotation matrix", subsection "Conversion from rotation matrix to axis–angle":


I won't write out the details, but the idea is that your rotation matrix should have the following properties:

  • it's a $3\times3$ real matrix (so it turns a real three-vector to a real three-vector).
  • it has determinant one (so it rotates without stretching).
  • it maps the axis of rotation to itself.
  • its trace is $1 + 2 \cos \theta$, where $\theta$ is the angle of rotation (this is clearly true for rotations about Cartesian axes, and the trace is independent of the axis).
  • it rotates in the proper direction: that is, clockwise or counterclockwise.
  • $\begingroup$ Hi, I took note of your answer and started re-writing the function. It finally works! Thank you and I'll take this as the accepted answer. $\endgroup$
    – AsianMario
    Jul 10, 2021 at 10:32
  • $\begingroup$ @AsianMario Yay! Glad it worked for you. Best of luck with your project. $\endgroup$
    – David
    Jul 10, 2021 at 14:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .