简体   繁体   中英

How to Create an Inverse Transformation Matrix

I'm working on an OpenGL project in Java, and it has come to point where I'd like to create the transformation matrices in my own code, so i can use them to do world-to-screen point transformations, and vice versa. I've created a Matrix class with support for transformations, and that is all working quite nicely. However, I'm having trouble actually figuring out how to create an inverse transform.

So my question is this:

  • Given an arbitrary affine (4x4) transformation matrix, how do you create the inverse transformation matrix? Are some matrices uninvertible? What are the limitations and caveats of inverting a transformation matrix?

From my research, I've heard various methods of doing so, with the simplest being to transpose then negate the matrix. However, this doesn't seem to be actually working. I've heard that this method doesn't work on some matrices, and even that some matrices are uninvertible.

I'm looking for more than just a "plug in this equation" answer, because I'd actually like to understand what's going on when I invert a matrix. This also excludes "just use this library" answers. I might move to a matrix library in the future, but for now I'd like to create it myself.

Edit: Before anyone asks, this is NOT homework. This is a personal project.

Edit: Apparently there's a whole list of strategies for calculating inverse matrices here: http://en.wikipedia.org/wiki/Invertible_matrix

Here is some code that I used in my Computer Graphics course, basically I used the Gauss Jordan elimination for calculating the inverse of a matrix. For a matrix to be invertible its determinant value must be not equal to zero. I have not handled that case in my code though, I am not going to do it all for you.

Matrix4* Matrix4::FindInverse(Matrix4 &a){

int n = R;
int i = 0;
int j = 0;
float pivot = 0;
Matrix4* invA = NULL;
//TODO: Check whether the matrix is invertible.Else Return
invA = new Matrix4();
invA->SetMatrix4(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1);


for(i = 0; i < n; i++){
    pivot = a.v[i][i];

    if(pivot != 1.0 and pivot != 0){
        for(int t = i; t < n; t++){
            a.v[i][t] = a.v[i][t]/pivot;
            invA->v[i][t] = invA->v[i][t]/pivot;
        }
    }

    //Update to the new pivot which must be 1.0
    pivot = a.v[i][i];

    for(j = 0; j < n; j++){
        if( j==i ){
            continue;

        }
        else{
            float l = a.v[j][i]/pivot;
            for(int m = 0; m < n; m++){
                a.v[j][m] = a.v[j][m] - l * a.v[i][m];
                invA->v[j][m] = invA->v[j][m] - (l * invA->v[i][m]);
            }
        }
    }
}
return invA;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM