gpt4 book ai didi

java - LWJGL 只旋转一个物体

转载 作者:行者123 更新时间:2023-11-30 11:34:37 25 4
gpt4 key购买 nike



我正在尝试仅旋转我在 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++;
}

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

最佳答案

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

请记住,转换顺序很重要。旋转始终大约为 0,0,0,因此如果您先平移,它会围绕圆中的一个点“旋转”。如果您希望它围绕自己的轴旋转(您可能会这样做)先旋转,然后平移。

这篇文章中有一节是关于转换顺序的。

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

关于java - LWJGL 只旋转一个物体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15559864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com