1
\$\begingroup\$

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);
    glLoadIdentity();
    glTranslatef((GLfloat)coord.getX(), (GLfloat)coord.getY(), 0.0f);
    glDrawArrays(GL_QUADS, 0, 4);
}
\$\endgroup\$
  • \$\begingroup\$ rotating the texture. Is that what are you trying to achieve ?? \$\endgroup\$ – concept3d Sep 25 '14 at 11:17
  • \$\begingroup\$ Do you know what that glTranslate you're calling does? There's also a glRotate... \$\endgroup\$ – Anko Sep 25 '14 at 11:57
  • \$\begingroup\$ @concept3d: Yes, that's what I want! But I don't know how to do. \$\endgroup\$ – Giang TT Sep 25 '14 at 12:12
  • \$\begingroup\$ @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. \$\endgroup\$ – Giang TT Sep 25 '14 at 12:13
  • \$\begingroup\$ try this: glRotatef((GLfloat)30.0f,0.0f,0.0f,1.0f); and it will rotate your texture 30 degrees in clockwise direction. \$\endgroup\$ – The Light Spark Sep 25 '14 at 12:34
4
\$\begingroup\$

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!

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

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);
glLoadIdentity();
glRotatef(50.0, 0,0,1);

glMatrixMode(GL_MODEL_VIEW);
\$\endgroup\$
  • 2
    \$\begingroup\$ This answer is technically correct, but IME when people new to OpenGL say "rotate a texture" they do really mean "rotate the quad on which the texture is mapped" because the distinction between the two is not firm in their mind yet. \$\endgroup\$ – Josh Sep 25 '14 at 16:04
  • \$\begingroup\$ Thanks @JoshPetrie for the clarification. Honestly I wasn't sure that's why I used "As I understand from your question is that you to rotate the actual textures and not the polygon vertices." And thought the OP would clarify. \$\endgroup\$ – concept3d Sep 25 '14 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.