How do you create an off-center PerspectiveCamera in WPF?
-
19-09-2019 - |
Question
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?
Solution
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.