63
void main(void)
{
  vec4 clipCoord = glModelViewProjectionmatrix * gl_Vertex;
  gl_Position = clipCoord;

  gl_FrontColor = gl_Color;

  vec3 ndc = clipCoord.xyz / clipCoord.w;

So the clipCoord is just doing standard fixed pipeline transforms. Why do I divide by w, and what do I get from this?

1 Answer 1

70

W is the fourth coordinate of a three dimensional vertex; This vertex is called homogeneous vertex coordinate.

In few words, the W component is a factor which divides the other vector components. When W is 1.0, the homogeneous vertex coordinates are "normalized". To compare two vertices, you should normalize the W value to 1.0.

Think of the vertex (1,1,1,1). Now increase the W value (w > 1.0). The normalized position is scaling! and it is going to the origin. Think of the vertex (1,1,1,1). Now decrease the W value (W < 1.0). The normalized position is going to an infinite point.

Apart from scaling vertex coordinates, the W coordinate is necessary since you have to multiply a 4x4 matrix (the model view and/or the projection matrices) with a 4x1 matrix (the vertex).

Of course, the Red Book is the definite guide:

Red Book Appendix

The foregoing link is dead as of 14 SEP 2024. Try Appendix E here:

Red Book

9
  • 15
    why would this avlue ever be not 1 ?
    – anon
    Commented Mar 11, 2010 at 7:40
  • 7
    @anon: in short, because a projection matrix changes its value. Look at the Appendix Luca is pointing at, at the bottom part. a typical projection does w_out = -z_in. That's why people sometimes call this the "perspective divide".
    – Bahbar
    Commented Mar 11, 2010 at 8:41
  • 8
    @anon, from this thread there are two common values, w=1 for a point, and w=0 for a vector.
    – Air
    Commented May 19, 2014 at 19:11
  • 5
    A consequence of how homogeneous coordinates work is that, if W is exactly 0, the vector does not behave as a "position" but as a "direction" (think directional light); i.e., it represents all the lines parallel to a given line. Commented Oct 8, 2014 at 3:15
  • 34
    I finally understood it when I read in the Red Book that "homogeneous vertex (x, y, z, w)T corresponds to the three-dimensional point (x/w, y/w, z/w)T" and that "the sequence of points (1, 2, 0, 1), (1, 2, 0, 0.01), and (1, 2.0, 0.0, 0.0001), corresponds to the euclidean points (1, 2), (100, 200), and (10000, 20000)"
    – user31389
    Commented Apr 12, 2016 at 11:04

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