Java 为什么不是';这个JOGL代码翻译不正确吗?
我不明白为什么它不是绕Z轴旋转,而是绕立方体对象本身旋转,而是在矩阵中绕0,0,0旋转 我还尝试了以下对中代码:Java 为什么不是';这个JOGL代码翻译不正确吗?,java,opengl,transformation,jogl,Java,Opengl,Transformation,Jogl,我不明白为什么它不是绕Z轴旋转,而是绕立方体对象本身旋转,而是在矩阵中绕0,0,0旋转 我还尝试了以下对中代码: public Cube(int x, int y, int z, int x2, int y2, int z2){ int tmpX = x; int tmpY = y; int tmpZ = z; if(x > x2){x = x2; x2 = tmpX;} if(y > y2){y = y2; y2 = tmpY;}
public Cube(int x, int y, int z, int x2, int y2, int z2){
int tmpX = x;
int tmpY = y;
int tmpZ = z;
if(x > x2){x = x2; x2 = tmpX;}
if(y > y2){y = y2; y2 = tmpY;}
if(z > z2){z = z2; z2 = tmpZ;}
int centerX = x2 - x;
int centerY = y2 - y;
int centerZ = z2 - z;
GL11.glTranslatef(centerX, centerY, centerZ);
GL11.glRotatef(rot, 0f, 0f, 1f);
GL11.glBegin(GL11.GL_QUADS);
//front
color(255, 0, 0);
v3d(x, y, z);
v3d(x, y2, z);
v3d(x2, y2, z);
v3d(x2, y, z);
//top
color(0, 255, 0);
v3d(x, y, z);
v3d(x2, y, z);
v3d(x2, y, z2);
v3d(x, y, z2);
//back
color(0, 0, 255);
v3d(x2, y2, z2);
v3d(x, y2, z2);
v3d(x, y, z2);
v3d(x2, y, z2);
//bottom
color(255, 255, 0);
v3d(x, y2, z);
v3d(x2, y2, z);
v3d(x2, y2, z2);
v3d(x, y2, z2);
//left
color(255, 0, 255);
v3d(x, y, z);
v3d(x, y2, z);
v3d(x, y2, z2);
v3d(x, y, z2);
//right
color(0, 255, 255);
v3d(x2, y, z);
v3d(x2, y2, z);
v3d(x2, y2, z2);
v3d(x2, y, z2);
GL11.glEnd();
GL11.glRotatef(-rot, 0f, 0f, 1f);
GL11.glTranslatef(-centerX, -centerY, -centerZ);
rot += 0.75f;
if(rot > 360){
rot -= 360;
}
}
但在测试了一些数学(15-5=10,15+7.5=12.5)后,第一个更合理。
有什么想法吗?我认为您可能正试图将数学变换的学习过于直接地转移到OpenGL中。从字面上解释你写的东西,你从原点开始平移,应用旋转,然后尝试以相反的顺序向底部撤销这两个。看起来您正试图“撤消”这些转换,以便稍后进行其他转换 当我们从数学上讨论转换时,这是有意义的,但是在代码中,我们在
glPushMatrix()
和glPopMatrix()
中有一个很好的快捷方式,在这里,您可以使用这两个快捷方式在特定范围内“包装”转换。它与函数作用域或循环作用域非常相似——在该块中发生的任何转换都不会影响该作用域之外的任何内容
好的:
int xWidth = x2 - x;
int yWidth = y2 - y;
int zWidth = z2 - z;
int centerX = x + (xWidth / 2);
int centerY = y + (yWidth / 2);
int centerZ = z + (zWidth / 2);
GL11.glTranslatef(centerX, centerY, centerZ);
GL11.glRotatef(rot, 0f, 0f, 1f);
这会将变换应用于centerX、centerY和centerZ,然后应用围绕z轴的旋转
坏的:
int xWidth = x2 - x;
int yWidth = y2 - y;
int zWidth = z2 - z;
int centerX = x + (xWidth / 2);
int centerY = y + (yWidth / 2);
int centerZ = z + (zWidth / 2);
GL11.glTranslatef(centerX, centerY, centerZ);
GL11.glRotatef(rot, 0f, 0f, 1f);
此处的glRotatef
和gltranslateef
是不必要的,可以注释掉或删除
以下是使用代码定义单个“对象”范围的方式:
GL11.glRotatef(-rot, 0f, 0f, 1f);
GL11.glTranslatef(-centerX, -centerY, -centerZ);
因此,如果希望包含其他不受立方体变换和旋转影响的对象,可以将这些变换包装在推/弹出块中。这里还有一个关于推送和弹出的方法,比我这里的方法要好一点。我不知道使用我的解决方案,您是否能得到期望的结果(我还不太了解您的请求),但您应该尝试在第一个源代码中,在不进行后续修改的情况下,交换对glTranslate的调用和对glRotate的调用。因为在OpenGL中,转换顺序确实很重要。希望这对你有帮助。(通常,必须按照所需转换的相反顺序使用glRotate、glTranslate、glScale对转换进行编码。)此外,为了保存/恢复转换,请查看glPushMatrix()和glPopMatrix()。