# rotate opengl mesh relative to camera

I have a cube in opengl. It's position is determined by multiplying it's specific model matrix, the view matrix, and the projection matrix and then passing that to the shader as per this tutorial (http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/). I want to rotate it relative to the camera.

The only way I can think of getting the correct axis is by multiplying the inverse of the model matrix (because that's where all the previous rotations and tranforms are stored) times the view matrix times the axis of rotation (x or y).

I feel like there's got to be a better way to do this like use something other than model, view and projection matrices, or maybe I'm doing something wrong. That's what all the tutorials I've seen use.

PS I'm also trying to keep with opengl 4 core stuff.

edit: If quaternions would fix my problems, could someone point me to a good tutorial/example for switching from 4x4 matrices to quaternions. I'm a little daunted by the task.

• I couldn't understand the "I want to rotate it relative to the camera" part. Perhaps you could show us with a drawing/screenshot what you are trying to achieve? Aug 7, 2012 at 21:31
• Here's an example of rotating relative to camera vs. what's happening right now. i.imgur.com/8PmVg.png Aug 9, 2012 at 15:19
• I would post an answer, but I've already kinda written one elsewhere. Aug 10, 2012 at 22:38
• Thanks, I've checked out your pages, but I guess I never saw that one. This definitely answers my question. I think eventually I'm going to try out quaternions. Aug 11, 2012 at 13:28
• Nevermind, posted answer below. Aug 11, 2012 at 15:22

glm::mat4 cubeInv = glm::inverse(cubeMat);