Advertisement

matrix to glRotate data

Started by September 27, 2001 12:09 AM
7 comments, last by Nibbles 23 years, 4 months ago
Hi, I have my rotations calculated (i hope) in a 4x4 matrix, but now how to i use glRotatef(...) with it? thanks, Scott Email Website
"If you try and don''t succeed, destroy all evidence that you tried."
If you already have the matrix precalculated, you can use something like this:

glLoadIdentity();
glMultMatrix(m);

The multmatrix command will multiply the matrix currently in use by the specified matrix. Another thing to watch for, is that the matrix must be in column major order, like so:

if
m[16] = { 0, 1 , 2, 3, 4, 5, ... , 14, 15 };

then the matrix looks like:

[[0, 4, 8, 12],
[1, 5, 9, 13],
[2, 6, 10, 14],
[3, 7, 11, 15]]

That should help...
Advertisement
below is what I''m using so far, and by what you said, i''m going to have to change it. Would you agree?

  void Multiply_Matrix(float mat1[4][4], float mat2[4][4], float dest[4][4]){   int i,j;   for(i=0; i<4; i++)      for(j=0; j<4; j++)         dest[i][j]=mat1[i][0]*mat2[0][j]+mat1[i][1]*mat2[1][j]+mat1[i][2]*mat2[2][j]+mat1[i][3]*mat2[3][j];}void Rotate(float matrix[4][4], float ax, float ay, float az){	float xmat[4][4], ymat[4][4], zmat[4][4];	float mat1[4][4], mat2[4][4];	float XRotationMatrix[4][4] = {		( 1.0f,		0.0f,		0.0f,		0.0f ),		( 0.0f,		cosf(ax),	sinf(ax),	0.0f ),		( 0.0f,		sinf(ax),	cosf(ax),	0.0f ),		( 0.0f,		0.0f,		0.0f,		1.0f )	};	float YRotationMatrix[4][4] = {		( cosf(ay),	0.0f,	sinf(ay),	0.0f ),		( 0.0f,		1.0f,	0.0f,		0.0f ),		( sinf(ay),	0.0f,	cosf(ay),	0.0f ),		( 0.0f,		0.0f,	0.0f,		1.0f )	};	float ZRotationMatrix[4][4] = {		( cosf(az),	sinf(az),	0.0f,	0.0f ),		( sinf(az),	cosf(az),	0.0f,	0.0f ),		( 0.0f,		0.0f,		1.0f,	0.0f ),		( 0.0f,		0.0f,		0.0f,	1.0f )	};	Multiply_Matrix(matrix,ymat,mat1);	Multiply_Matrix(mat1,xmat,mat2);	Multiply_Matrix(mat2,zmat,matrix);}  


Thanks,
Scott

Email
Website

"If you try and don''t succeed, destroy all evidence that you tried."
I suck at math, so I''d probably do it the other way.

Do the rotations with glRotate, then read back the matrix if you need it.

- Pete
yeah, but then i''d have the gimbal lock problem wouldn''t I? that''s the main reason i''m using matrices.

Scott

Email
Website

"If you try and don''t succeed, destroy all evidence that you tried."
But in the code above, the matrix is being created using plain rotations - isn''t that still going to get gimbal lock?

- Pete
Advertisement
Hi Scott!!

In my eyes it look like you are trying to archive the same that you can get using glRotatef();
Nothing more...nothing less!! (exept for a few lines more code)

Take Care!

- -- ---[XaOs]--- -- -

[ project fy ]
- -- ---[XaOs]--- -- -[ Project fy ||| CyberTux ]
damn, which shows that i need help
I''d like to use matrices for rotations and translations instead of using both quaternions and matrices because the quaternion code i do have working for rotations is a mess, and i''d rather use matrices.

help would really be appreciated,
Thank,
Scott

Email
Website

"If you try and don''t succeed, destroy all evidence that you tried."
If you''ve got the matrix already, maybe you could just use glMultMatrix instead of figuring out the rotations? I don''t know how translations will affect this though :-(

- Pete

This topic is closed to new replies.

Advertisement