title: Best place to store model matrix in OpenGL?
body: <p>I'm currently refactoring my OpenGL program (used to be one single enormous file) to use C++ classes. The basic framework looks like this:</p>
  
  <p>I have an interface <code>Drawable</code> with the function <code>virtual void Render(GLenum type) const = 0;</code> and a bunch of classes implementing this interface (<code>Sphere</code>, <code>Cube</code>, <code>Grid</code>, <code>Plane</code>, <code>PLYMesh</code> and <code>OBJMesh</code>).</p>
  
  <p>In my <code>main.cpp</code> I'm setting up a scene containing multiple of these objects, each with its own shader program. After setting uniform buffer objects and each program's individual uniforms, I'm calling <code>glutMainLoop()</code>.</p>
  
  <p>In my <code>Display</code> function called each frame, the first thing I'm doing is setting up all the transformation matrices and finally call the above mentioned <code>Render</code> function for every object in the scene:</p>
  
  <pre><code>void Display()
  {
      // Clear framebuffer
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
  
      modelViewMatrix  = glm::mat4(1.0);
      projectionMatrix = glm::mat4(1.0);
      normalMatrix     = glm::mat4(1.0);
  
      modelViewMatrix = glm::lookAt(glm::vec3(0.0, 0.0, mouse_translate_z), glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0));
      modelViewMatrix = glm::rotate(modelViewMatrix, -mouse_rotate_x, glm::vec3(1.0f, 0.0f, 0.0f));
      modelViewMatrix = glm::rotate(modelViewMatrix, -mouse_rotate_y, glm::vec3(0.0f, 1.0f, 0.0f));
  
      projectionMatrix = glm::perspective(45.0f, (GLfloat)WINDOW_WIDTH / (GLfloat)WINDOW_HEIGHT, 1.0f, 10000.f);
  
      // No non-uniform scaling (only use mat3(normalMatrix in shader))
      normalMatrix = modelViewMatrix;
  
      glBindBuffer(GL_UNIFORM_BUFFER, ubo_global_matrices);
          glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::mat4), glm::value_ptr(modelViewMatrix));
          glBufferSubData(GL_UNIFORM_BUFFER, 1 * sizeof(glm::mat4), sizeof(glm::mat4), glm::value_ptr(projectionMatrix));
          glBufferSubData(GL_UNIFORM_BUFFER, 2 * sizeof(glm::mat4), sizeof(glm::mat4), glm::value_ptr(normalMatrix));
      glBindBuffer(GL_UNIFORM_BUFFER, 0);
  
      // ************************************************** //
      // **************** DRAWING COMMANDS **************** //
      // ************************************************** //
  
      // Grid
      if (grid-&gt;GetIsRendered())
      {
          program_GRID_NxN-&gt;Use();
              grid-&gt;Render(GL_LINES);
          program_GRID_NxN-&gt;UnUse();
      }
  
      // Plane
      ...
  
      // Sphere
      ...
  
      // Swap front and back buffer and redraw scene
      glutSwapBuffers();
      glutPostRedisplay();
  }
  </code></pre>
  
  <p>My question now is the following: With the current code, I'm using the same <code>ModelView</code> matrix for every object. What if I wanna translate only the sphere, or rotate only the plane without changing the vertex positions? Where is the best place to store the model matrix in a large OpenGL program? What about putting a protected member variable <code>glam::mat4 modelMatrix</code> into the <code>Drawable</code> interface? Also, should the model and the view matrix be split (for example using a <code>Camera</code> class containing the view matrix only)?</p>