Search code examples
javaopengltextureslwjglslick2d

lwjgl and slick - rotate slick texture


I am creating a program which draws an object, which needs to "point" toward the mouse. To do so I want to draw the image rotated.

I have this code for drawing the object:

glBindTexture(GL_TEXTURE_2D,img.getTextureID());
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(x-r,y-r);
glTexCoord2f(1,0);
glVertex2f(x+r,y-r);
glTexCoord2f(1,1);
glVertex2f(x+r,y+r);
glTexCoord2f(0,1);
glVertex2f(x-r,y+r);
glEnd();

Pretty basic. But I was having a hard time finding out how to rotate the texture, or draw the texture rotated.


Solution

  • To elaborate on @genpfault's answer:

    If you want your texture to rotate around its center, you need to draw it so its center is at (0,0). Try something like this:

    glPushMatrix();
    glTranslatef(x,y); // move to the proper position
    glRotatef( angle, 0, 0, 1 ); // now rotate
    
    glBindTexture(GL_TEXTURE_2D,img.getTextureID());
    glBegin(GL_QUADS);
    glTexCoord2f(0,0);
    glVertex2f(-r,-r);
    glTexCoord2f(1,0);
    glVertex2f(+r,-r);
    glTexCoord2f(1,1);
    glVertex2f(+r,+r);
    glTexCoord2f(0,1);
    glVertex2f(-r,+r);
    glEnd();
    
    glPopMatrix(); // pop off the rotation and transformation
    

    Note that glRotatef's angle is in degrees, not radians.