TheIndicator 122 Report post Posted April 4, 2006 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. 0 Share this post Link to post Share on other sites
haegarr 7374 Report post Posted April 4, 2006 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. 0 Share this post Link to post Share on other sites
jpetrie 13199 Report post Posted April 4, 2006 Google is your friend. :DThe 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. 0 Share this post Link to post Share on other sites
TheIndicator 122 Report post Posted April 4, 2006 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. 0 Share this post Link to post Share on other sites
jyk 2094 Report post Posted April 4, 2006 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). 0 Share this post Link to post Share on other sites
TheIndicator 122 Report post Posted April 4, 2006 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. 0 Share this post Link to post Share on other sites
jyk 2094 Report post Posted April 4, 2006 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. 0 Share this post Link to post Share on other sites
smart_idiot 1298 Report post Posted April 5, 2006 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. 0 Share this post Link to post Share on other sites
jyk 2094 Report post Posted April 5, 2006 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. 0 Share this post Link to post Share on other sites