Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL 2D polygonal shape drawing and manipulation?

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?

like image 390
Ahmet Yildirim Avatar asked Apr 01 '12 14:04

Ahmet Yildirim


People also ask

What kind of polygons can OpenGL draw?

OpenGL can render only convex polygons, but many nonconvex polygons arise in practice. To draw these nonconvex polygons, you typically subdivide them into convex polygons—usually triangles, as shown in Figure 2-12—and then draw the triangles.

How does OpenGL draw?

OpenGL can draw only a few basic shapes, including points, lines, and triangles. There is no built-in support for curves or curved surfaces; they must be approximated by simpler shapes. The basic shapes are referred to as primitives. A primitive in OpenGL is defined by its vertices.

What is glvertex2f in OpenGL?

The glVertex function commands are used within glBegin/glEnd pairs to specify point, line, and polygon vertices. The current color, normal, and texture coordinates are associated with the vertex when glVertex is called. When only x and y are specified, z defaults to 0.0 and w defaults to 1.0.


1 Answers

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
like image 185
Angus Johnson Avatar answered Nov 15 '22 20:11

Angus Johnson