OpenGL ES glRotatef performing shear instead of rotate?

3.6k views Asked by At

I am able to draw a sprite on the screen of an iPhone, but when I try to rotate it I am getting some weird results. It seems to be stretching the sprite in the y direction more the closer the sprite gets to pointing down the y-axis (90 and 270 degrees). It displays correctly when pointing down the x and -x axes (0 and 180 degrees). It is basically like it is shearing instead of rotating. Here are the essentials of the code (projection matrix is ortho):

glPushMatrix();
    glLoadIdentity();
    glTranslatef( position.x, position.y, -1.0f );      
    glRotatef( rotation, 0.0f, 0.0f, 1.0f );
    glScalef( halfSize.x, halfSize.y, 1.0f );

    vertices[0] = 1.0f;
    vertices[1] = 1.0f;
    vertices[2] = 0.0f;
    vertices[3] = 1.0f;
    vertices[4] = -1.0f;
    vertices[5] = 0.0f;
    vertices[6] = -1.0f;
    vertices[7] = 1.0f;
    vertices[8] = 0.0f;
    vertices[9] = -1.0f;
    vertices[10] = -1.0f;   
    vertices[11] = 0.0f;

    glVertexPointer( 3, GL_FLOAT, 0, vertices );
    glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
glPopMatrix();

Can anybody explain to me how to fix this please?

halfsize is just half the x and y extent of the sprite; removing the glScalef call does not make any difference.

Here is my matrix setup:

glMatrixMode(GL_PROJECTION); 
glLoadIdentity();
glOrthof(0, 320, 480, 0, 0.01, 5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

OK, hopefully this screenshot will demonstrate what's happening:

1

There are 1 answers

3
geofftnz On BEST ANSWER

If you are scaling by the same amount in the x and y directions, then your projection is causing the distortion.

Just a hunch, but maybe try swapping the 320 and 480 in your Ortho projection. (In case the X and Y on the iPhone is swapped)