gluTessellator on iPhone Help

Nibbie
Posts: 4
Joined: 2009.05
Post: #1
Hi, I've been working for a while trying to get gluTessellator working in my iPhone app. I feel like what I'm doing should work, and I can't really figure out why it isn't. I'm using the iGLU framework, using the code of a coworker(whose work I'm attempting to move over) as a starting point and this tutorial as a guide. Basically what I've tried to do is to switch out the glBegin/End and use of glVertex to more ES-friendly arrays. I've been trying to do this by dropping the vertices generated by the vertex callback into an array, and then running a draw command at the end. The problem is that I seem to be getting gibberish out of all this.

When I attempt to draw the star from the tutorial using:

Code:
        Poly.Set_Winding_Rule(GLU_TESS_WINDING_POSITIVE);
        Poly.Begin_Polygon();
        Poly.Begin_Contour();
        Poly.Render_Contour(vertices2, 5);
        Poly.End_Contour();
        Poly.End_Polygon();

With this implementation:

Code:
#import "myTesselator.h"

GLdouble *vertices;
int vertNum = 0;
GLenum    glType;


static    GLUtesselator    *tobj; // the tessellation object


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


int Tess_Poly::Init(GLvoid)
{
    // Create a new tessellation object
    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 = (GLdouble*) malloc( sizeof( GLdouble* ) * 75 );
    
    return(1);
}



int Tess_Poly::Set_Winding_Rule(GLenum winding_rule)
{
    // Set the winding rule
    gluTessProperty(tobj, GLU_TESS_WINDING_RULE, winding_rule);
    
    return(1);
}




