vectormatrix-multiplicationsharpdxprojection-matrix

Sharpdx Matrix4 * Vec4 vs Transformcoordinate


In trying to write a 3d software renderer from scratch i'm in the process of implementing matrices and specifically the projection matrix which has caused me no small amount of confusion.

I've followed the instructions here (https://mathinsight.org/matrix_vector_multiplication) on how to multiply two matrices together and a matrix and vector together, here is my code:


Matrix4x4 Matrix4x4::operator*(Matrix4x4& m2) {
    Matrix4x4 r;
    // row 1
    r.m00 = (this->m00 * m2.m00) + (this->m01 * m2.m10) + (this->m02 * m2.m20) + (this->m03 * m2.m30);
    r.m01 = (this->m00 * m2.m01) + (this->m01 * m2.m11) + (this->m02 * m2.m21) + (this->m03 * m2.m31);
    r.m02 = (this->m00 * m2.m02) + (this->m01 * m2.m12) + (this->m02 * m2.m22) + (this->m03 * m2.m32);
    r.m03 = (this->m00 * m2.m03) + (this->m01 * m2.m13) + (this->m02 * m2.m23) + (this->m03 * m2.m33);
    // row 2
    r.m10 = (this->m10 * m2.m00) + (this->m11 * m2.m10) + (this->m12 * m2.m20) + (this->m13 * m2.m30);
    r.m11 = (this->m10 * m2.m01) + (this->m11 * m2.m11) + (this->m12 * m2.m21) + (this->m13 * m2.m31);
    r.m12 = (this->m10 * m2.m02) + (this->m11 * m2.m12) + (this->m12 * m2.m22) + (this->m13 * m2.m32);
    r.m13 = (this->m10 * m2.m03) + (this->m11 * m2.m13) + (this->m12 * m2.m23) + (this->m13 * m2.m33);
    // row 3
    r.m20 = (this->m20 * m2.m00) + (this->m21 * m2.m10) + (this->m22 * m2.m20) + (this->m23 * m2.m30);
    r.m21 = (this->m20 * m2.m01) + (this->m21 * m2.m11) + (this->m22 * m2.m21) + (this->m23 * m2.m31);
    r.m22 = (this->m20 * m2.m02) + (this->m21 * m2.m12) + (this->m22 * m2.m22) + (this->m23 * m2.m32);
    r.m23 = (this->m20 * m2.m03) + (this->m21 * m2.m13) + (this->m22 * m2.m23) + (this->m23 * m2.m33);
    // row 4
    r.m30 = (this->m30 * m2.m00) + (this->m31 * m2.m10) + (this->m32 * m2.m20) + (this->m33 * m2.m30);
    r.m31 = (this->m30 * m2.m01) + (this->m31 * m2.m11) + (this->m32 * m2.m21) + (this->m33 * m2.m31);
    r.m32 = (this->m30 * m2.m02) + (this->m31 * m2.m12) + (this->m32 * m2.m22) + (this->m33 * m2.m32);
    r.m33 = (this->m30 * m2.m03) + (this->m31 * m2.m13) + (this->m32 * m2.m23) + (this->m33 * m2.m33);

    return r;
}

Vector4 Matrix4x4::operator*(Vector4& v) {
    Vector4 r(
        this->m00 * v.x + this->m01 * v.y + this->m02 * v.z + this->m03 * v.w,
        this->m10 * v.x + this->m11 * v.y + this->m12 * v.z + this->m13 * v.w,
        this->m20 * v.x + this->m21 * v.y + this->m22 * v.z + this->m23 * v.w,
        this->m30 * v.x + this->m31 * v.y + this->m32 * v.z + this->m33 * v.w);
    return r;
}

