PDA

View Full Version : Texture mapping to trapezium.

gib
03-16-2003, 10:34 PM
I want to draw an image (using texture mapping) in a certain way that distorts the image. For example: take a square and squash the sides together at the top, giving a trapezium that has parallel top and bottom sizes, but the top side is shorter than the bottom. I want to map a square image onto this quad, in such a way that there is no distortion in the Y direction, but a scaling in the X direction that varies with y.

Mathematically it is something like:

-1 <= x <= 1, -1 <= y <= 1, 0 < a < 1,

and the mapping is (x,y) -> (x',y')

where

x' = x*((a-1)*y + a+1)/2
y' = y

(The top of the trapezium has -a < x' < a).

I have found that splitting the image up into
triangles (or quads) requires a large number of faces to avoid ugly effects. What happens is straight lines in the image become kinked. Using tex mapping in the usual way, it seems to be impossible to avoid this kinking, all you can do is make the triangles so small that the kinking becomes unnoticeable.

Is there a better way to do this?

Gib

JustHanging
03-16-2003, 11:11 PM
Hi,

You can use the homogenous q coordinate to get rid of the distortion. Replace
glTexCoord2f(s, t)
with
glTexCoord4f(s*q, t*q, 0, q)

Unfortunately I don't remember how to calculate the q coordinate. I found the solution through trial and error and I don't have the code here... It's got something to do with the length of the edge, like for the bottom vertice q=the length of the bottom edge and for the top vertice it's the length of the top edge. Or something else like that, could be 1/length too...

-Ilkka

tellaman
03-17-2003, 06:47 AM
http://www.r3.nu/~cass/qcoord/

gib
03-17-2003, 11:32 AM
I have implemented a method using glTexCoord4d() (thanks Eero!), but although it produces a smooth mapping, as you say, it is not the mapping I want. Using qcoord gives a projective mapping (I think that's the right term) - effectively the image looks as if it is painted on a horizontal plane and viewed from an elevated point. The mapping I want leaves y alone while squashing the image in the X direction. The difference is quite obvious when you see it.

Gib

Don't Disturb
03-20-2003, 02:31 AM
Yeah, unfortunately the only way to solve this AFAIK is to divide your trapezium into smaller quads that are approximately parrallelograms.

gib
03-21-2003, 05:52 PM
Thanks. I won't spend any more time searching for a better way.

Gib