PDA

View Full Version : rotating texture about center



fodder
06-01-2004, 05:41 PM
I'm making a 2d game that uses textures for sprites. I want to make some turrets rotate, but they rotate about the lower left hand corner of their texture. I need them to rotate about their center which is also the center of the texture.
here is my initialization code:
int Initialize()
{
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER,0);
glOrtho(0.0, 640.0, 0.0, 480.0, -10.0, 10.0);
glLoadIdentity(); // reset projection matrix
glEnable(GL_TEXTURE_2D);
...
and here is my function that draws the texture:

void Draw_Texture(long width, long height, GLuint texture, float x, float y, float layer, float rotate)
{
//transformations are performed in reverse order
glPushMatrix();
glTranslatef(x, y, layer);
glRotatef(rotate, 0, 0, 1);
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(0, 0);
glTexCoord2f(1, 0); glVertex2f(width, 0);
glTexCoord2f(1, 1); glVertex2f(width, height);
glTexCoord2f(0, 1); glVertex2f(0, height);
glEnd();
glPopMatrix();
}
The solution must be simple, but everything I've tried hasn't worked. Any advice would be much appreciated.
,Chris

06-01-2004, 05:45 PM
void Draw_Texture(long width, long height, GLuint texture, float x, float y, float layer, float rotate)
{
//transformations are performed in reverse order
glPushMatrix();
glTranslatef(x, y, layer);
glRotatef(rotate, 0, 0, 1);
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(0, 0);
glTexCoord2f(1, 0); glVertex2f(width, 0);
glTexCoord2f(1, 1); glVertex2f(width, height);
glTexCoord2f(0, 1); glVertex2f(0, height);
glEnd();
glPopMatrix();
}Um, try:


glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(-width/2.0f, -height/2.0f);
glTexCoord2f(1, 0); glVertex2f(width/2.0f, -height/2.0f);
glTexCoord2f(1, 1); glVertex2f(width/2.0f, height/2.0f);
glTexCoord2f(0, 1); glVertex2f(-width/2.0f, height/2.0f);
glEnd();

fodder
06-02-2004, 07:57 AM
Thanks it worked! I knew it was simple.