43 #include "glimports.h"
44 #include "glrenderer.h"
45 #include "glsurfeval.h"
46 #include "nurbsconsts.h"
47 #include "bezierPatchMesh.h"
63 static int STAT_num_of_triangles=0;
64 static int STAT_num_of_eval_vertices=0;
65 static int STAT_num_of_quad_strips=0;
74 extern "C" {
void evalUStripExt(
int n_upper, REAL v_upper, REAL* upper_val,
75 int n_lower, REAL v_lower, REAL* lower_val);}
77 extern "C" {
void evalVStripExt(
int n_left, REAL u_left, REAL* left_val,
78 int n_right, REAL u_right, REAL* right_val);
84 void OpenGLSurfaceEvaluator::LOD_eval_list(
int level)
95 inBPMListEvalEM(global_bpm);
99 OpenGLSurfaceEvaluator::OpenGLSurfaceEvaluator()
103 for (i=0; i<VERTEX_CACHE_SIZE; i++) {
112 global_uprime = -1.0;
113 global_vprime = -1.0;
114 global_vprime_BV = -1.0;
115 global_uprime_BU = -1.0;
116 global_uorder_BU = 0;
117 global_vorder_BU = 0;
118 global_uorder_BV = 0;
119 global_vorder_BV = 0;
120 global_baseData = NULL;
123 output_triangles = 0;
126 beginCallBackN = NULL;
128 vertexCallBackN = NULL;
129 normalCallBackN = NULL;
130 colorCallBackN = NULL;
131 texcoordCallBackN = NULL;
132 beginCallBackData = NULL;
133 endCallBackData = NULL;
134 vertexCallBackData = NULL;
135 normalCallBackData = NULL;
136 colorCallBackData = NULL;
137 texcoordCallBackData = NULL;
141 auto_normal_flag = 0;
142 callback_auto_normal = 0;
148 em_vertex.uprime = -1.0;
149 em_vertex.vprime = -1.0;
150 em_normal.uprime = -1.0;
151 em_normal.vprime = -1.0;
152 em_color.uprime = -1.0;
153 em_color.vprime = -1.0;
154 em_texcoord.uprime = -1.0;
155 em_texcoord.vprime = -1.0;
162 OpenGLSurfaceEvaluator::~OpenGLSurfaceEvaluator()
164 for (
int ii= 0; ii< VERTEX_CACHE_SIZE; ii++) {
165 delete vertexCache[ii];
175 OpenGLSurfaceEvaluator::disable(
long type)
177 glDisable((GLenum) type);
185 OpenGLSurfaceEvaluator::enable(
long type)
187 glEnable((GLenum) type);
195 OpenGLSurfaceEvaluator::mapgrid2f(
long nu, REAL u0, REAL u1,
long nv, REAL v0, REAL v1)
197 #ifdef USE_INTERNAL_EVAL
198 inMapGrid2f((
int) nu, (REAL) u0, (REAL) u1, (
int) nv,
199 (REAL) v0, (REAL) v1);
213 glMapGrid2d((GLint) nu, (GLdouble) u0, (GLdouble) u1, (GLint) nv,
214 (GLdouble) v0, (GLdouble) v1);
220 OpenGLSurfaceEvaluator::polymode(
long style)
223 if(! output_triangles)
229 glPolygonMode((GLenum) GL_FRONT_AND_BACK, (GLenum) GL_FILL);
232 glPolygonMode((GLenum) GL_FRONT_AND_BACK, (GLenum) GL_LINE);
235 glPolygonMode((GLenum) GL_FRONT_AND_BACK, (GLenum) GL_POINT);
243 OpenGLSurfaceEvaluator::bgnline(
void)
246 bezierPatchMeshBeginStrip(global_bpm, GL_LINE_STRIP);
249 glBegin((GLenum) GL_LINE_STRIP);
254 OpenGLSurfaceEvaluator::endline(
void)
257 bezierPatchMeshEndStrip(global_bpm);
265 OpenGLSurfaceEvaluator::range2f(
long type, REAL *from, REAL *to)
270 OpenGLSurfaceEvaluator::domain2f(REAL ulo, REAL uhi, REAL vlo, REAL vhi)
275 OpenGLSurfaceEvaluator::bgnclosedline(
void)
278 bezierPatchMeshBeginStrip(global_bpm, GL_LINE_LOOP);
281 glBegin((GLenum) GL_LINE_LOOP);
286 OpenGLSurfaceEvaluator::endclosedline(
void)
289 bezierPatchMeshEndStrip(global_bpm);
301 OpenGLSurfaceEvaluator::bgntmesh(
void)
309 bezierPatchMeshBeginStrip(global_bpm, GL_TRIANGLES);
312 glBegin((GLenum) GL_TRIANGLES);
318 OpenGLSurfaceEvaluator::swaptmesh(
void)
325 OpenGLSurfaceEvaluator::endtmesh(
void)
331 bezierPatchMeshEndStrip(global_bpm);
339 OpenGLSurfaceEvaluator::bgntfan(
void)
343 bezierPatchMeshBeginStrip(global_bpm, GL_TRIANGLE_FAN);
346 glBegin((GLenum) GL_TRIANGLE_FAN);
351 OpenGLSurfaceEvaluator::endtfan(
void)
354 bezierPatchMeshEndStrip(global_bpm);
362 OpenGLSurfaceEvaluator::evalUStrip(
int n_upper, REAL v_upper, REAL* upper_val,
int n_lower, REAL v_lower, REAL* lower_val)
364 #ifdef USE_INTERNAL_EVAL
365 inEvalUStrip(n_upper, v_upper, upper_val,
366 n_lower, v_lower, lower_val);
370 evalUStripExt(n_upper, v_upper, upper_val,
371 n_lower, v_lower, lower_val);
388 if(upper_val[0] <= lower_val[0])
393 leftMostV[0] = upper_val[0];
394 leftMostV[1] = v_upper;
401 leftMostV[0] = lower_val[0];
402 leftMostV[1] = v_lower;
418 coord2f(leftMostV[0], leftMostV[1]);
423 coord2f(lower_val[j], v_lower);
438 coord2f(leftMostV[0], leftMostV[1]);
442 for(k=n_upper-1; k>=i; k--)
444 coord2f(upper_val[k], v_upper);
455 if(upper_val[i] <= lower_val[j])
458 coord2f(lower_val[j], v_lower);
469 if(upper_val[k] > lower_val[j])
479 coord2f(upper_val[l], v_upper);
484 coord2f(leftMostV[0], leftMostV[1]);
494 leftMostV[0] = upper_val[k];
495 leftMostV[1] = v_upper;
502 coord2f(upper_val[i], v_upper);
506 coord2f(leftMostV[0], leftMostV[1]);
517 if(lower_val[k] >= upper_val[i])
519 coord2f(lower_val[k], v_lower);
530 leftMostV[0] = lower_val[j-1];
531 leftMostV[1] = v_lower;
549 OpenGLSurfaceEvaluator::evalVStrip(
int n_left, REAL u_left, REAL* left_val,
int n_right, REAL u_right, REAL* right_val)
551 #ifdef USE_INTERNAL_EVAL
552 inEvalVStrip(n_left, u_left, left_val,
553 n_right, u_right, right_val);
557 evalVStripExt(n_left, u_left, left_val,
558 n_right, u_right, right_val);
575 if(left_val[0] <= right_val[0])
580 botMostV[0] = u_left;
581 botMostV[1] = left_val[0];
588 botMostV[0] = u_right;
589 botMostV[1] = right_val[0];
604 coord2f(botMostV[0], botMostV[1]);
606 coord2f(u_right, right_val[j]);
621 coord2f(botMostV[0], botMostV[1]);
625 for(k=n_left-1; k>=i; k--)
627 coord2f(u_left, left_val[k]);
638 if(left_val[i] <= right_val[j])
641 coord2f(u_right, right_val[j]);
652 if(left_val[k] > right_val[j])
662 coord2f(u_left, left_val[l]);
667 coord2f(botMostV[0], botMostV[1]);
677 botMostV[0] = u_left;
678 botMostV[1] = left_val[k];
685 coord2f(u_left, left_val[i]);
689 coord2f(botMostV[0], botMostV[1]);
700 if(right_val[k] >= left_val[i])
702 coord2f(u_right, right_val[k]);
713 botMostV[0] = u_right;
714 botMostV[1] = right_val[j-1];
731 OpenGLSurfaceEvaluator::bgnqstrip(
void)
734 bezierPatchMeshBeginStrip(global_bpm, GL_QUAD_STRIP);
737 glBegin((GLenum) GL_QUAD_STRIP);
741 STAT_num_of_quad_strips++;
746 OpenGLSurfaceEvaluator::endqstrip(
void)
749 bezierPatchMeshEndStrip(global_bpm);
762 OpenGLSurfaceEvaluator::bgnmap2f(
long)
769 if(global_bpm != NULL) {
770 bezierPatchMeshListDelete(global_bpm);
785 if(normalCallBackN != NULL ||
786 normalCallBackData != NULL)
787 auto_normal_flag = 1;
789 auto_normal_flag = 0;
806 glPushAttrib((GLbitfield) GL_EVAL_BIT);
813 glPushAttrib((GLbitfield) GL_EVAL_BIT);
817 glGetIntegerv(GL_POLYGON_MODE, gl_polygon_mode);
828 OpenGLSurfaceEvaluator::endmap2f(
void)
841 inBPMListEvalEM(global_bpm);
869 bezierPatchMeshListDelete(global_bpm);
884 fprintf(stderr,
"num_vertices=%i,num_triangles=%i,num_quads_strips=%i\n", STAT_num_of_eval_vertices,STAT_num_of_triangles,STAT_num_of_quad_strips);
890 glPolygonMode( GL_FRONT, (GLenum) gl_polygon_mode[0]);
891 glPolygonMode( GL_BACK, (GLenum) gl_polygon_mode[1]);
903 OpenGLSurfaceEvaluator::map2f(
915 #ifdef USE_INTERNAL_EVAL
916 inMap2f((
int) _type, (REAL) _ulower, (REAL) _uupper,
917 (
int) _ustride, (
int) _uorder, (REAL) _vlower,
918 (REAL) _vupper, (
int) _vstride, (
int) _vorder,
926 if(global_bpm == NULL)
927 global_bpm = bezierPatchMeshMake2(10,10);
929 (global_bpm->bpatch == NULL &&
930 (_type == GL_MAP2_VERTEX_3 || _type == GL_MAP2_VERTEX_4))
932 (global_bpm->bpatch_normal == NULL &&
933 (_type == GL_MAP2_NORMAL))
935 (global_bpm->bpatch_color == NULL &&
936 (_type == GL_MAP2_INDEX || _type == GL_MAP2_COLOR_4))
938 (global_bpm->bpatch_texcoord == NULL &&
939 (_type == GL_MAP2_TEXTURE_COORD_1 ||
940 _type == GL_MAP2_TEXTURE_COORD_2 ||
941 _type == GL_MAP2_TEXTURE_COORD_3 ||
942 _type == GL_MAP2_TEXTURE_COORD_4 )
945 bezierPatchMeshPutPatch(global_bpm, (
int) _type, _ulower, _uupper,(
int) _ustride,(
int) _uorder,_vlower, _vupper, (
int) _vstride, (
int) _vorder, pts);
950 bezierPatchMeshPutPatch(temp, (
int) _type, _ulower, _uupper,(
int) _ustride,(
int) _uorder,_vlower, _vupper, (
int) _vstride, (
int) _vorder, pts);
951 global_bpm = bezierPatchMeshListInsert(global_bpm, temp);
963 glMap2f((GLenum) _type, (GLfloat) _ulower, (GLfloat) _uupper,
964 (GLint) _ustride, (GLint) _uorder, (GLfloat) _vlower,
965 (GLfloat) _vupper, (GLint) _vstride, (GLint) _vorder,
966 (
const GLfloat *) pts);
978 OpenGLSurfaceEvaluator::mapmesh2f(
long style,
long umin,
long umax,
long vmin,
long vmax)
984 #ifdef USE_INTERNAL_EVAL
985 inEvalMesh2((
int)umin, (
int)vmin, (
int)umax, (
int)vmax);
993 bezierPatchMeshBeginStrip(global_bpm, GL_POLYGON);
994 bezierPatchMeshInsertUV(global_bpm, global_grid_u0, global_grid_v0);
995 bezierPatchMeshInsertUV(global_bpm, global_grid_u1, global_grid_v1);
996 bezierPatchMeshInsertUV(global_bpm, (REAL)global_grid_nu, (REAL)global_grid_nv);
997 bezierPatchMeshInsertUV(global_bpm, (REAL)umin, (REAL)vmin);
998 bezierPatchMeshInsertUV(global_bpm, (REAL)umax, (REAL)vmax);
999 bezierPatchMeshEndStrip(global_bpm);
1005 if(global_grid_nu == 0 || global_grid_nv == 0)
1007 du = (global_grid_u1 - global_grid_u0) / (REAL)global_grid_nu;
1008 dv = (global_grid_v1 - global_grid_v0) / (REAL)global_grid_nv;
1010 if(global_grid_nu >= global_grid_nv){
1012 for(i=umin; i<umax; i++){
1013 REAL u1 = (i==global_grid_nu)? global_grid_u1:(global_grid_u0 + i*du);
1014 REAL u2 = ((i+1) == global_grid_nu)? global_grid_u1: (global_grid_u0+(i+1)*du);
1017 for(j=vmax; j>=vmin; j--){
1018 REAL v1 = (j == global_grid_nv)? global_grid_v1: (global_grid_v0 +j*dv);
1028 for(i=vmin; i<vmax; i++){
1029 REAL v1 = (i==global_grid_nv)? global_grid_v1:(global_grid_v0 + i*dv);
1030 REAL v2 = ((i+1) == global_grid_nv)? global_grid_v1: (global_grid_v0+(i+1)*dv);
1033 for(j=umax; j>=umin; j--){
1034 REAL u1 = (j == global_grid_nu)? global_grid_u1: (global_grid_u0 +j*du);
1049 glEvalMesh2((GLenum) GL_FILL, (GLint) umin, (GLint) umax,
1050 (GLint) vmin, (GLint) vmax);
1053 glEvalMesh2((GLenum) GL_LINE, (GLint) umin, (GLint) umax,
1054 (GLint) vmin, (GLint) vmax);
1057 glEvalMesh2((GLenum) GL_POINT, (GLint) umin, (GLint) umax,
1058 (GLint) vmin, (GLint) vmax);
1066 STAT_num_of_quad_strips += (umax-umin)*(vmax-vmin);
1075 OpenGLSurfaceEvaluator::evalcoord2f(
long, REAL u, REAL v)
1079 #ifdef NO_EVALUATION
1092 OpenGLSurfaceEvaluator::evalpoint2i(
long u,
long v)
1094 #ifdef NO_EVALUATION
1102 OpenGLSurfaceEvaluator::point2i(
long u,
long v )
1104 #ifdef NO_EVALUATION
1108 #ifdef USE_INTERNAL_EVAL
1109 inEvalPoint2( (
int)u, (
int)v);
1113 if(output_triangles)
1118 du = (global_grid_u1 - global_grid_u0) / (REAL)global_grid_nu;
1119 dv = (global_grid_v1 - global_grid_v0) / (REAL)global_grid_nv;
1120 fu = (u==global_grid_nu)? global_grid_u1:(global_grid_u0 + u*du);
1121 fv = (v == global_grid_nv)? global_grid_v1: (global_grid_v0 +v*dv);
1126 glEvalPoint2((GLint) u, (GLint) v);
1132 STAT_num_of_eval_vertices++;
1140 OpenGLSurfaceEvaluator::coord2f( REAL u, REAL v )
1142 #ifdef NO_EVALUATION
1146 #ifdef USE_INTERNAL_EVAL
1147 inEvalCoord2f( u, v);
1151 if(output_triangles)
1152 bezierPatchMeshInsertUV(global_bpm, u,v);
1155 glEvalCoord2f((GLfloat) u, (GLfloat) v);
1162 STAT_num_of_eval_vertices++;
1169 OpenGLSurfaceEvaluator::newtmeshvert(
long u,
long v )
1171 #ifdef NO_EVALUATION
1178 vertexCache[0]->invoke(
this);
1179 vertexCache[1]->invoke(
this);
1186 vertexCache[which]->saveEvalPoint(u, v);
1195 OpenGLSurfaceEvaluator::newtmeshvert( REAL u, REAL v )
1197 #ifdef NO_EVALUATION
1204 vertexCache[0]->invoke(
this);
1205 vertexCache[1]->invoke(
this);
1212 vertexCache[which]->saveEvalCoord(u, v);
1223 void OpenGLSurfaceEvaluator::putCallBack(GLenum which,
void (GLAPIENTRY *fn)() )
1225 void OpenGLSurfaceEvaluator::putCallBack(GLenum which, _GLUfuncptr fn )
1230 case GLU_NURBS_BEGIN:
1231 beginCallBackN = (void (GLAPIENTRY *) (GLenum)) fn;
1234 endCallBackN = (void (GLAPIENTRY *) (void)) fn;
1236 case GLU_NURBS_VERTEX:
1237 vertexCallBackN = (void (GLAPIENTRY *) (
const GLfloat*)) fn;
1239 case GLU_NURBS_NORMAL:
1240 normalCallBackN = (void (GLAPIENTRY *) (
const GLfloat*)) fn;
1242 case GLU_NURBS_COLOR:
1243 colorCallBackN = (void (GLAPIENTRY *) (
const GLfloat*)) fn;
1245 case GLU_NURBS_TEXTURE_COORD:
1246 texcoordCallBackN = (void (GLAPIENTRY *) (
const GLfloat*)) fn;
1248 case GLU_NURBS_BEGIN_DATA:
1249 beginCallBackData = (void (GLAPIENTRY *) (GLenum,
void*)) fn;
1251 case GLU_NURBS_END_DATA:
1252 endCallBackData = (void (GLAPIENTRY *) (
void*)) fn;
1254 case GLU_NURBS_VERTEX_DATA:
1255 vertexCallBackData = (void (GLAPIENTRY *) (
const GLfloat*,
void*)) fn;
1257 case GLU_NURBS_NORMAL_DATA:
1258 normalCallBackData = (void (GLAPIENTRY *) (
const GLfloat*,
void*)) fn;
1260 case GLU_NURBS_COLOR_DATA:
1261 colorCallBackData = (void (GLAPIENTRY *) (
const GLfloat*,
void*)) fn;
1263 case GLU_NURBS_TEXTURE_COORD_DATA:
1264 texcoordCallBackData = (void (GLAPIENTRY *) (
const GLfloat*,
void*)) fn;
1272 OpenGLSurfaceEvaluator::beginCallBack(GLenum which,
void *data)
1274 if(beginCallBackData)
1275 beginCallBackData(which, data);
1276 else if(beginCallBackN)
1277 beginCallBackN(which);
1281 OpenGLSurfaceEvaluator::endCallBack(
void *data)
1284 endCallBackData(data);
1285 else if(endCallBackN)
1290 OpenGLSurfaceEvaluator::vertexCallBack(
const GLfloat *vert,
void* data)
1292 if(vertexCallBackData)
1293 vertexCallBackData(vert, data);
1294 else if(vertexCallBackN)
1295 vertexCallBackN(vert);
1300 OpenGLSurfaceEvaluator::normalCallBack(
const GLfloat *normal,
void* data)
1302 if(normalCallBackData)
1303 normalCallBackData(normal, data);
1304 else if(normalCallBackN)
1305 normalCallBackN(normal);
1309 OpenGLSurfaceEvaluator::colorCallBack(
const GLfloat *color,
void* data)
1311 if(colorCallBackData)
1312 colorCallBackData(color, data);
1313 else if(colorCallBackN)
1314 colorCallBackN(color);
1318 OpenGLSurfaceEvaluator::texcoordCallBack(
const GLfloat *texcoord,
void* data)
1320 if(texcoordCallBackData)
1321 texcoordCallBackData(texcoord, data);
1322 else if(texcoordCallBackN)
1323 texcoordCallBackN(texcoord);