FreeWRL/FreeX3D  3.0.0
glsurfeval.h
1 /*
2  * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3  * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice including the dates of first publication and
13  * either this permission notice or a reference to
14  * http://oss.sgi.com/projects/FreeB/
15  * shall be included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  *
25  * Except as contained in this notice, the name of Silicon Graphics, Inc.
26  * shall not be used in advertising or otherwise to promote the sale, use or
27  * other dealings in this Software without prior written authorization from
28  * Silicon Graphics, Inc.
29  */
30 
31 /*
32  * glsurfeval.h
33  *
34  */
35 
36 #ifndef __gluglsurfeval_h_
37 #define __gluglsurfeval_h_
38 
39 #include "basicsurfeval.h"
40 #include "bezierPatchMesh.h" //in case output triangles
41 #include <libnurbs2.h>
42 //#include <GL/gl.h>
43 //#include <GL/glu.h>
44 
45 class SurfaceMap;
47 class StoredVertex;
48 
49 #define TYPECOORD 1
50 #define TYPEPOINT 2
51 
52 /* Cache up to 3 vertices from tmeshes */
53 #define VERTEX_CACHE_SIZE 3
54 
55 /*for internal evaluator callback stuff*/
56 #ifndef IN_MAX_BEZIER_ORDER
57 #define IN_MAX_BEZIER_ORDER 40 /*XXX should be bigger than machine order*/
58 #endif
59 
60 #ifndef IN_MAX_DIMENSION
61 #define IN_MAX_DIMENSION 4
62 #endif
63 
64 typedef struct surfEvalMachine{
65  REAL uprime;//cached previusly evaluated uprime.
66  REAL vprime;
67  int k; /*the dimension*/
68  REAL u1;
69  REAL u2;
70  int ustride;
71  int uorder;
72  REAL v1;
73  REAL v2;
74  int vstride;
75  int vorder;
76  REAL ctlPoints[IN_MAX_BEZIER_ORDER*IN_MAX_BEZIER_ORDER*IN_MAX_DIMENSION];
77  REAL ucoeff[IN_MAX_BEZIER_ORDER]; /*cache the polynomial values*/
78  REAL vcoeff[IN_MAX_BEZIER_ORDER];
79  REAL ucoeffDeriv[IN_MAX_BEZIER_ORDER]; /*cache the polynomial derivatives*/
80  REAL vcoeffDeriv[IN_MAX_BEZIER_ORDER];
82 
83 
84 
85 class StoredVertex {
86 public:
87  StoredVertex() { type = 0; }
88  ~StoredVertex(void) {}
89  void saveEvalCoord(REAL x, REAL y)
90  {coord[0] = x; coord[1] = y; type = TYPECOORD; }
91  void saveEvalPoint(long x, long y)
92  {point[0] = x; point[1] = y; type = TYPEPOINT; }
93  void invoke(OpenGLSurfaceEvaluator *eval);
94 
95 private:
96  int type;
97  REAL coord[2];
98  long point[2];
99 };
100 
102 public:
104  virtual ~OpenGLSurfaceEvaluator( void );
105  void polymode( long style );
106  void range2f( long, REAL *, REAL * );
107  void domain2f( REAL, REAL, REAL, REAL );
108  void addMap( SurfaceMap * ) { }
109 
110  void enable( long );
111  void disable( long );
112  void bgnmap2f( long );
113  void map2f( long, REAL, REAL, long, long,
114  REAL, REAL, long, long, REAL * );
115  void mapgrid2f( long, REAL, REAL, long, REAL, REAL );
116  void mapmesh2f( long, long, long, long, long );
117  void evalcoord2f( long, REAL, REAL );
118  void evalpoint2i( long, long );
119  void endmap2f( void );
120 
121  void bgnline( void );
122  void endline( void );
123  void bgnclosedline( void );
124  void endclosedline( void );
125  void bgntmesh( void );
126  void swaptmesh( void );
127  void endtmesh( void );
128  void bgnqstrip( void );
129  void endqstrip( void );
130 
131  void bgntfan( void );
132  void endtfan( void );
133  void evalUStrip(int n_upper, REAL v_upper, REAL* upper_val,
134  int n_lower, REAL v_lower, REAL* lower_val);
135  void evalVStrip(int n_left, REAL u_left, REAL* left_val,
136  int n_right, REAL u_right, REAL* right_val);
137 
138  void coord2f( REAL, REAL );
139  void point2i( long, long );
140 
141  void newtmeshvert( REAL, REAL );
142  void newtmeshvert( long, long );
143 
144 #ifdef _WIN32
145  void putCallBack(GLenum which, void (GLAPIENTRY *fn)() );
146 #else
147  void putCallBack(GLenum which, _GLUfuncptr fn );
148 #endif
149 
150  int get_vertices_call_back()
151  {
152  return output_triangles;
153  }
154  void put_vertices_call_back(int flag)
155  {
156  output_triangles = flag;
157  }
158 
159  void put_callback_auto_normal(int flag)
160  {
161  callback_auto_normal = flag;
162  }
163 
164  int get_callback_auto_normal()
165  {
166  return callback_auto_normal;
167  }
168 
169  void set_callback_userData(void* data)
170  {
171  userData = data;
172  }
173 
174  /**************begin for LOD_eval_list***********/
175  void LOD_eval_list(int level);
176 
177 
178 
179 
180 private:
181  StoredVertex *vertexCache[VERTEX_CACHE_SIZE];
182  int tmeshing;
183  int which;
184  int vcount;
185 
186  GLint gl_polygon_mode[2];/*to save and restore so that
187  *no side effect
188  */
189  bezierPatchMesh *global_bpm; //for output triangles
190  int output_triangles; //true 1 or false 0
191 
192 
193 
194  void (GLAPIENTRY *beginCallBackN) (GLenum type);
195  void (GLAPIENTRY *endCallBackN) (void);
196  void (GLAPIENTRY *vertexCallBackN) (const GLfloat *vert);
197  void (GLAPIENTRY *normalCallBackN) (const GLfloat *normal);
198  void (GLAPIENTRY *colorCallBackN) (const GLfloat *color);
199  void (GLAPIENTRY *texcoordCallBackN) (const GLfloat *texcoord);
200 
201  void (GLAPIENTRY *beginCallBackData) (GLenum type, void* data);
202  void (GLAPIENTRY *endCallBackData) (void* data);
203  void (GLAPIENTRY *vertexCallBackData) (const GLfloat *vert, void* data);
204  void (GLAPIENTRY *normalCallBackData) (const GLfloat *normal, void* data);
205  void (GLAPIENTRY *colorCallBackData) (const GLfloat *color, void* data);
206  void (GLAPIENTRY *texcoordCallBackData) (const GLfloat *texcoord, void* data);
207 
208  void beginCallBack (GLenum type, void* data);
209  void endCallBack (void* data);
210  void vertexCallBack (const GLfloat *vert, void* data);
211  void normalCallBack (const GLfloat *normal, void* data);
212  void colorCallBack (const GLfloat *color, void* data);
213  void texcoordCallBack (const GLfloat *texcoord, void* data);
214 
215 
216  void* userData; //the opaque pointer for Data callback functions.
217 
218  /*LOD evaluation*/
219  void LOD_triangle(REAL A[2], REAL B[2], REAL C[2],
220  int level);
221  void LOD_eval(int num_vert, REAL* verts, int type, int level);
222 
223  int LOD_eval_level; //set by LOD_eval_list()
224 
225  /*************begin for internal evaluators*****************/
226 
227  /*the following global variables are only defined in this file.
228  *They are used to cache the precomputed Bezier polynomial values.
229  *These calues may be used consecutively in which case we don't have
230  *recompute these values again.
231  */
232  int global_uorder; /*store the uorder in the previous evaluation*/
233  int global_vorder; /*store the vorder in the previous evaluation*/
234  REAL global_uprime;
235  REAL global_vprime;
236  REAL global_vprime_BV;
237  REAL global_uprime_BU;
238  int global_uorder_BV; /*store the uorder in the previous evaluation*/
239  int global_vorder_BV; /*store the vorder in the previous evaluation*/
240  int global_uorder_BU; /*store the uorder in the previous evaluation*/
241  int global_vorder_BU; /*store the vorder in the previous evaluation*/
242 
243  REAL global_ucoeff[IN_MAX_BEZIER_ORDER]; /*cache the polynomial values*/
244  REAL global_vcoeff[IN_MAX_BEZIER_ORDER];
245  REAL global_ucoeffDeriv[IN_MAX_BEZIER_ORDER]; /*cache the polynomial derivatives*/
246  REAL global_vcoeffDeriv[IN_MAX_BEZIER_ORDER];
247 
248  REAL global_BV[IN_MAX_BEZIER_ORDER][IN_MAX_DIMENSION];
249  REAL global_PBV[IN_MAX_BEZIER_ORDER][IN_MAX_DIMENSION];
250  REAL global_BU[IN_MAX_BEZIER_ORDER][IN_MAX_DIMENSION];
251  REAL global_PBU[IN_MAX_BEZIER_ORDER][IN_MAX_DIMENSION];
252  REAL* global_baseData;
253 
254  int global_ev_k; /*the dimension*/
255  REAL global_ev_u1;
256  REAL global_ev_u2;
257  int global_ev_ustride;
258  int global_ev_uorder;
259  REAL global_ev_v1;
260  REAL global_ev_v2;
261  int global_ev_vstride;
262  int global_ev_vorder;
263  REAL global_ev_ctlPoints[IN_MAX_BEZIER_ORDER*IN_MAX_BEZIER_ORDER*IN_MAX_DIMENSION];
264 
265  REAL global_grid_u0;
266  REAL global_grid_u1;
267  int global_grid_nu;
268  REAL global_grid_v0;
269  REAL global_grid_v1;
270  int global_grid_nv;
271 
272 /*functions*/
273  void inDoDomain2WithDerivs(int k, REAL u, REAL v,
274  REAL u1, REAL u2, int uorder,
275  REAL v1, REAL v2, int vorder,
276  REAL *baseData,
277  REAL *retPoint, REAL *retdu, REAL *retdv);
278  void inPreEvaluate(int order, REAL vprime, REAL *coeff);
279  void inPreEvaluateWithDeriv(int order, REAL vprime, REAL *coeff, REAL *coeffDeriv);
280  void inComputeFirstPartials(REAL *p, REAL *pu, REAL *pv);
281  void inComputeNormal2(REAL *pu, REAL *pv, REAL *n);
282  void inDoEvalCoord2(REAL u, REAL v,
283  REAL *retPoint, REAL *retNormal);
284  void inDoEvalCoord2NOGE(REAL u, REAL v,
285  REAL *retPoint, REAL *retNormal);
286  void inMap2f(int k,
287  REAL ulower,
288  REAL uupper,
289  int ustride,
290  int uorder,
291  REAL vlower,
292  REAL vupper,
293  int vstride,
294  int vorder,
295  REAL *ctlPoints);
296 
297  void inMapGrid2f(int nu, REAL u0, REAL u1,
298  int nv, REAL v0, REAL v1);
299 
300  void inEvalMesh2(int lowU, int lowV, int highU, int highV);
301  void inEvalPoint2(int i, int j);
302  void inEvalCoord2f(REAL u, REAL v);
303 
304 void inEvalULine(int n_points, REAL v, REAL* u_vals,
305  int stride, REAL ret_points[][3], REAL ret_normals[][3]);
306 
307 void inEvalVLine(int n_points, REAL u, REAL* v_vals,
308  int stride, REAL ret_points[][3], REAL ret_normals[][3]);
309 
310 void inEvalUStrip(int n_upper, REAL v_upper, REAL* upper_val,
311  int n_lower, REAL v_lower, REAL* lower_val
312  );
313 void inEvalVStrip(int n_left, REAL u_left, REAL* left_val, int n_right, REAL u_right, REAL* right_val);
314 
315 void inPreEvaluateBV(int k, int uorder, int vorder, REAL vprime, REAL *baseData);
316 void inPreEvaluateBU(int k, int uorder, int vorder, REAL uprime, REAL *baseData);
317 void inPreEvaluateBV_intfac(REAL v )
318  {
319  inPreEvaluateBV(global_ev_k, global_ev_uorder, global_ev_vorder, (v-global_ev_v1)/(global_ev_v2-global_ev_v1), global_ev_ctlPoints);
320  }
321 
322 void inPreEvaluateBU_intfac(REAL u)
323  {
324  inPreEvaluateBU(global_ev_k, global_ev_uorder, global_ev_vorder, (u-global_ev_u1)/(global_ev_u2-global_ev_u1), global_ev_ctlPoints);
325  }
326 
327 void inDoDomain2WithDerivsBV(int k, REAL u, REAL v,
328  REAL u1, REAL u2, int uorder,
329  REAL v1, REAL v2, int vorder,
330  REAL *baseData,
331  REAL *retPoint, REAL* retdu, REAL *retdv);
332 
333 void inDoDomain2WithDerivsBU(int k, REAL u, REAL v,
334  REAL u1, REAL u2, int uorder,
335  REAL v1, REAL v2, int vorder,
336  REAL *baseData,
337  REAL *retPoint, REAL* retdu, REAL *retdv);
338 
339 
340 void inDoEvalCoord2NOGE_BV(REAL u, REAL v,
341  REAL *retPoint, REAL *retNormal);
342 
343 void inDoEvalCoord2NOGE_BU(REAL u, REAL v,
344  REAL *retPoint, REAL *retNormal);
345 
346 void inBPMEval(bezierPatchMesh* bpm);
347 void inBPMListEval(bezierPatchMesh* list);
348 
349 /*-------------begin for surfEvalMachine -------------*/
350 surfEvalMachine em_vertex;
351 surfEvalMachine em_normal;
352 surfEvalMachine em_color;
353 surfEvalMachine em_texcoord;
354 
355 int auto_normal_flag; //whether to output normla or not in callback
356  //determined by GL_AUTO_NORMAL and callback_auto_normal
357 int callback_auto_normal; //GLU_CALLBACK_AUTO_NORMAL_EXT
358 int vertex_flag;
359 int normal_flag;
360 int color_flag;
361 int texcoord_flag;
362 
363 void inMap2fEM(int which, //0:vert,1:norm,2:color,3:tex
364  int dimension,
365  REAL ulower,
366  REAL uupper,
367  int ustride,
368  int uorder,
369  REAL vlower,
370  REAL vupper,
371  int vstride,
372  int vorder,
373  REAL *ctlPoints);
374 
375 void inDoDomain2WithDerivsEM(surfEvalMachine *em, REAL u, REAL v,
376  REAL *retPoint, REAL *retdu, REAL *retdv);
377 void inDoDomain2EM(surfEvalMachine *em, REAL u, REAL v,
378  REAL *retPoint);
379  void inDoEvalCoord2EM(REAL u, REAL v);
380 
381 void inBPMEvalEM(bezierPatchMesh* bpm);
382 void inBPMListEvalEM(bezierPatchMesh* list);
383 
384 /*-------------end for surfEvalMachine -------------*/
385 
386 
387  /*************end for internal evaluators*****************/
388 
389 };
390 
391 inline void StoredVertex::invoke(OpenGLSurfaceEvaluator *eval)
392 {
393  switch(type) {
394  case TYPECOORD:
395  eval->coord2f(coord[0], coord[1]);
396  break;
397  case TYPEPOINT:
398  eval->point2i(point[0], point[1]);
399  break;
400  default:
401  break;
402  }
403 }
404 
405 #endif /* __gluglsurfeval_h_ */