summaryrefslogtreecommitdiffstats
path: root/src/glu/mesa/nurbscrv.c
blob: 4483e1f776171994a37670799c4fcb2c3b6f8bb0 (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

/*
 * Mesa 3-D graphics library
 * Version:  3.3
 * Copyright (C) 1995-2000  Brian Paul
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


/*
 * NURBS implementation written by Bogdan Sikorski (bogdan@cira.it)
 * See README2 for more info.
 */


#ifdef PC_HEADER
#include "all.h"
#else
#include <math.h>
#include <stdlib.h>
#include "gluP.h"
#include "nurbs.h"
#endif


static int
get_curve_dim(GLenum type)
{
   switch (type) {
   case GL_MAP1_VERTEX_3:
      return 3;
   case GL_MAP1_VERTEX_4:
      return 4;
   case GL_MAP1_INDEX:
      return 1;
   case GL_MAP1_COLOR_4:
      return 4;
   case GL_MAP1_NORMAL:
      return 3;
   case GL_MAP1_TEXTURE_COORD_1:
      return 1;
   case GL_MAP1_TEXTURE_COORD_2:
      return 2;
   case GL_MAP1_TEXTURE_COORD_3:
      return 3;
   case GL_MAP1_TEXTURE_COORD_4:
      return 4;
   default:
      abort();			/* TODO: is this OK? */
   }
   return 0;			/*never get here */
}

static GLenum
test_nurbs_curve(GLUnurbsObj * nobj, curve_attribs * attribs)
{
   GLenum err;
   GLint tmp_int;

   if (attribs->order < 0) {
      call_user_error(nobj, GLU_INVALID_VALUE);
      return GLU_ERROR;
   }
   glGetIntegerv(GL_MAX_EVAL_ORDER, &tmp_int);
   if (attribs->order > tmp_int || attribs->order < 2) {
      call_user_error(nobj, GLU_NURBS_ERROR1);
      return GLU_ERROR;
   }
   if (attribs->knot_count < attribs->order + 2) {
      call_user_error(nobj, GLU_NURBS_ERROR2);
      return GLU_ERROR;
   }
   if (attribs->stride < 0) {
      call_user_error(nobj, GLU_NURBS_ERROR34);
      return GLU_ERROR;
   }
   if (attribs->knot == NULL || attribs->ctrlarray == NULL) {
      call_user_error(nobj, GLU_NURBS_ERROR36);
      return GLU_ERROR;
   }
   if ((err = test_knot(attribs->knot_count, attribs->knot, attribs->order))
       != GLU_NO_ERROR) {
      call_user_error(nobj, err);
      return GLU_ERROR;
   }
   return GLU_NO_ERROR;
}

static GLenum
test_nurbs_curves(GLUnurbsObj * nobj)
{
   /* test the geometric data */
   if (test_nurbs_curve(nobj, &(nobj->curve.geom)) != GLU_NO_ERROR)
      return GLU_ERROR;
   /* now test the attributive data */
   /* color */
   if (nobj->curve.color.type != GLU_INVALID_ENUM)
      if (test_nurbs_curve(nobj, &(nobj->curve.color)) != GLU_NO_ERROR)
	 return GLU_ERROR;
   /* normal */
   if (nobj->curve.normal.type != GLU_INVALID_ENUM)
      if (test_nurbs_curve(nobj, &(nobj->curve.normal)) != GLU_NO_ERROR)
	 return GLU_ERROR;
   /* texture */
   if (nobj->curve.texture.type != GLU_INVALID_ENUM)
      if (test_nurbs_curve(nobj, &(nobj->curve.texture)) != GLU_NO_ERROR)
	 return GLU_ERROR;
   return GLU_NO_ERROR;
}

/* prepare the knot information structures */
static GLenum
fill_knot_structures(GLUnurbsObj * nobj, knot_str_type * geom_knot,
		     knot_str_type * color_knot, knot_str_type * normal_knot,
		     knot_str_type * texture_knot)
{
   GLint order;
   GLfloat *knot;
   GLint nknots;
   GLint t_min, t_max;

   geom_knot->unified_knot = NULL;
   knot = geom_knot->knot = nobj->curve.geom.knot;
   nknots = geom_knot->nknots = nobj->curve.geom.knot_count;
   order = geom_knot->order = nobj->curve.geom.order;
   geom_knot->delta_nknots = 0;
   t_min = geom_knot->t_min = order - 1;
   t_max = geom_knot->t_max = nknots - order;
   if (fabs(knot[t_min] - knot[t_max]) < EPSILON) {
      call_user_error(nobj, GLU_NURBS_ERROR3);
      return GLU_ERROR;
   }
   if (fabs(knot[0] - knot[t_min]) < EPSILON) {
      /* knot open at beggining */
      geom_knot->open_at_begin = GL_TRUE;
   }
   else
      geom_knot->open_at_begin = GL_FALSE;
   if (fabs(knot[t_max] - knot[nknots - 1]) < EPSILON) {
      /* knot open at end */
      geom_knot->open_at_end = GL_TRUE;
   }
   else
      geom_knot->open_at_end = GL_FALSE;
   if (nobj->curve.color.type != GLU_INVALID_ENUM) {
      color_knot->unified_knot = (GLfloat *) 1;
      knot = color_knot->knot = nobj->curve.color.knot;
      nknots = color_knot->nknots = nobj->curve.color.knot_count;
      order = color_knot->order = nobj->curve.color.order;
      color_knot->delta_nknots = 0;
      t_min = color_knot->t_min = order - 1;
      t_max = color_knot->t_max = nknots - order;
      if (fabs(knot[t_min] - knot[t_max]) < EPSILON) {
	 call_user_error(nobj, GLU_NURBS_ERROR3);
	 return GLU_ERROR;
      }
      if (fabs(knot[0] - knot[t_min]) < EPSILON) {
	 /* knot open at beggining */
	 color_knot->open_at_begin = GL_TRUE;
      }
      else
	 color_knot->open_at_begin = GL_FALSE;
      if (fabs(knot[t_max] - knot[nknots - 1]) < EPSILON) {
	 /* knot open at end */
	 color_knot->open_at_end = GL_TRUE;
      }
      else
	 color_knot->open_at_end = GL_FALSE;
   }
   else
      color_knot->unified_knot = NULL;
   if (nobj->curve.normal.type != GLU_INVALID_ENUM) {
      normal_knot->unified_knot = (GLfloat *) 1;
      knot = normal_knot->knot = nobj->curve.normal.knot;
      nknots = normal_knot->nknots = nobj->curve.normal.knot_count;
      order = normal_knot->order = nobj->curve.normal.order;
      normal_knot->delta_nknots = 0;
      t_min = normal_knot->t_min = order - 1;
      t_max = normal_knot->t_max = nknots - order;
      if (fabs(knot[t_min] - knot[t_max]) < EPSILON) {
	 call_user_error(nobj, GLU_NURBS_ERROR3);
	 return GLU_ERROR;
      }
      if (fabs(knot[0] - knot[t_min]) < EPSILON) {
	 /* knot open at beggining */
	 normal_knot->open_at_begin = GL_TRUE;
      }
      else
	 normal_knot->open_at_begin = GL_FALSE;
      if (fabs(knot[t_max] - knot[nknots - 1]) < EPSILON) {
	 /* knot open at end */
	 normal_knot->open_at_end = GL_TRUE;
      }
      else
	 normal_knot->open_at_end = GL_FALSE;
   }
   else
      normal_knot->unified_knot = NULL;
   if (nobj->curve.texture.type != GLU_INVALID_ENUM) {
      texture_knot->unified_knot = (GLfloat *) 1;
      knot = texture_knot->knot = nobj->curve.texture.knot;
      nknots = texture_knot->nknots = nobj->curve.texture.knot_count;
      order = texture_knot->order = nobj->curve.texture.order;
      texture_knot->delta_nknots = 0;
      t_min = texture_knot->t_min = order - 1;
      t_max = texture_knot->t_max = nknots - order;
      if (fabs(knot[t_min] - knot[t_max]) < EPSILON) {
	 call_user_error(nobj, GLU_NURBS_ERROR3);
	 return GLU_ERROR;
      }
      if (fabs(knot[0] - knot[t_min]) < EPSILON) {
	 /* knot open at beggining */
	 texture_knot->open_at_begin = GL_TRUE;
      }
      else
	 texture_knot->open_at_begin = GL_FALSE;
      if (fabs(knot[t_max] - knot[nknots - 1]) < EPSILON) {
	 /* knot open at end */
	 texture_knot->open_at_end = GL_TRUE;
      }
      else
	 texture_knot->open_at_end = GL_FALSE;
   }
   else
      texture_knot->unified_knot = NULL;
   return GLU_NO_ERROR;
}

/* covert the NURBS curve into a series of adjacent Bezier curves */
static GLenum
convert_curve(knot_str_type * the_knot, curve_attribs * attrib,
	      GLfloat ** new_ctrl, GLint * ncontrol)
{
   GLenum err;

   if ((err = explode_knot(the_knot)) != GLU_NO_ERROR) {
      if (the_knot->unified_knot) {
	 free(the_knot->unified_knot);
	 the_knot->unified_knot = NULL;
      }
      return err;
   }
   if (the_knot->unified_knot) {
      free(the_knot->unified_knot);
      the_knot->unified_knot = NULL;
   }
   if ((err = calc_alphas(the_knot)) != GLU_NO_ERROR) {
      free(the_knot->new_knot);
      return err;
   }
   free(the_knot->new_knot);
   if ((err = calc_new_ctrl_pts(attrib->ctrlarray, attrib->stride, the_knot,
				attrib->dim, new_ctrl, ncontrol))
       != GLU_NO_ERROR) {
      free(the_knot->alpha);
      return err;
   }
   free(the_knot->alpha);
   return GLU_NO_ERROR;
}

/* covert curves - geometry and possible attribute ones into equivalent */
/* sequence of adjacent Bezier curves */
static GLenum
convert_curves(GLUnurbsObj * nobj, GLfloat ** new_geom_ctrl,
	       GLint * ncontrol, GLfloat ** new_color_ctrl,
	       GLfloat ** new_normal_ctrl, GLfloat ** new_texture_ctrl)
{
   knot_str_type geom_knot, color_knot, normal_knot, texture_knot;
   GLint junk;
   GLenum err;

   *new_color_ctrl = *new_normal_ctrl = *new_texture_ctrl = NULL;

   if (fill_knot_structures(nobj, &geom_knot, &color_knot, &normal_knot,
			    &texture_knot) != GLU_NO_ERROR)
      return GLU_ERROR;

   /* unify knots - all knots should have the same number of working */
   /* ranges */
   if (
       (err =
	select_knot_working_range(nobj, &geom_knot, &color_knot, &normal_knot,
				  &texture_knot)) != GLU_NO_ERROR) {
      return err;
   }
   /* convert the geometry curve */
   nobj->curve.geom.dim = get_curve_dim(nobj->curve.geom.type);
   if ((err = convert_curve(&geom_knot, &(nobj->curve.geom), new_geom_ctrl,
			    ncontrol)) != GLU_NO_ERROR) {
      free_unified_knots(&geom_knot, &color_knot, &normal_knot,
			 &texture_knot);
      call_user_error(nobj, err);
      return err;
   }
   /* if additional attributive curves are given convert them as well */
   if (color_knot.unified_knot) {
      nobj->curve.color.dim = get_curve_dim(nobj->curve.color.type);
      if ((err = convert_curve(&color_knot, &(nobj->curve.color),
			       new_color_ctrl, &junk)) != GLU_NO_ERROR) {
	 free_unified_knots(&geom_knot, &color_knot, &normal_knot,
			    &texture_knot);
	 free(*new_geom_ctrl);
	 call_user_error(nobj, err);
	 return err;
      }
   }
   if (normal_knot.unified_knot) {
      nobj->curve.normal.dim = get_curve_dim(nobj->curve.normal.type);
      if ((err = convert_curve(&normal_knot, &(nobj->curve.normal),
			       new_normal_ctrl, &junk)) != GLU_NO_ERROR) {
	 free_unified_knots(&geom_knot, &color_knot, &normal_knot,
			    &texture_knot);
	 free(*new_geom_ctrl);
	 if (*new_color_ctrl)
	    free(*new_color_ctrl);
	 call_user_error(nobj, err);
	 return err;
      }
   }
   if (texture_knot.unified_knot) {
      nobj->curve.texture.dim = get_curve_dim(nobj->curve.texture.type);
      if ((err = convert_curve(&texture_knot, &(nobj->curve.texture),
			       new_texture_ctrl, &junk)) != GLU_NO_ERROR) {
	 free_unified_knots(&geom_knot, &color_knot, &normal_knot,
			    &texture_knot);
	 free(*new_geom_ctrl);
	 if (*new_color_ctrl)
	    free(*new_color_ctrl);
	 if (*new_normal_ctrl)
	    free(*new_normal_ctrl);
	 call_user_error(nobj, err);
	 return err;
      }
   }
   return GLU_NO_ERROR;
}

/* main NURBS curve procedure */
void
do_nurbs_curve(GLUnurbsObj * nobj)
{
   GLint geom_order, color_order = 0, normal_order = 0, texture_order = 0;
   GLenum geom_type;
   GLint n_ctrl;
   GLfloat *new_geom_ctrl, *new_color_ctrl, *new_normal_ctrl,
      *new_texture_ctrl;
   GLfloat *geom_ctrl = 0, *color_ctrl = 0, *normal_ctrl = 0, *texture_ctrl = 0;
   GLint *factors;
   GLint i, j;
   GLint geom_dim, color_dim = 0, normal_dim = 0, texture_dim = 0;

   /* test the user supplied data */
   if (test_nurbs_curves(nobj) != GLU_NO_ERROR)
      return;

   if (convert_curves(nobj, &new_geom_ctrl, &n_ctrl, &new_color_ctrl,
		      &new_normal_ctrl, &new_texture_ctrl) != GLU_NO_ERROR)
      return;

   geom_order = nobj->curve.geom.order;
   geom_type = nobj->curve.geom.type;
   geom_dim = nobj->curve.geom.dim;

   if (glu_do_sampling_crv(nobj, new_geom_ctrl, n_ctrl, geom_order, geom_dim,
			   &factors) != GLU_NO_ERROR) {
      free(new_geom_ctrl);
      if (new_color_ctrl)
	 free(new_color_ctrl);
      if (new_normal_ctrl)
	 free(new_normal_ctrl);
      if (new_texture_ctrl)
	 free(new_texture_ctrl);
      return;
   }
   glEnable(geom_type);
   if (new_color_ctrl) {
      glEnable(nobj->curve.color.type);
      color_dim = nobj->curve.color.dim;
      color_ctrl = new_color_ctrl;
      color_order = nobj->curve.color.order;
   }
   if (new_normal_ctrl) {
      glEnable(nobj->curve.normal.type);
      normal_dim = nobj->curve.normal.dim;
      normal_ctrl = new_normal_ctrl;
      normal_order = nobj->curve.normal.order;
   }
   if (new_texture_ctrl) {
      glEnable(nobj->curve.texture.type);
      texture_dim = nobj->curve.texture.dim;
      texture_ctrl = new_texture_ctrl;
      texture_order = nobj->curve.texture.order;
   }
   for (i = 0, j = 0, geom_ctrl = new_geom_ctrl;
	i < n_ctrl; i += geom_order, j++, geom_ctrl += geom_order * geom_dim) {
      if (fine_culling_test_2D
	  (nobj, geom_ctrl, geom_order, geom_dim, geom_dim)) {
	 color_ctrl += color_order * color_dim;
	 normal_ctrl += normal_order * normal_dim;
	 texture_ctrl += texture_order * texture_dim;
	 continue;
      }
      glMap1f(geom_type, 0.0, 1.0, geom_dim, geom_order, geom_ctrl);
      if (new_color_ctrl) {
	 glMap1f(nobj->curve.color.type, 0.0, 1.0, color_dim,
		 color_order, color_ctrl);
	 color_ctrl += color_order * color_dim;
      }
      if (new_normal_ctrl) {
	 glMap1f(nobj->curve.normal.type, 0.0, 1.0, normal_dim,
		 normal_order, normal_ctrl);
	 normal_ctrl += normal_order * normal_dim;
      }
      if (new_texture_ctrl) {
	 glMap1f(nobj->curve.texture.type, 0.0, 1.0, texture_dim,
		 texture_order, texture_ctrl);
	 texture_ctrl += texture_order * texture_dim;
      }
      glMapGrid1f(factors[j], 0.0, 1.0);
      glEvalMesh1(GL_LINE, 0, factors[j]);
   }
   free(new_geom_ctrl);
   free(factors);
   if (new_color_ctrl)
      free(new_color_ctrl);
   if (new_normal_ctrl)
      free(new_normal_ctrl);
   if (new_texture_ctrl)
      free(new_texture_ctrl);
}