PDA

View Full Version : Rotating a Texture on a Quad



ramona
09-17-2008, 07:58 AM
Hi Please help if you know the answer to this: I'm trying to put a texture image on a quad, then constantly rotating it around the z axis (say a smiley face on a square). But somehow it keeps tiling the image or distorting it.

I've bound the texture to a square in a function called drawTexRect() using glTexCoord2f and glVertex3f with every corner of the square being a bit inside the texture i.e., going from 0.2 to 0.8 , I've experimented with other mappings too.
and then:

float angle = 0.1,
glBindTexture(GL_TEXTURE_2D, tex);
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glRotatef(angle, 0.0, 0.0, 1.0);
glMatrixMode(GL_MODEL_VIEW);
drawTexRect();

then the angle become angle+= 0.1 in my animateFunction so that the image keeps rotating. I've even tried
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_CLAMP); but the image kept moving out of the quad.

Many thanks

trinitrotoluene
09-17-2008, 08:57 AM
To rotate the texture around it's center you have to
1)translate to make the origin the center of the texture.
2)rotate by an angle.
3)translate it back (inverse transform of 1)



glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glTranslatef(0.5,0.5,0.0);
glRotatef(angle,0.0,0.0,1.0);
glTranslatef(-0.5,-0.5,0.0);
glMatrixMode(GL_MODELVIEW);


I tested this on a quad and it work fine.

ramona
09-18-2008, 09:40 AM
Thanks ever so much. I shall carry out your suggestion when I get back to my computer.

ramona
09-19-2008, 04:03 AM
It worked perfectly. Thank you!