views:

29

answers:

1

Hi there!

I'm trying to rotate a 2D image using OGL ES. After load it I can move it through the screen but when trying to rotate the image through its center, it has an odd behavior as the rotation center is the lower-left screen corner, not the center of the image itself.

Googling around I've read that I could push the current matrix, change whatever I need (translate the coords, rotate the image, etc) and then pop the matrix coming back to the previous matrix status... I did it but still not working as I'm looking for (but at least now seems that the original coords where it does the rotation are not the lower-left corner...)

Any thoughts? Anyone could spot where my problem is?

Any help would be much appreciated! Thanks!!

void drawImage(Image *img)
{

   GLfloat fX = (GLfloat)img->x;

   GLfloat fY = (GLfloat)(flipY(img->m_height+img->y));

   GLfloat   coordinates[] = { 0, img->m_textureHeight, img->m_textureWidth, img->m_textureHeight, 0, 0, img->m_textureWidth, 0 };
   GLfloat      vertices[] = 
   { 
      fX, fY, 0.0,
      img->m_width+fX, fY, 0.0,
      fX, img->m_height+fY, 0.0,
      img->m_width+fX, img->m_height+fY, 0.0 
   };   

   //Push and change de matrix, translate coords, rotate and scale image and then pop the matrix
   glPushMatrix(); //push texture matrix
   glTranslatef((int)fX, (int)fY, 0.0);  //translate texture matrix

   // rotate
   if (img->rotation != 0.0f )
      glRotatef( -img->rotation, 0.0f, 0.0f, 1.0f );

   // scale
   if (img->scaleX != 1.0f || img->scaleY != 1.0f)
      glScalef( img->scaleX, img->scaleY, 1.0f );

   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 

   glEnableClientState(GL_TEXTURE_COORD_ARRAY);

   glColor4f(1.0, 0.0, 0.0, 1.0);
   glBindTexture(GL_TEXTURE_2D, img->m_name);
   glVertexPointer(3, GL_FLOAT, 0, vertices);
   glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
   glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

   glDisableClientState(GL_TEXTURE_COORD_ARRAY);

   glPopMatrix();
}
A: 

Most importantly, you need to understand how to do this operation.

before doing a rotation you have to translate your self in the rotation origin and only then apply to rotation.

Check out this article which explains it well.

The simple breakdown is:

  • move object to origin.
  • Rotate.
  • Move object back.
JoshD
I'm trying to do that but something is not working fine. To my previous code I've added glTranslatef(-(int)fX, -(int)fY, 0.0); to come back to the original position after do all the changes, but still not working. I'm going to read the link you provide me. Thanks!
Maruku