title: OpenGL: Model stretched when translated from origin
body: <p>I'm working on a space flight simulator as my first real OpenGL project. I'm having trouble getting a cube to render properly when it isn't located at the origin. What happens is that the cube stretches out from the origin, and the degree of stretching depends on the amount translated from the origin.</p>
  
  <p>I believe that it has nothing to do with the shader, as I have tried two different vertex shaders with the same result. one of the shaders is a very simple shader:</p>
  
  <pre><code>#version 330 core
  
  // Input vertex data, different for all executions of this shader.
  layout(location = 0) in vec3 vertexPosition_modelspace;
  
  // Output data ; will be interpolated for each fragment.
  out vec3 fragmentColor;
  // Values that stay constant for the whole mesh.
  uniform mat4 MVP;
  
  void main(){    
  
      // Output position of the vertex, in clip space : MVP * position
      gl_Position =  MVP * vec4(vertexPosition_modelspace,1);
  
      // The color of each vertex will be interpolated
      // to produce the color of each fragment
      fragmentColor = vec3(255,255,255);
  }
  </code></pre>
  
  <p>It seems pretty clear to me that the problem is not with the shader.</p>
  
  <p>When the cube translated by 1 unit in each direction, it looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/oujZg.png" alt="enter image description here"></p>
  
  <p>When translating the camera in the positive Z direction, the cube will stretch more to follow the camera.</p>
  
  <p><strong>Before translating</strong> (camera located at (0,0,25) and model located at (5,5,5))</p>
  
  <p>MVP Matrix:    </p>
  
  <pre><code>(10.942525, 0.000000, 12.402489, 12.500000)   
  (0.000000, 14.590034, 12.402489, 12.500000)   
  (0.000000, 0.000000, 12.302468, 12.400000)   
  (0.000000, 0.000000, 24.804977, 25.000000) 
  </code></pre>
  
  <p>Transformation Matrix: </p>
  
  <pre><code>(0.100000, 0.000000, 0.000000, 0.500000)   
  (0.000000, 0.100000, 0.000000, 0.500000)   
  (0.000000, 0.000000, 0.100000, 0.500000)   
  (0.000000, 0.000000, 0.000000, 1.000000)   
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/aN5nW.png" alt="enter image description here"></p>
  
  <p><strong>After translating</strong> (camera translated in the positive z direction)</p>
  
  <p>Transformation Matrix: </p>
  
  <pre><code>(0.100000, 0.000000, 0.000000, 0.500000)
  (0.000000, 0.100000, 0.000000, 0.500000)
  (0.000000, 0.000000, 0.100000, 0.500000)
  (0.000000, 0.000000, 0.000000, 1.000000)
  </code></pre>
  
  <p>MVP Matrix: </p>
  
  <pre><code>(10.942525, 0.000000, 46.159237, 46.250000)
  (0.000000, 14.590034, 46.159237, 46.250000)
  (0.000000, 0.000000, 46.059216, 46.150002)
  (0.000000, 0.000000, 92.318474, 92.500000)
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/2yvso.png" alt="enter image description here"></p>
  
  <p>Here's my code for getting the transformation matrices from the models:</p>
  
  <pre><code>glm::mat4 MovableObject::getTransformationMatrix() const
  {
      return getTranslationMatrix() * getRotationMatrix() * getScaleMatrix();
  }
  
  glm::mat4 MovableObject::getScaleMatrix() const
  {
      glm::mat4 scaleMatrix = glm::mat4(1.0);
      scaleMatrix[0][0] = scaleFactors[0];
      scaleMatrix[1][1] = scaleFactors[1];
      scaleMatrix[2][2] = scaleFactors[2];
      return scaleMatrix;
  }
  
  glm::mat4 MovableObject::getRotationMatrix() const
  {
      return glm::toMat4(rotation);
  }
  
  glm::mat4 MovableObject::getTranslationMatrix() const
  {
      glm::mat4 translationMatrix = glm::mat4(1.);
      translationMatrix[0][3] = center[0];
      translationMatrix[1][3] = center[1];
      translationMatrix[2][3] = center[2];
      return translationMatrix;
  }
  </code></pre>
  
  <p>here's the rendering code for drawing the mesh:</p>
  
  <pre><code>void Mesh::render(glm::mat4 modelMatrix, const Camera&amp; camera)
  {
      glUseProgram(programID);
  
      glm::mat4 view = camera.getViewMatrix();
      glm::mat4 pvm = camera.getProjectionMatrix() * view * modelMatrix;
  
      std::cout &lt;&lt; "MVP Matrix: " &lt;&lt; glm::to_string(pvm) &lt;&lt; std::endl &lt;&lt; std::endl;
  
      // Send our transformation to the currently bound shader,
      // in the "MVP" uniform
      glUniformMatrix4fv(matrixID, 1, GL_FALSE, &amp;pvm[0][0]);
      glUniformMatrix4fv(modelMatrixID, 1, GL_FALSE, &amp;modelMatrix[0][0]);
      glUniformMatrix4fv(viewMatrixID, 1, GL_FALSE, &amp;view[0][0]);
  
      glUniform3f(lightID, 0.f, 0.f, 0.f); // TODO: hardwiring at 0 for now
  
      // Bind our texture in Texture Unit 0
      glActiveTexture(GL_TEXTURE0);
      glBindTexture(GL_TEXTURE_2D, texture);
      // Set "myTextureSampler" sampler to user Texture Unit 0
      glUniform1i(textureID, 0);
  
      // 1st attribute buffer : vertices
      glEnableVertexAttribArray(0);
      glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
      glVertexAttribPointer(
                            0,                  // attribute
                            3,                  // size
                            GL_FLOAT,           // type
                            GL_FALSE,           // normalized?
                            0,                  // stride
                            (void*)0            // array buffer offset
                            );
  
      // 2nd attribute buffer : UVs
      glEnableVertexAttribArray(1);
      glBindBuffer(GL_ARRAY_BUFFER, uvBuffer);
      glVertexAttribPointer(
                            1,                                // attribute
                            2,                                // size
                            GL_FLOAT,                         // type
                            GL_FALSE,                         // normalized?
                            0,                                // stride
                            (void*)0                          // array buffer offset
                            );
  
      // 3rd attribute buffer : normals
      glEnableVertexAttribArray(2);
      glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
      glVertexAttribPointer(
                            2,                                // attribute
                            3,                                // size
                            GL_FLOAT,                         // type
                            GL_FALSE,                         // normalized?
                            0,                                // stride
                            (void*)0                          // array buffer offset
                            );
  
      // Draw the triangles !
      glDrawArrays(GL_TRIANGLES, 0, vertices.size() );
  
      glDisableVertexAttribArray(0);
      glDisableVertexAttribArray(1);
      glDisableVertexAttribArray(2);
  }
  </code></pre>
  
  <p>and here's the code for getting the camera matrices:</p>
  
  <pre><code>glm::mat4 Camera::getProjectionMatrix() const
  {
      return glm::perspective(FoV, 4.0f / 3.0f, 0.1f, 1000.0f);
  }
  
  glm::mat4 Camera::getViewMatrix() const
  {
      glm::mat4 matrix = glm::toMat4(rotation);
      glm::vec3 up = glm::vec3(matrix[0][1], matrix[1][1], matrix[2][1]);
      glm::vec3 forward = -glm::vec3(matrix[0][2], matrix[1][2], matrix[2][2]);
  
  
      return glm::lookAt(center, center + forward, up);
  }
  </code></pre>