#include #include #define NURP 16 GLfloat ctl[NURP][NURP][3]; GLfloat speed[NURP][NURP]; int up[NURP][NURP]; int showPoints = 0; GLUnurbsObj *theNurb; void init_surface(void) { int u, v; for (u = 0; u < NURP; u++) { for (v = 0; v < NURP; v++) { ctl[u][v][0] = 2.0*((GLfloat)u - 1.5); ctl[u][v][1] = 2.0*((GLfloat)v - 1.5); ctl[u][v][2] = 2.0; up[u][v] = 0; } } for(u = 0; u < NURP; u++) speed[0][u] =1; } /* Initialize material property and depth buffer. */ void myinit(void) { GLfloat mat_diffuse[] = { 0.7, 0.7, 0.7, 1.0 }; GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat mat_shininess[] = { 100.0 }; glClearColor (0.0, 0.0, 0.0, 1.0); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); init_surface(); theNurb = gluNewNurbsRenderer(); gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25.0); gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_FILL); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef (0.0, 0.0, -5.0); } GLfloat knots[NURP*2] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; void display(void) { int i, j; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glTranslatef(0.0, 0.0, -3.0); glRotatef(330.0, 1.,0.,0.); glScalef (0.10, 0.10, 0.10); gluBeginSurface(theNurb); gluNurbsSurface(theNurb, NURP*2, knots, NURP*2, knots, NURP * 3, 3, &ctl[0][0][0], NURP, NURP, GL_MAP2_VERTEX_3); gluEndSurface(theNurb); glPopMatrix(); glutSwapBuffers(); } void reshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective (45.0, (GLdouble)w/(GLdouble)h, 3.0, 80.0); glMatrixMode(GL_MODELVIEW); } void menu(int value) { switch (value) { case 0: gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_FILL); break; case 1: gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON); break; } glutPostRedisplay(); } int down = 0, lastx; int flagmov[4] = {0, 0, 0, 0}; float zeta = 0.9; void motion(int x, int y) { if (down) { glRotatef(lastx - x, 0, 1, 0); lastx = x; glutPostRedisplay(); } } int num; int movup = 0; void idle(void) { int i, j; num += (movup)?1:-1; movup = num < -10 || num > 10 ? !movup : movup; for(i = 0; i < NURP; i++) for(j = 0; j < NURP; j++) ctl[i][j][2] = (int)(num*sin((double)i+1)); glutPostRedisplay(); } void mouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON) { if (state == GLUT_DOWN) { lastx = x; down = 1; } else { down = 0; } } } static void key(unsigned char k, int x, int y) { switch (k) { case 27: /* Escape */ exit(0); break; default: return; } glutPostRedisplay(); } /* Main Loop */ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB); glutCreateWindow(argv[0]); myinit(); glutReshapeFunc(reshape); glutDisplayFunc(display); glutCreateMenu(menu); glutAddMenuEntry("Solid", 0); glutAddMenuEntry("Wireframe", 1); glutAttachMenu(GLUT_RIGHT_BUTTON); glutMouseFunc(mouse); glutMotionFunc(motion); glutKeyboardFunc(key); glutIdleFunc(idle); glutMainLoop(); return 0; }