views:

125

answers:

1

Hi everyone !

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
I am trying to model a pyramid using OpenGL ES 2.0 on the iPhone.
I would like to allow the user to rotate this pyramid using the touch screen, so he can view every face of the shape.
I am using my own matrix utilities, inspired by the library given in the book OpenGL ES 2.0 Programming Guide.
I have a simple vertex shader that takes 2 uniforms and 2 attributes.
Here is my vertex shader :

uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
attribute vec4 vertex;
attribute vec4 color;
varying vec4 colorVarying;

void main()
{
 colorVarying = color;
 gl_Position = projectionMatrix * modelViewMatrix * vertex;
}

So, during my rendering, I create 2 matrices : the modelView and the projection matrix.

Here is how my projection matrix is initialized :

[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];

My pyramid vertices are set as follow :

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

 };

Finally, here is my drawing 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];

Everything seems to work fine, my pyramid is here, translated backward on the Z axis.
The problem shows up when I start to rotate the shape, it looks like it is rotating around the user, not around itself.
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.

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.

Have you any idea about what I did wrong ?

Thanks a lot !

Tom

+1  A: 

It sounds like the problem is in the matrix management code - specifically, that the code to update mvMatrix (the modelViewMatrix) is actually altering projectionMatrix instead.

Chris Peredun
Indeed! The problem was precisely into my matrix multiplication code, that was giving me the same result not depending on the order of the operations.
super_tomtom