2013-08-17 24 views
0

我暴露了我的問題! 我旋轉立方體重新計算每個頂點在每個旋轉的個別座標,我必須說 我得到了很好的結果。如果立方體沿一個單軸旋轉一切都是完美的,但是, 沿着兩個或三個軸的頂點旋轉範圍變寬,導致立方體在一點時間後具有平流層大小。 下面是代碼旋轉,後面我認爲是隱藏 的問題。 旋轉矩陣 的相應行的單個頂點的分量的乘法使得頂點從其軌跡分離,但我不明白爲什麼。通過重新修剪每個頂點旋轉立方體

//for each cube 
    for(contatoreOnDraw=0;contatoreOnDraw<numberOfCube;contatoreOnDraw++) 
    { 

     x=contatoreOnDraw*3; 
     y=(contatoreOnDraw*3)+1; 
     z=(contatoreOnDraw*3)+2; 
     gl.glPushMatrix(); 
     gl.glTranslatef(translation[row][x], translation[row][y], translation[row][z]); 
     float angle=2; 


     //Rotation matrix 3x3 
     c =(float) Math.cos(angle*(Math.PI/180)); 
     s =(float) Math.sin(angle*(Math.PI/180)); 
     rotation[0][0] = (rX*rX*(1-c)) + c; 
     rotation[0][1] = (rX*rY*(1-c))-rZ*s; 
     rotation[0][2] = (rX*rZ*(1-c))+rY*s; 
     rotation[1][0] = (rY*rX*(1-c))+rZ*s; 
     rotation[1][1] = (rY*rY*(1-c)) + c; 
     rotation[1][2] = (rY*rZ*(1-c))-rX*s; 
     rotation[2][0] = (rX*rZ*(1-c))-rY*s; 
     rotation[2][1] = (rY*rZ*(1-c))+rX*s; 
     rotation[2][2] = (rZ*rZ*(1-c)) + c; 

     //Updating each vertex component 
     for(int i=0;i<70;i=i+3) 
     { 
      vX_tmp=(rotation[0][0]*cubes[contatoreOnDraw].getVertices(i))+(rotation[0][1]*cubes[contatoreOnDraw].getVertices(i+1))+(rotation[0][2]*cubes[contatoreOnDraw].getVertices(i+2)); 
      vY_tmp=(rotation[1][0]*cubes[contatoreOnDraw].getVertices(i))+(rotation[1][1]*cubes[contatoreOnDraw].getVertices(i+1))+(rotation[1][2]*cubes[contatoreOnDraw].getVertices(i+2)); 
      vZ_tmp=(rotation[2][0]*cubes[contatoreOnDraw].getVertices(i))+(rotation[2][1]*cubes[contatoreOnDraw].getVertices(i+1))+(rotation[2][2]*cubes[contatoreOnDraw].getVertices(i+2)); 
      cubes[contatoreOnDraw].setVertices(i, vX_tmp); 
      cubes[contatoreOnDraw].setVertices(i+1, vY_tmp); 
      cubes[contatoreOnDraw].setVertices(i+2, vZ_tmp); 

     } 
     cubes[contatoreOnDraw].draw(gl); 
     gl.glPopMatrix(); 

    } 

感謝所有!

+0

爲什麼你甚至會這樣旋轉?爲什麼不使用'glRotate'函數? – Vallentin

+0

,因爲我需要直接操縱旋轉矩陣。 我已經完成了glRotate的版本,現在我需要這個版本.. – ExEcUtIvE

+0

@ExEcUtIvE:你可以使用glMultMatrix或glLoadMatrix加載你自己的變換矩陣。不要在CPU上執行轉換。利用GPU的計算能力,這很容易勝過CPU。也看看着色器。 – datenwolf

回答

0

我找到了解決方案,我還沒有標準化矢量(rX,rY,rZ)。 正常化後,所有的工作完美!

@datenwolf: 感謝您的回覆,我已經在GPU上完成了相同的操作,但是我想在CPU上執行其他問題!