Search code examples
pythonopenglrotationtexturesprimitive

Rotate a quad around a centre point in OpenGL


I'm making a 2D game. I want to be able to render a texture on the screen after rotating it a certain amount around a centre point. Basically this is for a level rotation around a player. The player position being the rotation point and the direction of the player as the angle. This code wont work:

def draw_texture(texture,offset,size,a,rounded,rotation,point):
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity() #Loads model matrix
    glColor4f(1,1,1,float(a)/255.0)
    glTranslatef(point[0],point[1],0)
    glRotatef(rotation,0,0,1)
    glBindTexture(GL_TEXTURE_2D, texture)
    if rounded == 0:
        glBegin(GL_QUADS)
        glTexCoord2f(0.0, 0.0)
        glVertex2i(*offset) #Top Left
        glTexCoord2f(0.0, 1.0)
        glVertex2i(offset[0],offset[1] + size[1]) #Bottom Left
        glTexCoord2f(1.0, 1.0)
        glVertex2i(offset[0] + size[0],offset[1] + size[1]) #Bottom, Right
        glTexCoord2f(1.0, 0.0)
        glVertex2i(offset[0] + size[0],offset[1]) #Top, Right
        glEnd()
    else:
        #Nothing important here
    glEnd()

Any way to get it working? Thank you.


Solution

  • try reversing

    glTranslatef(point[0],point[1],0)
    

    and

    glRotatef(rotation,0,0,1)
    

    you're translating to the player, but then rotating about the origin (not the player)

    Illustration from the red book: rotate