【发布时间】:2012-02-02 10:44:51
【问题描述】:
我正在尝试围绕它的中心点旋转一个三角形。我知道 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
这是输出:
【问题讨论】:
-
我注意到我的三角形甚至不是等边的。我一直得到 sqrt(3)/6 作为等边的中心点......
-
你会碰巧有证明的链接吗?
-
@AramKocharyan:对不起,我给了你一个流浪汉。那是三角形的高度,而不是中心。根据 Vyktor 的评论,该中心在 y 轴上仅为 1/3。