   Order of transformations
  Hey guys,I'm working with OpenGL now and I have decided to not allow myself to do things the trial-and-error way anymore. One thing I want to learn is multiplication order of matrices.So far, I've learned that:OpenGL uses column vectors/matrices and therefore vectors are transformed like this:v' = CBAv. Where v is the initial vector and A is the first transformation to be applied, B the second etc..What i don't understand is what "v is transformed by A and then by B" actually means. I want to visualize this and learn how it works, but every time I try, it seems to work in exactly the opposite way -_-.For example. Say we have a translation matrix A that only translates along the vertical axis and a rotation matrix B. Say we apply the translation first and then the rotation. This picture shows what would make sense to me, in this case.http://img705.imageshack.us/i/transforms.jpg/You translate, and then rotate about the origin. If we would increase the angle here, the square would orbit about the origin, because the rotation is the last transform to take place.I use the following line to compute the matrix and pass it to my shader:    Shader.setMat4('ModelViewProjectionMatrix', matMultiply( matRotateZ(20), matTranslate(0, 0.6, 0)) ); (Rightmost matrix should be applied first, according to what I learned).However, if i test it, the following will happen:http://img249.imageshack.us/i/transforms2.jpg/The result is a square that has a fixed position right above the origin. Increasing the angle will only let it rotate about it's local center.As you see, I have also made up a "transformation order" in the second picture. However, if I use that, I have the result I wanted in the first place. Apparently, I'm a bit out-of-sync here with what's really happening.What kind of mistake am I making here? How should I visualize this? Can this be caused by a bug in my matrix functions?I'd be very gratefull if someone enlightened me (some images would probably be very effective!)Thanks a bunch!   
  You can either look at the transformation order starting from the matrix closest to the vertex and moving away from it, or starting from the farthest matrix from the vertex and moving towards it.  For instance, withv' = CBAvYou can either look at 'A', then 'B', then 'C', or 'C', then 'B', then 'C'.  When you look at the starting from the closest one, each transformation in order is applied and forgotten.  In other words, they each happen in turn, in world space.  You first do 'Av' and get some result, call it v".  Then you're left with CBv".  As far as the other transformations are concerned, 'A' never existed.  'B' just sees v".  And once 'B' is applied, all 'C' is going to see is whatever is left.  So you apply a transformation, get the results, and that becomes the new vertex for the next transformation.If you start looking from the farthest transformation 'C', things always apply in local space.  You apply 'C', which does something do your object (like translate).  Then you apply 'B', which does something else.  If it's a rotate, then it will rotate around the local origin of the object, which will cause it to spin in place.  And if 'A' is a scale, it will scale relative to it's local origin.I hope that made some sense :);  Thanks for your explanation. It clears things up a bit.I think the first way of looking at it, is the commonly used one. Vectors are transformed by one matrix at a time. Rotations and scale-operations happen with respect to the world-origin.However, I did some tests and I still cant get it to work like that:    //Draw a green square: order => Scale => Rotate => Translate   //First we scale and rotate with respect to the world origin.//Then we translate upward. As a result, the square should be rotating about its own local center (position should be fixed).    Shader.setVec4f('Color', Vec4f(0, 1, 0, 1) );    Shader.setMat4('ModelViewProjectionMatrix', matMultiply( matMultiply(   matTranslate(0, 0.5, 0),   matRotateZ(angle)),   matScale(1.5) ) );    Mesh.Render();    //Draw a red square: order => Translate => Rotate => Scale    //We first translate, and then rotate and scale with respect to the world origin.//Because we translated first, the object    //should orbit about the world origin. Also, because of the scaling, it should lie a bit further away from the origin,//compared to the green square    Shader.setVec4f('Color', Vec4f(1, 0, 0, 1) );    Shader.setMat4('ModelViewProjectionMatrix', matMultiply( matMultiply( matScale(1.5), matRotateZ(angle) ), matTranslate(0, 0.5, 0) ) );    Mesh.Render();The comments describe my expectations, NOT what is actually happening (which is exactly the other way arround :( ).This is my multiplication code:function matMultiply(const aM1, aM2: TMatrix4): TMatrix4;begin  result[0,0]:=aM1[0,0]*aM2[0,0]+aM1[0,1]*aM2[1,0]+aM1[0,2]*aM2[2,0]+aM1[0,3]*aM2[3,0];  result[0,1]:=aM1[0,0]*aM2[0,1]+aM1[0,1]*aM2[1,1]+aM1[0,2]*aM2[2,1]+aM1[0,3]*aM2[3,1];  result[0,2]:=aM1[0,0]*aM2[0,2]+aM1[0,1]*aM2[1,2]+aM1[0,2]*aM2[2,2]+aM1[0,3]*aM2[3,2];  result[0,3]:=aM1[0,0]*aM2[0,3]+aM1[0,1]*aM2[1,3]+aM1[0,2]*aM2[2,3]+aM1[0,3]*aM2[3,3];  result[1,0]:=aM1[1,0]*aM2[0,0]+aM1[1,1]*aM2[1,0]+aM1[1,2]*aM2[2,0]+aM1[1,3]*aM2[3,0];  result[1,1]:=aM1[1,0]*aM2[0,1]+aM1[1,1]*aM2[1,1]+aM1[1,2]*aM2[2,1]+aM1[1,3]*aM2[3,1];  result[1,2]:=aM1[1,0]*aM2[0,2]+aM1[1,1]*aM2[1,2]+aM1[1,2]*aM2[2,2]+aM1[1,3]*aM2[3,2];  result[1,3]:=aM1[1,0]*aM2[0,3]+aM1[1,1]*aM2[1,3]+aM1[1,2]*aM2[2,3]+aM1[1,3]*aM2[3,3];  result[2,0]:=aM1[2,0]*aM2[0,0]+aM1[2,1]*aM2[1,0]+aM1[2,2]*aM2[2,0]+aM1[2,3]*aM2[3,0];  result[2,1]:=aM1[2,0]*aM2[0,1]+aM1[2,1]*aM2[1,1]+aM1[2,2]*aM2[2,1]+aM1[2,3]*aM2[3,1];  result[2,2]:=aM1[2,0]*aM2[0,2]+aM1[2,1]*aM2[1,2]+aM1[2,2]*aM2[2,2]+aM1[2,3]*aM2[3,2];  result[2,3]:=aM1[2,0]*aM2[0,3]+aM1[2,1]*aM2[1,3]+aM1[2,2]*aM2[2,3]+aM1[2,3]*aM2[3,3];  result[3,0]:=aM1[3,0]*aM2[0,0]+aM1[3,1]*aM2[1,0]+aM1[3,2]*aM2[2,0]+aM1[3,3]*aM2[3,0];  result[3,1]:=aM1[3,0]*aM2[0,1]+aM1[3,1]*aM2[1,1]+aM1[3,2]*aM2[2,1]+aM1[3,3]*aM2[3,1];  result[3,2]:=aM1[3,0]*aM2[0,2]+aM1[3,1]*aM2[1,2]+aM1[3,2]*aM2[2,2]+aM1[3,3]*aM2[3,2];  result[3,3]:=aM1[3,0]*aM2[0,3]+aM1[3,1]*aM2[1,3]+aM1[3,2]*aM2[2,3]+aM1[3,3]*aM2[3,3];end;(Result is the variable that is automaticly returned when the routine ends).I'm suspicious that it's this multiplication routine. I assume that pascal-multi-arrays are stored in row-major order (Just as in C++, me thinks), while OpenGL uses column major matrices.My guess is that the matrix parameters aM1 and aM2 are swapped. Is that correct?Thanks in advance!   