Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm gonna try this again

I've been trying to setup a simple camera class with OpenGL but I'm completely lost and I've made zero progress creating anything useful. I'm using modern OpenGL and the glm library for matrix math. To get the most basic thing I can think of down, I'd like to pan an arbitrarily positioned camera around. That means move it along its own Up and Side axes. Here's a picture of a randomly positioned camera looking at an object:

enter image description here

It should be clear what the Up (Green) and Side (Red) vectors on the camera are. Even though the picture shows otherwise, assume that the Model matrix is just the identity matrix. Here's what I do to try and get it to work:

Step 1: Create my View/Camera matrix (going to refer to it as the View matrix from now on) using glm::lookAt().

Step 2: Capture mouse X and Y positions.

Step 3: Create a translation matrix mapping changes in the X mouse position to the camera's Side vector, and mapping changes in the Y mouse position to the camera's Up vector. I get the Side vector from the first column of the View matrix. I get the Up vector from the second column of the View matrix.

Step 4: Apply the translation: viewMatrix = glm::translate(viewMatrix,translationVector);

But this doesn't work. I see that the mouse movement is mapped to some kind of perpendicular axes, but they're definitely not moving as you'd expect with respect to the camera. Could someone please explain what I'm doing wrong and point me in the right direction with this camera stuff?

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

Well how do you create the translationVector? How does the camera act?

The thing is that a World to Camera matrix is not a matrix that tells you the position and orientation of the Camera in world space. It's the inverse of that. You can't extract axii from it like you'd from a "regular" matrix. If you want to do this your way then try inversing the World to Camera matrix, then extracting the axii.

If you're concerned with performance, then you can either extract rows instead of columns (which is probably the fastest solution) or take the 3x3 rotation matrix and transpose it, then extract the vectors. The camera transformation is an affline transformation, so transposing the pure rotation matrix should give the same resaults as calculating the inverse (extracting rows instead of columns is effectively a transposition, it's just a shortcut).

share|improve this answer
    
I needed to invert the world to camera matrix as you mentioned. Thanks. –  Pris Jun 28 '12 at 16:13
add comment

Your Answer

 
discard

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.