Search code examples
openglglutesselation

Why is gluTess not working on first call?


I am using the gluTess* functions to draw non-convex polygons. To avoid redoing the tesselation at every step, I store the result in an array and use the vertex array capabilities of OpenGL to draw.

My problem is that, for some reason, the first time I use the gluTess* only part of the triangles are drawn. If I force to recreate the gluTess*, then everything is fine.

Note that, to recreate the gluTess*, I completely destroy the object containing the vertex array and recreate it (which forces the recalculation of the vertex array object).

Any idea on why that would be the case?

Some random ideas:

  • could it be because for the first OpenGL call the window is not yet at its full size?
  • do I need to set some OpenGL state, that would be done later, but not on the very first call?

Thanks.

Edit: As a test, I just created, destroyed, and recreated the vertex array. It solve the problem. Which means: without any change in the OpenGL state, the first call to gluTess* fails to tesselate the polygon correctly. But the second one succeed. Did anybody notice that before?

Edit (2): Here is the code:

VA va;
GLUtesselator *t = gluNewTess();
gluTessCallback(t, GLU_TESS_BEGIN_DATA, (GLvoid (*)())beginVA);
gluTessCallback(t, GLU_TESS_END_DATA, (GLvoid (*)())endVA);
gluTessCallback(t, GLU_TESS_VERTEX_DATA, (GLvoid (*)())vertexVA);
gluTessCallback(t, GLU_TESS_ERROR, (GLvoid (*)())&tessError);
gluTessCallback(t, GLU_TESS_COMBINE, (GLvoid (*)())&tessCombine);

gluTessProperty(t, GLU_TESS_BOUNDARY_ONLY, GL_FALSE);
gluTessProperty(t, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_NONZERO);

gluTessBeginPolygon(t, &va);
gluTessBeginContour(t);
foreach(Point3d& p, points)
  gluTessVertex(t, const_cast<GLdouble*>(p.c_data()), (void*)&p);
gluTessEndContour(t);
gluTessEndPolygon(t);
gluDeleteTess(t);

As the second call succeed, I suspect that beginVA, endVA and vertexVA work fine (as I said, the second call is made by destroying the data structure VA, which mostly hold the vertex array).

Edit (3): Here is the missing code:

struct VA
{
  std::vector<Point3d> positions; // Vertex array
  GLenum current_mode; // Drawing mode (GL_TRIANGLE, *_FAN or *_STRIP)
  Point3d v1, v2; // Two last vertices for _FAN or _STRIP
  bool has_v1, has_v2; // did we get the two last vertices?
  int n; // Index of the vertex, for *_STRIP
};


void beginVA(GLenum mode, VA *va)
{
  va->current_mode = mode; // Store the mode
  va->has_v1 = va->has_v2 = false; // We haven't had any vertex yet
  va->n = 0;
}

void endVA(VA *va)
{
  va->current_mode = 0; // Not really necessary, but cleaner
}

void vertexVA(Point3d *p, VA *va)
{
  ++va->n;
  if(va->current_mode == GL_TRIANGLES) // The simple case
    va->positions.push_back(*p);
  else if(!va->has_v1) {
    va->v1 = *p;
    va->has_v1 = true;
  } else if(!va->has_v2) {
    va->v2 = *p;
    va->has_v2 = true;
  } else if(va->current_mode == GL_TRIANGLE_STRIP) {
    if(va->n%2 == 1) {
      va->positions.push_back(va->v1);
      va->positions.push_back(va->v2);
      va->positions.push_back(*p);
    } else {
      va->positions.push_back(va->v2);
      va->positions.push_back(va->v1);
      va->positions.push_back(*p);
    }
    va->v1 = va->v2;
    va->v2 = *p;
  } else { // GL_TRIANGLE_FAN
    va->positions.push_back(va->v1);
    va->positions.push_back(va->v2);
    va->positions.push_back(*p);
    va->v2 = *p;
  }
}

Edit (4): In the end, the error was somewhere else. I must have been tired, I used a std::vector to store the result of the combine function. I don't know why it worked later, but it was sure normal it didn't work the first time! Sorry about that, I'll now close this subject.


Solution

  • OpenGL and GLU are independent from each other. They usually come together, but the GLU tesselation functions work independently from OpenGL.

    That you store the tesselated vertex data is a good thing, that's how it should be done.

    EDIT: answered by first two question edits:

    Did you try to issue a redraw without recreation of the tesselated data?

    It would be much easier to give a good and sensible answer if you'd show the relevant code and its context.

    EDIT: answered by third question edit:

    Next question: What's the signature of beginVA, endVA, vertexVA? And how does vertexVA insert the vertex into VA. How does VA look like? (class, structure definition, etc).

    May I offer a suggestion:

    struct VA
    {
      std::vector<Point3d> positions; // Vertex array
      std::vector<GLuint> triangle_face_indices;
      std::vector<GLuint> tristrip_face_indices;
      std::vector<GLuint> trifan_face_indices;
      GLenum current_mode; // Drawing mode (GL_TRIANGLE, *_FAN or *_STRIP)
    };
    
    void beginVA(GLenum mode, VA *va)
    {
      va->current_mode = mode; // Store the mode
    }
    
    void endVA(VA *va)
    {
      va->current_mode = 0; // Not really necessary, but cleaner
    }
    
    void vertexVA(void *p, VA *va)
    {
      GLuint idx = (GLuint)((intptr_t) p);
    
      switch(va->current_mode) {
      case      GL_TRIANGLES: va->triangle_face_indices.push_back(i); break;
      case GL_TRIANGLE_STRIP: va->tristrip_face_indices.push_back(i); break;
      case   GL_TRIANGLE_FAN: va->trifan_face_indices.push_back(i); break;
      }
    }
    

    used like this

    gluTessBeginPolygon(t, &va);
    gluTessBeginContour(t);
    for(GLuint i = 0; i < va.positions.size(); i++) {
      gluTessVertex(t, const_cast<GLdouble*>(va.positions[i].c_data()), (void*)((intptr_t)i));
    }
    gluTessEndContour(t);
    gluTessEndPolygon(t);
    gluDeleteTess(t);
    

    And draw using 3 consecutive calls to glDrawElements for either draw mode.