It works and i am able to translate, rotate and scale my objects around in 2d, but i run into my problem when i try to implement a projection and camera matrix. I've copied the projection and camera matrix from SharpDx since its math library uses row major same as me, it's matrix multiplication is identical to mine, but this is my confusion, SharpDx only has a: TransformCoordinate (https://github.com/sharpdx/SharpDX/blob/master/Source/SharpDX.Mathematics/Vector3.cs#L1388) for Vector3 and a: Transform (https://github.com/sharpdx/SharpDX/blob/master/Source/SharpDX.Mathematics/Vector4.cs#L1125) for Vector4 that performs matrix*vector operation, and they both seem to be in column major ?

So i've written this code to rotate a cube of 8 points in 3d:

        Vector4 cubeAr[8];
        cubeAr[0].x = -1; cubeAr[0].y = 1; cubeAr[0].z = 1; cubeAr[0].w = 1;
        cubeAr[1].x = 1; cubeAr[1].y = 1; cubeAr[1].z = 1; cubeAr[1].w = 1;
        cubeAr[2].x = -1; cubeAr[2].y = -1; cubeAr[2].z = 1; cubeAr[2].w = 1;
        cubeAr[3].x = -1; cubeAr[3].y = -1; cubeAr[3].z = -1; cubeAr[3].w = 1;
        cubeAr[4].x = -1; cubeAr[4].y = 1; cubeAr[4].z = -1; cubeAr[4].w = 1;
        cubeAr[5].x = 1; cubeAr[5].y = 1; cubeAr[5].z = -1; cubeAr[5].w = 1;
        cubeAr[6].x = 1; cubeAr[6].y = -1; cubeAr[6].z = 1; cubeAr[6].w = 1;
        cubeAr[7].x = 1; cubeAr[7].y = -1; cubeAr[7].z = -1; cubeAr[7].w = 1;

        Vector3 cameraPos(0, 0, 10);
        Vector3 cameraTarget(0, 0, 0);



        Matrix4x4 mT = Matrix4x4::GetTranslation(0, 0, 0);
        Matrix4x4 mR = Matrix4x4::GetRotationY(radians/2);
        Matrix4x4 mS = Matrix4x4::GetScale(1, 1, 1);
        Matrix4x4 mTRS = mT * mR * mS;
        Matrix4x4 viewMatrix = Matrix4x4::GetLookAtLH(cameraPos, cameraTarget, Vector3(0, 1, 0));
        Matrix4x4 projectionMatrix = Matrix4x4::GetPerspectiveFovRH(0.78f, (float)win.GetWidth() / win.GetHeight(), 0.01f, 1.0f);

        Matrix4x4 mMVP = mTRS * viewMatrix * projectionMatrix;
        Vector4 tmpV, tmpV2;
        for (int i = 0; i < 8; i++) {
            mMVP.Transpose();
            tmpV = mMVP * cubeAr[i];
            mMVP.Transpose();
            tmpV.w = 1.0 / tmpV.w;
            tmpV.x = tmpV.x * tmpV.w;
            tmpV.y = tmpV.y * tmpV.w;
            tmpV.z = tmpV.z * tmpV.w;

            float x = tmpV.x * (float)win.GetWidth() + (float)win.GetWidth() / 2.0f;
            float y = -tmpV.y * (float)win.GetHeight() + (float)win.GetHeight() / 2.0f;
            bool done = true;
            LR.DrawPixel(x, y, 0, 255, 0);
        }

It works successfully but i must transpose my modelViewProjection matrix before multiplying it with my vector.

I could write a TransformCoordinate or Transform function similar to SharpDx and avoid having to do the transpose, but i want to understand whats going on, why would SharpDx be written like this?

I followed these tutorials: https://mathinsight.org/matrix_vector_multiplication https://stunlock.gg/posts/linear_algebra/#matrices/thebasicrulesofmatrices/multiplyingmatrices(dotproduct)

I Expected to be able to just multiply my matrix with my vectors but every other example i could find of the perspective matrix and camera lookat function does it the other way like in SharpDx, where you use a transform function in the vector class that is column major?


Solution

  • .. Transform for Vector4 that performs matrix*vector operation, and they both seem to be in column major ?

    Yes, they seem to be column-major matrix*vector operations, but they actually are row-major vector-matrix operations .. which work the same way so it's easy to confuse them, but they're different in intent.

    In the D3D world, it's conventional to compose transforms by multiplying the first transform on the left by the last transform on the right. Which you might argue is the obvious way to do it, and that is how you are composing your transforms, but it also means that the vertex to be transformed has to go to the left of the transformation matrix: v * mMVP

    SharpDX works with that convention, so it has a function that implements that kind of transform, it is not a coincidence or weird quirk that its parameters have the vertex first and the matrix second, it's meant to be doing v * mMVP.

    With mMVP * v you get this:

    mMVP * v =
    (mTRS * viewMatrix * projectionMatrix) * v ==
    mTRS * (viewMatrix * (projectionMatrix * v))
    

    IE, project first, then apply the view transformation, then TRS, and that doesn't make sense, but it can be fixed by transposing mMVP because AB = (BTAT)T and vectors transpose implicitly.

    The OpenGL world uses the opposite convention, then mMVP * v is the conventional way to transform a vector but mMVP would have been created by composing transforms with the first transform on the right and the last transform on the left. Either way, the vector goes on the same side as the first transformation, that might be thought of as the "input side" of the transformation.