0

Right now, I'm having no problems adding polygons like triangles or squares, but the problem comes when I try to add something more complex.

This is what I use for a square:

GLfloat squareVertices[] = {
        50, 50,
        150, 50,
        50, 150,
        150, 150
    };
    GLfloat squareTexture[] = {
        0, 0,
        1, 0,
        0, 1,
        1, 1
    };


    glColor4f( 1, 0, 0, 1 );

    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
    glEnable(GL_BLEND);
    glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );


    glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, squareVertices);
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, squareTexture

That works perfectly, but what about an arbitrary number of points?

Let say that I have, for example, an L shape, with these points:

0,0         10,0






            10,80                     100,80

0,100       10,100                    100,100

This is an L (try to see lines joining the coordinates)

My question is, given these 7 points (or 8, or 100), how can I draw the figure?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Antonio MG
  • 20,382
  • 3
  • 43
  • 62

2 Answers2

1

You will have to convert the shape to a list of triangles.

Andreas Brinck
  • 51,293
  • 14
  • 84
  • 114
  • The problem is that in my application, I only going to receive an array of points (coordinates), but the shape can be an L, or something even much more complex. Is there any algorithm to create that? – Antonio MG Apr 18 '12 at 14:24
1

Given your list of coordinate points you need to generate both a triangulated mesh and a set of uv coordinates (if you really need them). Once it is triangulated you can draw with a gldrawarrays

Here are some resources that may help you:

Triangulation http://www.cs.unc.edu/~dm/CODE/GEM/chapter.html

Another Question that may help with UV generation: Calculating planar UV coordinates for arbitrary meshes

if you were using desktop OpenGL instead of OpenGL ES you could actually draw without triangulation using GL_POLYGONS...

Community
  • 1
  • 1
Justin Meiners
  • 10,754
  • 6
  • 50
  • 92