# Models are not rendered correctly. OpenGL

I'm working on my university OpenGL project and as a base of it I'm trying to combine several OpenGL tutorials, mainly tutorials from http://ogldev.atspace.co.uk/.

The problem I have is that models I'm rendering are not rendered properly. I don't want to create a post with a wall of code so I'm just supplying a screenshot and main rendering loop, hope that some of you recognize where the error might be or have some guesses. If you need any particular part of the code please let me know.

I am using OpenGL 3.3 and SDL2 for window management.

Rendering loop:

for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {

glm::mat4 world_matrix = glm::scale(glm::vec3(1, 1, 1));
world_matrix *= glm::rotate(world_matrix, 90.0f, glm::vec3(1.0, 0.0, 0.0));
world_matrix *= glm::translate(glm::vec3(i * 50, j * 50, 0));

//set methods of m_pEffect object set uniform values for the shader.
m_pEffect->SetModel(world_matrix);
m_pEffect->SetView(view);
m_pEffect->SetProjection(projection);

m_pMesh->Render();
}
}


#version 330

layout (location = 0) in vec3 Position;
layout (location = 1) in vec2 TexCoord;
layout (location = 2) in vec3 Normal;

uniform mat4 mProjection;
uniform mat4 mView;
uniform mat4 mModel;

out vec2 TexCoord0;
out vec3 Normal0;
out vec3 WorldPos0;

void main()
{
gl_Position = mProjection * mView * mModel * vec4(Position, 1.0);
TexCoord0   = TexCoord;
Normal0     = (mModel * vec4(Normal, 0.0)).xyz;
WorldPos0   = (mModel * vec4(Position, 1.0)).xyz;
}


• In the shader instead of mProjection * mView * mModel try using mModel * mView * mProjection or to achieve the same result , transpose the matrices before you call setView/setProjections/... – Raxvan Dec 19 '13 at 13:15
• @Raxvan I've tried that before, here is the result . When I look around with the mouse it feels like there is a really wide field of view. I am little confused with matrix multiplication order because in some tutorials the order is different. – Maksim Borisov Dec 19 '13 at 13:28
• @Raxvan You were right, thanks! In fragment shader I changed gl_Position = mProjection * mView * mModel * vec4(Position, 1.0); to gl_Position = vec4(Position, 1.0) * mModel * mView * mProjection; and it worked. As I've said I dont understand why, because in other examples I used first matrix multiplication order and it was correct there. – Maksim Borisov Dec 19 '13 at 13:52
• because in matrix multiplication A * B != B * A but transposed(B * A). This is important because when you multiply A * B in code and send it to the shader it might be sent as transposed matrix so A * B in code will not be equivalent to A * B in shader. opengl has a switch some place for this so it will not transpose the matrices, however this might not solve the problem because it's also important how the math library is made (matrix is stored on rows or colums) – Raxvan Dec 19 '13 at 14:15

Changing mProjection * mView * mModel to mModel * mView * mProjection should solve the problem in this case but it's not a general rule. Here is why:
Some math facts first, for matrix multiplication A * B != B * A; also A * B = transposed(B) * transposed(A)
So when you take a tutorial or code and you see in code MatrixA * MatrixB and you want to do the same in the shader you have to think how MatrixA and MatrixB will end up in the shader code (transposed or not transposed). If it will end up transposed then you need to do MatrixB * MatrixA to achieve the same result, otherwise A * B.