What is the W component on graphics programming. I read a blog about opengl that says that w must be equal to either 0 or 1 here. But the book I am currently reading has put w component to more than 1 value. So im kinda confuse what does it really do. The book I am reading is OpenGL es a quick start guide.
This is related to the affine transformation matrix. Basically, a
0 means that you are working with a vector (which has no position and is not affected by the affine portion of transformation matrices) and a
1 means that you are working with a point (which is affected by affine transformations).
Consider this 3x3 column-major 2D affine translation matrix:
1 0 5 0 1 3 0 0 1
If you apply this to the vector
<2, 4, 0> then when the matrix rows are multiplied we get:
2 0 0 = 2 0 4 0 = 4 0 0 0 = 0
And the result is the input vector. No affine translation applied. This is exactly what we want: you can't "move" a vector.
Now let's apply this to the point
(7, 8, 1):
7 0 5 = 12 0 8 3 = 11 0 0 1 = 1
And we get the point
(12, 11, 1) which is the original point plus the translation portion of the matrix, which again is exactly what we want.
You will notice that the
w component stays the same based on the input; that is, transforming a vector produces a vector and transforming a point produces a point.
There are additional benefits to this simple math. Think of the operations of adding or substracting vectors or subtracting points. Two vectors added or subtracted create a resulting vector.
1 2 0 + 5 3 0 = 6 5 0
Likewise, with subtraction, the math works out.
Now, let's substract two points. We know that this should result in a vector (the difference between the points) and not another point. Conveniently:
5 3 1 - 1 2 1 = 4 1 0
Perfect. Of course, if you add two points together then you end up with a
w of 2, but then adding points is not a legal operation (it has no meaningful result). Adding a point to a vector should result in a new point, which of course works:
5 3 1 + 4 4 0 = 9 7 1
Everything works out. The
w component perfectly encodes whether we have a point or a vector and simultaneously allows the application of affine transformation matrices correctly for vectors vs points. This allows the use of a single transformation matrix for both points and vectors, which is useful when we want to apply it to both vertex positions (points) and vertex normals (vectors) or the like.