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;
}
}
我不明白为什么这不是围绕立方体对象本身围绕 Z 轴旋转,而是它似乎只是在矩阵中围绕 0,0,0 旋转。
我也试过这个居中代码:
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);
但是在测试了一些数学之后,第一个更有意义(15 - 5 = 10, 15 + 7.5 = 12.5)。有任何想法吗?