title: OpenGL ES 2.0: Object rotation problem
body: <p>I know the following question have been asked many times on different forums, but after viewing different solutions, it still did not fix my problem, or maybe I did not get what they mean<br>
  I am trying to model a pyramid using OpenGL ES 2.0 on the iPhone.<br>
  I would like to allow the user to rotate this pyramid using the touch screen, so he can view every face of the shape.<br>
  I am using my own matrix utilities, inspired by the library given in the book <a href="http://www.opengles-book.com/" rel="nofollow noreferrer">OpenGL ES 2.0 Programming Guide</a>.<br>
  I have a simple vertex shader that takes 2 uniforms and 2 attributes.<br>
  Here is my vertex shader :</p>
  
  <pre><code>uniform mat4 modelViewMatrix;
  uniform mat4 projectionMatrix;
  attribute vec4 vertex;
  attribute vec4 color;
  varying vec4 colorVarying;
  
  void main()
  {
   colorVarying = color;
   gl_Position = projectionMatrix * modelViewMatrix * vertex;
  }
  </code></pre>
  
  <p>So, during my rendering, I create 2 matrices :  the modelView and the projection matrix.</p>
  
  <p>Here is how my projection matrix is initialized :</p>
  
  <pre><code>[pMatrix frustumWithAngleOfView:70.0f
                            ratio:(float)backingWidth / (float)backingHeight
                            zNear:1.0f
                             zFar:100.0f];
  [program use];
  glUniformMatrix4fv(unif_projectionMatrix, 1, GL_FALSE, pMatrix.vertices);
  [program unuse];
  </code></pre>
  
  <p>My pyramid vertices are set as follow :</p>
  
  <pre><code>GLfloat pyramid[] = {
    //Base
    -1.0, -1.0, 1.0, 1.0,
    -1.0, -1.0, -1.0, 1.0,
    1.0, -1.0, -1.0, 1.0,
    1.0, -1.0, 1.0, 1.0,
    //Front face
    0.0, 1.0, 0.0, 1.0,
    -1.0, -1.0, 1.0, 1.0,
    1.0, -1.0, 1.0, 1.0,
    //Right face
    0.0, 1.0, 0.0, 1.0,
    1.0, -1.0, 1.0, 1.0,
    1.0, -1.0, -1.0, 1.0,
    //Back face
    0.0, 1.0, 0.0, 1.0,
    1.0, -1.0, -1.0, 1.0,
    -1.0, -1.0, -1.0, 1.0,
    //Left face
    0.0, 1.0, 0.0, 1.0,
    -1.0, -1.0, -1.0, 1.0,
    -1.0, -1.0, 1.0, 1.0
  
   };
  </code></pre>
  
  <p>Finally, here is my drawing code :</p>
  
  <pre><code>[EAGLContext setCurrentContext:context];
  
  glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
  glViewport(0, 0, backingWidth, backingHeight);
  
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
  // Use shader program
  [program use];
  
  // Apply transformations on the ModelView matrix
  [mvMatrix identity];
  [mvMatrix translateX:0 Y:0 Z:-6.0];
  [mvMatrix rotateX:rotatex Y:rotatey Z:rotatez];
  
  // Set the ModelView matrix uniform in the v-shader
  glUniformMatrix4fv(unif_modelViewMatrix, 1, GL_FALSE, mvMatrix.vertices);
  // Set the vertex attribute in the v-shader
  glVertexAttribPointer(attr_vertex, 4, GL_FLOAT, GL_FALSE, 0, pyramid);
  glEnableVertexAttribArray(attr_vertex);
  
  // Draw
  glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  glDrawArrays(GL_TRIANGLES, 4, 3);
  glDrawArrays(GL_TRIANGLES, 7, 3);
  glDrawArrays(GL_TRIANGLES, 10, 3);
  glDrawArrays(GL_TRIANGLES, 13, 3);
  
  glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
  [context presentRenderbuffer:GL_RENDERBUFFER];
  </code></pre>
  
  <p>Everything seems to work fine, my pyramid is here, translated backward on the Z axis.<br>
  The problem shows up when I start to rotate the shape, it looks like it is rotating around the user, not around itself.<br>
  So for example, when I touch the screen from left to right, I see the pyramid going out of the screen at the right edge, and then come back into screen from the left edge, as if it had done a full circle around me :p.</p>
  
  <p>I am pretty much sure about my matrix management code, so I decided to do not post it here to avoid too much code in this question. I can give it if necessary.</p>
  
  <p>Have you any idea about what I did wrong ?</p>
  
  <p>Thanks a lot !</p>
  
  <p>Tom</p>