Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.


Vertex shader:

#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;               

enter image description here

share|improve this question
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

2 Answers 2

up vote 0 down vote accepted

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.

If a matrix ends up transposed or not is dependent on the rendering Api and the matrix library you used. When you sent the matrix to the shader the api will copy a series of floats and interpret that as a matrix, but it doesn't know if the first 4 floats represent a column or a row in the matrix.

share|improve this answer

It's hard to tell from the picture, but I've had errors before that looked like this and it had to do with rendering models using indices. If you are using short integers for your index element buffer you might want to try to use a regular unsigned int. If your model has more than 65k vertices then when you render using something like glDrawElement() you will get the resulting problem.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.