summaryrefslogtreecommitdiff
path: root/src/glu/sgi/libnurbs/interface/bezierPatchMesh.cc
blob: ac7ff84fc4ae950fd428408d1738ba2e61d29b63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
** License Applicability. Except to the extent portions of this file are
** made subject to an alternative license as permitted in the SGI Free
** Software License B, Version 1.1 (the "License"), the contents of this
** file are subject only to the provisions of the License. You may not use
** this file except in compliance with the License. You may obtain a copy
** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
** 
** http://oss.sgi.com/projects/FreeB
** 
** Note that, as provided in the License, the Software is distributed on an
** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
** 
** Original Code. The Original Code is: OpenGL Sample Implementation,
** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
** Copyright in any portions created by third parties is as indicated
** elsewhere herein. All Rights Reserved.
** 
** Additional Notice Provisions: The application programming interfaces
** established by SGI in conjunction with the Original Code are The
** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
** Window System(R) (Version 1.3), released October 19, 1998. This software
** was created using the OpenGL(R) version 1.2.1 Sample Implementation
** published by SGI, but has not been independently verified as being
** compliant with the OpenGL(R) version 1.2.1 Specification.
**
*/
/*
*/

#include "gluos.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <GL/gl.h>
#include "bezierEval.h"
#include "bezierPatchMesh.h"

static int isDegenerate(float A[2], float B[2], float C[2]);

void drawStrips(float *vertex_array, float *normal_array, int *length_array, GLenum *type_array, int num_strips)
{
  int i,j,k;
  k=0;
  /*k is the index of the first component of the current vertex*/
  for(i=0; i<num_strips; i++)
    {
      glBegin(type_array[i]);
      for(j=0; j<length_array[i]; j++)
	{
	  glNormal3fv(normal_array+k);
	  glVertex3fv(vertex_array+k);
	  k += 3;
	}
      glEnd();
    }
}

void bezierPatchMeshListDelDeg(bezierPatchMesh* list)
{
  bezierPatchMesh* temp;
  for(temp=list; temp != NULL; temp = temp->next)
    {
      bezierPatchMeshDelDeg(temp);
    }
}

void bezierPatchMeshListDelete(bezierPatchMesh *list)
{
  if(list == NULL) return;
  bezierPatchMeshListDelete(list->next);
  bezierPatchMeshDelete(list);  
}




bezierPatchMesh* bezierPatchMeshListReverse(bezierPatchMesh* list)
{
 bezierPatchMesh* ret=NULL;
 bezierPatchMesh* temp;
 bezierPatchMesh* nextone;
  for(temp = list; temp != NULL; temp = nextone)
    {
      nextone = temp->next;
      ret=bezierPatchMeshListInsert(ret, temp);
    }
 return ret;
}

/*maptype is either GL_MAP2_VERTEX_3 or GL_MAP2_VERTEX_4
 */
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)
{
  int i,j,k;
  int dimension;
  int the_ustride;
  int the_vstride;

  if(maptype == GL_MAP2_VERTEX_3) dimension = 3;
  else if (maptype==GL_MAP2_VERTEX_4) dimension = 4;
  else {
    fprintf(stderr, "error in inMap2f, maptype=%i is wrong, maptype,map is invalid\n", maptype);
    return NULL;
  }

  bezierPatchMesh *ret = (bezierPatchMesh*) malloc(sizeof(bezierPatchMesh));
  assert(ret);

  ret->bpatch_normal = NULL;
  ret->bpatch_color  = NULL;
  ret->bpatch_texcoord = NULL;
  ret->bpatch = bezierPatchMake(umin, vmin, umax, vmax, uorder, vorder, dimension);

  /*copy the control points there*/
  the_ustride = vorder * dimension;
  the_vstride = dimension;
  for(i=0; i<uorder; i++)
    for(j=0; j<vorder; j++)
      for(k=0; k<dimension; k++)
	ret->bpatch->ctlpoints[i * the_ustride + j*the_vstride+k] = ctlpoints[i*ustride+j*vstride+k];
  

  ret->size_UVarray = size_UVarray;
  ret->size_length_array = size_length_array;
  ret->UVarray = (float*) malloc(sizeof(float) * size_UVarray);
  assert(ret->UVarray);
  ret->length_array = (int *)malloc(sizeof(int) * size_length_array);
  assert(ret->length_array);
  ret->type_array = (GLenum *)malloc(sizeof(GLenum) * size_length_array);
  assert(ret->type_array);

  ret->index_UVarray = 0;
  ret->index_length_array = 0;

  ret->vertex_array = NULL;
  ret->normal_array = NULL;
  ret->color_array  = NULL;
  ret->texcoord_array = NULL;

  ret->next = NULL;
  return ret;
}

bezierPatchMesh *bezierPatchMeshMake2(int size_UVarray, int size_length_array)
{
  bezierPatchMesh *ret = (bezierPatchMesh*) malloc(sizeof(bezierPatchMesh));
  assert(ret);

  ret->bpatch = NULL;
  ret->bpatch_normal = NULL;
  ret->bpatch_color  = NULL;
  ret->bpatch_texcoord = NULL;

  ret->size_UVarray = size_UVarray;
  ret->size_length_array = size_length_array;
  ret->UVarray = (float*) malloc(sizeof(float) * size_UVarray);
  assert(ret->UVarray);
  ret->length_array = (int *)malloc(sizeof(int) * size_length_array);
  assert(ret->length_array);
  ret->type_array = (GLenum *)malloc(sizeof(GLenum) * size_length_array);
  assert(ret->type_array);

  ret->index_UVarray = 0;
  ret->index_length_array = 0;

  ret->vertex_array = NULL;
  ret->normal_array = NULL;
  ret->color_array  = NULL;
  ret->texcoord_array = NULL;

  ret->next = NULL;
  return ret;
}

void bezierPatchMeshPutPatch(bezierPatchMesh *bpm, int maptype, float umin, float umax, int ustride, int uorder, float vmin, float vmax, int vstride, int vorder, float *ctlpoints)
{
  switch(maptype){
  case GL_MAP2_VERTEX_3:
    bpm->bpatch = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 3, ustride, vstride, ctlpoints);
    break;
  case GL_MAP2_VERTEX_4:
    bpm->bpatch = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 4,ustride, vstride, ctlpoints );
    break;
  case GL_MAP2_NORMAL:
    bpm->bpatch_normal = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 3, ustride, vstride, ctlpoints);
    break;
  case GL_MAP2_INDEX:
    bpm->bpatch_color = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 1, ustride, vstride, ctlpoints);
    break;
  case GL_MAP2_COLOR_4:
    bpm->bpatch_color = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 4, ustride, vstride, ctlpoints);
    break;
  case GL_MAP2_TEXTURE_COORD_1:
    bpm->bpatch_texcoord = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 1, ustride, vstride, ctlpoints);
    break;
  case GL_MAP2_TEXTURE_COORD_2:
    bpm->bpatch_texcoord = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 2, ustride, vstride, ctlpoints);
    break;    
  case GL_MAP2_TEXTURE_COORD_3:
    bpm->bpatch_texcoord = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 3, ustride, vstride, ctlpoints);
    break;    
  case GL_MAP2_TEXTURE_COORD_4:
    bpm->bpatch_texcoord = bezierPatchMake2(umin, vmin, umax, vmax, uorder, vorder, 4, ustride, vstride, ctlpoints);
    break;    
  default:
    fprintf(stderr, "error in bezierPatchMeshPutPatch, maptype=%i is wrong, maptype,map is invalid\n", maptype);
  }
}
  

/*delete everything including the arrays. So if you want to output the
 *pointers of the arrays, you should not use this function to deallocate space.
 *you should dealocate manually
 */
void bezierPatchMeshDelete(bezierPatchMesh *bpm)
{
  if(bpm->bpatch != NULL)
    bezierPatchDelete(bpm->bpatch);
  if(bpm->bpatch_normal != NULL)
    bezierPatchDelete(bpm->bpatch_normal);
  if(bpm->bpatch_color != NULL)
    bezierPatchDelete(bpm->bpatch_color);
  if(bpm->bpatch_texcoord != NULL)
    bezierPatchDelete(bpm->bpatch_texcoord);
  
  free(bpm->UVarray);
  free(bpm->length_array);
  free(bpm->vertex_array);
  free(bpm->normal_array);
  free(bpm->type_array);
  free(bpm);
}
 
/*begin a strip
 *type is the primitive type:
 */
void bezierPatchMeshBeginStrip(bezierPatchMesh *bpm, GLenum type)
{
  bpm->counter = 0;
  bpm->type = type;
}

/*signal the end of the current strip*/
void bezierPatchMeshEndStrip(bezierPatchMesh *bpm)
{
  int i;
  
  /*if there are no vertices in this strip, then nothing needs to be done*/
  if(bpm->counter == 0) return;
  
  /*if the length_array is full, it should be expanded*/
  if(bpm->index_length_array >= bpm->size_length_array)
    {
      int *temp = (int*) malloc(sizeof(int) * (bpm->size_length_array*2 + 1));
      assert(temp);
      GLenum *temp_type = (GLenum*) malloc(sizeof(GLenum) * (bpm->size_length_array*2 + 1));
      assert(temp_type);
      /*update the size*/
      bpm->size_length_array = bpm->size_length_array*2 + 1;
      
      /*copy*/
      for(i=0; i<bpm->index_length_array; i++)
	{
	  temp[i] = bpm->length_array[i];
	  temp_type[i] = bpm->type_array[i];
	}
      
      /*deallocate old array*/
      free(bpm->length_array);
      free(bpm->type_array);
      
      /*point to the new array which is twice as bigger*/
      bpm->length_array = temp;
      bpm->type_array = temp_type;
    }
  bpm->type_array[bpm->index_length_array] = bpm->type;
  bpm->length_array[bpm->index_length_array++] = bpm->counter;

}

/*insert (u,v) */
void bezierPatchMeshInsertUV(bezierPatchMesh *bpm, float u, float v)
{
  int i;
  /*if the UVarray is full, it should be expanded*/
  if(bpm->index_UVarray+1 >= bpm->size_UVarray)
    {
      float *temp = (float*) malloc(sizeof(float) * (bpm->size_UVarray * 2 + 2));
      assert(temp);
      
      /*update the size*/
      bpm->size_UVarray = bpm->size_UVarray*2 + 2;
      
      /*copy*/
      for(i=0; i<bpm->index_UVarray; i++)
	{
	  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;
}