I am very new to matrix math. I have the following that sets the rotation in a 4x4 matrix. It is pretty ugly, so can anyone suggest how I could clean this up?
I would like to not have to call MultiplyTwoMatrixes
twice. A better way of copying the 3x3 matrix into the end 4x4 matrix would be nice as well.
void MultiplyTwoMatrixes(float aMatrix[9], float bMatrix[9], float product[9])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
product[j + i * 3] = 0;
for (int k = 0; k < 3; k++)
{
product[j + i * 3] += aMatrix[k + i * 3] * bMatrix[j + k * 3];
}
}
}
}
void setrotation(float x, float y, float z)
{
const float* oldMatrix = node->GetTransform();
float xRot[9] = { 1, 0, 0, 0, cos(x), sin(x), 0, -sin(x), cos(x) };
float yRot[9] = { cos(y), 0 ,-sin(y), 0, 1, 0, sin(y), 0, cos(y) };
float zRot[9] = { cos(z), sin(z), 0, -sin(z), cos(z), 0, 0, 0, 1 };
float xyResult[9];
float xyzResult[9];
float newMatrix[16];
MultiplyTwoMatrixes(xRot, yRot, xyResult);
MultiplyTwoMatrixes(xyResult, zRot, xyzResult);
newMatrix[0] = xyzResult[0]; newMatrix[1] = xyzResult[1]; newMatrix[2] = xyzResult[2];
newMatrix[4] = xyzResult[3]; newMatrix[5] = xyzResult[4]; newMatrix[6] = xyzResult[5];
newMatrix[8] = xyzResult[6]; newMatrix[9] = xyzResult[7]; newMatrix[10] = xyzResult[8];
newMatrix[12] = oldMatrix[12]; newMatrix[13] = oldMatrix[13]; newMatrix[14] = oldMatrix[14];
node->SetTransform(newMatrix);
}