PDA

View Full Version : Matrix multiply order



3DPrgmer
12-31-2016, 03:49 AM
Hi,

i just wonder how i need to multiply my matrices.

My model can have a parent and that parent can have an other parent,....



QMatrix4x4 MshFile::getParentMatrix(std::string parent) const
{
QMatrix4x4 matrix;

for (auto& it : *m_models)
{
if (!strcmp(parent.c_str(), it->name.c_str()))
{
matrix = it->m4x4Translation * getParentMatrix(it->parent);
break;
}
}

return matrix;
}


Do i multiply the correct way in that algorithm?

GClements
12-31-2016, 06:26 AM
i just wonder how i need to multiply my matrices.

It depends upon whether you're multiplying a row vector on the left or a column vector on the right.

For a column-vector on the right (OpenGL convention), M1*(M2*(M3*v)) = (M1*M2*M3)*v, i.e. the left-most matrix is at the root node while the right-most is at the leaf node.

For a row-vector on the left (DirectX convention), it's the other way around: ((v*M3)*M2)*M1 = v*(M3*M2*M1).

Note that GLSL supports both options. M*v interprets v as a column vector, v*M interprets it as a row vector. The former is more typical, in which case you'd normally multiply with the current matrix on the left and an incremental transformation on the right as you descend the transformation hierarchy, or the other way around if you're ascending.

BBeck1
01-01-2017, 06:07 AM
I'm not sure I fully understand the question you are asking, but in order to have the parent-child relationship you have to multiply the parent's matrix times the child's matrix and the result is the matrix you actually use for the child. You use the parent's matrix directly, assuming it is not a child itself. If you have grand-children you have to multiply the whole family tree branch to get the matrix that you will use for the child. This is what actually links them together.

Reversing the order of multiplication changes whether things are rotating around the local axis of the global axis. Or you might say that it changes whether it revolves around it's own axis or the axis of the parent, with the top most parent's "parent" being the world origin.