Wednesday, April 11, 2018

mathematics - What does the graphics card do with the fourth element of a vector as the final position?


From this question it appears you would want a four-element position vector, as it is simpler to modify its position with matrix multiplication.


On its own this would imply the fourth element should simply be ignored when considering it as a representation of a 3D point (assuming no transformation), but I know this is not true, as when I supply a vector4 to the GPU, if the fourth element is not one, it is not rendered - why?


What is the significance of the fourth element, once it is in the rasterizer?


EDIT: On review this question was somewhat poorly worded; it would be more accurate for the second paragraph to say: "if the value of the fourth element is not within a certain range, it is not rendered 'correctly'/'as expected'".



Answer




The fourth component is a trick to keep track of perspective projection. When you do a perspective projection, you want to divide by z: x' = x/z, y' = y/z, but this isn't an operation that can be implemented by a 3x3 matrix operating on a vector of x, y, z. The trick that has become standard for doing this is to append a fourth coordinate, w, and declare that x, y, z will always be divided by w after all transformations are applied and before rasterization.


Perspective projection is then accomplished by having a matrix that moves z into w, so that you end up dividing by z. But it also gives you the flexibility to leave w = 1.0 if you don't want to do a divide; for instance if you just want a parallel projection, or a rotation or whatever.


The ability to encode positions as w = 1, directions as w = 0 and use the fourth row/column of a matrix for translation is a nice side benefit, but it's not the primary reason for appending w. One could use affine transformations (a 3x3 matrix plus a 3-component translation vector) to accomplish translation without any w in sight. (One would have to keep track of what's a position and what's a direction, and apply different transformation functions to each; that's a bit inconvenient, but not really a big deal.)


(BTW, mathematically, vectors augmented with w are known as homogeneous coordinates, and they live in a place called projective space. However, you don't need to understand the higher math to do 3D graphics.)


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...