Debian Codegraph @

debian/external_deps/fetk/sg/src/glu/nurbs.c

raw
1 /* $Id: nurbs.c,v 1.2 2000/11/02 00:28:54 mholst Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 2.0
6 * Copyright (C) 1995-1996 Brian Paul
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 /*
24 * NURBS implementation written by Bogdan Sikorski ([email protected])
25 * See README2 for more info.
26 */
27
28
29 #include <math.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include "nurbs.h"
33
34
35 void
36 call_user_error( GLUnurbsObj *nobj, GLenum error )
37 {
38 nobj->error=error;
39 if(nobj->error_callback != NULL) {
40 (*(nobj->error_callback))(error);
41 }
42 else {
43 printf("NURBS error %d %s\n", error, gluErrorString(error) );
44 }
45 }
46
47
48
49 GLUnurbsObj *gluNewNurbsRenderer( void )
50 {
51 GLUnurbsObj *n;
52 GLfloat tmp_viewport[4];
53 GLint i,j;
54
55 n = (GLUnurbsObj *) malloc( sizeof(GLUnurbsObj) );
56 if (n) {
57 /* init */
58 n->culling=GL_FALSE;
59 n->nurbs_type=GLU_NURBS_NONE;
60 n->error=GLU_NO_ERROR;
61 n->error_callback=NULL;
62 n->auto_load_matrix=GL_TRUE;
63 n->sampling_tolerance=50.0;
64 n->display_mode=GLU_FILL;
65 /* in case the user doesn't supply the sampling matrices */
66 /* set projection and modelview to identity */
67 for(i=0;i<4;i++)
68 for(j=0;j<4;j++)
69 if(i==j)
70 {
71 n->sampling_matrices.model[i*4+j]=1.0;
72 n->sampling_matrices.proj[i*4+j]=1.0;
73 }
74 else
75 {
76 n->sampling_matrices.model[i*4+j]=0.0;
77 n->sampling_matrices.proj[i*4+j]=0.0;
78 }
79 /* and set the viewport sampling matrix to current ciewport */
80 glGetFloatv(GL_VIEWPORT,tmp_viewport);
81 for(i=0;i<4;i++)
82 n->sampling_matrices.viewport[i]=tmp_viewport[i];
83 n->trim=NULL;
84 }
85 return n;
86 }
87
88
89
90 void gluDeleteNurbsRenderer( GLUnurbsObj *nobj )
91 {
92 if (nobj) {
93 free( nobj );
94 }
95 }
96
97
98
99 void gluLoadSamplingMatrices( GLUnurbsObj *nobj,
100 const GLfloat modelMatrix[16],
101 const GLfloat projMatrix[16],
102 const GLint viewport[4] )
103 {
104 GLint i;
105
106 for(i=0;i<16;i++)
107 {
108 nobj->sampling_matrices.model[i]=modelMatrix[i];
109 nobj->sampling_matrices.proj[i]=projMatrix[i];
110 }
111 for(i=0;i<4;i++)
112 nobj->sampling_matrices.viewport[i]=viewport[i];
113 }
114
115
116 void gluNurbsProperty( GLUnurbsObj *nobj, GLenum property, GLfloat value )
117 {
118 GLenum val;
119
120 switch (property) {
121 case GLU_SAMPLING_TOLERANCE:
122 if(value <= 0.0)
123 {
124 call_user_error(nobj,GLU_INVALID_VALUE);
125 return;
126 }
127 nobj->sampling_tolerance=value;
128 break;
129 case GLU_DISPLAY_MODE:
130 val=(GLenum)value;
131 if(val!=GLU_FILL && val!=GLU_OUTLINE_POLYGON && val!=GLU_OUTLINE_PATCH)
132 {
133 call_user_error(nobj,GLU_INVALID_ENUM);
134 return;
135 }
136 if(nobj->nurbs_type==GLU_NURBS_CURVE)
137 {
138 call_user_error(nobj,GLU_NURBS_ERROR26);
139 return;
140 }
141 nobj->display_mode=val;
142 if(val==GLU_OUTLINE_PATCH)
143 fprintf(stderr,"NURBS, for the moment, can display only in POLYGON mode\n");
144 break;
145 case GLU_CULLING:
146 val=(GLenum)value;
147 if(val!=GL_TRUE && val!=GL_FALSE)
148 {
149 call_user_error(nobj,GLU_INVALID_ENUM);
150 return;
151 }
152 nobj->culling = (GLboolean) value;
153 break;
154 case GLU_AUTO_LOAD_MATRIX:
155 val=(GLenum)value;
156 if(val!=GL_TRUE && val!=GL_FALSE)
157 {
158 call_user_error(nobj,GLU_INVALID_ENUM);
159 return;
160 }
161 nobj->auto_load_matrix = (GLboolean) value;
162 break;
163 default:
164 call_user_error(nobj,GLU_NURBS_ERROR26);
165 }
166 }
167
168
169 void gluGetNurbsProperty( GLUnurbsObj *nobj, GLenum property, GLfloat *value )
170 {
171 switch (property) {
172 case GLU_SAMPLING_TOLERANCE:
173 *value = nobj->sampling_tolerance;
174 break;
175 case GLU_DISPLAY_MODE:
176 *value = (GLfloat) nobj->display_mode;
177 break;
178 case GLU_CULLING:
179 *value = nobj->culling ? 1.0 : 0.0;
180 break;
181 case GLU_AUTO_LOAD_MATRIX:
182 *value = nobj->auto_load_matrix ? 1.0 : 0.0;
183 break;
184 default:
185 call_user_error(nobj,GLU_INVALID_ENUM);
186 }
187 }
188
189
190
191 void gluBeginCurve( GLUnurbsObj *nobj )
192 {
193 if(nobj->nurbs_type==GLU_NURBS_CURVE)
194 {
195 call_user_error(nobj,GLU_NURBS_ERROR6);
196 return;
197 }
198 nobj->nurbs_type=GLU_NURBS_CURVE;
199 nobj->curve.geom.type=GLU_INVALID_ENUM;
200 nobj->curve.color.type=GLU_INVALID_ENUM;
201 nobj->curve.texture.type=GLU_INVALID_ENUM;
202 nobj->curve.normal.type=GLU_INVALID_ENUM;
203 }
204
205
206 void gluEndCurve( GLUnurbsObj * nobj )
207 {
208 if(nobj->nurbs_type==GLU_NURBS_NONE)
209 {
210 call_user_error(nobj,GLU_NURBS_ERROR7);
211 return;
212 }
213 if(nobj->curve.geom.type==GLU_INVALID_ENUM)
214 {
215 call_user_error(nobj,GLU_NURBS_ERROR8);
216 nobj->nurbs_type=GLU_NURBS_NONE;
217 return;
218 }
219 glPushAttrib( (GLbitfield) (GL_EVAL_BIT | GL_ENABLE_BIT) );
220 glDisable(GL_MAP1_VERTEX_3);
221 glDisable(GL_MAP1_VERTEX_4);
222 glDisable(GL_MAP1_INDEX);
223 glDisable(GL_MAP1_COLOR_4);
224 glDisable(GL_MAP1_NORMAL);
225 glDisable(GL_MAP1_TEXTURE_COORD_1);
226 glDisable(GL_MAP1_TEXTURE_COORD_2);
227 glDisable(GL_MAP1_TEXTURE_COORD_3);
228 glDisable(GL_MAP1_TEXTURE_COORD_4);
229 glDisable(GL_MAP2_VERTEX_3);
230 glDisable(GL_MAP2_VERTEX_4);
231 glDisable(GL_MAP2_INDEX);
232 glDisable(GL_MAP2_COLOR_4);
233 glDisable(GL_MAP2_NORMAL);
234 glDisable(GL_MAP2_TEXTURE_COORD_1);
235 glDisable(GL_MAP2_TEXTURE_COORD_2);
236 glDisable(GL_MAP2_TEXTURE_COORD_3);
237 glDisable(GL_MAP2_TEXTURE_COORD_4);
238 do_nurbs_curve(nobj);
239 glPopAttrib();
240 nobj->nurbs_type=GLU_NURBS_NONE;
241 }
242
243
244 void gluNurbsCurve( GLUnurbsObj *nobj, GLint nknots, GLfloat *knot,
245 GLint stride, GLfloat *ctlarray, GLint order, GLenum type )
246 {
247 if(nobj->nurbs_type==GLU_NURBS_TRIM)
248 {
249 #if 0
250 /* TODO: NOT IMPLEMENTED YET */
251 nurbs_trim *ptr1;
252 trim_list *ptr2;
253
254 if(type!=GLU_MAP1_TRIM_2 && type!=GLU_MAP1_TRIM_3)
255 {
256 call_user_error(nobj,GLU_NURBS_ERROR14);
257 return;
258 }
259 for(ptr1=nobj->trim;ptr1->next;ptr1=ptr1->next);
260 if(ptr1->trim_loop)
261 {
262 for(ptr2=ptr1->trim_loop;ptr2->next;ptr2=ptr2->next);
263 if((ptr2->next=(trim_list *)malloc(sizeof(trim_list)))==NULL)
264 {
265 call_user_error(nobj,GLU_OUT_OF_MEMORY);
266 return;
267 }
268 ptr2=ptr2->next;
269 }
270 else
271 {
272 if((ptr2=(trim_list *)malloc(sizeof(trim_list)))==NULL)
273 {
274 call_user_error(nobj,GLU_OUT_OF_MEMORY);
275 return;
276 }
277 ptr1->trim_loop=ptr2;
278 }
279 ptr2->trim_type=GLU_TRIM_NURBS;
280 ptr2->curve.nurbs_curve.knot_count=nknots;
281 ptr2->curve.nurbs_curve.knot=knot;
282 ptr2->curve.nurbs_curve.stride=stride;
283 ptr2->curve.nurbs_curve.ctrlarray=ctlarray;
284 ptr2->curve.nurbs_curve.order=order;
285 ptr2->curve.nurbs_curve.dim= (type==GLU_MAP1_TRIM_2 ? 2 : 3 );
286 ptr2->curve.nurbs_curve.type=type;
287 ptr2->next=NULL;
288 #endif
289 }
290 else
291 {
292 if(type==GLU_MAP1_TRIM_2 || type==GLU_MAP1_TRIM_3)
293 {
294 call_user_error(nobj,GLU_NURBS_ERROR22);
295 return;
296 }
297 if(nobj->nurbs_type!=GLU_NURBS_CURVE)
298 {
299 call_user_error(nobj,GLU_NURBS_ERROR10);
300 return;
301 }
302 switch(type)
303 {
304 case GL_MAP1_VERTEX_3:
305 case GL_MAP1_VERTEX_4:
306 if(nobj->curve.geom.type!=GLU_INVALID_ENUM)
307 {
308 call_user_error(nobj,GLU_NURBS_ERROR8);
309 return;
310 }
311 nobj->curve.geom.type=type;
312 nobj->curve.geom.knot_count=nknots;
313 nobj->curve.geom.knot=knot;
314 nobj->curve.geom.stride=stride;
315 nobj->curve.geom.ctrlarray=ctlarray;
316 nobj->curve.geom.order=order;
317 break;
318 case GL_MAP1_INDEX:
319 case GL_MAP1_COLOR_4:
320 nobj->curve.color.type=type;
321 nobj->curve.color.knot_count=nknots;
322 nobj->curve.color.knot=knot;
323 nobj->curve.color.stride=stride;
324 nobj->curve.color.ctrlarray=ctlarray;
325 nobj->curve.color.order=order;
326 break;
327 case GL_MAP1_NORMAL:
328 nobj->curve.normal.type=type;
329 nobj->curve.normal.knot_count=nknots;
330 nobj->curve.normal.knot=knot;
331 nobj->curve.normal.stride=stride;
332 nobj->curve.normal.ctrlarray=ctlarray;
333 nobj->curve.normal.order=order;
334 break;
335 case GL_MAP1_TEXTURE_COORD_1:
336 case GL_MAP1_TEXTURE_COORD_2:
337 case GL_MAP1_TEXTURE_COORD_3:
338 case GL_MAP1_TEXTURE_COORD_4:
339 nobj->curve.texture.type=type;
340 nobj->curve.texture.knot_count=nknots;
341 nobj->curve.texture.knot=knot;
342 nobj->curve.texture.stride=stride;
343 nobj->curve.texture.ctrlarray=ctlarray;
344 nobj->curve.texture.order=order;
345 break;
346 default:
347 call_user_error(nobj,GLU_INVALID_ENUM);
348 }
349 }
350 }
351
352
353 void gluBeginSurface( GLUnurbsObj *nobj )
354 {
355 switch(nobj->nurbs_type)
356 {
357 case GLU_NURBS_NONE:
358 nobj->nurbs_type=GLU_NURBS_SURFACE;
359 nobj->surface.geom.type=GLU_INVALID_ENUM;
360 nobj->surface.color.type=GLU_INVALID_ENUM;
361 nobj->surface.texture.type=GLU_INVALID_ENUM;
362 nobj->surface.normal.type=GLU_INVALID_ENUM;
363 break;
364 case GLU_NURBS_TRIM:
365 call_user_error(nobj,GLU_NURBS_ERROR16);
366 break;
367 case GLU_NURBS_SURFACE:
368 case GLU_NURBS_NO_TRIM:
369 case GLU_NURBS_TRIM_DONE:
370 call_user_error(nobj,GLU_NURBS_ERROR27);
371 break;
372 case GLU_NURBS_CURVE:
373 call_user_error(nobj,GLU_NURBS_ERROR6);
374 break;
375 }
376 }
377
378
379 void gluEndSurface( GLUnurbsObj * nobj )
380 {
381 switch(nobj->nurbs_type)
382 {
383 case GLU_NURBS_NONE:
384 call_user_error(nobj,GLU_NURBS_ERROR13);
385 break;
386 case GLU_NURBS_TRIM:
387 call_user_error(nobj,GLU_NURBS_ERROR12);
388 break;
389 case GLU_NURBS_TRIM_DONE:
390 /* if(nobj->trim->trim_loop==NULL)
391 {
392 call_user_error(nobj,GLU_NURBS_ERROR18);
393 return;
394 }*/
395 /* no break - fallthrough */
396 case GLU_NURBS_NO_TRIM:
397 glPushAttrib( (GLbitfield) (GL_EVAL_BIT | GL_ENABLE_BIT) );
398 glDisable(GL_MAP2_VERTEX_3);
399 glDisable(GL_MAP2_VERTEX_4);
400 glDisable(GL_MAP2_INDEX);
401 glDisable(GL_MAP2_COLOR_4);
402 glDisable(GL_MAP2_NORMAL);
403 glDisable(GL_MAP2_TEXTURE_COORD_1);
404 glDisable(GL_MAP2_TEXTURE_COORD_2);
405 glDisable(GL_MAP2_TEXTURE_COORD_3);
406 glDisable(GL_MAP2_TEXTURE_COORD_4);
407 /* glDisable(GL_MAP1_VERTEX_3);
408 glDisable(GL_MAP1_VERTEX_4);
409 glDisable(GL_MAP1_INDEX);
410 glDisable(GL_MAP1_COLOR_4);
411 glDisable(GL_MAP1_NORMAL);
412 glDisable(GL_MAP1_TEXTURE_COORD_1);
413 glDisable(GL_MAP1_TEXTURE_COORD_2);
414 glDisable(GL_MAP1_TEXTURE_COORD_3);
415 glDisable(GL_MAP1_TEXTURE_COORD_4);*/
416 do_nurbs_surface(nobj);
417 glPopAttrib();
418 break;
419 default:
420 call_user_error(nobj,GLU_NURBS_ERROR8);
421 }
422 nobj->nurbs_type=GLU_NURBS_NONE;
423 }
424
425
426 void gluNurbsSurface( GLUnurbsObj *nobj,
427 GLint sknot_count, GLfloat *sknot,
428 GLint tknot_count, GLfloat *tknot,
429 GLint s_stride, GLint t_stride,
430 GLfloat *ctrlarray,
431 GLint sorder, GLint torder,
432 GLenum type )
433 {
434 if(nobj->nurbs_type==GLU_NURBS_NO_TRIM || nobj->nurbs_type==GLU_NURBS_TRIM ||
435 nobj->nurbs_type==GLU_NURBS_TRIM_DONE)
436 {
437 if(type==GL_MAP2_VERTEX_3 || type==GL_MAP2_VERTEX_4)
438 {
439 call_user_error(nobj,GLU_NURBS_ERROR8);
440 return;
441 }
442 }
443 else
444 if(nobj->nurbs_type!=GLU_NURBS_SURFACE)
445 {
446 call_user_error(nobj,GLU_NURBS_ERROR11);
447 return;
448 }
449 switch(type)
450 {
451 case GL_MAP2_VERTEX_3:
452 case GL_MAP2_VERTEX_4:
453 nobj->surface.geom.sknot_count=sknot_count;
454 nobj->surface.geom.sknot=sknot;
455 nobj->surface.geom.tknot_count=tknot_count;
456 nobj->surface.geom.tknot=tknot;
457 nobj->surface.geom.s_stride=s_stride;
458 nobj->surface.geom.t_stride=t_stride;
459 nobj->surface.geom.ctrlarray=ctrlarray;
460 nobj->surface.geom.sorder=sorder;
461 nobj->surface.geom.torder=torder;
462 nobj->surface.geom.type=type;
463 nobj->nurbs_type=GLU_NURBS_NO_TRIM;
464 break;
465 case GL_MAP2_INDEX:
466 case GL_MAP2_COLOR_4:
467 nobj->surface.color.sknot_count=sknot_count;
468 nobj->surface.color.sknot=sknot;
469 nobj->surface.color.tknot_count=tknot_count;
470 nobj->surface.color.tknot=tknot;
471 nobj->surface.color.s_stride=s_stride;
472 nobj->surface.color.t_stride=t_stride;
473 nobj->surface.color.ctrlarray=ctrlarray;
474 nobj->surface.color.sorder=sorder;
475 nobj->surface.color.torder=torder;
476 nobj->surface.color.type=type;
477 break;
478 case GL_MAP2_NORMAL:
479 nobj->surface.normal.sknot_count=sknot_count;
480 nobj->surface.normal.sknot=sknot;
481 nobj->surface.normal.tknot_count=tknot_count;
482 nobj->surface.normal.tknot=tknot;
483 nobj->surface.normal.s_stride=s_stride;
484 nobj->surface.normal.t_stride=t_stride;
485 nobj->surface.normal.ctrlarray=ctrlarray;
486 nobj->surface.normal.sorder=sorder;
487 nobj->surface.normal.torder=torder;
488 nobj->surface.normal.type=type;
489 break;
490 case GL_MAP2_TEXTURE_COORD_1:
491 case GL_MAP2_TEXTURE_COORD_2:
492 case GL_MAP2_TEXTURE_COORD_3:
493 case GL_MAP2_TEXTURE_COORD_4:
494 nobj->surface.texture.sknot_count=sknot_count;
495 nobj->surface.texture.sknot=sknot;
496 nobj->surface.texture.tknot_count=tknot_count;
497 nobj->surface.texture.tknot=tknot;
498 nobj->surface.texture.s_stride=s_stride;
499 nobj->surface.texture.t_stride=t_stride;
500 nobj->surface.texture.ctrlarray=ctrlarray;
501 nobj->surface.texture.sorder=sorder;
502 nobj->surface.texture.torder=torder;
503 nobj->surface.texture.type=type;
504 break;
505 default:
506 call_user_error(nobj,GLU_INVALID_ENUM);
507 }
508 }
509
510
511 void
512 gluNurbsCallback( GLUnurbsObj *nobj, GLenum which, void (*fn)(GLenum))
513 {
514 nobj->error_callback=fn;
515 if(which!=GLU_ERROR)
516 call_user_error(nobj,GLU_INVALID_ENUM);
517 }
518
519 void
520 gluBeginTrim( GLUnurbsObj *nobj )
521 {
522 #if 0
523 nurbs_trim *ptr;
524 #endif
525
526 if(nobj->nurbs_type!=GLU_NURBS_TRIM_DONE)
527 if(nobj->nurbs_type!=GLU_NURBS_NO_TRIM)
528 {
529 call_user_error(nobj,GLU_NURBS_ERROR15);
530 return;
531 }
532 nobj->nurbs_type=GLU_NURBS_TRIM;
533 fprintf(stderr,"NURBS - trimming not supported yet\n");
534 #if 0
535 if((ptr=(nurbs_trim *)malloc(sizeof(nurbs_trim)))==NULL)
536 {
537 call_user_error(nobj,GLU_OUT_OF_MEMORY);
538 return;
539 }
540 if(nobj->trim)
541 {
542 nurbs_trim *tmp_ptr;
543
544 for(tmp_ptr=nobj->trim;tmp_ptr->next;tmp_ptr=tmp_ptr->next);
545 tmp_ptr->next=ptr;
546 }
547 else
548 nobj->trim=ptr;
549 ptr->trim_loop=NULL;
550 ptr->segments=NULL;
551 ptr->next=NULL;
552 #endif
553 }
554
555 void
556 gluPwlCurve( GLUnurbsObj *nobj, GLint count, GLfloat *array, GLint stride,
557 GLenum type)
558 {
559 #if 0
560 nurbs_trim *ptr1;
561 trim_list *ptr2;
562 #endif
563 if(nobj->nurbs_type==GLU_NURBS_CURVE)
564 {
565 call_user_error(nobj,GLU_NURBS_ERROR9);
566 return;
567 }
568 if(nobj->nurbs_type==GLU_NURBS_NONE)
569 {
570 call_user_error(nobj,GLU_NURBS_ERROR19);
571 return;
572 }
573 if(type!=GLU_MAP1_TRIM_2 && type!=GLU_MAP1_TRIM_3)
574 {
575 call_user_error(nobj,GLU_NURBS_ERROR14);
576 return;
577 }
578 #if 0
579 for(ptr1=nobj->trim;ptr1->next;ptr1=ptr1->next);
580 if(ptr1->trim_loop)
581 {
582 for(ptr2=ptr1->trim_loop;ptr2->next;ptr2=ptr2->next);
583 if((ptr2->next=(trim_list *)malloc(sizeof(trim_list)))==NULL)
584 {
585 call_user_error(nobj,GLU_OUT_OF_MEMORY);
586 return;
587 }
588 ptr2=ptr2->next;
589 }
590 else
591 {
592 if((ptr2=(trim_list *)malloc(sizeof(trim_list)))==NULL)
593 {
594 call_user_error(nobj,GLU_OUT_OF_MEMORY);
595 return;
596 }
597 ptr1->trim_loop=ptr2;
598 }
599 ptr2->trim_type=GLU_TRIM_PWL;
600 ptr2->curve.pwl_curve.pt_count=count;
601 ptr2->curve.pwl_curve.ctrlarray=array;
602 ptr2->curve.pwl_curve.stride=stride;
603 ptr2->curve.pwl_curve.dim= (type==GLU_MAP1_TRIM_2 ? 2 : 3 );
604 ptr2->curve.pwl_curve.type=type;
605 ptr2->next=NULL;
606 #endif
607 }
608
609 void
610 gluEndTrim( GLUnurbsObj *nobj )
611 {
612 if(nobj->nurbs_type!=GLU_NURBS_TRIM)
613 {
614 call_user_error(nobj,GLU_NURBS_ERROR17);
615 return;
616 }
617 nobj->nurbs_type=GLU_NURBS_TRIM_DONE;
618 }
619