如何在WPF中创建偏离中心的PerspectiveCamera? [英] How do you create an off-center PerspectiveCamera in WPF?

查看:369
本文介绍了如何在WPF中创建偏离中心的PerspectiveCamera?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着或多或少地重新创建 Johnny Lee的Wii头部追踪应用程序,但是使用用于跟踪的增强现实工具包和用于图形的WPF。为此,我需要使用顶部,底部,右侧和左侧参数创建一个透视相机来创建我的视角,而不是视野和纵横比(对于那些熟悉OpenGL的人,我想使用WPF等效glFrustum而不是gluPerspective)

I'm trying to more or less recreate Johnny Lee's Wii head tracking app, but using an augmented reality toolkit for the tracking, and WPF for graphics. To do this, I need to create a perspective camera using the top, bottom, right, and left parameters to create my viewing frustum, instead of field of view and aspect ratio (to those familiar with OpenGL, I want to use the WPF equivalent of glFrustum instead of gluPerspective)

问题是,这些选项在WPF的PerspectiveCamera类中似乎不可用。如果我不得不使用MatrixCamera,我可能会手动创建投影矩阵,但我想避免这种情况。有没有人知道有更好的方法来做到这一点?

The problem is, those options don't seem to be available on WPF's PerspectiveCamera class. I could probably create the projection matrix manually if I had to and use MatrixCamera, but I'd like to avoid that. Does anyone know of a better way to do this?

推荐答案

我从来没有找到一种内置的方式来做到这一点,所以我写了我自己的。 背后的数学可以在OpenGL glFrustum文档中找到。如果有人遇到这个问题,这应该适合你:

I never did find a built-in way to do this, so I wrote my own. The math behind it can be found in the OpenGL glFrustum docs. If anyone else ever runs into this problem, this should work for you:

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设置为该方法的返回值,重新设置。

Just set MatrixCamera.ProjectionMatrix to the return value of that method, and you're all set.

这篇关于如何在WPF中创建偏离中心的PerspectiveCamera?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