2



我正在尝试仅旋转我使用 LWJGL 在 Java 中创建的场景中的两个对象之一。
现在,当我尝试旋转一个对象时,它不会围绕它的中心旋转,而是以大圆圈旋转。在谷歌上搜索后,我现在知道它应该像这样工作:

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++;
}

我现在尝试解决这个问题几个小时,但无法让它工作。

4

1 回答 1

1

您应该在绘制四边形之前调用glRotate()and 。glTranslate()

请记住,转换的顺序很重要。旋转总是大约0,0,0,所以如果你先平移,它会绕着圆中的一个点“环绕”。如果您希望它围绕自己的轴旋转(您可能会这样做)首先旋转,然后平移。

本文中有一节是关于转换顺序的。

http://www.glprogramming.com/red/chapter03.html

于 2013-03-21T23:09:57.517 回答