1
\$\begingroup\$

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!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Commented May 21, 2014 at 4:11
  • \$\begingroup\$ @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 ;) \$\endgroup\$ Commented May 21, 2014 at 6:18

1 Answer 1

6
\$\begingroup\$

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

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Awesome answer, thanks for the visuals and link. It is making more sense now. =) \$\endgroup\$ Commented May 21, 2014 at 15:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .