How do you create an off-center PerspectiveCamera in WPF?

99封情书 提交于 2020-01-02 06:00:30

问题


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)

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?


回答1:


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);
}

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



来源:https://stackoverflow.com/questions/1308395/how-do-you-create-an-off-center-perspectivecamera-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!