简体   繁体   中英

Vector3 Matrix Multiplication

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM