PDA

View Full Version : 4x4 Matrix multiplication doing odd things



Mitchell M
01-02-2014, 01:14 PM
Hello hopefully I posted this in the right sub-forum. I am trying to create a simple simulator and because I need to rotate and translate things along their local axises and when I looked around it looked like most people used matrices to do that sort of thing. Because glMultMatrix takes in 1-d arrays and I didn't really want to switch between types I stored all my matrices in float arrays. Where I am getting stuck is when I try to multiply a rotation matrix like



matrixFloat[0] = 1; matrixFloat[4] = 0; matrixFloat[8] = 0; matrixFloat[12] = 0;
matrixFloat[1] = 0; matrixFloat[5] = cos(angleX); matrixFloat[9] = -sin(angleX); matrixFloat[13] = 0;
matrixFloat[2] = 0; matrixFloat[6] = sin(angleX); matrixFloat[10] = cos(angleX); matrixFloat[14] = 0;
matrixFloat[3] = 0; matrixFloat[7] = 0; matrixFloat[11] = 0; matrixFloat[15] = 1;


by my main ship matrix it causes weird things to happen like the model gets skewed, becomes smaller, and moves away from the camera. The problem I think lies in the multiplication system I am using



matrixFloat[0] = a[0] * b[0] + a[4] * b[1] + a[8] * b[2] + a[12] * b[3];
matrixFloat[1] = a[1] * b[0] + a[5] * b[1] + a[9] * b[2] + a[13] * b[3];
matrixFloat[2] = a[2] * b[0] + a[6] * b[1] + a[10] * b[2] + a[14] * b[3];
matrixFloat[3] = a[3] * b[0] + a[7] * b[1] + a[11] * b[2] + a[15] * b[3];
matrixFloat[4] = a[0] * b[4] + a[4] * b[5] + a[8] * b[6] + a[12] * b[7];
matrixFloat[5] = a[1] * b[4] + a[5] * b[5] + a[9] * b[6] + a[13] * b[7];
matrixFloat[6] = a[2] * b[4] + a[6] * b[5] + a[10] * b[6] + a[14] * b[7];
matrixFloat[7] = a[3] * b[4] + a[7] * b[5] + a[11] * b[6] + a[15] * b[7];
matrixFloat[8] = a[0] * b[8] + a[4] * b[9] + a[8] * b[10] + a[12] * b[11];
matrixFloat[9] = a[1] * b[8] + a[5] * b[9] + a[9] * b[10] + a[13] * b[11];
matrixFloat[10] = a[2] * b[8] + a[6] * b[9] + a[10] * b[10] + a[14] * b[11];
matrixFloat[11] = a[3] * b[8] + a[7] * b[9] + a[11] * b[10] + a[15] * b[11];
matrixFloat[12] = a[0] * b[12] + a[4] * b[13] + a[8] * b[14] + a[12] * b[15];
matrixFloat[13] = a[1] * b[12] + a[5] * b[13] + a[9] * b[14] + a[13] * b[15];
matrixFloat[14] = a[2] * b[12] + a[6] * b[13] + a[10] * b[14] + a[14] * b[15];
matrixFloat[15] = a[3] * b[12] + a[7] * b[13] + a[11] * b[14] + a[15] * b[15];


maybe that only works on major-column matrices? Any help would be really appreciated.

Thanks
Mitchell

tonyo_au
01-02-2014, 05:48 PM
Have a look the glm library.

Mitchell M
01-03-2014, 10:43 AM
OK I'll take a look into it thanks for your help.

Mitchell M
01-03-2014, 03:38 PM
Ok I looked into the function mesa3d uses for glMultMatrix in order to fix the problem and now everything works fine