2009-08-20 48 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该方法的返回值,你所有的设置。