views:

12

answers:

0

Hello,

I am trying to use polygon Tesselation with iGLU : http://code.google.com/p/iphone-glu/ But can't find out how does it work (with OpenGLES) ?

So I have a 2D (x and y) polygon declare as follow :

// *2 because x and y
GLfloat poly[nbOfPoint*2] = {1.0, 0.0,
                             0.0, 1.0, etc..}

So how can I triangulate my polygon ? If you have any example I will be so happy ^^.

Thank you !

Edit : I take a look at : http://flipcode.net/archives/Efficient_Polygon_Triangulation.shtml but still don't understanding how does it work... because it's for openGL and not OpenGLES (exemple : there is no glBegin in ES).

Edit2 : So here is what I have (try to) done but nothing appear on screen :( :

- (void)drawView
{
  glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
  glViewport(0, 0, backingWidth, backingHeight);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glMatrixMode(GL_PROJECTION);  
  glLoadIdentity();

  GLdouble poly[4][2] = {{0.0, 0.0},
                        {-1.0, 0.0},
                        {0.0, 1.0},
                        {-1.0, -1.0}};

  Tesselation tess;
  tess.Init();
  tess.Set_Winding_Rule(GLU_TESS_WINDING_POSITIVE);
  tess.Begin_Polygon();
  tess.Begin_Contour();
  tess.Render_Contour(poly, 2);
  tess.End_Contour();
  tess.End_Polygon();


  glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);    
  [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

And here is the implementation about Tesselation (that I found on an other forum) :

#include "glu.h"

class Tesselation
{  
  public:

  int Init();
  int Set_Winding_Rule(GLenum winding_rule);
  int Render_Contour(GLdouble obj_data[][2], int num_vertices);
  int Begin_Polygon();
  int End_Polygon();
  int Begin_Contour();
  int End_Contour();
  int End();
};

And .cpp :

#include "Tesselation.h"
#include <cstdlib>

GLfloat *vertices;
int vertNum = 0;
GLenum  glType;
static  GLUtesselator   *tobj;

void combineCallback(GLfloat coords[3], GLfloat *vertex_data[4], GLfloat weight[4], GLfloat **dataOut);
void vertexCallback(GLvoid *vertex);
void tessBegin(GLenum type );
void draw();

int Tesselation::Init()
{
  tobj = gluNewTess();

  // Set callback functions
  gluTessCallback(tobj, GLU_TESS_VERTEX, (GLvoid (*) ( )) &vertexCallback);
  gluTessCallback(tobj, GLU_TESS_BEGIN, (GLvoid (*) ( )) &tessBegin);
  gluTessCallback(tobj, GLU_TESS_END, (GLvoid (*) ( )) &draw);
  gluTessCallback(tobj, GLU_TESS_COMBINE, (GLvoid (*) ( ))&combineCallback);

  vertices = (GLfloat*) malloc( sizeof( GLfloat* ) * 75 );
  return 1;
}

int Tesselation::Set_Winding_Rule(GLenum winding_rule)
{
  gluTessProperty(tobj, GLU_TESS_WINDING_RULE, winding_rule); 

    return(1);
}

int Tesselation::Render_Contour(GLdouble obj_data[][2], int num_vertices)
{
  for(int x = 0; x < num_vertices; x++) //loop through the vertices
    {
        gluTessVertex(tobj, obj_data[x], obj_data[x]); //store the vertex
    }   
    return(1);
}

int Tesselation::Begin_Polygon()
{
  gluTessBeginPolygon(tobj, NULL);
    return(1);
}

int Tesselation::End_Polygon()
{
  gluTessEndPolygon(tobj);
    return(1);
}
int Tesselation::Begin_Contour()
{
  gluTessBeginContour(tobj);
    return(1);
}

int Tesselation::End_Contour()
{
  gluTessEndContour(tobj);
    return(1);
}

int Tesselation::End()
{
  gluDeleteTess(tobj);
    return(1);
}

void combineCallback(GLfloat coords[3], GLfloat *vertex_data[4], GLfloat weight[4], GLfloat **dataOut)
{
    GLfloat *vertex;
    vertex = (GLfloat *) malloc(6 * sizeof(GLfloat));
    vertex[0] = coords[0];
    vertex[1] = coords[1];
    vertex[2] = coords[2];

    for (int i = 3; i < 6; i++) {
        vertex[i] = weight[0] * ((weight[0] > 0.0f) ? vertex_data[0][i] : 0.0) +
        weight[1] * ((weight[1] > 0.0f) ? vertex_data[1][i] : 0.0) +
        weight[2] * ((weight[2] > 0.0f) ? vertex_data[2][i] : 0.0) +
        weight[3] * ((weight[3] > 0.0f) ? vertex_data[3][i] : 0.0);
    }


    *dataOut = vertex;
}

void vertexCallback(GLvoid *vertex)
{
    GLfloat *ptr;

    ptr = (GLfloat *) vertex;

    vertices[vertNum * 3] = ptr[0];
    vertices[vertNum * 3 + 1] = ptr[1];
    vertices[vertNum * 3 + 2] = ptr[2]; 

    vertNum++;
}

void tessBegin(GLenum type)
{
    glType = type;  
}

void draw()
{
  GLfloat *verticesF;
    verticesF = (GLfloat*) malloc( sizeof( GLfloat* ) * 75 );

    for (int i = 0; i < 75; i++) verticesF[i] = (float) vertices[i];

    glVertexPointer(2, GL_FLOAT, 0, verticesF);
    glDrawArrays(glType, 0, vertNum);

    vertNum = 0;
}