2009-08-20 46 views
4

我試圖或多或少地重新創建Johnny Lee's Wii head tracking app,但使用增強現實工具包進行跟蹤,並使用WPF進行圖形處理。爲此,我需要使用頂部,底部,右側和左側參數創建一個透視相機來創建我的視角,而不是視野和縱橫比(對於那些熟悉OpenGL的人,我想使用WPF等效的glFrustum而不是gluPerspective)如何在WPF中創建偏離中心的PerspectiveCamera?

問題是,這些選項似乎不可用在WPF的PerspectiveCamera類。如果我不得不使用MatrixCamera,我可能會手動創建投影矩陣,但我想避免這種情況。有誰知道更好的方法來做到這一點?

+0

「更好」是什麼意思? – genpfault 2009-08-20 21:06:25

+0

我的意思是一些爲我做數學的方法,而不是像我不得不在下面的答案中那樣強迫我做。 – 2009-08-24 18:46:15

回答

7

我從來沒有找到一種內置的方式來做到這一點,所以我寫了我自己的。 The math behind it can be found in the OpenGL glFrustum docs。如果任何人曾經運行到這個問題,這應該爲你工作:

public Matrix3D CreateFrustumMatrix(double left, double right, double bottom, double top, double near, double far) 
{ 
    var a = (right + left)/(right - left); 
    var b = (top + bottom)/(top - bottom); 
    var c = -(far + near)/(far - near); 
    var d = -2 * far * near/(far - near); 

    return new Matrix3D(
     2 * near/(right - left), 0,       0, 0, 
     0,       2 * near/(top - bottom), 0, 0, 
     a,       b,       c, -1, 
     0,       0,       d, 0); 
} 

只需設置MatrixCamera.ProjectionMatrix該方法的返回值,你所有的設置。