Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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 ( 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.

share|improve this question
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? – r2d2rigo Aug 7 '12 at 21:31
Here's an example of rotating relative to camera vs. what's happening right now. – shuall Aug 9 '12 at 15:19
I would post an answer, but I've already kinda written one elsewhere. – Nicol Bolas Aug 10 '12 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. – shuall Aug 11 '12 at 13:28
Nevermind, posted answer below. – shuall Aug 11 '12 at 15:22

1 Answer 1

I ended up just doing something like this

glm::mat4 cubeInv = glm::inverse(cubeMat);
glm::vec4 rX = cubeInv * ViewMat * glm::vec4(0.f, -1.f, 0.f, 1.f);
glm::vec4 rY = cubeInv * ViewMat * glm::vec4(-1.f, 0.f, 0.f, 1.f);
cubeMat = glm::rotate(cubeMat, (wWidth/2 - xpos) * delta, glm::vec3(rX.x, rX.y, rX.z));
cubeMat = glm::rotate(cubeMat, (wHeight/2 - ypos) * delta, glm::vec3(rY.x, rY.y, rY.z));

Nicol Bolas's answer ended up being more of a camera-relative translation, so that the cube was always oriented towards the camera, but moving the mouse meant moving the cube along a sphere around the camera. I was looking for more of a rotation around camera-relative axis, and my original assumption turned out to be correct. I hope this poorly drawn picture will explain the differences better. Definately reading through those tutorials though.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.