'Scuse me if this has been covered already. I'm new here. I saw lots of stuff on the topic of quaternions, but I didn't see anything covering optimizing a quat. to mat. function. I'm too sleepy to keep looking right now, so I'll just post this.

I managed to optimize the function down to 13 multiplies. I was curious if it was possible to optimize it anymore than that. Here's my code:
void pquat_to_matrix(pvector4f qinp, GLfloat m[4][4])
{
pquat q;
float q00, q01, q11, q12, q22, q13, q03, q23, q20;
/* Since everything gets multiplied by 2, scale the quat
* by sqrt(2), that way when 2 values are multiplied,
* the end result is the same. */
pvector4f_copy(qinp,q);
pvector4f_scale(q,SQRT2,q);
/* Each mult. gets used twice, so just do one mult
* for each and store in a temp var */
q00 = q[0]*q[0]; q20 = q[2]*q[0];
q01 = q[0]*q[1]; q11 = q[1]*q[1];
q12 = q[1]*q[2]; q22 = q[2]*q[2];
q03 = q[0]*q[3]; q13 = q[1]*q[3]; q23 = q[2]*q[3];
/* Create the matrix */
m[0][0]=1-(q11+q22); m[1][0]= (q01-q23); m[2][0]= (q20+q13); m[3][0]=0;
m[0][1]= (q01+q23); m[1][1]=1-(q22+q00); m[2][1]= (q12-q03); m[3][1]=0;
m[0][2]= (q20-q13); m[1][2]= (q12+q03); m[2][2]=1-(q11+q00); m[3][2]=0;
m[0][3]=0 ; m[1][3]=0 ; m[2][3]=0 ; m[3][3]=1;
}
[edited by - jitspoe on May 25, 2002 6:22:03 AM]