int Tess_Poly::Render_Contour(GLdouble **obj_data, 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 Tess_Poly::Begin_Polygon(GLvoid)
{
    gluTessBeginPolygon(tobj, NULL);
    
    return(1);
}


int Tess_Poly::End_Polygon(GLvoid)
{
    gluTessEndPolygon(tobj);
    
    return(1);
}


int Tess_Poly::Begin_Contour(GLvoid)
{
    gluTessBeginContour(tobj);
    
    return(1);
}




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


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


void combineCallback(GLdouble coords[3], GLdouble *vertex_data[4], GLfloat weight[4], GLdouble **dataOut)
{
    GLdouble *vertex;
    vertex = (GLdouble *) malloc(6 * sizeof(GLdouble));
    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)
{
    GLdouble *ptr;
    
    ptr = (GLdouble *) vertex;
//    glVertex3dv((GLdouble *) ptr);
    
    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(GLvoid)
{
    glVertexPointer(3, GL_FLOAT, 0, vertices);
    glDrawArrays(glType, 0, vertNum + 1);
    
    NSLog(@"Vert Num: %i", vertNum + 1);
    NSLog(@"Vertex1: %f, %f, %f", vertices[0], vertices[1], vertices[2]);
    NSLog(@"Vertex2: %f, %f, %f", vertices[3], vertices[4], vertices[5]);
    NSLog(@"Vertex3: %f, %f, %f", vertices[6], vertices[7], vertices[8]);
    NSLog(@"Vertex4: %f, %f, %f", vertices[9], vertices[10], vertices[11]);
    NSLog(@"Vertex5: %f, %f, %f", vertices[12], vertices[13], vertices[14]);
    NSLog(@"Vertex6: %f, %f, %f", vertices[15], vertices[16], vertices[17]);
    NSLog(@"Vertex7: %f, %f, %f", vertices[16], vertices[17], vertices[18]);
    NSLog(@"Vertex8: %f, %f, %f", vertices[19], vertices[20], vertices[21]);
    NSLog(@"Vertex9: %f, %f, %f", vertices[22], vertices[23], vertices[24]);
    NSLog(@"Vertex10: %f, %f, %f", vertices[25], vertices[26], vertices[27]);
    NSLog(@"Vertex11: %f, %f, %f", vertices[26], vertices[27], vertices[28]);
    NSLog(@"Vertex12: %f, %f, %f", vertices[29], vertices[30], vertices[31]);

    
    vertNum = 0;

}

The result looks something like this:

Code:
2009-05-20 15:15:56.804 iPhone Map[64951:20b] Vert Num: 8
2009-05-20 15:15:56.805 iPhone Map[64951:20b] Vertex1: 2.100000, 0.900000, 0.000000
2009-05-20 15:15:56.807 iPhone Map[64951:20b] Vertex2: 1.725000, 0.650000, 0.000000
2009-05-20 15:15:56.810 iPhone Map[64951:20b] Vertex3: 1.600000, 0.900000, 0.000000
2009-05-20 15:15:56.811 iPhone Map[64951:20b] Vertex4: 1.350000, 0.400000, 0.000000
2009-05-20 15:15:56.811 iPhone Map[64951:20b] Vertex5: 1.100000, 0.900000, 0.000000
2009-05-20 15:15:56.812 iPhone Map[64951:20b] Vertex6: 0.975000, 0.650000, 0.000000
2009-05-20 15:15:56.812 iPhone Map[64951:20b] Vertex7: 0.650000, 0.000000, 0.600000
2009-05-20 15:15:56.812 iPhone Map[64951:20b] Vertex8: 0.900000, 0.000000, 0.000000
2009-05-20 15:15:56.813 iPhone Map[64951:20b] Vertex9: 0.000000, 0.000000, 0.000000
2009-05-20 15:15:56.813 iPhone Map[64951:20b] Vertex10: 0.000000, 0.000000, 0.000000
2009-05-20 15:15:56.813 iPhone Map[64951:20b] Vertex11: 0.000000, 0.000000, 0.000000
2009-05-20 15:15:56.814 iPhone Map[64951:20b] Vertex12: 0.000000, 0.000000, 0.000000
2009-05-20 15:15:56.816 iPhone Map[64951:20b] Vert Num: 10
2009-05-20 15:15:56.823 iPhone Map[64951:20b] Vertex1: 2.100000, -0.100000, 0.000000
2009-05-20 15:15:56.824 iPhone Map[64951:20b] Vertex2: 1.350000, 0.400000, 0.000000
2009-05-20 15:15:56.824 iPhone Map[64951:20b] Vertex3: 1.725000, 0.650000, 0.000000
2009-05-20 15:15:56.825 iPhone Map[64951:20b] Vertex4: 1.600000, 0.900000, 0.000000
2009-05-20 15:15:56.825 iPhone Map[64951:20b] Vertex5: 1.100000, 0.900000, 0.000000
2009-05-20 15:15:56.826 iPhone Map[64951:20b] Vertex6: 1.350000, 1.400000, 0.000000
2009-05-20 15:15:56.826 iPhone Map[64951:20b] Vertex7: 1.400000, 0.000000, 1.350000
2009-05-20 15:15:56.827 iPhone Map[64951:20b] Vertex8: 0.400000, 0.000000, 0.600000
2009-05-20 15:15:56.827 iPhone Map[64951:20b] Vertex9: -0.100000, 0.000000, 0.975000
2009-05-20 15:15:56.827 iPhone Map[64951:20b] Vertex10: 0.650000, 0.000000, 0.000000
2009-05-20 15:15:56.828 iPhone Map[64951:20b] Vertex11: 0.000000, 0.000000, 0.000000
2009-05-20 15:15:56.829 iPhone Map[64951:20b] Vertex12: 0.000000, 0.000000, 0.000000

[Image: 92fmr.png]

Is there any glaring error in this that I'm missing, or is there a working iPhone implementation available somewhere?
Quote this message in a reply
Sage
Posts: 1,234
Joined: 2002.10
Post: #2
GLdouble doesn't exist in OpenGL ES.

But assuming that you got that code to compile, you're asking GL to interpret doubles as floats. Which will be gibberish.
Quote this message in a reply
Member
Posts: 25
Joined: 2009.05
Post: #3
Quote:I'm using the iGLU framework

Hm, google knows nothing about iGLU framework. Can you post direct link on that?
Thanks.
Quote this message in a reply
Nibbie
Posts: 4
Joined: 2009.05
Post: #4
arekkusu Wrote:GLdouble doesn't exist in OpenGL ES.

But assuming that you got that code to compile, you're asking GL to interpret doubles as floats. Which will be gibberish.

It does exist as part of the GLU library. It did just occurred to me that the vertices array is all GLdoubles, but then I'm telling glVertexPointer that the values are floats, so that might be a problem. I'll look into it soon when I get back around to working with this code.

ShiftZ Wrote:Hm, google knows nothing about iGLU framework. Can you post direct link on that?
Thanks.

http://code.google.com/p/iphone-glu/
Quote this message in a reply
Nibbie
Posts: 4
Joined: 2009.05
Post: #5
Ha! Thank you so much! That was exactly the problem, and double->float was exactly the issue. It makes so much sense now. I added a conversion and my draw method looks like this now:

Code:
void draw(GLvoid)
{
    GLfloat *verticesF;
    verticesF = (GLfloat*) malloc( sizeof( GLfloat* ) * 75 );
    
    for (int i = 0; i < 75; i++)
         verticesF[i] = (float) vertices[i];
    
    
    glVertexPointer(3, GL_FLOAT, 0, verticesF);
    glDrawArrays(glType, 0, vertNum);
    
    vertNum = 0;

}

And I have a star:
[Image: 770x1.png]

Thanks again!
Quote this message in a reply
Nibbie
Posts: 4
Joined: 2009.05
Post: #6
Oh and this is an idea of how this will be used in the final product (It's clearly necessary in this case Smile) :

[Image: rxuu4.png]

And this is sort of the goal look based on what I'm porting: http://imgur.com/kedxn.png

Now to get knee deep in some buffering work to keep this baby from killing itself Smile.
Quote this message in a reply
Post Reply