1

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
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
CodeMonkey
  • 3,418
  • 4
  • 30
  • 53
  • 1
    The code you have posted looks correct... Are you sure the problem isn't elsewhere? Like you're using the wrong uniform location or something? – Matthew Marshall Jan 09 '12 at 19:26
  • 1
    I can't see theres anything wrong. – CodeMonkey Jan 09 '12 at 20:33
  • 2
    Matthew thank you so much!!! I'm a idiot! I got it wrong because of this example code. I have to call glUniforms to update my data. All this time I've treated them as i do Vertex Arrays(set em one in init code). But ofcourse my matrices needs updating when i transform them. – CodeMonkey Jan 09 '12 at 20:51
  • 2
    My god this also solved my other code where i couldnt understand it should be so hard to calculate a damn normal vector to create reflection from a sphere :) i never updated the camera cordinates! everything works now, thank you. If you make a answer ill click it if you want points. – CodeMonkey Jan 09 '12 at 20:57

1 Answers1

0

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).

Maurizio Benedetti
  • 3,557
  • 19
  • 26