views:

139

answers:

1
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 ransforms. Now, the ndc ... why do I divide by w, and what do I get from this?

+2  A: 

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 wich divide 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 to 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 to 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

Luca
why would this avlue ever be not 1 ?
anon
@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
@anon: actually I used the w value for scaling without modifying the model view matrix. That's because using a scaled modelview matrix can't handle normals correctly, since that matrix scales also normals vectors, which is bad (they has to be normalized).
Luca