如何在WPF中创建偏离中心的PerspectiveCamera?

时间:2009-08-20 19:43:51

标签: wpf opengl graphics augmented-reality

我正在尝试或多或少地重新创建Johnny Lee's Wii head tracking app,但使用增强现实工具包进行跟踪,使用WPF进行图形处理。要做到这一点,我需要使用top,bottom,right和left参数创建一个透视摄像头来创建我的视锥体,而不是视野和宽高比(对于熟悉OpenGL的人,我想使用WPF等效物) glFrustum而不是gluPerspective)

问题是,WPF的PerspectiveCamera类似乎没有这些选项。如果我不得不使用MatrixCamera,我可以手动创建投影矩阵,但我想避免这种情况。有谁知道更好的方法吗?

1 个答案:

答案 0 :(得分: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设置为该方法的返回值,即可完成设置。