DarkFX

01-04-2004, 10:55 AM

I have 4x4 matrix (GL_MODELVIEW of the current object) ...

How to find inverse matrix?

(i want to get object space position of light)

How to find inverse matrix?

(i want to get object space position of light)

View Full Version : inverse matrix

DarkFX

01-04-2004, 10:55 AM

I have 4x4 matrix (GL_MODELVIEW of the current object) ...

How to find inverse matrix?

(i want to get object space position of light)

How to find inverse matrix?

(i want to get object space position of light)

AlexH

01-04-2004, 02:21 PM

This worked well for me, and is reasonably simple :

fmatrix4 &fmatrix4::invert(void)

{

fmatrix4 D;

int i, j, k;

D.identity();

for( i = 0; i < 4; i++ ) {

float tmp = 1.0f / data[i*4+i];

for( j = 3; j >= 0; j-- ) {

D.data[i*4+j] *= tmp;

data[i*4+j] *= tmp;

}

for( j = 0; j < 4; j++ ) if( j != i ) {

tmp = data[j*4+i];

for( k = 3; k >= 0; k-- ) {

D.data[j*4+k] -= D.data[i*4+k] * tmp;

data[j*4+k] -= data[i*4+k] * tmp;

}

}

}

*this = D;

return( *this );

}

Oh yes: and if your matrix is guaranteed to only encode a rotation in the upper 3x3 part, you could of course use a simple transpose. Optional translation can also be taken into account, without needing a full inversion. If you have scaling, shearing or even a projection (rather unlikely for the modelview matrix), then you need a full invert.

[This message has been edited by AlexH (edited 01-04-2004).]

fmatrix4 &fmatrix4::invert(void)

{

fmatrix4 D;

int i, j, k;

D.identity();

for( i = 0; i < 4; i++ ) {

float tmp = 1.0f / data[i*4+i];

for( j = 3; j >= 0; j-- ) {

D.data[i*4+j] *= tmp;

data[i*4+j] *= tmp;

}

for( j = 0; j < 4; j++ ) if( j != i ) {

tmp = data[j*4+i];

for( k = 3; k >= 0; k-- ) {

D.data[j*4+k] -= D.data[i*4+k] * tmp;

data[j*4+k] -= data[i*4+k] * tmp;

}

}

}

*this = D;

return( *this );

}

Oh yes: and if your matrix is guaranteed to only encode a rotation in the upper 3x3 part, you could of course use a simple transpose. Optional translation can also be taken into account, without needing a full inversion. If you have scaling, shearing or even a projection (rather unlikely for the modelview matrix), then you need a full invert.

[This message has been edited by AlexH (edited 01-04-2004).]

DarkFX

01-08-2004, 03:04 AM

Thanks! It work very well ...

Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.