View Full Version : How to get texture coordinates from screen coordinates

geckosenator

01-27-2006, 02:44 AM

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.

Thanks for any help

u can use barycentric coordinates to do that

Brian Paul

01-27-2006, 03:53 PM

I'd try the following:

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

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

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

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

5. 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.

-Brian

geckosenator

01-28-2006, 05:33 AM

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.

Zulfiqar Malik

01-28-2006, 07:23 AM

Like zed said, rectangular or not, barycenteric coordinates can be your friend in this one.

Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.