Search code examples
iphoneiosopengl-estexture-mapping

Imitate Polygon Stipple with OpenGL ES on iOS


I want to fill a concave polygon with a simple repeating pattern. I can already draw the polygon properly, unfortunately I am having problems with filling it. In OpenGL I could do this easily with POLYGON_STIPPLE. However this functionality is not available in OpenGL ES.

I figured that I probably need to use textures instead of stippling. However I cannot figure out how to calculate the correct texture coordinates as all the triangles are of completely different sizes, but I still want the pattern to fit well right next to each other.

Are there any good starting points that explain how to fill polygons with a repeating texture, where the polygon is a little more complex than one triangle or rectangle?


Solution

  • Calculating the texture coordinates is not as hard as I thought. Filling the polygon with a pattern can be done like this:

    1. Load texture (from image or define byte array)
    2. Bind texture
    3. Set texture parameter so that texture repeats itself. This will have the effect, that for every coordinate bigger than 1 the texture will just repeat all over again.

      glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
      glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
      
    4. Calculate texture coordinates. Each vertix coordinate c corresponds to a texture coordinate calculated like this: (c.x/texture.width, c.y/texture.height)