Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a difference between GLSL/GLM and Python/numpy when it comes to 4×4 matrix multiplication?

Basically, this works:

glUniformMatrix4fv(glGetUniformLocation(self.shader.program, "projection"), 1, GL_FALSE, self.P)
glUniformMatrix4fv(glGetUniformLocation(self.shader.program, "view"), 1, GL_FALSE, self.V)
glUniformMatrix4fv(glGetUniformLocation(self.shader.program, "model"), 1, GL_FALSE, self.M)
#version 330 core

layout (location=0) in vec3 vertexPos;
layout (location=1) in vec2 vertexTexCoord;
layout (location=2) in vec3 vertexNormal;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

out vec2 fragmentTexCoord;

void main(){
    gl_Position = projection * view * model * vec4(vertexPos, 1.0);
    fragmentTexCoord = vertexTexCoord;
}

But this doesn’t:

self.PVM = np.matmul(self.P, np.matmul(self.V, self.M))
glUniformMatrix4fv(glGetUniformLocation(self.shader.program, "PVM"), 1, GL_FALSE, self.PVM)
#version 330 core

layout (location=0) in vec3 vertexPos;
layout (location=1) in vec2 vertexTexCoord;
layout (location=2) in vec3 vertexNormal;

uniform mat4 PVM;

out vec2 fragmentTexCoord;

void main(){
    gl_Position = PVM * vec4(vertexPos, 1.0);
    fragmentTexCoord = vertexTexCoord;
}

Passing in each matrix individually then doing the multiplication within the shader produces expected result (i.e. I can see the model and move around, etc.). Calculating the PVM matrix in Python first makes the model disappear. The above code is all I’m changing.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

numpy.matmul behaves different than a GLSL matrix multiplication. Compare numpy.matmul and GLSL Programming/Vector and Matrix Operations. You have to reverse the order of the matrix multiplication:

self.PVM = np.matmul(self.P, np.matmul(self.V, self.M))

self.PVM = np.matmul(self.M, np.matmul(self.V, self.P))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading