# How can rotate a 2D textured quad in legacy OpenGL immediate mode?

I have a texture of a tank, and I want to it appearance on the screen depends on its current direction, so I decided to use rotating texture functions of OpenGL. I followed some advices through Google and received unsuccessful results. Anyone can help me with this problem?

This is my render() function:

#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3

void GameObject::render()
{
glBindTexture(GL_TEXTURE_2D, textureID);
glTranslatef((GLfloat)coord.getX(), (GLfloat)coord.getY(), 0.0f);
}

• rotating the texture. Is that what are you trying to achieve ?? Sep 25 '14 at 11:17
• Do you know what that glTranslate you're calling does? There's also a glRotate...
– Anko
Sep 25 '14 at 11:57
• @concept3d: Yes, that's what I want! But I don't know how to do. Sep 25 '14 at 12:12
• @Anko: I know there it is, I followed tutorials and advices on Internet but they can't help... I'm a beginner, I have some trouble while learning OpenGL so the most important target to me right now is how to solve this, I will try to understand that later. Sep 25 '14 at 12:13
• try this: glRotatef((GLfloat)30.0f,0.0f,0.0f,1.0f); and it will rotate your texture 30 degrees in clockwise direction. Sep 25 '14 at 12:34

glRotatef(GLfloat angle, Glfloat x, GLfloat y, GLfloat z);

The Above function is what you need for your purpose. It " multiplies the current matrix by a rotation matrix", as the OpenGL documentation says. Here's how you use it-

angle is the angle you want to rotate. (Duh.) The next three parameters define the axis around which you would like to rotate. You input the x, y, and z components of the unit vector pointing in the direction of your desired axis of rotation. For example, if we wanted to rotate in the x axis, our vector would be [1.0f,0.0f,0.0f]. You can choose any arbitrary axis you want here. For our case, since we are doing 2D, we want to rotate around the z-axis, so we set x = 0.0f, y = 0.0f and z = 1.0f.

And as @concept3d mentioned, you need to specify that you want to make this rotation apply to the texture matrix, and he gives a way of tell OpenGL that.

Note that after setting the rotation with glRotatef all things renderd after that will be rotated by that angle you specified. Be sure to set this for each object, if different rotation are need.

Hope that helps. Happy rotating!

• I did it, thanks so much! Sorry because I don't have enough reputation to vote up for you. Have a nice day. Sep 26 '14 at 5:39

As I understand from your question is that you to rotate the actual textures and not the polygon vertices. This can be done by modifying the Texture Matrix not the ModelView Matrix.(you're using fixed pipeline).

In order to modify the texture matrix that actually transforms the UVs you need to enable it first.

glMatrixMode(GL_TEXTURE);