我有一個用opengl製作的3D立方體,它隨機旋轉並偶爾會出現三次立體顯示。我想讓立方體落在其中一邊(x,-x,y,-y,z,-z)上。我管理它到目前爲止確定多維數據集的頂端 - 要顯示的頂端。 但是,我無法操縱立方體「倒退」的矩陣。旋轉立方體只顯示一面
假設我可以看到立方體的X,Y和Z面,我想旋轉立方體,這樣我只能看到X面。據我所知,爲了實現這一點,我需要繞Y軸和Z軸旋轉立方體。
作爲例子,我希望旋轉以下對y和z軸矩陣:
[0] = 0.90366703 [1] = - 0.4241817 [2] = - 0.058799066 [3] = 0.0 [4] = -0.3704742 [5] = - 0.70550096 [6] = - 0.6041675 [7] = 0.0 [8] = 0.21479362 [9] = 0.56774914 [10] = - 0.7946859 [12] = 0.0 [13] = 0.0 [14] = 0.0 [15] = 1.0
這是如何我試圖定義的角度:
float[] camera_org = new float[3];
GL11 gl11 = (GL11) gl;
gl11.glGetFloatv(GL11.GL_MODELVIEW_MATRIX, mdl);
camera_org[0] = -(mdl.get(0) * mdl.get(12) + mdl.get(1) * mdl.get(13) + mdl.get(2) * mdl.get(14));
camera_org[1] = -(mdl.get(4) * mdl.get(12) + mdl.get(5) * mdl.get(13) + mdl.get(6) * mdl.get(14));
camera_org[2] = -(mdl.get(8) * mdl.get(12) + mdl.get(9) * mdl.get(13) + mdl.get(10) * mdl.get(14));
Log.i("CubeOrientation", camera_org[0] + " " + camera_org[1] + " " + camera_org[2]
+ " "+ 90/6 * camera_org[0] + "° " + 90/6 * camera_org[1] + "° " + 90/6 * camera_org[2] + "°");
float angle_x = camera_org[0] < 0 ? 90/6 * camera_org[0] : -90/6 * camera_org[0];
float angle_y = camera_org[1] < 0 ? 90/6 * camera_org[1] : -90/6 * camera_org[1];
float angle_z = camera_org[2] < 0 ? 90/6 * camera_org[2] : -90/6 * camera_org[2];
angle_x = angle_x < 0 ? angle_x + 90 : angle_x - 90;
angle_y = angle_y < 0 ? angle_y + 90 : angle_y - 90;
angle_z = angle_z < 0 ? angle_z + 90 : angle_z - 90;
這是如何我試圖使計算:
float x1 = matrix[0];
float y1 = matrix[1];
float z1 = matrix[2];
float x2 = matrix[4];
float y2 = matrix[5];
float z2 = matrix[6];
float x3 = matrix[8];
float y3 = matrix[9];
float z3 = matrix[10];
float[] xz1 = rotateY(angle_y, x1, z1);
float[] xz2 = rotateY(angle_y, x2, z2);
float[] xz3 = rotateY(angle_y, x3, z3);
matrix[0] = xz1[0]; // x
x1 = xz1[0];
matrix[2] = xz1[1]; // z
matrix[4] = xz2[0]; // x
x2 = xz2[0];
matrix[6] = xz2[1]; // z
matrix[8] = xz3[0]; // x
x3 = xz3[0];
matrix[10] = xz3[1]; // z
float[] xy1 = rotateZ(angle_z, x1, y1);
float[] xy2 = rotateZ(angle_z, x2, y2);
float[] xy3 = rotateZ(angle_z, x3, y3);
matrix[0] = xy1[0]; // x
matrix[1] = xy1[1]; // y
matrix[4] = xy2[0]; // x
matrix[5] = xy2[1]; // y
matrix[8] = xy3[0]; // x
matrix[9] = xy3[1]; // y
這是我正在努力計算旋轉:
/**
* Rotate X.
*
* @param angle_x
* @param y
* @param z
* @return [0] = y, [1] = z
*/
private float[] rotateX(float angle_x, float y, float z)
{
float[] res = new float[2];
res[0] = (float) (y * Math.cos(angle_x) - z * Math.sin(angle_x));
res[1] = (float) (y * Math.sin(angle_x) + z * Math.cos(angle_x));
return res;
}
/**
* Rotate Y.
*
* @param angle_y
* @param x
* @param z
* @return [0] = x, [1] = z
*/
private float[] rotateY(float angle_y, float x, float z)
{
float[] res = new float[2];
res[0] = (float) (x * Math.cos(angle_y) + z * Math.sin(angle_y));
res[1] = (float) (-x * Math.sin(angle_y) + z * Math.cos(angle_y));
return res;
}
/**
* Rotate Z.
*
* @param angle_z
* @param x
* @param y
* @return [0] = x, [1] = y
*/
private float[] rotateZ(float angle_z, float x, float y)
{
float[] res = new float[2];
res[0] = (float) (x * Math.cos(angle_z) - y * Math.sin(angle_z));
res[1] = (float) (y * Math.cos(angle_z) + x * Math.sin(angle_z));
return res;
}
有沒有人做過類似的東西有時或能幫助我嗎?
非常感謝!