how to map texture to non rectangular polygon ?

In all tutorials the examples are about aplying texture to rectangular surfaces by using glBegin(GL_QUADS).

How can I map texture to a custom polygonal surface ?

By specifying texture coordinates at each vertex to control how the texture is applied to the surface. Texture coords are typically 2d (assuming you’re working with a regular 2d texture) and range from 0 to 1 across its surface (regardless of actual texture size).

The easiest way to do something straight away is just to duplicate your vertex coords into your texture coords, although that might look somewhat freaky depending on the scale of your objects…

thanx, but that’ not really answer to my question. I know how to aply texture to QUAD, but can anybody provide some sample code for aplying texture to a polygon (pentagon for example).

You can render either tris or quads. And that’s it.

If you want to render an N-Gon, you’ve to tesselate it first (into tris usually) the texture is then applied as usual.

Btw JOGL has a nice tesselator included (it’s a part of glu) :wink:

First, oNyx, there is no restriction on the number of vertices in an OpenGL poly, but the behavior is undefined if they are not planar. You obviously can’t make a poly with fewer than 3, but there’s no upper limit.

Second, igorbuzz, the process of mapping a poly with ANY number of vertices is a subjective process. I think where you’re having difficulty, is you’re visualizing using only the outer corners of the texture : (0,0) , (0,1), (1,0), (1,1) in ST (or UV) space. I think for a pentagon (or any high-vertex poly) you’ll want to visualize where you want the corners to go in ST (or UV) space first, and then simply assign the coordinates of your visualization. I’d reccomend you research UV-mapping and UV-mapping tools (maybe write your own…), as they help considerably with the coordinate-placement process.

Best,

First, oNyx, there is no restriction on the number of vertices
in an OpenGL poly[…]

Actually, I hadn’t said something like that :>

However, his question was rather general therefor I anserwed accordingly. To render any given poly shape (also convex/self intersecting), you’ve to tesselate it (just in case heh).


Getting the UVs isn’t that difficult once you wrapped your head around it. Take a look a this tutorial:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=38

They explain there how to map a texture onto a triangle.

Also take a look at how to draw some text on the screen. Since all chars are put into one texture you’ve to caluclate the uv coors accordingly.