我试图只旋转我在Java中创建的场景中的两个对象中的一个
LWJGL。
现在,当我尝试旋转一个物体时,它不会围绕它的中心旋转,而是以大圆圈旋转。在Google上搜索后,我现在知道它应该像这样工作:
glPushMatrix();
glBegin();
//Code in here
glEnd();
glRotate();
glPopMatrix();
但它不适合我。物体不旋转。这是我的渲染代码:
public void render() {
glPushMatrix();
glRotatef(rotate, 0, 1.0f, 0);
glBegin(GL_QUADS);
// Top
glVertex3f(-centerX + size, centerY + size, -centerZ - size);
glVertex3f(-centerX - size, centerY + size, -centerZ - size);
glVertex3f(-centerX - size, centerY + size, -centerZ + size);
glVertex3f(-centerX + size, centerY + size, -centerZ + size);
// Bottom
glVertex3f(-centerX + size, centerY - size, -centerZ + size);
glVertex3f(-centerX - size, centerY - size, -centerZ + size);
glVertex3f(-centerX - size, centerY - size, -centerZ - size);
glVertex3f(-centerX + size, centerY - size, -centerZ - size);
// One side
glVertex3f(-centerX + size, centerY + size, -centerZ + size);
glVertex3f(-centerX - size, centerY + size, -centerZ + size);
glVertex3f(-centerX - size, centerY - size, -centerZ + size);
glVertex3f(-centerX + size, centerY - size, -centerZ + size);
// Moar sides
glVertex3f(-centerX + size, centerY - size, -centerZ - size);
glVertex3f(-centerX - size, centerY - size, -centerZ - size);
glVertex3f(-centerX - size, centerY + size, -centerZ - size);
glVertex3f(-centerX + size, centerY + size, -centerZ - size);
// Last side
glVertex3f(-centerX - size, centerY + size, -centerZ + size);
glVertex3f(-centerX - size, centerY + size, -centerZ - size);
glVertex3f(-centerX - size, centerY - size, -centerZ - size);
glVertex3f(-centerX - size, centerY - size, -centerZ + size);
// Real last side
glVertex3f(-centerX + size, centerY + size, -centerZ - size);
glVertex3f(-centerX + size, centerY + size, -centerZ + size);
glVertex3f(-centerX + size, centerY - size, -centerZ + size);
glVertex3f(-centerX + size, centerY - size, -centerZ - size);
glEnd();
glPopMatrix();
rotate++;
}
我现在尝试将此问题解决了几个小时,但无法使其正常工作。
答案 0 :(得分:1)
在绘制四边形之前,您应该致电glRotate()
和glTranslate()
。
请记住,转换的顺序很重要。旋转始终约为0,0,0
,因此如果您首先进行平移,它将围绕圆中的某个点进行“轨道”排序。如果你想让它围绕它自己的轴旋转(你可能会这样做)先旋转,然后翻译。
本文中有一节介绍转换的顺序。