簡體   English   中英

矩陣類乘法產生錯誤的結果

[英]Matrix class multiplication yielding incorrect results

我為作業創建了一個基本的數學庫,其中涉及Matrix3和Matrix4類以及其他一些類。 對於OpenGL,為了將多維數據集投影到屏幕上,我為Projection * View * Model算法創建了lookAt函數和Perspective函數,該算法可為場景生成適當的視圖。 它們被設計為替代GLM庫/其他庫中的相應功能,因為將來它們必須與我的Matrix類交互。

lookAt函數產生正確的結果,而Projection函數產生接近的結果,並進行一些臨時的手動調整以使其提供相同的結果。 但是,當我將它們與Projection * View(外觀)* Model(身份矩陣)相乘時,我的庫產生的結果與GLM的結果(MVP Matrix4)完全不同。 我的與圖形計算器匹配,而GLM沒有。 GLM實際上在屏幕上正確顯示了多維數據集,而我的那個看起來好像多維數據集已爆裂。 矩陣本身幾乎沒有相似性。

GLM庫

        // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
    glm::mat4 ProjectionGLM = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
    // Camera matrix
    glm::mat4 ViewGLM = glm::lookAt(
        glm::vec3(4, 3, 3), // Camera is at (4,3,3), in World Space
        glm::vec3(0, 0, 0), // and looks at the origin
        glm::vec3(0, 1, 0)  // Head is up (set to 0,-1,0 to look upside-down)
        );
    // Model matrix : an identity matrix (model will be at the origin)
    glm::mat4 ModelGLM = glm::mat4(1.0f);  // Changes for each model !
                                        // Our ModelViewProjection : multiplication of our 3 matrices
    glm::mat4 MVP = ProjectionGLM * ViewGLM * ModelGLM; // Remember, matrix multiplication is the other way around
    glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);

輸出矩陣(MVP矩陣4)

定制圖書館

    Vector3 vectornew1(4, 3, 3);
    Vector3 vectornew2(0, 0, 0); 
    Vector3 vectornew3(0, 1, 0);

    // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
    Matrix4 Projection = Matrix4::Perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
    Projection = Matrix4::Transpose(Projection);
    // Camera matrix
    Matrix4 View = Matrix4::lookAt(
        vectornew1, // Camera is at (4,3,3), in World Space
        vectornew2, // and looks at the origin
        vectornew3  // Head is up (set to 0,-1,0 to look upside-down)
        );
    // Model matrix : an identity matrix (model will be at the origin)
    Matrix4 Model = Matrix4::Identity();  // Changes for each model !
                                        // Our ModelViewProjection : multiplication of our 3 matrices
    Matrix4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around
    glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP.data[0][0]);

自定義Matrix4乘法代碼:

temp.data[0][0] = this->data[0][0] * Other.data[0][0] + this->data[0][1] * Other.data[1][0] + this->data[0][2] * Other.data[2][0] + this->data[0][3] * Other.data[3][0];
temp.data[0][1] = this->data[0][0] * Other.data[0][1] + this->data[0][1] * Other.data[1][1] + this->data[0][2] * Other.data[2][1] + this->data[0][3] * Other.data[3][1];
temp.data[0][2] = this->data[0][0] * Other.data[0][2] + this->data[0][1] * Other.data[1][2] + this->data[0][2] * Other.data[2][2] + this->data[0][3] * Other.data[3][2];
temp.data[0][3] = this->data[0][0] * Other.data[0][3] + this->data[0][1] * Other.data[1][3] + this->data[0][2] * Other.data[2][3] + this->data[0][3] * Other.data[3][3];

temp.data[1][0] = this->data[1][0] * Other.data[0][0] + this->data[1][1] * Other.data[1][0] + this->data[1][2] * Other.data[2][0] + this->data[1][3] * Other.data[3][0];
temp.data[1][1] = this->data[1][0] * Other.data[0][1] + this->data[1][1] * Other.data[1][1] + this->data[1][2] * Other.data[2][1] + this->data[1][3] * Other.data[3][1];
temp.data[1][2] = this->data[1][0] * Other.data[0][2] + this->data[1][1] * Other.data[1][2] + this->data[1][2] * Other.data[2][2] + this->data[1][3] * Other.data[3][2];
temp.data[1][3] = this->data[1][0] * Other.data[0][3] + this->data[1][1] * Other.data[1][3] + this->data[1][2] * Other.data[2][3] + this->data[1][3] * Other.data[3][3];

temp.data[2][0] = this->data[2][0] * Other.data[0][0] + this->data[2][1] * Other.data[1][0] + this->data[2][2] * Other.data[2][0] + this->data[2][3] * Other.data[3][0];
temp.data[2][1] = this->data[2][0] * Other.data[0][1] + this->data[2][1] * Other.data[1][1] + this->data[2][2] * Other.data[2][1] + this->data[2][3] * Other.data[3][1];
temp.data[2][2] = this->data[2][0] * Other.data[0][2] + this->data[2][1] * Other.data[1][2] + this->data[2][2] * Other.data[2][2] + this->data[2][3] * Other.data[3][2];
temp.data[2][3] = this->data[2][0] * Other.data[0][3] + this->data[2][1] * Other.data[1][3] + this->data[2][2] * Other.data[2][3] + this->data[2][3] * Other.data[3][3];

