2011-05-16 38 views
1

我正在做一個使用右手座標的3D圖形「藍色屏幕上無紋理的立方體」測試。然而,它們會出現奇怪的剪切或扭曲(在Orthographic中,顯示左側在窗口結束前結束;在Perspective中,它看起來像顯示從左後延伸到右前方)。我需要一個用於右手正投影和透視投影矩陣的公式

不知道問題實際是什麼,但投影矩陣似乎是一個很好的開始。

我現在使用的那些:

// Went back to where I found it and found out this is a 
    // left-handed projection. Oops! 
    public static Matrix4x4 Orthographic(float width, float height, 
     float near, float far) 
    { 
     float farmnear = far - near; 
     return new Matrix4x4(
      2/width, 0, 0, 0, 
      0, 2/height, 0, 0, 
      0, 0, 1/farmnear, -near/farmnear, 
      0, 0, 0, 1 
      ); 
    } 


    // Copied from a previous project. 
    public static Matrix4x4 PerspectiveFov(float fov, float aspect, 
     float near, float far) 
    { 
     float yScale = 1.0F/(float)Math.Tan(fov/2); 
     float xScale = yScale/aspect; 
     float farmnear = far - near; 
     return new Matrix4x4(
      xScale, 0, 0, 0, 
      0, yScale, 0, 0, 
      0, 0, far/(farmnear), 1, 
      0, 0, -near * far/(farmnear), 1 
      ); 
    } 

感謝

+0

什麼是您的轉換管道?或者你是否在使用OpenGL或Direct3D等3D圖形API? – 2011-05-16 16:36:19

+0

我使用SlimDX(基本上是DirectX的.NET包裝器)作爲後端,我自己的庫在上面。而且......我不確定什麼是「轉換管道」,但這裏有一些相關代碼的Pastebin:http://pastebin.com/3DziVJSV - 我知道這是一種黑客行爲,這是基本功能的早期測試。 – 2011-05-16 16:55:51

+0

對於「轉換管道」,我的意思是將頂點的三維座標轉換爲屏幕座標。但既然你說你使用的是Direct3D,那就很明顯了。 – 2011-05-16 17:06:47

回答

0

解決 - 感謝您的幫助。這不是投影矩陣;它是LookAt/View矩陣中的一個錯誤的變量。現在所有東西都顯示正確,它已被修復。

public static Matrix4x4 LookAt(Vector3 eye, Vector3 target, Vector3 up) 
    { 
     Vector3 zAxis = target; zAxis.Subtract(ref eye); zAxis.Normalize(); 
     Vector3 xAxis = up; xAxis.Cross(zAxis); xAxis.Normalize(); 
     Vector3 yAxis = zAxis; yAxis.Cross(xAxis); 
     return new Matrix4x4(
      xAxis.X, yAxis.X, zAxis.Z, 0, // There. 
      xAxis.Y, yAxis.Y, zAxis.Y, 0, 
      xAxis.Z, yAxis.Z, zAxis.Z, 0, 
      -xAxis.Dot(eye), -yAxis.Dot(eye), -zAxis.Dot(eye), 1 
      ); 
    } 

糟糕。 :)