2012-08-07 50 views
1

這裏是一個情形:OpenTK/OpenGL的局部軸旋轉

目的是通過描述:

  • 位置
  • 量表
  • 旋轉

首先我應用模型視圖(相機),然後使用以下矩陣進行平移和旋轉:

private Matrix4d AnglesToMatrix(Vector3d angles) 
    { 
     Vector3d left = Vector3d.UnitX; 
     Vector3d up = Vector3d.UnitY; 
     Vector3d forward = Vector3d.UnitZ; 

     AnglesToAxes(angles, ref left, ref up, ref forward); 

     return new Matrix4d(
      new Vector4d(left.X, up.X, forward.X, 0), 
      new Vector4d(left.Y, up.Y, forward.Y, 0), 
      new Vector4d(left.Z, up.Z, forward.Z, 0), 
      new Vector4d(0, 0, 0, 1)); 
    } 

    private void AnglesToAxes(Vector3d angles, ref Vector3d left, ref Vector3d up, ref Vector3d forward) 
    { 
     const double DEG2RAD = 0.0174532925; 
     double sx, sy, sz, cx, cy, cz, theta; 

     // rotation angle about X-axis (pitch) 
     theta = angles.X * DEG2RAD; 
     sx = Math.Sin(theta); 
     cx = Math.Cos(theta); 

     // rotation angle about Y-axis (yaw) 
     theta = angles.Y * DEG2RAD; 
     sy = Math.Sin(theta); 
     cy = Math.Cos(theta); 

     // rotation angle about Z-axis (roll) 
     theta = angles.Z * DEG2RAD; 
     sz = Math.Sin(theta); 
     cz = Math.Cos(theta); 

     // determine left axis 
     left.X = cy * cz; 
     left.Y = sx * sy * cz + cx * sz; 
     left.Z = -cx * sy * cz + sx * sz; 

     // determine up axis 
     up.X = -cy * sz; 
     up.Y = -sx * sy * sz + cx * cz; 
     up.Z = cx * sy * sz + sx * cz; 

     // determine forward axis 
     forward.X = sy; 
     forward.Y = -sx * cy; 
     forward.Z = cx * cy; 
    } 

at,最後我適用比例尺。除了基於全局座標軸的旋轉之外,它們都很好看。

如何使用本地軸旋轉對象?

使問題準確。當我在Y軸上旋轉物體45度時,X軸和Z軸與它一起旋轉,然後再用另一個旋轉軸使用新軸。

爲了避免處罰形式的弊端...我讀了所有與3D空間旋轉相關的主題,非他們給了我解決方案。上面的代碼是應用各種嘗試的結果,但它產生的結果是一樣的:

 GL.Rotate(Rotation.X, Vector3d.UnitX); 
     GL.Rotate(Rotation.Y, Vector3d.UnitY); 
     GL.Rotate(Rotation.Z, Vector3d.UnitZ); 

編輯: 事實證明,我們的設計師有大約在3D對象的旋轉不良預期,但仍是問題存在。至於使用的語言,我們寫這在C#中,但如果你點我在C或C溶液++,我會處理這件事:d

我們目前使用的(爲了可以配置):

GL.Rotate(Rotation.X, Vector3d.UnitX); 
GL.Rotate(Rotation.Y, Vector3d.UnitY); 
GL.Rotate(Rotation.Z, Vector3d.UnitZ); 

但這將圍繞世界軸旋轉對象。我們要的是使用本地對象軸這樣的假設,我們有XYZ軸旋轉:

GL.Rotate(Rotation.X, Vector3d.UnitX); 
GL.Rotate(Rotation.Y, newYaxis); 
GL.Rotate(Rotation.Z, newZaxis); 

或假設我們有YXZ軸旋轉

GL.Rotate(Rotation.Y, Vector3d.UnitY); 
GL.Rotate(Rotation.X, newXaxis); 
GL.Rotate(Rotation.Z, newZaxis); 

最有效的方法是預先計算旋轉矩陣,但我仍然想知道如何確定旋轉後的新軸。 (它接縫,我不得不重訪三角學書)。如果有人有解決方案,將真正快速計算旋轉矩陣,我將不勝感激。現在我將嘗試在每次傳遞中使用三角計算。

回答

0

你能說出你在這裏設想的究竟是什麼樣的情況嗎?

我不相信你的請求有任何意義。對於任何旋轉,您可以在另一個操作(全局座標軸)之前或在另一個操作(局部座標軸)之後應用它。因此,如果要應用三次旋轉,則必須先旋轉一個軸旋轉,再次旋轉另一個軸,而另一個軸必須旋轉第三個。

你說你想要應用三次旋轉,但是你希望每次旋轉在另外兩次之前發生。他們不可能都是第一位的。

如果你對你試圖達到什麼樣的旋轉進行更多的描述(圖片更喜歡),也許我們可以澄清這個誤解,但是你現在要求的東西在物理現實中對我沒有意義。

+0

旋轉必須按特定的順序發生,但在圍繞一個軸旋轉後,我必須計算下一個旋轉的新軸。 – dr4cul4 2012-08-10 07:21:57