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旋轉。JOGL代碼爲什麼不能正確翻譯?
我也嘗試這對於定心代碼:
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)之後的第一個更有意義。 任何想法?
我不知道我的解決方案,你得到期望的結果(我不能很好地理解你的要求),但你應該嘗試,在第一源沒有後來的修改,將呼叫交換到glTranslate和調用glRotate。因爲在OpenGL中,轉換順序確實很重要。希望這會幫助你。而且)(一般情況下,你必須在期望tranformations相反的順序代碼glRotate,glTranslate,glScale的轉換。),以保存/恢復轉換,看看glPushMatrix()和glPopMatrix(。 – loloof64