2
\$\begingroup\$

Lets say I have my XNA app window that is sized at 640 x 480 pixels. Now lets say I have a cube model with its poly's facing in to make a room. This cube is sized 640 units wide by 480 units high by 480 units deep. Lets say the camera is somewhere in front of the box looking at it.

How can I set up the view and projection matrices such that the front edge of the box lines up exactly with the edges of the application window?

It seems like this should probably involve the Matrix.CreatePerspectiveOffCenter method, but I don't fully understand how the parameters translate on to the screen.

For reference, the end result will be something like Johhny Lee's wii head tracking demo: http://www.youtube.com/watch?v=Jd3-eiid-Uw&feature=player_embedded

P.S. I realize that his source code is available, but I am afraid I haven't been able to make heads or tails out of it.

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

(None of this advice is specific to the XNA function you named; it applies equally well to e.g. glFrustum. I don't know XNA.)

You need to think about the view frustum. The rectangle in 3D space which is "at the location of" the viewport is precisely the "near plane" face of the frustum, and the left, right, bottom, and top values are the positions of the edges of that face.

First, compute the coordinates of the box, in the coordinate system where the camera is at (0,0,0), the box is axis-aligned, and the box's "hole" face is parallel to the XY plane and on the negative-Z side of it. Then set the left, right, bottom, and top values to the corresponding coordinates of the edges of that face of the box. Set the near plane distance to the negated (i.e. positive) Z coordinate of the box face, and set the far plane distance to whatever you see fit.

This will cause everything in the volume in front of the box/monitor to be clipped. If you don't want that, then change the near plane value — but you must scale the left, right, bottom, and top values by the same amount. (Visualize the view frustum — as the near face gets closer to the camera, the coordinates defining its edges get proportionally smaller to preserve the same angles.)

In the following diagram, l, t, b, r, and n are the coordinates I've been discussing. The near face of the frustum, which is also your box face, is the smaller rectangle; the rest of the box does not correspond to anything.

(Diagram of a view frustum) (image from Wikimedia Commons, by Martin Kraus, CC-BY-SA licensed)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .