Monday, December 14, 2015

c# - Problems with converting from Screen Space to World Space


I have an unproject function I've written, which takes in x and y values between -1 and 1, and a z value between 0 and 1 (0 is near plane, 1 is far plane). I create a vector off of this, and multiply it onto Matrix.Invert(ViewMatrix * ProjectionMatrix) to get the world-space coordinates of that screen position at that depth. The problem I'm having is that the given depth values don't result in the right position (at least as I understand it).


Projection Problem


Above is a rough sketch of what I'm seeing. The black lines are the frustrum edges, red lines are near and far plane, and green line is the plane drawn at the given z depth. My assumption was that the depth would be linear, with z = 0.5 being halfway between the near and far plane, however the values all sit very close to the near plane until z is real close to 1, at which point it rapidly approaches the far plane. Am I doing my calculations wrong somewhere? Or did I just misunderstand how projections work?



Answer



The short answer is that the standard perspective projection has a side effect of focusing Z resolution near the camera, which is actually beneficial. A perspective projection turns out to be linear in 1/Z rather than Z, meaning that there are very large changes in Z near to the camera, but smaller changes as the distance increases. Here's an article explaining the effect.


Also here's another article that discusses alternatives to the traditional perspective transform, which has some graphs sort of depicting what's going on.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...