# Why are my objects rotating with my camera?

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.

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:

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


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!

• 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