4
$\begingroup$

For example, when you want to compute the world space position of a fragment in the fragment shader, you can construct the fragment's NDC coordinates, then multiply by the inverse of whatever transform was used on the triangle that was rasterized to that fragment.

And when this reverse process includes some projective transformation, you have to divide by w after doing that multiplication. Just like if you did the forward process.

But why? My intuition would be that you instead multiply by w (the original one for the fragment, which is stored in e.g. gl_FragCoord.w as 1/w) before applying the transformation, or something like that.

It kind of makes sense that you have to do a perspective divide after applying the inverse projection transform - after all it's going to alter the x,y,z and w components of the result in projecty ways, but I don't understand why this would then correctly give you back the original world space position for the fragment instead of an arbitrary result.

$\endgroup$

1 Answer 1

3
$\begingroup$

The way homogeneous coordinates (x, y, z, w) work is that if you multiply the vector by any nonzero value, it still represents the same point in 3D projective space. These different xyzw representations of the same point are all considered equivalent to each other: one is just as good as another. The point (1, 2, 3, 4) is the same as the point (2, 4, 6, 8), or (−1, −2, −3, −4), etc.

For converting back from projective space to the ordinary 3D space embedded in it, it's convenient to normalize to w = 1. That's all that the division by w is really doing. It's not part of the transformation per se.

It's a lot like normalizing a vector, by dividing it by its magnitude. It still points the same direction, but it's convenient for later calculations to ensure it has length 1 (in cases where the direction is the thing you care about and the length doesn't matter).

Just as you might normalize a direction vector after passing it through some 3D transformations, you can "normalize" projective points represented by homogeneous coordinates, after passing them through some projective transformations, to restore them to w = 1.

The 4×4 matrix is the actual projective transformation, and the inverse transformation is just given by the matrix inverse. Recovering the exact original w value before transformation is meaningless, and isn't possible anyway since the division by w loses that information—just as normalizing a direction vector loses the information about its original length, but that's ok since the length wasn't relevant anyway.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.