使用旋转矩阵旋转纹理2d

Rotate Texture2d using rotation matrix

本文关键字:旋转 纹理 2d      更新时间:2023-10-16

我想在 cocos2d-x(opengl es) 中旋转 2d 纹理,因为我搜索我应该使用旋转矩阵如:

(x = cos(deg) * x - sin(deg) * y y = sin(deg) *

x + cos(deg) * y)

但是当我想实现这个公式时,我失败了,代码可能是这样的(我希望原始的 x 和 y 相同):我将代码更新为此,但仍然无法正常工作!

     GLfloat    coordinates[] = {
        0.0f,   text->getMaxS(),
        text->getMaxS(),text->getMaxT(),
        0.0f,   0.0f,
        text->getMaxS(),0.0f };
     glMatrixMode(GL_MODELVIEW);
    glPushMatrix();

    GLfloat vertices[] = {  rect.origin.x,      rect.origin.y,                          1.0f,
                            rect.origin.x + rect.size.width,        rect.origin.y,                          1.0f,
                            rect.origin.x,                          rect.origin.y + rect.size.height,       1.0f,
                            rect.origin.x + rect.size.width,        rect.origin.y + rect.size.height,       1.0f };
    glMultMatrixf(vertices);
    glTranslatef(p1.x, p1.y, 0.0f);
    glRotatef(a,0,0,1);
    glBindTexture(GL_TEXTURE_2D, text->getName());
    glVertexPointer(3, GL_FLOAT, 0, vertices);
    glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
    glColor4f( 0, 0, 250, 1);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glRotatef(-a, 0.0f, 0.0f, -1.0f);
    glPopMatrix();

如果您在二维中使用 OpenGL,您仍然可以使用各种转换函数。 对于glRotate(),您必须向其传递 (0,0,1) 轴:

glRotate(angle,0,0,1);

你似乎对OpenGL的几个方面感到困惑。目前你有这个:

glMultMatrixf(vertices);
glTranslatef(p1.x, p1.y, 0.0f);
glRotatef(a,0,0,1);
glBindTexture(GL_TEXTURE_2D, text->getName());
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
glColor4f( 0, 0, 250, 1);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glRotatef(-a, 0.0f, 0.0f, -1.0f);
glPopMatrix();

这表面上是:

// reinterpret your vertices as a matrix; multiply the
// current matrix by the one formed from your vertices
glMultMatrixf(vertices);
// rotate by a degrees, then translate to p1
glTranslatef(p1.x, p1.y, 0.0f);
glRotatef(a,0,0,1);
// bind the relevant texture, supply the vertices
// as vertices this time, supply texture coordinates
glBindTexture(GL_TEXTURE_2D, text->getName());
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
// set a colour of (0, 0, 1, 1) — probably you
// wanted glColor4ub to set a colour of (0, 0, 250/255, 1)?
glColor4f( 0, 0, 250, 1);
// draw some geometry
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// perform the same rotation as before a second time;
// giving either a and -1, or -a and 1 would have been
// the opposite rotation
glRotatef(-a, 0.0f, 0.0f, -1.0f);
// remove the current matrix from the stack
glPopMatrix();

您可能想要的是:

// push the current matrix, so that the following
// transformations can be undone with a pop
glPushMatrix();
// rotate by a degrees, then translate to p1
glTranslatef(p1.x, p1.y, 0.0f);
glRotatef(a,0,0,1);
// bind the relevant texture, supply the vertices
// as vertices this time, supply texture coordinates
glBindTexture(GL_TEXTURE_2D, text->getName());
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
// set a colour of (0, 0, 250/255, 1)
glColor4ub( 0, 0, 250, 1);
// draw some geometry
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// remove the current matrix from the stack
glPopMatrix();

当前变换矩阵应用于所有几何图形。您不会标记它们应用于哪些几何图形,哪些不应用于几何图形。推送和弹出应该配对,并且是影响转换的方式,而不是在您想要时受到先前转换的影响。因此,您不需要手动执行反向旋转,并且在执行所有更改矩阵的操作之前需要推动。出于上述原因,我也将您切换到glColor4ub - 您似乎使用无符号字节来推送颜色,但 OpenGL 使用从 0.0 到 1.0 的范围来表示颜色。 glColor4ub将自动从前者映射到后者。