简体   繁体   中英

How to rotate texture in OpenGL?

I am new to android OpenGL. I am able to rotate the texture in a circular path but the problem is when i am rotating the image is tilted. So that the image is not proper.

If you want your image to rotate, you need to translate it to the origin, do your rotation there, and then translate it back to where it originally was.

If you want your image to move around the circle, you could try translating it to the origin, doing the rotation in the opposite direction, translating it back, and then performing the rotation you're already doing.

There are two ways to compensate this:

  • compensate the rotation of the quad, caused by the revolution around the hub

or

  • rotate the texture coordinate space with the quad.

Solution 1:

/* This draws a textured quad at the origin; use the modelview to position it */
void draw_textured_quad(void);

void draw_dial(float quad_angular_position)
{
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();

    glRotatef(-angular_position, 0, 0, 1); /* counterrotate the quad */
    glTranslatef(dial_radius, 0, 0); /* move to the dial */
    glRotatef(angular_position, 0, 0, 1); /* revolve around the dial */
    draw_textured_quad();

    glPopMatrix();
}

Solution 2:

/* This draws a textured quad at the origin; use the modelview to position it */
void draw_textured_quad(void);

void draw_dial(float quad_angular_position)
{
    glMatrixMode(GL_TEXTURE);
    glPushMatrix();
    glRotatef(angular_position, 0, 0, 1); /* rotate the texture_space _with_ the quad */

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glTranslatef(dial_radius, 0, 0); /* move to the dial */
    glRotatef(angular_position, 0, 0, 1); /* revolve around the dial */
    draw_textured_quad();

    glMatrixMode(GL_TEXTURE);
    glPopMatrix();

    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
}

Another way of dealing with this problem would be to change the sequence of vertex and text-coordinates

Original:

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y, z);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, y, z + length);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x + width, y + height, z + length);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, y + height, z);
glEnd();

Rotating the image clockwise by 90 deg:

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y + height, z + length);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z + length);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y, z);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y + height, z);
glEnd();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM