To get a vertex position in screen coordinates, there are 3 operations to do:

1- multiply the vertex position by the modelview projection matrix.

2- do the perspective divide.

3- do the viewport transformation.

My question is what exactly is the perspective divide and what is the equation to perform it.

Thanks.

Just divide your coordinates by W.

After the modelview/projection transformation, your vertex is in “clip space”. X, Y and Z are then in the range -W to W (anything outside this range is clipped). By dividing everything by W, your coordinates are now in the -1 to 1 range (“normalized device coordinates”).

– Tom

For more intuition of what “divide by W” does…

Look at the projection matrix produced by glFrustum. (see the GL spec). Note that the last row is (0, 0, -1, 0). The result of applying this matrix to an (x,y,z,1) coordinate is that the W component becomes -z, i.e. (x’, y’, z’, -z).

So, when you “divide by W”, you’re really dividing by -z. Don’t worry about the negation. The greater the magnitude of z, the further the coordinate is from the viewing position. Thus, dividing x’ and y’ by z makes further coordinates move closer to (0, 0). So distant objects appear smaller.

-Brian