42 #include <libnurbs2.h>
44 #include "bezierEval.h"
45 #include "bezierPatchMesh.h"
47 static int isDegenerate(
float A[2],
float B[2],
float C[2]);
49 void drawStrips(
float *vertex_array,
float *normal_array,
int *length_array, GLenum *type_array,
int num_strips)
55 for(i=0; i<num_strips; i++)
57 glBegin(type_array[i]);
58 for(j=0; j<length_array[i]; j++)
60 glNormal3fv(normal_array+k);
61 glVertex3fv(vertex_array+k);
72 for(temp=list; temp != NULL; temp = temp->next)
74 bezierPatchMeshDelDeg(temp);
80 if(list == NULL)
return;
81 bezierPatchMeshListDelete(list->next);
82 bezierPatchMeshDelete(list);
93 for(temp = list; temp != NULL; temp = nextone)
96 ret=bezierPatchMeshListInsert(ret, temp);
103 bezierPatchMesh *bezierPatchMeshMake(
int maptype,
float umin,
float umax,
int ustride,
int uorder,
float vmin,
float vmax,
int vstride,
int vorder,
float *ctlpoints,
int size_UVarray,
int size_length_array)
110 if(maptype == GL_MAP2_VERTEX_3) dimension = 3;
111 else if (maptype==GL_MAP2_VERTEX_4) dimension = 4;
113 fprintf(stderr,
"error in inMap2f, maptype=%i is wrong, maptype,map is invalid\n", maptype);
120 ret->bpatch_normal = NULL;
121 ret->bpatch_color = NULL;
122 ret->bpatch_texcoord = NULL;
123 ret->bpatch = bezierPatchMake(umin, vmin, umax, vmax, uorder, vorder, dimension);
126 the_ustride = vorder * dimension;
127 the_vstride = dimension;
128 for(i=0; i<uorder; i++)
129 for(j=0; j<vorder; j++)
130 for(k=0; k<dimension; k++)
131 ret->bpatch->ctlpoints[i * the_ustride + j*the_vstride+k] = ctlpoints[i*ustride+j*vstride+k];
134 ret->size_UVarray = size_UVarray;
135 ret->size_length_array = size_length_array;
136 ret->UVarray = (
float*) malloc(
sizeof(
float) * size_UVarray);
137 assert(ret->UVarray);
138 ret->length_array = (
int *)malloc(
sizeof(
int) * size_length_array);
139 assert(ret->length_array);
140 ret->type_array = (GLenum *)malloc(
sizeof(GLenum) * size_length_array);
141 assert(ret->type_array);
143 ret->index_UVarray = 0;
144 ret->index_length_array = 0;
146 ret->vertex_array = NULL;
147 ret->normal_array = NULL;
148 ret->color_array = NULL;
149 ret->texcoord_array = NULL;
155 bezierPatchMesh *bezierPatchMeshMake2(
int size_UVarray,
int size_length_array)
161 ret->bpatch_normal = NULL;
162 ret->bpatch_color = NULL;
163 ret->bpatch_texcoord = NULL;
165 ret->size_UVarray = size_UVarray;
166 ret->size_length_array = size_length_array;
167 ret->UVarray = (
float*) malloc(
sizeof(
float) * size_UVarray);
168 assert(ret->UVarray);
169 ret->length_array = (
int *)malloc(
sizeof(
int) * size_length_array);
170 assert(ret->length_array);
171 ret->type_array = (GLenum *)malloc(
sizeof(GLenum) * size_length_array);
172 assert(ret->type_array);
174 ret->index_UVarray = 0;
175 ret->index_length_array = 0;
177 ret->vertex_array = NULL;
178 ret->normal_array = NULL;
179 ret->color_array = NULL;
180 ret->texcoord_array = NULL;
186 void bezierPatchMeshPutPatch(
bezierPatchMesh *bpm,
int maptype,
float umin,
float umax,
int ustride,
int uorder,
float vmin,
float vmax,
int vstride,
int vorder,
float *ctlpoints)
189 case GL_MAP2_VERTEX_3:
190 bpm->bpatch = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 3, ustride, vstride, ctlpoints);
192 case GL_MAP2_VERTEX_4:
193 bpm->bpatch = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 4,ustride, vstride, ctlpoints );
196 bpm->bpatch_normal = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 3, ustride, vstride, ctlpoints);
199 bpm->bpatch_color = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 1, ustride, vstride, ctlpoints);
201 case GL_MAP2_COLOR_4:
202 bpm->bpatch_color = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 4, ustride, vstride, ctlpoints);
204 case GL_MAP2_TEXTURE_COORD_1:
205 bpm->bpatch_texcoord = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 1, ustride, vstride, ctlpoints);
207 case GL_MAP2_TEXTURE_COORD_2:
208 bpm->bpatch_texcoord = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 2, ustride, vstride, ctlpoints);
210 case GL_MAP2_TEXTURE_COORD_3:
211 bpm->bpatch_texcoord = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 3, ustride, vstride, ctlpoints);
213 case GL_MAP2_TEXTURE_COORD_4:
214 bpm->bpatch_texcoord = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 4, ustride, vstride, ctlpoints);
217 fprintf(stderr,
"error in bezierPatchMeshPutPatch, maptype=%i is wrong, maptype,map is invalid\n", maptype);
228 if(bpm->bpatch != NULL)
229 bezierPatchDelete(bpm->bpatch);
230 if(bpm->bpatch_normal != NULL)
231 bezierPatchDelete(bpm->bpatch_normal);
232 if(bpm->bpatch_color != NULL)
233 bezierPatchDelete(bpm->bpatch_color);
234 if(bpm->bpatch_texcoord != NULL)
235 bezierPatchDelete(bpm->bpatch_texcoord);
238 free(bpm->length_array);
239 free(bpm->vertex_array);
240 free(bpm->normal_array);
241 free(bpm->type_array);
260 if(bpm->counter == 0)
return;
263 if(bpm->index_length_array >= bpm->size_length_array)
265 int *temp = (
int*) malloc(
sizeof(
int) * (bpm->size_length_array*2 + 1));
267 GLenum *temp_type = (GLenum*) malloc(
sizeof(GLenum) * (bpm->size_length_array*2 + 1));
270 bpm->size_length_array = bpm->size_length_array*2 + 1;
273 for(i=0; i<bpm->index_length_array; i++)
275 temp[i] = bpm->length_array[i];
276 temp_type[i] = bpm->type_array[i];
280 free(bpm->length_array);
281 free(bpm->type_array);
284 bpm->length_array = temp;
285 bpm->type_array = temp_type;
287 bpm->type_array[bpm->index_length_array] = bpm->type;
288 bpm->length_array[bpm->index_length_array++] = bpm->counter;
297 if(bpm->index_UVarray+1 >= bpm->size_UVarray)
299 float *temp = (
float*) malloc(
sizeof(
float) * (bpm->size_UVarray * 2 + 2));
303 bpm->size_UVarray = bpm->size_UVarray*2 + 2;
306 for(i=0; i<bpm->index_UVarray; i++)
308 temp[i] = bpm->UVarray[i];
318 bpm->UVarray[bpm->index_UVarray] = u;
319 bpm->index_UVarray++;
320 bpm->UVarray[bpm->index_UVarray] = v;
321 bpm->index_UVarray++;
332 printf(
"the bezier patch is\n");
333 bezierPatchPrint(bpm->bpatch);
334 printf(
"index_length_array= %i\n", bpm->index_length_array);
335 printf(
"size_length_array =%i\n", bpm->size_length_array);
336 printf(
"index_UVarray =%i\n", bpm->index_UVarray);
337 printf(
"size_UVarray =%i\n", bpm->size_UVarray);
338 printf(
"UVarray is\n");
339 for(i=0; i<bpm->index_UVarray; i++)
340 printf(
"%f ", bpm->UVarray[i]);
342 printf(
"length_array is\n");
343 for(i=0; i<bpm->index_length_array; i++)
344 printf(
"%i ", bpm->length_array[i]);
360 for(temp = list; temp != NULL; temp = temp->next)
362 bezierPatchMeshPrint(temp);
370 for(temp=list; temp != NULL; temp = temp->next)
372 sum += temp->index_length_array;
381 for(temp=list; temp != NULL; temp = temp->next)
383 sum += temp->index_UVarray;
392 for(temp=list; temp != NULL; temp = temp->next)
394 sum += bezierPatchMeshNumTriangles(temp);
403 for(i=0; i<bpm->index_length_array; i++)
405 switch(bpm->type_array[i])
408 sum += bpm->length_array[i]/3;
410 case GL_TRIANGLE_FAN:
411 if(bpm->length_array[i] > 2)
412 sum += bpm->length_array[i]-2;
414 case GL_TRIANGLE_STRIP:
415 if(bpm->length_array[i] > 2)
416 sum += bpm->length_array[i]-2;
419 if(bpm->length_array[i]>2)
420 sum += (bpm->length_array[i]-2);
423 fprintf(stderr,
"error in bezierPatchMeshListNumTriangles, type invalid\n");
432 if(bpm == NULL)
return;
434 int *new_length_array;
435 GLenum *new_type_array;
436 int index_new_length_array;
438 int index_new_UVarray;
440 new_length_array = (
int*)malloc(
sizeof(
int) * bpm->index_length_array);
441 assert(new_length_array);
442 new_type_array = (GLenum*)malloc(
sizeof(GLenum) * bpm->index_length_array);
443 assert(new_length_array);
444 new_UVarray = (
float*) malloc(
sizeof(
float) * bpm->index_UVarray);
447 index_new_length_array = 0;
450 for(i=0; i<bpm->index_length_array; i++){
453 if( (bpm->length_array[i] != 3) || (!isDegenerate(bpm->UVarray+k, bpm->UVarray+k+2, bpm->UVarray+k+4)))
455 for(j=0; j<2* bpm->length_array[i]; j++)
456 new_UVarray[index_new_UVarray++] = bpm->UVarray[k++];
458 new_length_array[index_new_length_array] = bpm->length_array[i];
459 new_type_array[index_new_length_array] = bpm->type_array[i];
460 index_new_length_array++;
468 free(bpm->length_array);
469 free(bpm->type_array);
470 bpm->UVarray=new_UVarray;
471 bpm->length_array=new_length_array;
472 bpm->type_array=new_type_array;
473 bpm->index_UVarray = index_new_UVarray;
474 bpm->index_length_array = index_new_length_array;
486 float u0 = bpm->bpatch->umin;
487 float u1 = bpm->bpatch->umax;
488 int uorder = bpm->bpatch->uorder;
489 float v0 = bpm->bpatch->vmin;
490 float v1 = bpm->bpatch->vmax;
491 int vorder = bpm->bpatch->vorder;
492 int dimension = bpm->bpatch->dimension;
493 int ustride = dimension * vorder;
494 int vstride = dimension;
495 float *ctlpoints = bpm->bpatch->ctlpoints;
497 bpm->vertex_array = (
float*) malloc(
sizeof(
float)* (bpm->index_UVarray/2) * 3);
498 assert(bpm->vertex_array);
499 bpm->normal_array = (
float*) malloc(
sizeof(
float)* (bpm->index_UVarray/2) * 3);
500 assert(bpm->normal_array);
504 for(i=0; i<bpm->index_length_array; i++)
506 for(j=0; j<bpm->length_array[i]; j++)
509 v = bpm->UVarray[k+1];
510 bezierSurfEval(u0,u1,uorder, v0, v1, vorder, dimension, ctlpoints, ustride, vstride, u,v, bpm->vertex_array+l);
511 bezierSurfEvalNormal(u0,u1,uorder, v0, v1, vorder, dimension, ctlpoints, ustride, vstride, u,v, bpm->normal_array+l);
521 for(temp = list; temp != NULL; temp = temp->next)
523 bezierPatchMeshEval(temp);
533 for(i=0; i<bpm->index_length_array; i++)
535 glBegin(bpm->type_array[i]);
536 for(j=0; j<bpm->length_array[i]; j++)
538 glNormal3fv(bpm->normal_array+k);
539 glVertex3fv(bpm->vertex_array+k);
550 for(temp = list; temp != NULL; temp = temp->next)
552 bezierPatchMeshDraw(temp);
556 void bezierPatchMeshListCollect(
bezierPatchMesh* list,
float **vertex_array,
float **normal_array,
int **length_array, GLenum **type_array,
int *num_strips)
560 int total_num_vertices = bezierPatchMeshListTotalVert(list);
561 (*vertex_array) = (
float *) malloc(
sizeof(
float) * total_num_vertices*3);
562 assert(*vertex_array);
563 (*normal_array) = (
float *) malloc(
sizeof(
float) * total_num_vertices*3);
564 assert(*normal_array);
566 *num_strips = bezierPatchMeshListTotalStrips(list);
568 *length_array = (
int*) malloc(
sizeof(
int) * (*num_strips));
569 assert(*length_array);
571 *type_array = (GLenum*) malloc(
sizeof(GLenum) * (*num_strips));
576 for(temp = list; temp != NULL; temp = temp->next)
579 for(i=0; i<temp->index_length_array; i++)
581 for(j=0; j<temp->length_array[i]; j++)
583 (*vertex_array)[k] = temp->vertex_array[x];
584 (*vertex_array)[k+1] = temp->vertex_array[x+1];
585 (*vertex_array)[k+2] = temp->vertex_array[x+2];
587 (*normal_array)[k] = temp->normal_array[x];
588 (*normal_array)[k+1] = temp->normal_array[x+1];
589 (*normal_array)[k+2] = temp->normal_array[x+2];
594 (*type_array)[l] = temp->type_array[i];
595 (*length_array)[l++] = temp->length_array[i];
602 static int isDegenerate(
float A[2],
float B[2],
float C[2])
604 if( (A[0] == B[0] && A[1]==B[1]) ||
605 (A[0] == C[0] && A[1]==C[1]) ||
606 (B[0] == C[0] && B[1]==C[1])