/* * nurbs.c * This program shows a NURBS (Non-uniform rational B-splines) * surface, shaped like a heart. * (Ting, adpated from the redbook, 25/03/05) */ #include #include #include #ifndef WIN32 #define CALLBACK #endif #define S_NUMPOINTS 13 #define S_ORDER 3 #define S_NUMKNOTS (S_NUMPOINTS + S_ORDER) #define T_NUMPOINTS 3 #define T_ORDER 3 #define T_NUMKNOTS (T_NUMPOINTS + T_ORDER) #define SQRT2 1.41421356237309504880 GLenum expectedError; /* initialized local data */ GLfloat sknots[S_NUMKNOTS] = {-1.0, -1.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 9.0, 9.0}; GLfloat tknots[T_NUMKNOTS] = {1.0, 1.0, 1.0, 2.0, 2.0, 2.0}; GLfloat ctlpoints[S_NUMPOINTS][T_NUMPOINTS][4] = { { {4.,2.,2.,1.},{4.,1.6,2.5,1.},{4.,2.,3.0,1.} }, { {5.,4.,2.,1.},{5.,4.,2.5,1.},{5.,4.,3.0,1.} }, { {6.,5.,2.,1.},{6.,5.,2.5,1.},{6.,5.,3.0,1.} }, { {SQRT2*6.,SQRT2*6.,SQRT2*2.,SQRT2}, {SQRT2*6.,SQRT2*6.,SQRT2*2.5,SQRT2}, {SQRT2*6.,SQRT2*6.,SQRT2*3.0,SQRT2} }, { {5.2,6.7,2.,1.},{5.2,6.7,2.5,1.},{5.2,6.7,3.0,1.} }, { {SQRT2*4.,SQRT2*6.,SQRT2*2.,SQRT2}, {SQRT2*4.,SQRT2*6.,SQRT2*2.5,SQRT2}, {SQRT2*4.,SQRT2*6.,SQRT2*3.0,SQRT2} }, { {4.,5.2,2.,1.},{4.,4.6,2.5,1.},{4.,5.2,3.0,1.} }, { {SQRT2*4.,SQRT2*6.,SQRT2*2.,SQRT2}, {SQRT2*4.,SQRT2*6.,SQRT2*2.5,SQRT2}, {SQRT2*4.,SQRT2*6.,SQRT2*3.0,SQRT2} }, { {2.8,6.7,2.,1.},{2.8,6.7,2.5,1.},{2.8,6.7,3.0,1.} }, { {SQRT2*2.,SQRT2*6.,SQRT2*2.,SQRT2}, {SQRT2*2.,SQRT2*6.,SQRT2*2.5,SQRT2}, {SQRT2*2.,SQRT2*6.,SQRT2*3.0,SQRT2} }, { {2.,5.,2.,1.},{2.,5.,2.5,1.},{2.,5.,3.0,1.} }, { {3.,4.,2.,1.},{3.,4.,2.5,1.},{3.,4.,3.0,1.} }, { {4.,2.,2.,1.},{4.,1.6,2.5,1.},{4.,2.,3.0,1.} } }; GLUnurbsObj *theNurb, *theNurbCurve; /* Error Callback */ static void CALLBACK ErrorCallback(GLenum which) { if (which != expectedError) { fprintf(stderr, "Unexpected error occured (%d):\n", which); fprintf(stderr, " %s\n", gluErrorString(which)); } } /* Initialize the properties of the nurbs curves and surfaces */ void init(void) { theNurb = gluNewNurbsRenderer(); gluNurbsCallback(theNurb, GLU_ERROR, ErrorCallback); gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25.0); gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON); theNurbCurve = gluNewNurbsRenderer(); gluNurbsCallback(theNurbCurve, GLU_ERROR, ErrorCallback); gluNurbsProperty(theNurbCurve, GLU_SAMPLING_TOLERANCE, 50.0); expectedError = 0; } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glTranslatef (4., 4.5, 2.5); glRotatef (220.0, 1., 0., 0.); glRotatef (115.0, 0., 1., 0.); glTranslatef (-4., -4.5, -2.5); /* draw the tesselated surface */ draw_nurbs_surface(); /* draw the two border curves */ draw_nurbs_curve(0); draw_nurbs_curve(2); glPopMatrix(); glFlush(); } int draw_nurbs_surface (void) { /* draw a nurbs tesselated surface */ glColor3f(1.0,1.0,1.0); /* white */ gluBeginSurface(theNurb); gluNurbsSurface(theNurb, S_NUMKNOTS, sknots, T_NUMKNOTS, tknots, 4 * T_NUMPOINTS, 4, &ctlpoints[0][0][0], S_ORDER, T_ORDER, GL_MAP2_VERTEX_4); gluEndSurface(theNurb); return 1; } int draw_nurbs_curve (int i) { /* draw a nurbs curve */ glColor3f(1.0,0.0,0.0); /* red */ gluBeginCurve(theNurbCurve); gluNurbsCurve(theNurbCurve, S_NUMKNOTS, sknots, 4 * T_NUMPOINTS, &ctlpoints[0][i][0], S_ORDER, GL_MAP1_VERTEX_4); gluEndCurve(theNurbCurve); return 1; } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-1.0, 1.0, -1.5, 0.5, 0.8, 10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(7.0,4.5,4.0, 4.5,4.5,2.0, 6.0,-3.0,2.0); } static void key(unsigned char k, int x, int y) { switch (k) { case 27: /* Escape */ exit(0); break; default: return; } glutPostRedisplay(); } /* Main Loop * Open window with initial window size, title bar, * RGBA display mode, and handle input events. */ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow (argv[0]); init(); glutReshapeFunc (myReshape); glutDisplayFunc(display); glutKeyboardFunc(key); glutMainLoop(); return 0; /* ANSI C requires main to return int. */ }