FreeWRL/FreeX3D  3.0.0
glcurveval.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  * glcurveval.c++
37  *
38  */
39 
40 /* Polynomial Evaluator Interface */
41 
42 #include "gluos.h"
43 #include "glimports.h"
44 #include "glrenderer.h"
45 #include "glcurveval.h"
46 #include "nurbsconsts.h"
47 
48 OpenGLCurveEvaluator::OpenGLCurveEvaluator(void)
49 {
50  //no default callback functions
51  beginCallBackN = NULL;
52  endCallBackN = NULL;
53  vertexCallBackN = NULL;
54  normalCallBackN = NULL;
55  colorCallBackN = NULL;
56  texcoordCallBackN = NULL;
57  beginCallBackData = NULL;
58  endCallBackData = NULL;
59  vertexCallBackData = NULL;
60  normalCallBackData = NULL;
61  colorCallBackData = NULL;
62  texcoordCallBackData = NULL;
63 
64  userData = NULL;
65 
66  vertex_flag = 0;
67  normal_flag = 0;
68  color_flag = 0;
69  texcoord_flag = 0;
70 
71  em_vertex.uprime = -1.0;
72  em_normal.uprime = -1.0;
73  em_color.uprime = -1.0;
74  em_texcoord.uprime = -1.0;
75  output_triangles = 0; // don't output triangles by default
76 }
77 
78 OpenGLCurveEvaluator::~OpenGLCurveEvaluator(void)
79 {
80 }
81 
82 /* added nonsense to avoid the warning messages at compile time */
83 void
84 OpenGLCurveEvaluator::addMap(CurveMap *m)
85 {
86  m = m;
87 }
88 
89 void
90 OpenGLCurveEvaluator::range1f(long type, REAL *from, REAL *to)
91 {
92  type = type;
93  from = from;
94  to = to;
95 }
96 
97 void
98 OpenGLCurveEvaluator::domain1f(REAL ulo, REAL uhi)
99 {
100  ulo = ulo;
101  uhi = uhi;
102 }
103 
104 void
105 OpenGLCurveEvaluator::bgnline(void)
106 {
107  if(output_triangles)
108  beginCallBack(GL_LINE_STRIP, userData);
109 #ifdef HAVE_GL_H
110  else
111  glBegin((GLenum) GL_LINE_STRIP);
112 #endif
113 }
114 
115 void
116 OpenGLCurveEvaluator::endline(void)
117 {
118  if(output_triangles)
119  endCallBack(userData);
120 #ifdef HAVE_GL_H
121  else
122  glEnd();
123 #endif
124 }
125 
126 /*---------------------------------------------------------------------------
127  * disable - turn off a curve map
128  *---------------------------------------------------------------------------
129  */
130 void
131 OpenGLCurveEvaluator::disable(long type)
132 {
133  glDisable((GLenum) type);
134 }
135 
136 /*---------------------------------------------------------------------------
137  * enable - turn on a curve map
138  *---------------------------------------------------------------------------
139  */
140 void
141 OpenGLCurveEvaluator::enable(long type)
142 {
143  glEnable((GLenum) type);
144 }
145 
146 /*-------------------------------------------------------------------------
147  * mapgrid1f - define a lattice of points with origin and offset
148  *-------------------------------------------------------------------------
149  */
150 void
151 OpenGLCurveEvaluator::mapgrid1f(long nu, REAL u0, REAL u1)
152 {
153  if(output_triangles)
154  {
155  global_grid_u0 = u0;
156  global_grid_u1 = u1;
157  global_grid_nu = (int) nu;
158  }
159 #ifdef HAVE_GL_H
160  else
161  glMapGrid1f((GLint) nu, (GLfloat) u0, (GLfloat) u1);
162 #endif
163 }
164 
165 /*-------------------------------------------------------------------------
166  * bgnmap1 - preamble to curve definition and evaluations
167  *-------------------------------------------------------------------------
168  */
169 void
170 OpenGLCurveEvaluator::bgnmap1f(long)
171 {
172  if(output_triangles)
173  {
174  //initialized so that no maps are set initially
175  vertex_flag = 0;
176  normal_flag = 0;
177  color_flag = 0;
178  texcoord_flag = 0;
179  //no need to worry about gl states when doing callback
180  }
181 #ifdef HAVE_GL_H
182  else
183  glPushAttrib((GLbitfield) GL_EVAL_BIT);
184 #endif
185 }
186 
187 /*-------------------------------------------------------------------------
188  * endmap1 - postamble to a curve map
189  *-------------------------------------------------------------------------
190  */
191 void
192 OpenGLCurveEvaluator::endmap1f(void)
193 {
194  if(output_triangles)
195  {
196 
197  }
198 #ifdef HAVE_GL_H
199  else
200  glPopAttrib();
201 #endif
202 }
203 
204 /*-------------------------------------------------------------------------
205  * map1f - pass a desription of a curve map
206  *-------------------------------------------------------------------------
207  */
208 void
209 OpenGLCurveEvaluator::map1f(
210  long type, /* map type */
211  REAL ulo, /* lower parametric bound */
212  REAL uhi, /* upper parametric bound */
213  long stride, /* distance to next point in REALS */
214  long order, /* parametric order */
215  REAL *pts /* control points */
216 )
217 {
218  if(output_triangles)
219  {
220  int dimension = 0;
221  int which = 0;
222  switch(type){
223  case GL_MAP1_VERTEX_3:
224  which = 0;
225  dimension = 3;
226  break;
227  case GL_MAP1_VERTEX_4:
228  which=0;
229  dimension = 4;
230  break;
231  case GL_MAP1_INDEX:
232  which=2;
233  dimension = 1;
234  break;
235  case GL_MAP1_COLOR_4:
236  which=2;
237  dimension = 4;
238  break;
239  case GL_MAP1_NORMAL:
240  which=1;
241  dimension = 3;
242  break;
243  case GL_MAP1_TEXTURE_COORD_1:
244  which=3;
245  dimension = 1;
246  break;
247  case GL_MAP1_TEXTURE_COORD_2:
248  which=3;
249  dimension = 2;
250  break;
251 
252  case GL_MAP1_TEXTURE_COORD_3:
253  which=3;
254  dimension = 3;
255  break;
256  case GL_MAP1_TEXTURE_COORD_4:
257  which=3;
258  dimension = 4;
259  break;
260  }
261  inMap1f(which, dimension, ulo, uhi, stride, order, pts);
262  }
263 #ifdef HAVE_GL_H
264  else
265  glMap1f((GLenum) type, (GLfloat) ulo, (GLfloat) uhi, (GLint) stride,
266  (GLint) order, (const GLfloat *) pts);
267 #endif
268 }
269 
270 /*-------------------------------------------------------------------------
271  * mapmesh1f - evaluate a mesh of points on lattice
272  *-------------------------------------------------------------------------
273  */
274 void OpenGLCurveEvaluator::mapmesh1f(long style, long from, long to)
275 {
276  if(output_triangles)
277  {
278  inMapMesh1f((int) from, (int) to);
279  }
280 #ifdef HAVE_GL_H
281  else
282  {
283  switch(style) {
284  default:
285  case N_MESHFILL:
286  case N_MESHLINE:
287  glEvalMesh1((GLenum) GL_LINE, (GLint) from, (GLint) to);
288  break;
289  case N_MESHPOINT:
290  glEvalMesh1((GLenum) GL_POINT, (GLint) from, (GLint) to);
291  break;
292  }
293  }
294 #endif
295 }
296 
297 /*-------------------------------------------------------------------------
298  * evalpoint1i - evaluate a point on a curve
299  *-------------------------------------------------------------------------
300  */
301 void OpenGLCurveEvaluator::evalpoint1i(long i)
302 {
303 #ifdef HAVE_GL_H
304  glEvalPoint1((GLint) i);
305 #endif
306 }
307 
308 /*-------------------------------------------------------------------------
309  * evalcoord1f - evaluate a point on a curve
310  *-------------------------------------------------------------------------
311  */
312 void OpenGLCurveEvaluator::evalcoord1f(long, REAL u)
313 {
314 #ifdef HAVE_GL_H
315  glEvalCoord1f((GLfloat) u);
316 #endif
317 }
318 
319 void
320 #ifdef _WIN32
321 OpenGLCurveEvaluator::putCallBack(GLenum which, void (GLAPIENTRY *fn)())
322 #else
323 OpenGLCurveEvaluator::putCallBack(GLenum which, _GLUfuncptr fn)
324 #endif
325 {
326  switch(which)
327  {
328  case GLU_NURBS_BEGIN:
329  beginCallBackN = (void (GLAPIENTRY *) (GLenum)) fn;
330  break;
331  case GLU_NURBS_END:
332  endCallBackN = (void (GLAPIENTRY *) (void)) fn;
333  break;
334  case GLU_NURBS_VERTEX:
335  vertexCallBackN = (void (GLAPIENTRY *) (const GLfloat*)) fn;
336  break;
337  case GLU_NURBS_NORMAL:
338  normalCallBackN = (void (GLAPIENTRY *) (const GLfloat*)) fn;
339  break;
340  case GLU_NURBS_COLOR:
341  colorCallBackN = (void (GLAPIENTRY *) (const GLfloat*)) fn;
342  break;
343  case GLU_NURBS_TEXTURE_COORD:
344  texcoordCallBackN = (void (GLAPIENTRY *) (const GLfloat*)) fn;
345  break;
346  case GLU_NURBS_BEGIN_DATA:
347  beginCallBackData = (void (GLAPIENTRY *) (GLenum, void*)) fn;
348  break;
349  case GLU_NURBS_END_DATA:
350  endCallBackData = (void (GLAPIENTRY *) (void*)) fn;
351  break;
352  case GLU_NURBS_VERTEX_DATA:
353  vertexCallBackData = (void (GLAPIENTRY *) (const GLfloat*, void*)) fn;
354  break;
355  case GLU_NURBS_NORMAL_DATA:
356  normalCallBackData = (void (GLAPIENTRY *) (const GLfloat*, void*)) fn;
357  break;
358  case GLU_NURBS_COLOR_DATA:
359  colorCallBackData = (void (GLAPIENTRY *) (const GLfloat*, void*)) fn;
360  break;
361  case GLU_NURBS_TEXTURE_COORD_DATA:
362  texcoordCallBackData = (void (GLAPIENTRY *) (const GLfloat*, void*)) fn;
363  break;
364  }
365 }
366 
367 void
368 OpenGLCurveEvaluator::beginCallBack(GLenum which, void *data)
369 {
370  if(beginCallBackData)
371  beginCallBackData(which, data);
372  else if(beginCallBackN)
373  beginCallBackN(which);
374 }
375 
376 void
377 OpenGLCurveEvaluator::endCallBack(void *data)
378 {
379  if(endCallBackData)
380  endCallBackData(data);
381  else if(endCallBackN)
382  endCallBackN();
383 }
384 
385 void
386 OpenGLCurveEvaluator::vertexCallBack(const GLfloat *vert, void* data)
387 {
388  if(vertexCallBackData)
389  vertexCallBackData(vert, data);
390  else if(vertexCallBackN)
391  vertexCallBackN(vert);
392 }
393 
394 
395 void
396 OpenGLCurveEvaluator::normalCallBack(const GLfloat *normal, void* data)
397 {
398  if(normalCallBackData)
399  normalCallBackData(normal, data);
400  else if(normalCallBackN)
401  normalCallBackN(normal);
402 }
403 
404 void
405 OpenGLCurveEvaluator::colorCallBack(const GLfloat *color, void* data)
406 {
407  if(colorCallBackData)
408  colorCallBackData(color, data);
409  else if(colorCallBackN)
410  colorCallBackN(color);
411 }
412 
413 void
414 OpenGLCurveEvaluator::texcoordCallBack(const GLfloat *texcoord, void* data)
415 {
416  if(texcoordCallBackData)
417  texcoordCallBackData(texcoord, data);
418  else if(texcoordCallBackN)
419  texcoordCallBackN(texcoord);
420 }