Skip to content

[raymath] MatrixMultiply multiplies matricies in wrong order #3039

@nxuv

Description

@nxuv

Test case comparing Raylib implementation to GLM:

#include <raylib.h>
#include <raymath.h>
#include <stdio.h>
#include "glm/mat4x4.hpp"
#include <glm/vec3.hpp> // glm::vec3
#include <glm/vec4.hpp> // glm::vec4
#include <glm/mat4x4.hpp> // glm::mat4
#include <glm/ext/matrix_transform.hpp> // glm::translate, glm::rotate, glm::scale
#include <glm/ext/matrix_clip_space.hpp> // glm::perspective
#include <glm/ext/scalar_constants.hpp> // glm::pi

void printMatrix(Matrix mat) {
    printf("%.2f %.2f %.2f %.2f\n", mat.m0, mat.m4, mat.m8, mat.m12);
    printf("%.2f %.2f %.2f %.2f\n", mat.m1, mat.m5, mat.m9, mat.m13);
    printf("%.2f %.2f %.2f %.2f\n", mat.m2, mat.m6, mat.m10, mat.m14);
    printf("%.2f %.2f %.2f %.2f\n", mat.m3, mat.m7, mat.m11, mat.m15);
    printf("\n");
}

int main() {
    struct Matrix view = MatrixLookAt((Vector3) {0.0f, 2.0f, 5.0f}, (Vector3) {1.0f, 20.0f, 1.0f}, (Vector3) {0.5f, 1.0f, 0.0f});
    struct Matrix proj = MatrixPerspective(0.78f, 9.0f / 16.0f, 2.0f, 125.0f);
    struct Matrix vipr = MatrixMultiply(view, proj);
    struct Matrix vipr2 = MatrixMultiply(proj, view);
    glm::mat4 Projection = glm::perspective(0.78f, 9.0f / 16.0f, 2.0f, 125.f);
    glm::mat4 View = glm::lookAt(glm::vec3(0.0f, 2.0f, 5.0f), glm::vec3(1.0f, 20.0f, 1.0f), glm::vec3(0.5f, 1.0f, 0.0f));
    glm::mat4 ViewProj = View * Projection;  
    printf("raylib: MatrixMultiply(view, proj)\n");
    printMatrix(vipr);
    printf("raylib: MatrixMultiply(proj, view)\n");
    printMatrix(vipr2);
    printf("glm: MatrixMultiply(view, proj), aka view * proj\n");
    printf("%.2f %.2f %.2f %.2f\n", ViewProj[0][0], ViewProj[1][0], ViewProj[2][0], ViewProj[2][0]);
    printf("%.2f %.2f %.2f %.2f\n", ViewProj[0][1], ViewProj[1][1], ViewProj[2][1], ViewProj[2][1]);
    printf("%.2f %.2f %.2f %.2f\n", ViewProj[0][2], ViewProj[1][2], ViewProj[2][2], ViewProj[2][2]);
    printf("%.2f %.2f %.2f %.2f\n", ViewProj[0][3], ViewProj[1][3], ViewProj[2][3], ViewProj[2][3]);
    printf("\n");
}

Test result

raylib: MatrixMultiply(view, proj)
1.89 -0.94 -3.78 20.76
2.18 0.11 1.06 -5.55
0.06 1.01 -0.22 -4.96
0.05 0.97 -0.22 -0.87

raylib: MatrixMultiply(proj, view)
1.89 -0.53 -3.90 3.55
3.88 0.11 1.83 -1.78
-0.23 -2.37 -1.09 -0.88
0.00 0.00 -1.00 0.00

glm: MatrixMultiply(view, proj), aka view * proj
1.89 -0.53 -3.90 -3.90
3.88 0.11 1.83 1.83
-0.23 -2.37 -1.09 -1.09
0.00 0.00 -1.00 -1.00

Produced result indicates that raylib's MatrixMultiply multiplies matrices right to left instead of left to right.

Possibly same issue in other functions that use matrix multiplication (i.e. Vector3Unproject)

I know it's a duplicate of #3036, but please, look at code I provided. It uses GLM, same print order, same operations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions