Public Group

# Inverting a 4x4 Matrix

This topic is 4492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How do I go about inverting a 4x4 matrix using C++ with Opengl? I'm currently using an 16 size array for storing the matrix.

##### Share on other sites
AFAIK OpenGL lets matrix inversion be a task of your own code.

How to invert the matrix yourself may depend on what the matrix is feasible for: A general inversion could be done using sub-matrices and determinants. If the matrix is restricted to the affine subset of rotation and translation then the inversion could be done with much less effort by transposing the linear sub-matrix to yield in the inverse rotation, negating the translation, and multiplying the linear sub-matrix with the negated translation to yield the inverse translation.

You could also take into account that the matrix is homogeneous (I assume your matrix _is_ homogeneous) to lower the effort to spend.

##### Share on other sites

The theory is the same regardless of the storage of the matrix, the language you write in, or the API you use. Check out any one of those links or try your own Google search for "matrix inverse" or "matrix inversion." You will have to perform the operation manually; OpenGL doesn't expose the functionality.

##### Share on other sites
The problem for me is that I have a camera, and I need to work at the points of the camera which is different from all of the other geometry and so I need to get the matrix (ModelView?) that has translated the camera points so that all of my geometry is the same. I believe that the matrix I get returned needs to be inverted so that I can multiply the camera points by it so that then I can do proper functions with the points that I was looking for.

##### Share on other sites
Quote:
 Original post by TheIndicatorThe problem for me is that I have a camera, and I need to work at the points of the camera which is different from all of the other geometry and so I need to get the matrix (ModelView?) that has translated the camera points so that all of my geometry is the same. I believe that the matrix I get returned needs to be inverted so that I can multiply the camera points by it so that then I can do proper functions with the points that I was looking for.
Didn't follow all that, but if you query the view matrix from OpenGL, it's already inverted; you don't need to do it yourself. Keep in mind though that reading data back from OpenGL is generally not recommended, and that you will have to read it back at a certain point if you want the view matrix only (since OpenGL does not store the view and model matrices separately). All in all, it's better to do it yourself. As has been mentioned though, if it's just a camera inverse transform you need, it can probably be computed much more easily than in the general case (see haegarr's post for details).

##### Share on other sites
Quote:
 Original post by jykDidn't follow all that, but if you query the view matrix from OpenGL, it's already inverted; you don't need to do it yourself. Keep in mind though that reading data back from OpenGL is generally not recommended, and that you will have to read it back at a certain point if you want the view matrix only (since OpenGL does not store the view and model matrices separately). All in all, it's better to do it yourself. As has been mentioned though, if it's just a camera inverse transform you need, it can probably be computed much more easily than in the general case (see haegarr's post for details).

Bascially I am trying to do collision dectection on my object and my camera (using gluLookAt), and I need to change the camera coordinates to world space coordinates to do the collision dectection.

##### Share on other sites
Quote:
 Original post by TheIndicatorBascially I am trying to do collision dectection on my object and my camera (using gluLookAt), and I need to change the camera coordinates to world space coordinates to do the collision dectection.
You don't need matrix inversion for this. If you set up the camera matrix yourself (which presumably you did), then you already have the camera's position and orientation (if it's relevant) in world space.

##### Share on other sites
Even if he did need the matrix, he wouldn't need to invert it, the position vector should be on the right side or at the bottom of the matrix. In the case of OpenGL, it's the right.

. . . X
. . . Y
. . . Z
. . . .

You might need to negate it, though.

##### Share on other sites
Quote:
 Original post by smart_idiotEven if he did need the matrix, he wouldn't need to invert it, the position vector should be on the right side or at the bottom of the matrix. In the case of OpenGL, it's the right.. . . X. . . Y. . . Z. . . .You might need to negate it, though.
It's not particularly relevant (the OP should have the position of the camera stored separately anyway), but it might be worth clarifying that if it's the view matrix that is queried from OpenGL, extracting the position isn't quite as simple as described above; there are some additional computations that need to be performed as well.

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 10
• 14
• 30
• 13
• 11
• ### Forum Statistics

• Total Topics
631788
• Total Posts
3002356
×