在OpenGL中旋转三角形

Ara*_*yan 2 opengl rotation

我正试图围绕它的中心点旋转一个三角形.我知道OpenGL围绕原点旋转所以我需要将中间点转换为原点,然后旋转并转换回来.我已经注释掉了最后一行,以确保它至少围绕原点的中心旋转.它不是.尽管有翻译,它似乎是围绕它的旧起源旋转...注意ccc4和ccp生成浮点数.这是我的代码:

ccColor4B colors[] = {
    ccc4(255, 0, 0, 255),
    ccc4(0, 255, 0, 255),
    ccc4(0, 0, 255, 255)
};

CGPoint vertices[] = {
    ccp(0,0),
    ccp(50,100),
    ccp(100,0),
};

CGPoint middle[] = {ccp(50,50)};
CGPoint origin[] = {ccp(0,0)};

// Rotate the triangle
glPushMatrix();
glTranslatef(-50, -50, 0);
glRotatef(45, 0, 0, 1.0);
// glTranslatef(50, 50, 0);

// Draw the triangle
glLineWidth(2);
glVertexPointer(2, GL_FLOAT, 0, vertices);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
glColor4ub(0, 0, 255, 255);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);

// Revert rotation, we only want triangle to rotate 
glPopMatrix();

// Draw the points
glDisableClientState(GL_COLOR_ARRAY);

glPointSize(5);
glColor4ub(255, 255, 255, 255);
glVertexPointer(2, GL_FLOAT, 0, middle);
glDrawArrays(GL_POINTS, 0, 1);

glPointSize(5);
glColor4ub(0, 255, 0, 255);
glVertexPointer(2, GL_FLOAT, 0, origin);
glDrawArrays(GL_POINTS, 0, 1);

glEnableClientState(GL_COLOR_ARRAY);
// End points
Run Code Online (Sandbox Code Playgroud)

这是输出:

1 2

Mar*_*tos 6

您需要将变换视为相对于您调用它们的顺序反向应用.

实际上,在转换局部坐标系(LCS)方面更容易思考,而不是对象,这允许您按照它们被调用的顺序在心理上应用变换.要围绕中心旋转,请将LCS平移到中心,旋转,然后再将其翻译出来:

glTranslatef(50, 50, 0);
glRotatef(45, 0, 0, 1);
glTranslatef(-50, -50, 0);
Run Code Online (Sandbox Code Playgroud)