1
\$\begingroup\$

I am rendering a 3D model of an object, and I want the user to be able to rotate around that object by dragging the mouse. To do this, I want to use the cursor position to continually update the modelview matrix.

To start with, my modelview matrix is:

1 0 0 0
0 1 0 0
0 0 1 -100
0 0 0 1

So the camera is looking at the origin of the object, and 100 units away from it. Let's say the mouse now moves onto the right-hand side of the screen. This means I want the camera to rotate around the object -- by 90 degrees, for example.

In order to compute the new modelview matrix, I take the identity matrix, rotate it by 90 degrees, and then move away from the object along the camera's z-axis by using glm::translate().

However, the problem I am having is that glm::translate() seems to move the matrix with respect to the world coordinate system, not the camera coordinate system. Therefore, "moving away from the object along the camera's z-axis" does not hold, because the z-axes of the world and camera coordinate systems are no longer aligned after the rotation.

Please could somebody explain how I should be achieving this?

Thank you :)

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Rotations occur relative to the origin, so you want the camera to be 100 units away already before you rotate it if you want the camera to stay focused on the origin.

This image will hopefully make it clear (source page):

enter image description here

The view matrix you would use is then (I apply the transformations from right-to-left):

view = inverse( rotate(90) * translate(100) * identity )

Which follows from the fact that the view matrix is the inverse of the camera's world coordinate matrix.

\$\endgroup\$

You must log in to answer this question.

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