GLM matrix multiplication and OpenGL GLSL

南楼画角 提交于 2019-12-24 13:18:50

问题


I have similar code as in this question: some opengl and glm explanation

I have a combined matrix that I pass as a single uniform

//C++
mat4 combinedMatrix = projection * view * model;

//GLSL doesn't work
out_position = combinedMatrix * vec4(vertex, 1.0);

It doesn't work. But if I do all the multiplication in the shader so I pass in each individual matrix and get

//GLSL works
out_position = projection * view * model * vec4(vertex, 1.0);

It works. I can't see anything wrong with my matrices in the C++ code.

The following works too

//C++
mat4 combinedMatrix = projection * view * model;
vec4 p = combinedMatrix * v;
//pass in vertex p as a vec4

//GLSL works
out_position = vertex

回答1:


I think the problem could be in the matrix multiplication you do in your code.

How the following multiplication is performed?

mat4 combinedMatrix = projection * view * model

It looks to me quite odd, matrix multiplication cannot be done in this way unless I am totally wrong.

This is the way I perform it:

for (i=0; i<4; i++) {
    tmp.m[i][0] =   (srcA->m[i][0] * srcB->m[0][0]) +
                    (srcA->m[i][1] * srcB->m[1][0]) +
                    (srcA->m[i][2] * srcB->m[2][0]) +
                    (srcA->m[i][3] * srcB->m[3][0]) ;

    tmp.m[i][1] =   (srcA->m[i][0] * srcB->m[0][1]) +
                    (srcA->m[i][1] * srcB->m[1][1]) +
                    (srcA->m[i][2] * srcB->m[2][1]) +
                    (srcA->m[i][3] * srcB->m[3][1]) ;

    tmp.m[i][2] =   (srcA->m[i][0] * srcB->m[0][2]) +
                    (srcA->m[i][1] * srcB->m[1][2]) +
                    (srcA->m[i][2] * srcB->m[2][2]) +
                    (srcA->m[i][3] * srcB->m[3][2]) ;

    tmp.m[i][3] =   (srcA->m[i][0] * srcB->m[0][3]) +
                    (srcA->m[i][1] * srcB->m[1][3]) +
                    (srcA->m[i][2] * srcB->m[2][3]) +
                    (srcA->m[i][3] * srcB->m[3][3]) ;
}

memcpy(result, &tmp, sizeof(PATRIA_Matrix));

Probably I am wrong on this but I am quite sure you should follow this PATH.

The way I see your example it looks to me a pointer multiplication :( (though I don't have the specific of your mat4 matrix class/struct).



来源:https://stackoverflow.com/questions/8787547/glm-matrix-multiplication-and-opengl-glsl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!