1
votes

I have to perform in shader an inverse projection from a u/v of a render target.

What I do is:

Get NDC as

2*(u,v,depth) - 1 

Then world space as

tmp = (P*V)^-1 * (NDC,1.0);    
world space = tmp/tmp.w;

This apparently works, but I am confused about the w division there. Why this work? Shouldn't be a multiplication by a w somewhere (as in the "forward" pipeline there is the perspective division?)

3

3 Answers

3
votes

In the strict mathematical sense, you don't do the exact inversion of the forward transform .

You could do the exact inversion if you had the clip space w value for the NDC point you want to project back. In that case, you could undo the division by w by multiplying the NDC coords with that w to get the clip space, and after multiplying with the inverse projection matrix, you would get the original point back, with the original eye space w coordinate you put in. That one is typically 1, so you completely would avoid the division of the final result by w. Now you have the exact inversion, where the division is inverted by a multiplication.

It still works the way you do it because the 4D homogenous representation of a specific 3D point is scale-invariant. So instead of the w value you got from the forward transformation, you can use any w value to represent the same point (except 0). That just means that you get some different point in the homogenous space back - you don't get the original input w value, but something else. In practice, this does not matter in most cases, since you do not care about the exact 4D homogenous coordinates, but just the 3D point it represents, and you can get that by another divide.

0
votes

let's say your original position was (Pxyz, 1), after multiplication by P*V, that would give you (NDC*w0, w0)

the w divide then gives (NDC, 1).

multiplying (NDC, 1) by (P*V)^-1 gives you (Pxyz/w0, 1/w0).

So to get back to Pxyz, you divide Pxyz/w0 by 1/w0. That 1/w0 is what comes in tmp.w

-1
votes

if we want to use matrix to transform the position,we must make the w =1,if we want get the 3d position,we must devide w (make w=1), transform the 4D homogenous to 3d position .