0
\$\begingroup\$

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.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ 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? \$\endgroup\$
    – r2d2rigo
    Commented Aug 7, 2012 at 21:31
  • \$\begingroup\$ Here's an example of rotating relative to camera vs. what's happening right now. i.imgur.com/8PmVg.png \$\endgroup\$
    – shuall
    Commented Aug 9, 2012 at 15:19
  • 7
    \$\begingroup\$ I would post an answer, but I've already kinda written one elsewhere. \$\endgroup\$ Commented Aug 10, 2012 at 22:38
  • \$\begingroup\$ 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. \$\endgroup\$
    – shuall
    Commented Aug 11, 2012 at 13:28
  • \$\begingroup\$ Nevermind, posted answer below. \$\endgroup\$
    – shuall
    Commented Aug 11, 2012 at 15:22

1 Answer 1

1
\$\begingroup\$

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. https://i.sstatic.net/uR2Kt.png Definately reading through those tutorials though.

\$\endgroup\$

You must log in to answer this question.

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