Archived

This topic is now archived and is closed to further replies.

Lyve

Help please: Matrix issues............. :((((

Recommended Posts

Hello people, I'm trying some matrix things at the moment and I just don't get it whats wrong with my code. A simplified example in pseudo-code:
    

// store previous matrix

float mOld[16];
glGetFloatv( GL_MODELVIEW_MATRIX, mOld );

// modify OpenGL matrix in some way...

glMultMatrixf( m_myOwnMatrix );

// get the new matrix

float mNew[16];
glGetFloatv( GL_MODELVIEW_MATRIX, mNew );

// Now let's see if we can extract "m_myOwnMatrix" from the new OpenGL matrix for testing


mOld.Invert();

// remove previous matrix from new matrix

mNew *= mOld;

ASSERT( mNew == m_myOwnMatrix );

    
Note, this is no real C code. I'm using my own matrix class that is able to handle multiplication and the .Invert() thing. The last assert fails and I don't really know why. What am I doing wrong? _____________________________________ Visit http://www.nilsschneider.de for finest trance music, studio, bio, guestbook and more! [edited by - Lyve on March 16, 2003 3:21:26 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Lyve
Note, this is no real C code. I''m using my own matrix class that is able to handle multiplication and the .Invert() thing.

The last assert fails and I don''t really know why. What am I doing wrong?



Why don''t you post the code for your invert() and mult() methods? In my own experiences, It''s really easy to swap a var and not notice in matrix processing code. The thing I would do is test those first, just step through in the debugger with some simple matricies and make sure it''s doing what it''s supposed to.

- sighuh?

Share this post


Link to post
Share on other sites
Thanks for your answers. Here''s the invert code:


  
void CMatrix::Invert()
{
// SWAP( matrix[ 0], matrix[ 0], float );

SWAP( matrix[ 1], matrix[ 4], float );
SWAP( matrix[ 2], matrix[ 8], float );
SWAP( matrix[ 3], matrix[12], float );
// SWAP( matrix[ 4], matrix[ 1], float );

// SWAP( matrix[ 5], matrix[ 5], float );

SWAP( matrix[ 6], matrix[ 9], float );
SWAP( matrix[ 7], matrix[13], float );
// SWAP( matrix[ 8], matrix[ 2], float );

// SWAP( matrix[ 9], matrix[ 6], float );

// SWAP( matrix[10], matrix[10], float );

SWAP( matrix[11], matrix[14], float );
// SWAP( matrix[12], matrix[ 3], float );

// SWAP( matrix[13], matrix[ 7], float );

// SWAP( matrix[14], matrix[11], float );

// SWAP( matrix[15], matrix[15], float );

}


Well, the swap macro is okay, believe me, used VEERRRYYY often!

Share this post


Link to post
Share on other sites
Nah, forgot the mult code:


  
CMatrix CMatrix::Mult( CMatrix& _m1, CMatrix& _m2 )
{
CMatrix m;

m.matrix[ 0] = _m1.matrix[ 0] * _m2.matrix[ 0] + _m1.matrix[ 1] * _m2.matrix[ 4] + _m1.matrix[ 2] * _m2.matrix[ 8] + _m1.matrix[ 3] * _m2.matrix[12];
m.matrix[ 1] = _m1.matrix[ 0] * _m2.matrix[ 1] + _m1.matrix[ 1] * _m2.matrix[ 5] + _m1.matrix[ 2] * _m2.matrix[ 9] + _m1.matrix[ 3] * _m2.matrix[13];
m.matrix[ 2] = _m1.matrix[ 0] * _m2.matrix[ 2] + _m1.matrix[ 1] * _m2.matrix[ 6] + _m1.matrix[ 2] * _m2.matrix[10] + _m1.matrix[ 3] * _m2.matrix[14];
m.matrix[ 3] = _m1.matrix[ 0] * _m2.matrix[ 3] + _m1.matrix[ 1] * _m2.matrix[ 7] + _m1.matrix[ 2] * _m2.matrix[11] + _m1.matrix[ 3] * _m2.matrix[15];

m.matrix[ 4] = _m1.matrix[ 4] * _m2.matrix[ 0] + _m1.matrix[ 5] * _m2.matrix[ 4] + _m1.matrix[ 6] * _m2.matrix[ 8] + _m1.matrix[ 7] * _m2.matrix[12];
m.matrix[ 5] = _m1.matrix[ 4] * _m2.matrix[ 1] + _m1.matrix[ 5] * _m2.matrix[ 5] + _m1.matrix[ 6] * _m2.matrix[ 9] + _m1.matrix[ 7] * _m2.matrix[13];
m.matrix[ 6] = _m1.matrix[ 4] * _m2.matrix[ 2] + _m1.matrix[ 5] * _m2.matrix[ 6] + _m1.matrix[ 6] * _m2.matrix[10] + _m1.matrix[ 7] * _m2.matrix[14];
m.matrix[ 7] = _m1.matrix[ 4] * _m2.matrix[ 3] + _m1.matrix[ 5] * _m2.matrix[ 7] + _m1.matrix[ 6] * _m2.matrix[11] + _m1.matrix[ 7] * _m2.matrix[15];

m.matrix[ 8] = _m1.matrix[ 8] * _m2.matrix[ 0] + _m1.matrix[ 9] * _m2.matrix[ 4] + _m1.matrix[10] * _m2.matrix[ 8] + _m1.matrix[11] * _m2.matrix[12];
m.matrix[ 9] = _m1.matrix[ 8] * _m2.matrix[ 1] + _m1.matrix[ 9] * _m2.matrix[ 5] + _m1.matrix[10] * _m2.matrix[ 9] + _m1.matrix[11] * _m2.matrix[13];
m.matrix[10] = _m1.matrix[ 8] * _m2.matrix[ 2] + _m1.matrix[ 9] * _m2.matrix[ 6] + _m1.matrix[10] * _m2.matrix[10] + _m1.matrix[11] * _m2.matrix[14];
m.matrix[11] = _m1.matrix[ 8] * _m2.matrix[ 3] + _m1.matrix[ 9] * _m2.matrix[ 7] + _m1.matrix[10] * _m2.matrix[11] + _m1.matrix[11] * _m2.matrix[15];

m.matrix[12] = _m1.matrix[12] * _m2.matrix[ 0] + _m1.matrix[13] * _m2.matrix[ 4] + _m1.matrix[14] * _m2.matrix[ 8] + _m1.matrix[15] * _m2.matrix[12];
m.matrix[13] = _m1.matrix[12] * _m2.matrix[ 1] + _m1.matrix[13] * _m2.matrix[ 5] + _m1.matrix[14] * _m2.matrix[ 9] + _m1.matrix[15] * _m2.matrix[13];
m.matrix[14] = _m1.matrix[12] * _m2.matrix[ 2] + _m1.matrix[13] * _m2.matrix[ 6] + _m1.matrix[14] * _m2.matrix[10] + _m1.matrix[15] * _m2.matrix[14];
m.matrix[15] = _m1.matrix[12] * _m2.matrix[ 3] + _m1.matrix[13] * _m2.matrix[ 7] + _m1.matrix[14] * _m2.matrix[11] + _m1.matrix[15] * _m2.matrix[15];

return m;
}



_____________________________________
Visit http://www.nilsschneider.de for finest trance music, studio, bio, guestbook and more!

Share this post


Link to post
Share on other sites
Are you sure that ''=='' will work correctly? I don''t remember how c++ implements it for arrays but I''m willing to bet that it will only be true if the two arrays are the same thing in memory (not if they have the same data)

Also I''m almost positve you''re going to get precision errors trying that.

In fact, a better programming practice is to not even compare floating points directly because they can be inconsistent. Instead of say, "(p == q)", you should use "(p - q < .0001)". There are lots of resources on the net that explain how floats are evil.

Share this post


Link to post
Share on other sites
The assert() was only pseudo code and should only make clear that the two matrices arent the same now. The two matrices differ more than only some little floating point values.


_____________________________________
Visit http://www.nilsschneider.de for finest trance music, studio, bio, guestbook and more!

Share this post


Link to post
Share on other sites
hey again,

your "invert" procedure is actually computing the transpose. For some matrices, this is the same thing (matrices whose transpose is equal to the inverse are called ''orthogonal''). for example, rotation matrices are orthogonal, so this will work on them. However, its not true for all matrices.

Share this post


Link to post
Share on other sites
quote:
Original post by andy_fish
hey again,

your "invert" procedure is actually computing the transpose. For some matrices, this is the same thing (matrices whose transpose is equal to the inverse are called ''orthogonal''). for example, rotation matrices are orthogonal, so this will work on them. However, its not true for all matrices.


This might be my problem :-(. Is there some code for that anywhere or a guide how to invert an opengl matrix?

Lyve


_____________________________________
Visit http://www.nilsschneider.de for finest trance music, studio, bio, guestbook and more!

Share this post


Link to post
Share on other sites
As said before, the assert was only pseudo-code, the assert is NOT what I''m doing in my real code, I only wanted to make clear that the two matrices are not the same!


_____________________________________
Visit http://www.nilsschneider.de for finest trance music, studio, bio, guestbook and more!

Share this post


Link to post
Share on other sites
yes. the inverse you are using there is the transpose.
and don''t forget that floating point numbers arn''t infinite precision. so while you might get very similar answers, they almost certainly won''t be the same.

| - Project-X - my mega project.. getting warmer - | - adDeath - an ad blocker I made - | - email me - |

Share this post


Link to post
Share on other sites