如何在OpenGL中旋转纹理?

时间:2011-11-09 09:56:11

标签: android opengl-es

我是Android OpenGL的新手。我能够在圆形路径中旋转纹理,但问题是当我旋转图像时是倾斜的。这样图像就不合适了。

3 个答案:

答案 0 :(得分:7)

如果您希望图像旋转,则需要将其转换为原点,在那里进行旋转,然后将其转换回原来的位置。

如果您希望图像在圆周上移动,可以尝试将其转换为原点,沿相反方向旋转,将其平移,然后执行您已经在进行的旋转。

答案 1 :(得分:3)

有两种方法可以弥补这一点:

  • 补偿由轮毂周围的旋转引起的四边形的旋转

  • 使用四边形旋转纹理坐标空间。

解决方案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();
}

解决方案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();
}

答案 2 :(得分:0)

处理此问题的另一种方法是更改​​顶点和文本坐标的顺序

原文:

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();

将图像顺时针旋转90度:

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();