I am working with this C# library that wraps OpenGL. I am trying to understand why my objects in the scene are rotating as I yaw my camera (view matrix).

Here is an image where I have 4 separate cubes in a row with no spacing.

Not Rotated

If I yaw my camera you can see that the cubes have also rotated. Notice the red arrows which point to the side of a cube that is now visible:

After Rotation

I was expecting that these cubes would not rotate. It looked to me as if something in my code is wrong for their model matrix, but I'm not sure I understand what is happening.

Every render frame I am doing this for each cube:

shader["model_matrix"].SetValue(ModelMatrix);

ModelMatrix is calculated as follows, once:

this.ModelMatrix = Matrix4.CreateTranslation(new Vector3(worldX * size, worldY * size, worldZ * size));

world variables are offsets for the position of the cube, and size is just the length. Each render frame my view matrix is being set:

shader["view_matrix"].SetValue(camera.ViewMatrix);

Here is the vertex shader:

uniform mat4 projection_matrix;
uniform mat4 model_matrix;
uniform mat4 view_matrix;

attribute vec3 in_position;
attribute vec3 in_normal;

varying vec3 vertex_light_position;
varying vec3 vertex_normal;

void main(void)
{
  vertex_normal = in_normal;
  vertex_light_position = normalize(vec3(1, 1, 0));
  gl_Position = projection_matrix * model_matrix * view_matrix * vec4(in_position, 1);
}

I have been basing this extended example off of the tutorial here. I am probably missing something basic (I am just starting OpenGL), so thanks for any help in advance!

  • 2
    Usually the order of multiplication should be projection * (view * (model * (vertex position)))), since matrix multiplication is not commutative in general. You have the model & view matrices flipped in your vertex shader. – DMGregory May 21 '14 at 4:11
  • @DMGregory Reading those parenthesis agitates me... I suppose it is a good way to visualize OpenGL's use of post-multiplication, but for some reason seeing that series of transforms written that way bugs me more than it probably should ;) – Andon M. Coleman May 21 '14 at 6:18
  • You were spot on DMGregory. Thanks! – therealjohn May 21 '14 at 15:55
up vote 6 down vote accepted

As DMGregory commented, your model_matrix and view_matrix are in wrong order in your shader code. If you look at the tutorial's shader code, you'll see your mistake.

I really liked this tutorial about matrices. It simply explains how the order of transformations and rotations should be. There is also couple great general explanations about how graphics pipeline works in computerphile's channel on youtube, including information about matrices.

MVP matrix order MVP steps illustrated

  • 1
    Awesome answer, thanks for the visuals and link. It is making more sense now. =) – therealjohn May 21 '14 at 15:55

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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