11

I'm working on a 2D game project where I am expecting users to draw 2D polygons (closed path) such as:

Explanation

Possible Solutions:

1 - Draw by Points and Calculate Border Lines.
1 Problem - Calculation of Border Lines.

2 - Start with an ellipse and let the user reshape it by moving vertices.
2 Problem - When ellipse enlarges, it creates gaps between vertices where you can't reshape.

3 - Add and Move Vertices
3 Problem - Ear Clipping Triangulation stucks at somepoint (53th line while loop @ http://pastebin.com/Ug337mH2, goes into infinite loop)

**After some thinking, I decided I better work on the infinite loop (in method 3) problem rather than abandoning the add and move vertex method. What causes the infinite loop in the while loop at 53. line (see http://pastebin.com/Ug337mH2)?

My guess: ear clipping triangulation fails to attach some vertex to any triangle and keeps retrying.**

How I can easily implement polygon drawing in my game?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Max Abrahamsson
  • 1,460
  • 3
  • 17
  • 35
  • Is the user going to be constrained in how they draw the polygon? I mean will they be prevented from creating a new line that intersects an existing one? – Robinson Apr 01 '12 at 14:58
  • actually it's ok for lines to intersect each other... they are going to be grouped into seperate triangles anyway. So i think it would work even if they intersect. – Max Abrahamsson Apr 01 '12 at 15:06
  • solution 3 works pretty well except that infinite loop problem :/ . I kinda made a "fake" fix for infinite loop http://pastebin.com/nw4ZAM7d it ignores vertices causing infinite loop. – Max Abrahamsson Apr 01 '12 at 15:19
  • here is my achievement with method 3 - https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-prn1/p480x480/564250_10150693729363820_662333819_9058711_2073388123_n.jpg – Max Abrahamsson Apr 01 '12 at 15:28
  • My only question relates to how the tessellation of a "random" set of lines into a concave polygon is inherently ambiguous, so I assumed you would prevent lines from intersecting. – Robinson Apr 01 '12 at 15:54
  • it doesnt have to be concave.after some thinking, i decided i should work on that infinite loop problem because other solutions are way too complicated. – Max Abrahamsson Apr 01 '12 at 16:25

1 Answers1

4

Use OpenGL's tesselator:

  #include <gl/gl.h>
  #include <gl/glu.h>
  #include <vector>

  using namespace std;

  typedef vector< vector< GLdouble* > > contours;
  contours poly;
  //Initialize poly here

  GLUtesselator* tess = gluNewTess();
  gluTessCallback(tess, GLU_TESS_BEGIN, (void (CALLBACK*)())&BeginCallback);
  gluTessCallback(tess, GLU_TESS_VERTEX, (void (CALLBACK*)())&VertexCallback);
  gluTessCallback(tess, GLU_TESS_END, (void (CALLBACK*)())&EndCallback);
  gluTessCallback(tess, GLU_TESS_COMBINE, (void (CALLBACK*)())&CombineCallback);
  gluTessCallback(tess, GLU_TESS_ERROR, (void (CALLBACK*)())&ErrorCallback);
  gluTessNormal(tess, 0.0, 0.0, 1.0);
  gluTessProperty(tess, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_NONZERO);
  gluTessProperty(tess, GLU_TESS_BOUNDARY_ONLY, GL_FALSE); //GL_FALSE
  gluTessBeginPolygon(tess, NULL);
  for (UINT i = 0; i < poly.size(); ++i)
  {
      gluTessBeginContour(tess);
      for (UINT j = 0; j < poly[i].size(); ++j)
      {
          gluTessVertex(tess, poly[i][j], poly[i][j]);
      }
      gluTessEndContour(tess);
  }
  gluTessEndPolygon(tess);
  gluDeleteTess(tess); // Delete after tessellation
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Angus Johnson
  • 4,565
  • 2
  • 26
  • 28