4
\$\begingroup\$

I have an orthographic projection and I try to unproject a point from screen space.

Following are the view and projection matrices:

var w2 = ScreenWidthInPixels/2;
var h2 = ScreenHeightInPixels/2;

view  = Matrix.LookAtLH(new Vector3(0, 0, -1), new Vector3(0, 0, 0), 
                        Vector3.UnitY);
proj  = Matrix.OrthoOffCenterLH(-w2, w2, -h2, h2, 0.1f, 10f);

Here is how I unproject a Point p, the point is given in screen pixels:

var m = Vector3.Unproject(p, 0, 0, ScreenWidthInPixels, ScreenHeightInPixels, 
                  0.1f, 10f, // znear and zfar
                  view *proj);

My code doesn't work, the matrix m contains only Nan. When I try to invert view * proj I get back a Matrix with only zeros.

So I suspect my problem has something to do with the orthographic projection matrix.

Here are my questions:

  • Could the problem be caused by an underflow due to the large values in the OrthoOffCenterLH projection?
  • What parameters do I have to pass for x,y,width,height in Unproject(...)?
  • What significance has the minZ and maxZ parameter in Unproject(...)?
  • Does it matter what I pass for p.Z in Unproject(...)?
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

Could the problem be caused by an underflow due to the large values in the OrthoOffCenterLH projection?

Don't know exactly what you mean. But no, this seems to neither be an issue with precision or overflow/underflow.

What parameters do I have to pass for x,y,width,height in Unproject(...)?

Straight from the documentation
(x,y) is the location of the viewport, (0,0) should be fine. (width,height) is exactly what you pass in, the width and height of the viewport.

What significance has the minZ and maxZ parameter in Unproject(...)?

These are simply your near and far clipping planes. Read up on projections here. The article is geared towards OpenGL, but is actually quite agnostic and mostly math. Basically they have to match what you pass in for your OrthoOffCenterLH() call.

Does it matter what I pass for p.Z in Unproject(...)?

Yes it does. Z needs to be the depth value. Think about it this way, you have a point p that you multiplied by your model and view matrices. To project, you multiply p by the projection matrix.

But this conversion doesn't lop off the original Z value and just give you 2D x and y, it also gives you a depth value that gets written to the depth buffer. Without this depth value, how do you think you can unproject a 2D point back into 3D?

You will either need to read the depth buffer at the x,y coordinate you wish to unproject or compute this depth value your self.

\$\endgroup\$
4
  • \$\begingroup\$ thanks for your answer, in my case it is an orthographic projection, so I guess any z value will be good? As I dont have a z buffer (painters algorithm) I can just take the z values of the plane where my objects reside, is this correct? \$\endgroup\$ Dec 19, 2012 at 20:17
  • \$\begingroup\$ What z values are you using? Try something in the range [-1, 1]. \$\endgroup\$ Dec 19, 2012 at 20:26
  • \$\begingroup\$ i see a bit clearer now, thanks a lot. if no more answers come to my other questions I will mark yours as answer. \$\endgroup\$ Dec 20, 2012 at 10:31
  • 1
    \$\begingroup\$ I answered them in an edit. \$\endgroup\$ Dec 21, 2012 at 16:05

You must log in to answer this question.

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