Earlier today I was translating some code from c++ to asm, and I encountered a few lines which seemed weird to me.
I'm not that great at math, and I'm not a c++ programmer.
If you can make sense of this, please reply (asm translation would be nice but I don't expect that).

Please assume that vDir is a CVector, and that we've just jumped through hoops to calculate vDir and fRotation..
``CMatrix m;m .MakeRotation (fRotation, 0, 0, 1);m .PreMultiply (vDir, vDir);SetPosition (CVector3 (vDir .m_x, vDir .m_z, vDir .m_y));``

OK lets see, on the first line, we're defining a local variable called m which is a throwaway matrix.. on the second line, we're filling it with a pure rotation matrix , rotating about Z axis by fRotation angle.. on the third line is stuff I just don't comprehend, and on the fourth and final line, we are handing the resulting vDir vector to a method called SetPosition.

Now, I didn't know what PreMultiply was so I looked into it.
Normally, it's used to concatenate two matrices m1 and m2 such that m1 receives the transformation of m2 'out of order', ie if m1 contained translation and scaling, they would only be applied AFTER the rotation.
In our case however, the arguments are vectors, so what the hell?
Could it be vDir = vDir * ('M * vDir) where 'M is the transposed matrix?
Or am I totally on the wrong track?
Posted on 2006-08-24 05:25:37 by Homer
Send over the Premultiply proc and the declaration of vDir (what's its type?)
Posted on 2006-08-24 07:01:44 by Ultrano
vDir, as I said, is a CVector, ie a Vec3.

I have not seen it or its fields, but we can deduce the struct:
``CVector structm_x real4 ?m_y real4 ?m_z real4 ?CVector ends``

Note the m_ prefix, as well as the C in CVector, both indicating that CVector is a Class, not a simple struct, but regardless, we can treat it as such.

CMatrix implements a 4x4 matrix like D3DXMATRIX does.
CMatrix::PreMultiply... umm, I don't have code for it, but I don't want to use it for its 'usual' use..
The PreMultiply method NORMALLY takes two matrices as parameters, but like many mathy classes, it supports overloaded operators so it can work with other datatypes.
NORMALLY, PreMultiply (m1,m2) would multiply matrices m1 and m2 in a special way, with the result overwriting m1.
By special way, I mean that the transformation(s) which input m2 represents will be implemented BEFORE the transformation(s) which m1 represents.. effectively, we are INSERTING a transformation into m1, so that m1=m2*m1 rather than m1=m1*m2, make sense?
Anyway, enough about PreMultiply in regards to Matrix inputs, in this case, it appears that the output is Vec3, one input is Matrix and the other input is the same Vec3 as the output.
This appears to be a simple case of multiplying a matrix and a vector,such that vDir=vDir*M and if thats true, why use PreMultiply to achieve it? That's what bothered me, and why I asked.
Another thing that occurs to me is that our Matrix m is a PURE ROTATION MATRIX, normally we use PreMultiply to 'protect our other transforms from the input transform", but since we don't HAVE any other transforms in m, again I have to ask, why use PreMultiply rather than Multiply? There must be a good reason, right?
In short, I can't provide the code for PreMultiply, although its simply a matter of multiplying the inputs deliberately in the wrong order to usual. What I don't understand is why this abortion of an operation is being applied in this particular case (which is related to calculating various kinds of Orbits if that helps at all).

Posted on 2006-08-24 08:54:55 by Homer
Don't search for "reason" in most C++ coders' code ;) . "hey, it worked - I'll leave it like this."
Although, this kind of reminds me of my own 3D engine for PalmOS, where the camera matrix comes first, and is inverted (iirc to save some thousand cycles on each new rendered object).
Posted on 2006-08-24 12:27:09 by Ultrano