1
votes

Lets say we have:

Vector3 vec;//has some random values
Matrix4x4 mat;//has some random values as well

Now I want to multiply them to get a Vector3 out of it.

Vector3 mult = vec * mat;

Would this function fulfill that:

void Matrix4x4::Transform( Vector3 &oPoint )
{
    float fX = oPoint.GetX();
    float fY = oPoint.GetY();
    float fZ = oPoint.GetZ();

    oPoint.SetX( fX * this->matrix[0][0] + fY * this->matrix[0][1] + fZ * this->matrix[0][2] + this->matrix[0][3]);
    oPoint.SetY( fX * this->matrix[1][0] + fY * this->matrix[1][1] + fZ * this->matrix[1][2] + this->matrix[1][3]);
    oPoint.SetZ( fX * this->matrix[2][0] + fY * this->matrix[2][1] + fZ * this->matrix[2][2] + this->matrix[2][3]);
}

If not, can you lead me in the right direction.

1
You could certainly write code that uses it. But why is Matrix4x4 named with 4x4 if it's being treated as a 3x3 matrix? - Pete Becker
@PeteBecker: Maybe because it's for multiplication with a 3-vector. Mathematically this operation is not defined. You need a matrix of row rank 3 for this, i.e. a 3×n matrix. However we can trivially define that we all vectors and matrices we consider are of infinite rank and everything after the given number of elements is 0. In that case in a 3→4 multiplication the implicit 4th element was zero and match the code given by OP. - datenwolf
@datenwolf - yes, certainly. But that doesn't address whether it's appropriate to do so, or whether Jose intends to do that. - Pete Becker
I understand why you say its a 3x3 since the last operation I am doing is always zero. But all I want is that the vector I get out of here is correct and since you say it does then alright. Thanks for the reply guys. - Jose

1 Answers

1
votes

Your code seems ok. I assume that, as you are using OpenGL, your matrix is in column-major order, and that when you want to multiply vector of size 3 on 4x4 matrix, your fourth vector coord is 1.

But, as you are obviously doing computer graphics, you should use vectors of size 4 because of homogenous coordinates. Also I don't really understand, why you want to multiply vector on matrix, and not vice versa.