Texture Rotation with OpenGL

Hello people, I’m having trouble with rotating textures, it’s something that I have never done yet, and I need it badly.

Up to now I was using this function to draw my textures (I was able to make it support textures with multiple subimages and horizontal mirroring via the ‘facing’ parameter), but I want to add it a ‘angle’ parameter that rotates the texture:

void Draw_Texture(unsigned int texture, int x, int y, unsigned char img_w, unsigned char img_h, unsigned int tex_w, unsigned int tex_h, unsigned char img, bool facing)
   glEnable( GL_TEXTURE_2D );       
   // Bind the texture to which subsequent calls refer to
    glBindTexture( GL_TEXTURE_2D, texture );

   glBegin( GL_QUADS );
   // Top-left vertex (corner)
    glTexCoord2d( ((double)((img + 1 - facing) * img_w)) / ((double)tex_w), 0 );
   glVertex3f( x, y, 0 );
   // Bottom-left vertex (corner)
    glTexCoord2d( ((double)((img + facing) * img_w)) / ((double)tex_w), 0 );
   glVertex3f( x + img_w, y, 0 );
    // Bottom-right vertex (corner)
   glTexCoord2d( ((double)((img + facing) * img_w)) / ((double)tex_w), ((double) img_h) / ((double)tex_h) );
    glVertex3f( x + img_w, y + img_h, 0 );
   // Top-right vertex (corner)
    glTexCoord2d( ((double)((img + 1 - facing) * img_w)) / ((double)tex_w), ((double) img_h) / ((double)tex_h) );
   glVertex3f( x, y + img_h, 0 );

I know I have to use the glRotate function, but I do not know where or how, I’ve tried several things and I can get it to rotate, but the texture seems to rotate tracing a curve with center in the origin.

So I guess I have to use some glTranslate too… but I do not know how.

I would like the textures to rotate around an arbitrary point (this is all in 2D that I’m working on) that I would supply to the function.

Please help.

Thanks in advance!

glRotate rotates about the origin, so use glTranslate to put your arbitrary rotation point at the origin, rotate, and then transtale back.

Put all this in the texture matrix stack and the texture will be rotated.

glTranslate(center_x, center_y, 0);
glTranslate(-center_x, -center_y, 0);

Check out the bottom of this page:

You can use the texture matrix stack for texture coordinate rotation much in the same way you use the modelview stack for manipulating geometry. The only real difference is that the resulting texture matrix transforms texture coordinates instead of vertex positions.

You can also use texgen (automatic texture coordinate generation) to achieve pretty much the same thing, only you could think of it as basically working with the individual rows (planes) of the texture matrix (if that makes any sense). Texcoord generation is discussed on the same page above, and is an addition to, not a replacement for, the texture matrix.

In the programmable pipeline, you have complete control over all this, if that’s an option for you.

Hope this helps.

Edit: Bob beat me to it :wink:

Originally posted by Minstrel:
Edit: Bob beat me to it :wink: [/QB]
Funny thing is, it’s like third post in a row now. I post to a thread that’s been untouched for a day or so, and someone else replies the same minute as I do.

Thank you so much for your replies, guys.

I can succesfully draw a rotated texture at any location now. :slight_smile:

However I do have a new problem, I want the texture to rotate on a certain point (done) but also I want a certain point of the texture to be at that ‘rotate-point’. Know what I mean?

Say I have an sprite of an arm, I want it to rotate around the shoulder, which is, say, at (4,4) in the texture.

Thanks in advance!

Correct me if I’m wrong, but you would just adjust where you translate it before rotation.
Something like: center_x - width/2+4, center_y - height/2+4

It’s still too early for me to do math, but that should at least give you a start! :slight_smile:

You might want to have a peek at this:

It’s free, so you have no excuse :wink: