# UV calculation for a trapezoid

This topic is 3780 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am working on code for rendering a procedural road. A particular segment of a road is a quad, but its a quad that might have no parallel edges. This quad in reality contains many sub triangles because the road is a decal placed onto the terrain ( So these sub triangles are the clipped terrain polys ). So I need to calculate the UVs for each vert within this road segment and its giving me a lot of trouble. I tried dividing the quad into two triangles. Then for each vert I determine which triangle it is in and then calculate its barycentric coordinates, which are used to determin the UVs. The problem is that there is no smooth transition between the texture "alignment" between the two triangle halves of the quad. Image the top two X(s) are also connected. Then the result I'm getting is the lower triangle has a perfectly aligned / unstretched texture ( and it shouldnt' be ), and the upper triangle has the texture stretched quite extreme.
        X
X    |
|\   |
| \  |
|  \ |
|   \|
X----X

I also tried dividing the quad into 4 triangles like..
   X----X
|\  /|
| \/ |
| /\ |
|/  \|
X----X

With the idea that at least then all the triangles are sharing one point ( and the correct UV for that point ). With this approach, at the center point the texture does seem to roughly match up between the triangles, but along the rest of the edges there is a "break" in texture scale. It seems like using barycentric coords just isn't going to work unless there is a way for it to take all 4 points into account... Keep in mind, I cannot simply calculate the UVs of the corner points of the quad (I already have). I need to calculate the correct UV(s) for every captured/clipped terrain vert contained within this quad.

##### Share on other sites
I am currently looking into bilinear interpolation as a means of doing this. It seems to me that bilinear interpolation can do something similar to barycentric coordinates but when working with a quad rather than a triangle. I have yet to find a good C++ class or C math util function for doing this though.

##### Share on other sites
Bilinear interpolation seems to be giving a better result. However I am currently getting some "waviness" along the center of the texture in some quads (road segments). I'm not sure if this is a result of some other error or a limitation of bilinear interpolation.

This problem seems like it has a lot of similarity to normal perspective texture projection. Could a perspective projection be used to better effect than bilinear interpolation?

Note, I am not talking about using perspective projection for "actual" perspective rendering purposes. I just need a way to map a rectangular texture to a road segment that could be varying in width ( and could therefore be an arbitrary quad ).

##### Share on other sites
You should be able to transform the vertices into texture space for the road texture and determine the UV coordinates that way. As the road twists and turns, texture space will rotate to align with the direction the road is going, but the math will remain the same.

##### Share on other sites
Are you referring to calculating the uvs by multiplying the verts by a matrix? That was the first approach I took but it failed because the road can change in width.

So I can construct a transformation matrix for a particular road segment if I have its forwardVec, upVec, and rightVec. But if the left side of the road and the right have different fvecs, how can the entire segment be represented by a single matrix?

Perhaps if you have a separate matrix for each of the four road corner points and somehow use a weighted sum of all their results to find the uv of a particular point inside the segment... but then the question is once again, what type of interpolation to use?

##### Share on other sites
What i'm currently doing for my roads is this :
For each road quad, cut all underlying terrain triangles with the 4 planes formed by the quad's edges and always keep the polygon in front of all planes. For UVs, i use a ray-quadrilateral intersection routine in order to get the barycentric coords of all the vertices in the quad. It looks like you are doing the same thing. I use this for the intersection : An efficient ray-quadrilateral intersection test I haven't seen any problems with this technique, except for the cases when a road vertex is on the edge of a quad and the test may fail to generate correct UVs.

Hope that helps.

HellRaiZer

##### Share on other sites
Thank you for the informative post. Excellent link. I will compare my current technique with that. That site Journal of Graphics Tools looks excellent, too bad the articles aren't online, and its like \$100 an issue...

1. 1
Rutin
36
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633343
• Total Posts
3011431
• ### Who's Online (See full list)

There are no registered users currently online

×