0

I´m trying to create a view matrix for my program to be able to move and rotate the camera in OpenGL.

I have a camera struct that has the position and rotation vectors in it. From what I understood, to create the view matrix, you need to multiply the transform matrix with the rotation matrix to get the expected result.

So far I tried creating matrices for rotation and for transformation and multiply them like this:

> Transformation Matrix T =
1    0    0    -x
0    1    0    -y
0    0    1    -z
0    0    0    1

> Rotation Matrix Rx =
1    0         0          0
0    cos(-x)   -sin(-x)   0
0    sin(-x)   cos(-x)    0
0    0         0          1

> Rotation Matrix Ry =
cos(-y)    0    sin(-y)     0
0          1    0           0
-sin(-y)   0    cos(-y)     0
0          0    0           1

> Rotation Matrix Rz =
cos(-z)   -sin(-z) 0    0
sin(-z)   cos(-z)  0    0
0         0        1    0
0         0        0    1

View matrix = Rz * Ry * Rx * T

Notice that the values are negated, because if we want to move the camera to one side, the entire world is moving to the opposite side.

This solution seems to almost be working. The problem that I have is that when the camera is not at 0, 0, 0, if I rotate the camera, the position is changed. What I think is that if the camera is positioned at, let´s say, 0, 0, -20 and I rotate the camera, the position should remain at 0, 0, -20 right?

I feel like I´m missing something but I can´t seem to know what. Any help?

Edit 1: It´s an assignment for university, so I can´t use any built-in functions!

Edit 2: I tried changing the order of the operations and putting the translation in the left side, so T * Rz * Ry * Rx, but then the models rotate around themselves, and not around the camera.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
magmyr01
  • 13
  • 4
  • I can never remember matrix multiplication order, so I just experiment until it works. Note that view matrix order is the *opposite* of model matrix order since the view matrix is the inverse of the camera's model matrix. Also make sure not to get row-major/column-major mixed up. – user253751 Apr 13 '22 at 10:29
  • Thanks for your answer. Unfortunately, I already tried changing the order and then the models rotate around themselves and not around the camera, which is not the outcome I was expecting. – magmyr01 Apr 13 '22 at 10:38
  • models rotating around themselves would seem to suggest the model matrix is also mixed up – user253751 Apr 13 '22 at 10:41
  • FYI: [How do I rotate my camera correctly in my 3D world?](https://stackoverflow.com/a/60576857/7478597) – Scheff's Cat Apr 13 '22 at 11:02
  • but the order of the multiplication matters, so R * T is not the same as T * R. I shouldn't get the same result – magmyr01 Apr 13 '22 at 12:29

0 Answers0