temp.data[3][0] = this->data[3][0] * Other.data[0][0] + this->data[3][1] * Other.data[1][0] + this->data[3][2] * Other.data[2][0] + this->data[3][3] * Other.data[3][0];
temp.data[3][1] = this->data[3][0] * Other.data[0][1] + this->data[3][1] * Other.data[1][1] + this->data[3][2] * Other.data[2][1] + this->data[3][3] * Other.data[3][1];
temp.data[3][2] = this->data[3][0] * Other.data[0][2] + this->data[3][1] * Other.data[1][2] + this->data[3][2] * Other.data[2][2] + this->data[3][3] * Other.data[3][2];
temp.data[3][3] = this->data[3][0] * Other.data[0][3] + this->data[3][1] * Other.data[1][3] + this->data[3][2] * Other.data[2][3] + this->data[3][3] * Other.data[3][3];

GLM如何對矩陣乘法進行處理以產生截然不同的結果? 我的方法與圖形計算器匹配,並通過身份矩陣產生正確的結果。

我在引擎中嘗試了您的乘法(也基於glm,或至少針對它進行了測試),當我將其替換為Other和Other時,結果是正確的。 因此,這應該是正確的:

temp.data[0][0] = Other.data[0][0] * this->data[0][0] + Other.data[0][1] * this->data[1][0] + Other.data[0][2] * this->data[2][0] + Other.data[0][3] * this->data[3][0];
temp.data[0][1] = Other.data[0][0] * this->data[0][1] + Other.data[0][1] * this->data[1][1] + Other.data[0][2] * this->data[2][1] + Other.data[0][3] * this->data[3][1];
temp.data[0][2] = Other.data[0][0] * this->data[0][2] + Other.data[0][1] * this->data[1][2] + Other.data[0][2] * this->data[2][2] + Other.data[0][3] * this->data[3][2];
temp.data[0][3] = Other.data[0][0] * this->data[0][3] + Other.data[0][1] * this->data[1][3] + Other.data[0][2] * this->data[2][3] + Other.data[0][3] * this->data[3][3];
temp.data[1][0] = Other.data[1][0] * this->data[0][0] + Other.data[1][1] * this->data[1][0] + Other.data[1][2] * this->data[2][0] + Other.data[1][3] * this->data[3][0];
temp.data[1][1] = Other.data[1][0] * this->data[0][1] + Other.data[1][1] * this->data[1][1] + Other.data[1][2] * this->data[2][1] + Other.data[1][3] * this->data[3][1];
temp.data[1][2] = Other.data[1][0] * this->data[0][2] + Other.data[1][1] * this->data[1][2] + Other.data[1][2] * this->data[2][2] + Other.data[1][3] * this->data[3][2];
temp.data[1][3] = Other.data[1][0] * this->data[0][3] + Other.data[1][1] * this->data[1][3] + Other.data[1][2] * this->data[2][3] + Other.data[1][3] * this->data[3][3];
temp.data[2][0] = Other.data[2][0] * this->data[0][0] + Other.data[2][1] * this->data[1][0] + Other.data[2][2] * this->data[2][0] + Other.data[2][3] * this->data[3][0];
temp.data[2][1] = Other.data[2][0] * this->data[0][1] + Other.data[2][1] * this->data[1][1] + Other.data[2][2] * this->data[2][1] + Other.data[2][3] * this->data[3][1];
temp.data[2][2] = Other.data[2][0] * this->data[0][2] + Other.data[2][1] * this->data[1][2] + Other.data[2][2] * this->data[2][2] + Other.data[2][3] * this->data[3][2];
temp.data[2][3] = Other.data[2][0] * this->data[0][3] + Other.data[2][1] * this->data[1][3] + Other.data[2][2] * this->data[2][3] + Other.data[2][3] * this->data[3][3];
temp.data[3][0] = Other.data[3][0] * this->data[0][0] + Other.data[3][1] * this->data[1][0] + Other.data[3][2] * this->data[2][0] + Other.data[3][3] * this->data[3][0];
temp.data[3][1] = Other.data[3][0] * this->data[0][1] + Other.data[3][1] * this->data[1][1] + Other.data[3][2] * this->data[2][1] + Other.data[3][3] * this->data[3][1];
temp.data[3][2] = Other.data[3][0] * this->data[0][2] + Other.data[3][1] * this->data[1][2] + Other.data[3][2] * this->data[2][2] + Other.data[3][3] * this->data[3][2];
temp.data[3][3] = Other.data[3][0] * this->data[0][3] + Other.data[3][1] * this->data[1][3] + Other.data[3][2] * this->data[2][3] + Other.data[3][3] * this->data[3][3];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM