I would like to have a texturemapped quad on the screen, and it might be at an angle to the viewer, and with a perspective transformation, and I would like to know from screen coordinates (user clicks on texture) the u, v texture coordinates on the quad.

Even if it can’t be done with hardware is there a way to accurately do this in software, it doesn’t need to be very fast.

Take the window coordinate (x,y) and create two points (x,y,1) and (x,y,-1).

Use gluUnproject to transform both points back into object space, using the current modelview and projection matrices.

Compute the intersection point of the line (defined by those two points) and the plane containing the quad.

Compute the parametric location of the point inside the quad. This is pretty easy if the quad is rectangular, a bit more work otherwise.

If your texcoords are [0,1]x[0,1] the parametric point is your texcoord. Otherwise, you’ll have to plug the parametric point into some expressions that describe your texcoord mapping.

Look for an analytic geometry book if don’t understand these steps.

thanks, so after I do gluUnproject, I will have a vector for that pixel.

From there I can calculate the 3d space point the line intersects the plane by using the fact that the plane normal times the vector from any point on the plane to another point on it is zero.

if the quad is rectangular, I can just use the projection formulas and project the vector from a corner to the intersection point onto the vectors along the two sides and get point, then convert it to tex coords.

what do I do if the quad is not rectangular?
did I make any mistakes?
thanks.