Texture mapping an NGon?

I’m not sure how to go about figuring out how to map texture coordinates for a 2D NGon (N sided polygon) How can this be done?

The effect I’m trying to achieve is for the texture to fit on the polygon and stretch out accordingly so the whole texture fits on it.

Answer

Remember that when rendering an ngon in OpenGL, it’s just a whole bunch of triangles. Also, you’re taking some shape and trying to map it to a rectangle, so you have to be extremely critical of how you wish to do this as there many different mappings going from any shape to a rectangular texture.

For example, if I have a 5-gon that is shaped like a square with a point in between two corners, it’s easy to map to a texture. What happens when I pull that point out? Do the texture coordinates change when I move vertices around?

One way to do it is to map the circumference of the ngon to the circumference of the rectangle, where the distance traveled from vertex to vertex on the ngon is mapped to a UV coordinate around the circumference of the texture. For example, at 1/4 of the way around it, give it a UV coordinate of (1,0), at half way around the ngon, give the vertex a UV coordinate of (1,1) and 3/4 the way around it, give it a UV of (0,1)–you’ll need to interpolate between points, of course, since an ngon won’t line up perfectly at every vertex.