Getting to know the Q texture coordinate...

These days texture mapping hardware is really cheap, so we use texture maps a lot.  One of the more interesting aspects of texture mapping is the space that texture coordinates live in.  Most of us like to think of texture space as a simple 2D affine plane.   In most cases this is perfectly acceptable, and very intuitive, but there are times when it becomes problematic.

For example, suppose you have a quad that is trapezoidal in its spatial coordinates but square in its texture coordinates as shown below.

spatial coordinates texture coordinates

OpenGL will divide the quad into triangles and compute the slopes of the texture coordinates (ds/dx, ds/dy, dt/dx, dt/dy) and use those to interpolate the texture coordinate over the interior of the polygon.  For the lower left triangle, dx = 1 and ds = 1, but for the upper right triangle, dx < 1 while ds = 1.  This makes ds/dx for the upper right triangle greater than ds/dx for the lower one.  This produces an unpleasant image when texture mapped as shown below.

textured using only (s,t)

Well, this is no good!  Luckily, texture space is not simply a 2D affine plane even though we generally leave the r=0 and q=1defaults alone.  It's really a full-up projective space (P3)!  This is good, because instead of specifying the texture coordinates for the upper vertices as (s,t) coordinates of (0, 1) and (1, 1), we can specify them as (s,t,r,q) coordinates of (0, width, 0, width) and (width, width, 0, width)!  These coordinates correspond to the same location in the texture image, but LOOK at what happened to ds/dx - it's now the same for both triangles!!  They both have the same dq/dx  and dq/dy as well.  The image we get if we use this approach is shown below.

textured using (s,t,r,q)

Note that the image looks as if it's a long rectangular quad extending into the distance.  It is not.  It is still in the z=0 plane.  It can become quite confusing when using this technique with a perspective camera projection because of the "false depth perception" that this produces.  Still, it may be better than using only (s,t).  That is for you to decide.

It is helpful to dink around with the full projective texture coordinates to try to gain some intuition about how they can be used effectively.  Toward that end, here is a glut demo program that allows you to play with some of these parameters and see their effect.