Sign in to follow this  
TheIndicator

Inverting a 4x4 Matrix

Recommended Posts

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 this post


Link to post
Share on other sites
Google is your friend. :D

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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by TheIndicator
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.
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by TheIndicator
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.
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by smart_idiot
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.
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this