FreeWRL/FreeX3D  3.0.0
glinterface.cc
1 /*
2 ** License Applicability. Except to the extent portions of this file are
3 ** made subject to an alternative license as permitted in the SGI Free
4 ** Software License B, Version 1.1 (the "License"), the contents of this
5 ** file are subject only to the provisions of the License. You may not use
6 ** this file except in compliance with the License. You may obtain a copy
7 ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
8 ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
9 **
10 ** http://oss.sgi.com/projects/FreeB
11 **
12 ** Note that, as provided in the License, the Software is distributed on an
13 ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
14 ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
15 ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
16 ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
17 **
18 ** Original Code. The Original Code is: OpenGL Sample Implementation,
19 ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
20 ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
21 ** Copyright in any portions created by third parties is as indicated
22 ** elsewhere herein. All Rights Reserved.
23 **
24 ** Additional Notice Provisions: The application programming interfaces
25 ** established by SGI in conjunction with the Original Code are The
26 ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
27 ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
28 ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
29 ** Window System(R) (Version 1.3), released October 19, 1998. This software
30 ** was created using the OpenGL(R) version 1.2.1 Sample Implementation
31 ** published by SGI, but has not been independently verified as being
32 ** compliant with the OpenGL(R) version 1.2.1 Specification.
33 **
34 */
35 /*
36 */
37 
38 #include "gluos.h"
39 //#include <GL/gl.h>
40 //#include <GL/glu.h>
41 #include <libnurbs2.h>
42 #include <stdio.h>
43 #include "glimports.h"
44 #include "glrenderer.h"
45 #include "nurbsconsts.h"
46 
47 //#define DOWN_LOAD_NURBS
48 #ifdef DOWN_LOAD_NURBS
49 
50 #include "oglTrimNurbs.h"
51 static int surfcount = 0;
52 static oglTrimNurbs* otn = NULL;
53 nurbSurf* tempNurb = NULL;
54 oglTrimLoops* tempTrim = NULL;
55 #endif
56 
57 
58 //for LOD
59 extern "C" {void glu_LOD_eval_list(GLUnurbs *nurb, int level);}
60 
61 void glu_LOD_eval_list(GLUnurbs *nurb, int level)
62 {
63  nurb->LOD_eval_list(level);
64 }
65 
66 GLUnurbs * GLAPIENTRY
67 gluNewNurbsRenderer(void)
68 {
69  GLUnurbs *t;
70 
71  t = new GLUnurbs();
72  return t;
73 }
74 
75 void GLAPIENTRY
76 gluDeleteNurbsRenderer(GLUnurbs *r)
77 {
78  delete r;
79 }
80 
81 extern "C"
82 void GLAPIENTRY
83 
84 gluDeleteNurbsTessellatorEXT(GLUnurbsObj *r)
85 {
86  delete r;
87 }
88 
89 void GLAPIENTRY
90 gluBeginSurface(GLUnurbs *r)
91 {
92 #ifdef DOWN_LOAD_NURBS
93 surfcount++;
94 tempTrim = OTL_make(10,10);
95 #endif
96  r->bgnsurface(0);
97 }
98 
99 void GLAPIENTRY
100 gluBeginCurve(GLUnurbs *r)
101 {
102  r->bgncurve(0);
103 }
104 
105 void GLAPIENTRY
106 gluEndCurve(GLUnurbs *r)
107 {
108  r->endcurve();
109 }
110 
111 void GLAPIENTRY
112 gluEndSurface(GLUnurbs *r)
113 {
114 #ifdef DOWN_LOAD_NURBS
115 if(surfcount == 1)
116  otn = OTN_make(1);
117 OTN_insert(otn, tempNurb, tempTrim);
118 if(surfcount >= 1)
119 {
120 #ifdef DEBUG
121 printf("write file\n");
122 #endif
123 OTN_write(otn, "out.otn");
124 
125 }
126 #endif
127 
128  r->endsurface();
129 }
130 
131 void GLAPIENTRY
132 gluBeginTrim(GLUnurbs *r)
133 {
134 #ifdef DOWN_LOAD_NURBS
135 OTL_bgnTrim(tempTrim);
136 #endif
137 
138  r->bgntrim();
139 }
140 
141 void GLAPIENTRY
142 gluEndTrim(GLUnurbs *r)
143 {
144 #ifdef DOWN_LOAD_NURBS
145 OTL_endTrim(tempTrim);
146 #endif
147  r->endtrim();
148 }
149 
150 void GLAPIENTRY
151 gluPwlCurve(GLUnurbs *r, GLint count, INREAL array[],
152  GLint stride, GLenum type)
153 {
154 #ifdef DOWN_LOAD_NURBS
155 OTL_pwlCurve(tempTrim, count, array, stride, type);
156 #endif
157 
158  int realType;
159  switch(type) {
160  case GLU_MAP1_TRIM_2:
161  realType = N_P2D;
162  break;
163  case GLU_MAP1_TRIM_3:
164  realType = N_P2DR;
165  break;
166  default:
167  realType = type;
168  break;
169  }
170  r->pwlcurve(count, array, sizeof(INREAL) * stride, realType);
171 }
172 
173 void GLAPIENTRY
174 gluNurbsCurve(GLUnurbs *r, GLint nknots, INREAL knot[], GLint stride,
175  INREAL ctlarray[], GLint order, GLenum type)
176 {
177 #ifdef DOWN_LOAD_NURBS
178 OTL_nurbsCurve(tempTrim, nknots, knot, stride, ctlarray, order, type);
179 #endif
180 
181  int realType;
182 
183  switch(type) {
184  case GLU_MAP1_TRIM_2:
185  realType = N_P2D;
186  break;
187  case GLU_MAP1_TRIM_3:
188  realType = N_P2DR;
189  break;
190  default:
191  realType = type;
192  break;
193  }
194 
195  r->nurbscurve(nknots, knot, sizeof(INREAL) * stride, ctlarray, order,
196  realType);
197 }
198 
199 void GLAPIENTRY
200 gluNurbsSurface(GLUnurbs *r, GLint sknot_count, GLfloat *sknot,
201  GLint tknot_count, GLfloat *tknot,
202  GLint s_stride, GLint t_stride,
203  GLfloat *ctlarray, GLint sorder, GLint torder,
204  GLenum type)
205 {
206 #ifdef DOWN_LOAD_NURBS
207  {
208  int dimension;
209  switch(type){
210  case GL_MAP2_VERTEX_3:
211  dimension = 3;
212  break;
213  case GL_MAP2_VERTEX_4:
214  dimension = 4;
215  break;
216  default:
217  fprintf(stderr, "error in glinterface.c++, type no implemented\n");
218  exit(1);
219  }
220 tempNurb = nurbSurfMake(sknot_count, sknot,
221  tknot_count, tknot,
222  sorder, torder,
223  dimension,
224  ctlarray,
225  s_stride, t_stride);
226 
227  }
228 #endif
229 
230  r->nurbssurface(sknot_count, sknot, tknot_count, tknot,
231  sizeof(INREAL) * s_stride, sizeof(INREAL) * t_stride,
232  ctlarray, sorder, torder, type);
233 }
234 
235 void GLAPIENTRY
236 gluLoadSamplingMatrices(GLUnurbs *r, const GLfloat modelMatrix[16],
237  const GLfloat projMatrix[16],
238  const GLint viewport[4])
239 {
240  r->useGLMatrices(modelMatrix, projMatrix, viewport);
241 }
242 
243 void GLAPIENTRY
244 gluNurbsProperty(GLUnurbs *r, GLenum property, GLfloat value)
245 {
246  GLfloat nurbsValue;
247 
248  switch (property) {
249  case GLU_AUTO_LOAD_MATRIX:
250  r->setautoloadmode(value);
251  return;
252 
253  case GLU_CULLING:
254  if (value != 0.0) {
255  nurbsValue = N_CULLINGON;
256  } else {
257  nurbsValue = N_NOCULLING;
258  }
259  r->setnurbsproperty(GL_MAP2_VERTEX_3, N_CULLING, nurbsValue);
260  r->setnurbsproperty(GL_MAP2_VERTEX_4, N_CULLING, nurbsValue);
261  r->setnurbsproperty(GL_MAP1_VERTEX_3, N_CULLING, nurbsValue);
262  r->setnurbsproperty(GL_MAP1_VERTEX_4, N_CULLING, nurbsValue);
263  return;
264 
265  case GLU_SAMPLING_METHOD:
266  if (value == GLU_PATH_LENGTH) {
267  nurbsValue = N_PATHLENGTH;
268  } else if (value == GLU_PARAMETRIC_ERROR) {
269  nurbsValue = N_PARAMETRICDISTANCE;
270  } else if (value == GLU_DOMAIN_DISTANCE) {
271  nurbsValue = N_DOMAINDISTANCE;
272  r->set_is_domain_distance_sampling(1); //optimzing untrimmed case
273 
274  } else if (value == GLU_OBJECT_PARAMETRIC_ERROR) {
275  nurbsValue = N_OBJECTSPACE_PARA;
276  r->setautoloadmode( 0.0 );
277  r->setSamplingMatrixIdentity();
278  } else if (value == GLU_OBJECT_PATH_LENGTH) {
279  nurbsValue = N_OBJECTSPACE_PATH;
280  r->setautoloadmode( 0.0 );
281  r->setSamplingMatrixIdentity();
282  } else {
283  r->postError(GLU_INVALID_VALUE);
284  return;
285  }
286 
287  r->setnurbsproperty(GL_MAP2_VERTEX_3, N_SAMPLINGMETHOD, nurbsValue);
288  r->setnurbsproperty(GL_MAP2_VERTEX_4, N_SAMPLINGMETHOD, nurbsValue);
289  r->setnurbsproperty(GL_MAP1_VERTEX_3, N_SAMPLINGMETHOD, nurbsValue);
290  r->setnurbsproperty(GL_MAP1_VERTEX_4, N_SAMPLINGMETHOD, nurbsValue);
291  return;
292 
293  case GLU_SAMPLING_TOLERANCE:
294  r->setnurbsproperty(GL_MAP2_VERTEX_3, N_PIXEL_TOLERANCE, value);
295  r->setnurbsproperty(GL_MAP2_VERTEX_4, N_PIXEL_TOLERANCE, value);
296  r->setnurbsproperty(GL_MAP1_VERTEX_3, N_PIXEL_TOLERANCE, value);
297  r->setnurbsproperty(GL_MAP1_VERTEX_4, N_PIXEL_TOLERANCE, value);
298  return;
299 
300  case GLU_PARAMETRIC_TOLERANCE:
301  r->setnurbsproperty(GL_MAP2_VERTEX_3, N_ERROR_TOLERANCE, value);
302  r->setnurbsproperty(GL_MAP2_VERTEX_4, N_ERROR_TOLERANCE, value);
303  r->setnurbsproperty(GL_MAP1_VERTEX_3, N_ERROR_TOLERANCE, value);
304  r->setnurbsproperty(GL_MAP1_VERTEX_4, N_ERROR_TOLERANCE, value);
305  return;
306 
307 
308  case GLU_DISPLAY_MODE:
309 
310  if (value == GLU_FILL) {
311  nurbsValue = N_FILL;
312  } else if (value == GLU_OUTLINE_POLYGON) {
313  nurbsValue = N_OUTLINE_POLY;
314  } else if (value == GLU_OUTLINE_PATCH) {
315  nurbsValue = N_OUTLINE_PATCH;
316  } else {
317  r->postError(GLU_INVALID_VALUE);
318  return;
319  }
320  r->setnurbsproperty(N_DISPLAY, nurbsValue);
321 
322  break;
323 
324  case GLU_U_STEP:
325  r->setnurbsproperty(GL_MAP1_VERTEX_3, N_S_STEPS, value);
326  r->setnurbsproperty(GL_MAP1_VERTEX_4, N_S_STEPS, value);
327  r->setnurbsproperty(GL_MAP2_VERTEX_3, N_S_STEPS, value);
328  r->setnurbsproperty(GL_MAP2_VERTEX_4, N_S_STEPS, value);
329 
330  //added for optimizing untrimmed case
331  r->set_domain_distance_u_rate(value);
332  break;
333 
334  case GLU_V_STEP:
335  r->setnurbsproperty(GL_MAP1_VERTEX_3, N_T_STEPS, value);
336  r->setnurbsproperty(GL_MAP1_VERTEX_4, N_T_STEPS, value);
337  r->setnurbsproperty(GL_MAP2_VERTEX_3, N_T_STEPS, value);
338  r->setnurbsproperty(GL_MAP2_VERTEX_4, N_T_STEPS, value);
339 
340  //added for optimizing untrimmed case
341  r->set_domain_distance_v_rate(value);
342  break;
343 
344  case GLU_NURBS_MODE:
345  if(value == GLU_NURBS_RENDERER)
346  r->put_callbackFlag(0);
347  else if(value == GLU_NURBS_TESSELLATOR)
348  r->put_callbackFlag(1);
349  else
350  r->postError(GLU_INVALID_ENUM);
351  break;
352 
353  default:
354  r->postError(GLU_INVALID_ENUM);
355  return;
356  }
357 }
358 
359 void GLAPIENTRY
360 gluGetNurbsProperty(GLUnurbs *r, GLenum property, GLfloat *value)
361 {
362  GLfloat nurbsValue;
363 
364  switch(property) {
365  case GLU_AUTO_LOAD_MATRIX:
366  if (r->getautoloadmode()) {
367  *value = GL_TRUE;
368  } else {
369  *value = GL_FALSE;
370  }
371  break;
372  case GLU_CULLING:
373  r->getnurbsproperty(GL_MAP2_VERTEX_3, N_CULLING, &nurbsValue);
374  if (nurbsValue == N_CULLINGON) {
375  *value = GL_TRUE;
376  } else {
377  *value = GL_FALSE;
378  }
379  break;
380  case GLU_SAMPLING_METHOD:
381  r->getnurbsproperty(GL_MAP2_VERTEX_3, N_SAMPLINGMETHOD, value);
382  if(*value == N_PATHLENGTH)
383  *value = GLU_PATH_LENGTH;
384  else if(*value == N_PARAMETRICDISTANCE)
385  *value = GLU_PARAMETRIC_ERROR;
386  else if(*value == N_DOMAINDISTANCE)
387  *value = GLU_DOMAIN_DISTANCE;
388  else if(*value == N_OBJECTSPACE_PATH)
389  *value = GLU_OBJECT_PATH_LENGTH;
390  else if(*value == N_OBJECTSPACE_PARA)
391  *value = GLU_OBJECT_PARAMETRIC_ERROR;
392  break;
393  case GLU_SAMPLING_TOLERANCE:
394  r->getnurbsproperty(GL_MAP2_VERTEX_3, N_PIXEL_TOLERANCE, value);
395  break;
396  case GLU_PARAMETRIC_TOLERANCE:
397  r->getnurbsproperty(GL_MAP2_VERTEX_3, N_ERROR_TOLERANCE, value);
398  break;
399 
400  case GLU_U_STEP:
401  r->getnurbsproperty(GL_MAP2_VERTEX_3, N_S_STEPS, value);
402  break;
403  case GLU_V_STEP:
404  r->getnurbsproperty(GL_MAP2_VERTEX_3, N_T_STEPS, value);
405  break;
406  case GLU_DISPLAY_MODE:
407  r->getnurbsproperty(N_DISPLAY, &nurbsValue);
408  if (nurbsValue == N_FILL) {
409  *value = GLU_FILL;
410  } else if (nurbsValue == N_OUTLINE_POLY) {
411  *value = GLU_OUTLINE_POLYGON;
412  } else {
413  *value = GLU_OUTLINE_PATCH;
414  }
415  break;
416 
417  case GLU_NURBS_MODE:
418  if(r->is_callback())
419  *value = GLU_NURBS_TESSELLATOR;
420  else
421  *value = GLU_NURBS_RENDERER;
422  break;
423 
424  default:
425  r->postError(GLU_INVALID_ENUM);
426  return;
427  }
428 }
429 
430 extern "C" void GLAPIENTRY
431 gluNurbsCallback(GLUnurbs *r, GLenum which, _GLUfuncptr fn )
432 {
433  switch (which) {
434  case GLU_NURBS_BEGIN:
435  case GLU_NURBS_END:
436  case GLU_NURBS_VERTEX:
437  case GLU_NURBS_NORMAL:
438  case GLU_NURBS_TEXTURE_COORD:
439  case GLU_NURBS_COLOR:
440  case GLU_NURBS_BEGIN_DATA:
441  case GLU_NURBS_END_DATA:
442  case GLU_NURBS_VERTEX_DATA:
443  case GLU_NURBS_NORMAL_DATA:
444  case GLU_NURBS_TEXTURE_COORD_DATA:
445  case GLU_NURBS_COLOR_DATA:
446  r->putSurfCallBack(which, fn);
447  break;
448 
449  case GLU_NURBS_ERROR:
450  r->errorCallback = (void (APIENTRY *)( GLenum e )) fn;
451  break;
452  default:
453  r->postError(GLU_INVALID_ENUM);
454  return;
455  }
456 }
457 
458 extern "C"
459 void GLAPIENTRY
460 gluNurbsCallbackDataEXT(GLUnurbs* r, void* userData)
461 {
462  r->setNurbsCallbackData(userData);
463 }
464 
465 extern "C"
466 void GLAPIENTRY
467 gluNurbsCallbackData(GLUnurbs* r, void* userData)
468 {
469  gluNurbsCallbackDataEXT(r,userData);
470 }