# Mapping rectangular texture on trapezoid-like shape

I have a problem with rendering rectangular textures on non-rectangular surfaces. While googling, I found a thread with person having a pretty similar problem: http://www.gamedev.net/topic/419296-skewedsheared-texture-mapping-in-opengl/

But in my case, I cannot be sure about surface's exact properties. It's not regular trapezoid, it can be slightly bent. Which means, that I cannot just calculate correct texture matrix, or at least I don't know how.

I've tried to render triangle fan-like shape, but it just changed the characteristics of final distortion.

Depends on what you are trying to achieve, you could define your figure in 3D space to make the distortion as suggested in the link.

If it's not 3D you are looking for then an applicable hack is to use a polygon "net", split the figure into a suitable number of small squares and you can do the transformation as you see fit. For some simple transformations it may be sufficient to stripe the figure using triangles that span the full figure in one direction but are thin in the other.

I suppose you could also use a custom pixel shader to achieve the same result.

• My figure is already in 3D space, I can't really change it's corner coordinates. Tesselation idea is my last resort, it's not absolutely correct from mathematics point of view, but maybe it will be good-enough approximation. Jul 11, 2011 at 18:51

You're going to have to recreate your shape as a 3D mesh and run it through a software renderer to get any decent output that isn't a single-purpose hack like the texture matrix solution. If you attempt this in 2D you don't have enough information, like you've noticed.

Let me introduce you to a great article on perspective texture mapping in software: tutorial on 3D software rendering

• As I already commented, my shape is already in 3D space, I cannot change it's corner coordinates. But thank you for the link. Jul 11, 2011 at 18:52
• Though probably software texture mapping would solve my issue, I don't know if I have that much time for implementation. Seems like I will stick with tesselation idea. Jul 12, 2011 at 7:26

There is no easy solution to your problem. In common 3D graphics surfaces are composed out of triangles for a reasons (easy to use, always planar, etc..). Even when you order to draw quads GPU renders them as pairs of triangles.

Your problem comes from the fact, that it is impossible to properly split quad into triangles when they are not planar and/or their sides are not equal. It is clearly visible on your linked forum post pictures.

The only solution I could think of is tessellation. It can be done on different stages though:

• On modeling stage, you could ask your artists to use finer grid and manually split bent quads/trapezoids into triangles