📄 bezierpatchmesh.cc
字号:
temp[i] = bpm->UVarray[i];
}
/*deallocate old array*/
free(bpm->UVarray);
/*pointing to the new arrays*/
bpm->UVarray = temp;
}
/*insert the new UV*/
bpm->UVarray[bpm->index_UVarray] = u;
bpm->index_UVarray++;
bpm->UVarray[bpm->index_UVarray] = v;
bpm->index_UVarray++;
/*update counter: one more vertex*/
bpm->counter++;
}
void bezierPatchMeshPrint(bezierPatchMesh *bpm)
{
int i;
printf("the bezier patch is\n");
bezierPatchPrint(bpm->bpatch);
printf("index_length_array= %i\n", bpm->index_length_array);
printf("size_length_array =%i\n", bpm->size_length_array);
printf("index_UVarray =%i\n", bpm->index_UVarray);
printf("size_UVarray =%i\n", bpm->size_UVarray);
printf("UVarray is\n");
for(i=0; i<bpm->index_UVarray; i++)
printf("%f ", bpm->UVarray[i]);
printf("length_array is\n");
for(i=0; i<bpm->index_length_array; i++)
printf("%i ", bpm->length_array[i]);
printf("\n");
}
/*insert a new patch in front of the current linked list and return the new list*/
bezierPatchMesh* bezierPatchMeshListInsert(bezierPatchMesh* list, bezierPatchMesh* bpm)
{
bpm->next=list;
return bpm;
}
/*print all the patches*/
void bezierPatchMeshListPrint(bezierPatchMesh* list)
{
bezierPatchMesh *temp;
for(temp = list; temp != NULL; temp = temp->next)
{
bezierPatchMeshPrint(temp);
}
}
int bezierPatchMeshListTotalStrips(bezierPatchMesh* list)
{
int sum=0;
bezierPatchMesh *temp;
for(temp=list; temp != NULL; temp = temp->next)
{
sum += temp->index_length_array;
}
return sum;
}
int bezierPatchMeshListTotalVert(bezierPatchMesh* list)
{
int sum=0;
bezierPatchMesh *temp;
for(temp=list; temp != NULL; temp = temp->next)
{
sum += temp->index_UVarray;
}
return sum/2;
}
int bezierPatchMeshListNumTriangles(bezierPatchMesh* list)
{
int sum=0;
bezierPatchMesh* temp;
for(temp=list; temp != NULL; temp = temp->next)
{
sum += bezierPatchMeshNumTriangles(temp);
}
return sum;
}
int bezierPatchMeshNumTriangles(bezierPatchMesh* bpm)
{
int i;
int sum=0;
for(i=0; i<bpm->index_length_array; i++)
{
switch(bpm->type_array[i])
{
case GL_TRIANGLES:
sum += bpm->length_array[i]/3;
break;
case GL_TRIANGLE_FAN:
if(bpm->length_array[i] > 2)
sum += bpm->length_array[i]-2;
break;
case GL_TRIANGLE_STRIP:
if(bpm->length_array[i] > 2)
sum += bpm->length_array[i]-2;
break;
case GL_QUAD_STRIP:
if(bpm->length_array[i]>2)
sum += (bpm->length_array[i]-2);
break;
default:
fprintf(stderr,"error in bezierPatchMeshListNumTriangles, type invalid\n");
}
}
return sum;
}
/*delete degenerate triangles*/
void bezierPatchMeshDelDeg(bezierPatchMesh* bpm)
{
if(bpm == NULL) return;
int i,j,k;
int *new_length_array;
GLenum *new_type_array;
int index_new_length_array;
float *new_UVarray;
int index_new_UVarray;
new_length_array = (int*)malloc(sizeof(int) * bpm->index_length_array);
assert(new_length_array);
new_type_array = (GLenum*)malloc(sizeof(GLenum) * bpm->index_length_array);
assert(new_length_array);
new_UVarray = (float*) malloc(sizeof(float) * bpm->index_UVarray);
assert(new_UVarray);
index_new_length_array = 0;
index_new_UVarray=0;
k=0;
for(i=0; i<bpm->index_length_array; i++){
/*(if not degenerate, we have to copy*/
if( (bpm->length_array[i] != 3) || (!isDegenerate(bpm->UVarray+k, bpm->UVarray+k+2, bpm->UVarray+k+4)))
{
for(j=0; j<2* bpm->length_array[i]; j++)
new_UVarray[index_new_UVarray++] = bpm->UVarray[k++];
new_length_array[index_new_length_array] = bpm->length_array[i];
new_type_array[index_new_length_array] = bpm->type_array[i];
index_new_length_array++;
}
else
{
k += 6;
}
}
free(bpm->UVarray);
free(bpm->length_array);
free(bpm->type_array);
bpm->UVarray=new_UVarray;
bpm->length_array=new_length_array;
bpm->type_array=new_type_array;
bpm->index_UVarray = index_new_UVarray;
bpm->index_length_array = index_new_length_array;
}
/*(u,v) to XYZ
*the xyz and normals are stored in vertex_array,
*and normal_array. the spaces of both are allocated here
*/
void bezierPatchMeshEval(bezierPatchMesh* bpm)
{
int i,j,k,l;
float u,v;
float u0 = bpm->bpatch->umin;
float u1 = bpm->bpatch->umax;
int uorder = bpm->bpatch->uorder;
float v0 = bpm->bpatch->vmin;
float v1 = bpm->bpatch->vmax;
int vorder = bpm->bpatch->vorder;
int dimension = bpm->bpatch->dimension;
int ustride = dimension * vorder;
int vstride = dimension;
float *ctlpoints = bpm->bpatch->ctlpoints;
bpm->vertex_array = (float*) malloc(sizeof(float)* (bpm->index_UVarray/2) * 3);
assert(bpm->vertex_array);
bpm->normal_array = (float*) malloc(sizeof(float)* (bpm->index_UVarray/2) * 3);
assert(bpm->normal_array);
k=0;
l=0;
for(i=0; i<bpm->index_length_array; i++)
{
for(j=0; j<bpm->length_array[i]; j++)
{
u = bpm->UVarray[k];
v = bpm->UVarray[k+1];
bezierSurfEval(u0,u1,uorder, v0, v1, vorder, dimension, ctlpoints, ustride, vstride, u,v, bpm->vertex_array+l);
bezierSurfEvalNormal(u0,u1,uorder, v0, v1, vorder, dimension, ctlpoints, ustride, vstride, u,v, bpm->normal_array+l);
k += 2;
l += 3;
}
}
}
void bezierPatchMeshListEval(bezierPatchMesh* list)
{
bezierPatchMesh* temp;
for(temp = list; temp != NULL; temp = temp->next)
{
bezierPatchMeshEval(temp);
}
}
void bezierPatchMeshDraw(bezierPatchMesh* bpm)
{
int i,j,k;
k=0;
/*k is the index of the first component of the current vertex*/
for(i=0; i<bpm->index_length_array; i++)
{
glBegin(bpm->type_array[i]);
for(j=0; j<bpm->length_array[i]; j++)
{
glNormal3fv(bpm->normal_array+k);
glVertex3fv(bpm->vertex_array+k);
k+= 3;
}
glEnd();
}
}
void bezierPatchMeshListDraw(bezierPatchMesh* list)
{
bezierPatchMesh* temp;
for(temp = list; temp != NULL; temp = temp->next)
{
bezierPatchMeshDraw(temp);
}
}
void bezierPatchMeshListCollect(bezierPatchMesh* list, float **vertex_array, float **normal_array, int **length_array, GLenum **type_array, int *num_strips)
{
int i,j,k,l;
bezierPatchMesh *temp;
int total_num_vertices = bezierPatchMeshListTotalVert(list);
(*vertex_array) = (float *) malloc(sizeof(float) * total_num_vertices*3);
assert(*vertex_array);
(*normal_array) = (float *) malloc(sizeof(float) * total_num_vertices*3);
assert(*normal_array);
*num_strips = bezierPatchMeshListTotalStrips(list);
*length_array = (int*) malloc(sizeof(int) * (*num_strips));
assert(*length_array);
*type_array = (GLenum*) malloc(sizeof(GLenum) * (*num_strips));
assert(*type_array);
k=0;
l=0;
for(temp = list; temp != NULL; temp = temp->next)
{
int x=0;
for(i=0; i<temp->index_length_array; i++)
{
for(j=0; j<temp->length_array[i]; j++)
{
(*vertex_array)[k] = temp->vertex_array[x];
(*vertex_array)[k+1] = temp->vertex_array[x+1];
(*vertex_array)[k+2] = temp->vertex_array[x+2];
(*normal_array)[k] = temp->normal_array[x];
(*normal_array)[k+1] = temp->normal_array[x+1];
(*normal_array)[k+2] = temp->normal_array[x+2];
x += 3;
k += 3;
}
(*type_array)[l] = temp->type_array[i];
(*length_array)[l++] = temp->length_array[i];
}
}
}
static int isDegenerate(float A[2], float B[2], float C[2])
{
if( (A[0] == B[0] && A[1]==B[1]) ||
(A[0] == C[0] && A[1]==C[1]) ||
(B[0] == C[0] && B[1]==C[1])
)
return 1;
else
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -