title: What is the correct order of Transformations when calculating Matrices in OpenGL?
body: <p>I am following the tutorials at LearnOpenGL.com and I am confused about the order of Matrices. </p>
  
  <p>The <a href="https://learnopengl.com/Getting-started/Transformations" rel="nofollow noreferrer">Transformations</a> chapter tells:</p>
  
  <blockquote>
    <p>Matrix multiplication is not commutative, which means their order is important. When multiplying matrices the right-most matrix is first multiplied with the vector so you should read the multiplications from right to left. It is advised to first do scaling operations, then rotations and lastly translations when combining matrices otherwise they might (negatively) affect each other. For example, if you would first do a translation and then scale, the translation vector would also scale!</p>
  </blockquote>
  
  <p>So If I am not wrong, the order is <code>Translate * Rotate * Scale * vector_to_transform</code>.</p>
  
  <p>But immediately in the next Chapter, when calculating the LookAt matrix, the multiplication order is flipped. Here is the code snippet from the website:</p>
  
  <pre><code>// Custom implementation of the LookAt function
  glm::mat4 calculate_lookAt_matrix(glm::vec3 position, glm::vec3 target, glm::vec3 worldUp)
  {
      // 1. Position = known
      // 2. Calculate cameraDirection
      glm::vec3 zaxis = glm::normalize(position - target);
      // 3. Get positive right axis vector
      glm::vec3 xaxis = glm::normalize(glm::cross(glm::normalize(worldUp), zaxis));
      // 4. Calculate camera up vector
      glm::vec3 yaxis = glm::cross(zaxis, xaxis);
  
      // Create translation and rotation matrix
      // In glm we access elements as mat[col][row] due to column-major layout
      glm::mat4 translation = glm::mat4(1.0f); // Identity matrix by default
      translation[3][0] = -position.x; // Third column, first row
      translation[3][1] = -position.y;
      translation[3][2] = -position.z;
      glm::mat4 rotation = glm::mat4(1.0f);
      rotation[0][0] = xaxis.x; // First column, first row
      rotation[1][0] = xaxis.y;
      rotation[2][0] = xaxis.z;
      rotation[0][1] = yaxis.x; // First column, second row
      rotation[1][1] = yaxis.y;
      rotation[2][1] = yaxis.z;
      rotation[0][2] = zaxis.x; // First column, third row
      rotation[1][2] = zaxis.y;
      rotation[2][2] = zaxis.z; 
  
      // Return lookAt matrix as combination of translation and rotation matrix
      return rotation * translation; // Remember to read from right to left (first translation then rotation)
  }
  </code></pre>
  
  <p>At the end of the code snippet, the matrix is calculated as <code>rotation * translation</code>, even though the matrix is going to be multiplied as,</p>
  
  <pre><code>gl_position = projection * lookAt * model * vec4(vertexPosition, 1.0);
  </code></pre>
  
  <p>as Column-major matrices must be pre-multiplied to the vector. </p>
  
  <p>Please help me understand this.</p>