0

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

(x = cos(deg) * x - sin(deg) * yy = 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();
4

2 回答 2

2

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

glRotate(angle,0,0,1);
于 2012-04-28T19:12:31.097 回答
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();

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

于 2012-04-28T22:23:27.160 回答