Original Post
Here''s my code for matrix multiplications and 3D rotation, tell me if you see any areas for improevment or flat out errors. Thanks!
----
void MatMul(MATRIX* first, MATRIX* second, MATRIX* dest)
{
int c, r; // column and row counters
for( c = 0; c < 4; c++ )
{
for( r = 0; r < 4; r++ )
{
dest->data[c][r] = first->data[0][r] * second->data[c][0] +
first->data[1][r] * second->data[c][1] +
first->data[2][r] * second->data[c][2] +
first->data[3][r] * second->data[c][3];
}
}
}
///////////////
void VecRotate(VECTOR* srcVector, VECTOR* destVector, float x, float y, float z)
{
float xSin, xCos, ySin, yCos, zSin, zCos;
xSin = (float)sin(x * PI / 180);
xCos = (float)cos(x * PI / 180);
ySin = (float)sin(y * PI / 180);
yCos = (float)cos(y * PI / 180);
zSin = (float)sin(z * PI / 180);
zCos = (float)cos(z * PI / 180);
MATRIX xRotMat = { 1, 0, 0, 0,
0, xCos, xSin, 0,
0, -xSin, xCos, 0,
0, 0, 0, 1 };
MATRIX yRotMat = { yCos, 0, -ySin, 0,
0, 1, 0, 0,
ySin, 0, yCos, 0,
0, 0, 0, 1 };
MATRIX zRotMat = { zCos, zSin, 0, 0,
-zSin, zCos, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 };
MATRIX temp, final;
MatMul(&xRotMat, &yRotMat, &temp);
MatMul(&temp, &zRotMat, &final);
destVector->data[0] = final.data[0][0] * srcVector->data[0] + final.data[1][0] * srcVector->data[1] + final.data[2][0] * srcVector->data[2] + final.data[3][0];
destVector->data[1] = final.data[0][1] * srcVector->data[0] + final.data[1][1] * srcVector->data[1] + final.data[2][1] * srcVector->data[2] + final.data[3][1];
destVector->data[2] = final.data[0][2] * srcVector->data[0] + final.data[1][2] * srcVector->data[1] + final.data[2][2] * srcVector->data[2] + final.data[3][2];
}