PDA

View Full Version : Tessellations and textures



atteal
12-15-2010, 08:23 AM
Hi i am trying to use tessellation in opengl in order to draw concave polygons through JNI calls. As regards tessellation the shape is perfectly drawn however i tried to map a texture on to it rather than just fill it with a solid colour however the shape is just white there is no texture onto it. Do you have any hints about this or maybe links to a tutorial using textures with tessellations? I enclosed the code below:



void create_tessellator()
{
//create new tesselator object
tess = gluNewTess();

//register call back functions
gluTessCallback(tess, GLU_TESS_BEGIN, glBegin);
gluTessCallback(tess, GLU_TESS_END, glEnd);
gluTessCallback(tess, GLU_TESS_VERTEX, glVertex3dv);

}

void CALLBACK vertexCallback(GLvoid *vertexdata)
{

struct vertexData * pointer;
GLfloat xmin, xmax, ymin, ymax,xrange,yrange,xcoor, ycoor;

pointer = (struct vertexData *) vertexdata;

//printf("value is: %f\n",*((GLdouble*) (pointer->vertex) ));

xmin = pointer->xmin;
ymin = pointer->ymin;
xrange = pointer->xrange;
yrange = pointer->yrange;


xcoor = (*((GLdouble*)(pointer->vertex))-xmin)/xrange;
ycoor = (*( (GLdouble*)(pointer->vertex) + 1)-ymin)/yrange;


printf("*************\nvalue of x: %f\t y:%f\n", *((GLdouble*)(pointer->vertex)), *((GLdouble*)(pointer->vertex)+1));

printf("xcoor=%f\tycoor=%f\n",xcoor,ycoor);


glEnable(GL_TEXTURE_2D);

//using the texture
glBindTexture(GL_TEXTURE_2D, texName);


//setting parameters for the texture environment (how it will replace the surface values with the taxture values)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);


glTexCoord2f((GLfloat)xcoor,(GLfloat) ycoor);
glVertex3dv(pointer->vertex);

}


JNIEXPORT void JNICALL Java_DrawingFrame_polygonCB
(JNIEnv *env, jobject canvas, jfloat xstart, jfloat ystart, jint brop2, jint fillmode, jint colr, jint colg, jint colb, jint numdelta, jdoubleArray deltaentries)
{
float xmin,ymin,xmax, ymax, xrange, yrange;
struct vertexData vdata;
struct vertexData* vdataptr;


if (tess == NULL)
create_tessellator(); //tesselator has to be created only nce for all the shapes that need to be drawn

gluTessCallback(tess, GLU_TESS_VERTEX, vertexCallback);

texinit(1, 1, 255,0, 0,0, 255, 0);
if (tess == NULL)
create_tessellator(); //tesselator has to be created only nce for all the shapes that need to be drawn


jdouble* data = (*env)->GetDoubleArrayElements(env, deltaentries, 0);

// define concave quad with a hole as the one below as example
// 1----------2
// | 7----6 |
// | | | |
// | /4----5 |
// 0/---------3

//glEnable(GL_COLOR_LOGIC_OP);
//glLogicOp(brop2);

GLdouble points[numdelta][3];

for(int i=0;i<=numdelta-2;i+=2)
{
if(i==0)
{
xmin = *data;
xmax = *data;
}
else
{
if(*(data + i) < xmin)
xmin = *(data + i);
if(*(data + i) > xmax)
xmax = *(data + i);
}
}

for(int i=1;i<=numdelta-1;i+=2)
{
if(i==1)
{
ymin = *data;
ymax = *data;
}
else
{
if(*(data + i) < ymin)
ymin = *(data + i);
if(*(data + i) > ymax)
ymax = *(data + i);
}
}

//printf("xmin: %f\txmax: %f\tymin: %f\tymax: %f\t\n", xmin, xmax, ymin, ymax);

xrange = xmax - xmin;
yrange = ymax - ymin;

printf("fillmode= %d\n", fillmode);
gluTessProperty(tess, GLU_TESS_WINDING_RULE,
fillmode == 1 ?GLU_TESS_WINDING_ODD:GLU_TESS_WINDING_NONZERO);
//glColor3ub(colr, colg, colb);

gluTessBeginPolygon(tess, NULL); // with NULL data
gluTessBeginContour(tess); // outer quad

for (int i = 0; i < numdelta ; i+=2)
{
points[i][0] = *(data +i);
points[i][1] = *(data +i+1);
points[i][2] = 0;


vdataptr = malloc(sizeof(struct vertexData));
vdataptr->vertex = points[i];
vdataptr->xmin = xmin;
vdataptr->ymin = ymin;
vdataptr->xrange = xrange;
vdataptr->yrange = yrange;

printf("1:%f\t 2:%f\t 3:%f\t\n", points[i][0], points[i][1],points[i][2]);
gluTessVertex(tess, points[i],vdataptr);
}

gluTessEndContour(tess);
gluTessEndPolygon(tess);


//glDisable(GL_COLOR_LOGIC_OP);


}