/* * ok, it's a very bad piece of code, but I wrote it hastily and * was trying to just accomplish the effect, without thinking of * possible optimizations and cleanlyness * * todo: 1. do everything with 1 NURB (at most 2) 2. clean up the idle function -- remove hardcoded values of lr 3. clean up the idle() so that init_surface is not called all that often. 4. rewrite cleanly */ #include #include #define NURP 3 #define LAYERS 1 void init_surface(int lr); /* silly forward declaration */ GLfloat ctl[LAYERS][NURP][NURP][3]; int showPoints = 0; int rewind = 0; int fill = 0; int rot = 0; int spin_x, spin_y, spin_z; /* x-y rotation and zoom */ int h, w; /* height, width of window */ int old_x, old_y, move_z; GLUnurbsObj *theNurb[LAYERS]; static GLuint texname; void move_down(int lr) { ctl[lr][0][0][2] = 0; ctl[lr][2][0][2] = 0; ctl[lr][0][2][2] = 0; ctl[lr][2][2][2] = 0; } void init_all() { int lr = 0; for(lr = 0;lr < LAYERS; lr++) init_surface(lr); move_down(1); } /* * Initializes the control points of the surface to a small hill. * The control points range from -3 to +3 in x, y, and z */ void init_surface(int lr) { ctl[lr][0][0][0] = -1; ctl[lr][0][0][1] = -1; ctl[lr][0][0][2] = 6; ctl[lr][0][1][0] = -3; ctl[lr][0][1][1] = -1; ctl[lr][0][1][2] = 2; ctl[lr][0][2][0] = -1; ctl[lr][0][2][1] = -1; ctl[lr][0][2][2] = 6; ctl[lr][1][0][0] = -1; ctl[lr][1][0][1] = -3; ctl[lr][1][0][2] = 2; ctl[lr][1][1][0] = -1; ctl[lr][1][1][1] = -1; ctl[lr][1][1][2] = 2; ctl[lr][1][2][0] = -1; ctl[lr][1][2][1] = 1; ctl[lr][1][2][2] = 2; ctl[lr][2][0][0] = -1; ctl[lr][2][0][1] = -1; ctl[lr][2][0][2] = 6; ctl[lr][2][1][0] = 1; ctl[lr][2][1][1] = -1; ctl[lr][2][1][2] = 2; ctl[lr][2][2][0] = -1; ctl[lr][2][2][1] = -1; ctl[lr][2][2][2] = 6; } /* Initialize material property and depth buffer. */ void myinit(void) { int lr; float dfuse[] = {1.0, 1.0, 1.0, 1.0}; GLfloat mat_diffuse[] = { 0.7f, 0.7f, 0.7f, 1.0f }; GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; GLfloat mat_shininess[] = { 100.0f }; glClearColor (0.0, 0.0, 0.0, 1.0); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess); glLightfv(GL_LIGHT0, GL_DIFFUSE, dfuse); glLightfv(GL_LIGHT1, GL_DIFFUSE, dfuse); glLightfv(GL_LIGHT2, GL_DIFFUSE, dfuse); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glEnable(GL_LIGHT2); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); glPointSize(4); init_all(); for(lr = 0; lr < LAYERS; lr++) { theNurb[lr] = gluNewNurbsRenderer(); gluNurbsProperty(theNurb[lr], GLU_SAMPLING_TOLERANCE, 50.0); gluNurbsProperty(theNurb[lr], GLU_DISPLAY_MODE, GLU_FILL); } glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef (0.0, 0.0, -5.0); } float pos[3] = {1, 1, -2}; float pos2[3] = {0, 0, 1}; float pos3[3] = {1, 0, -1}; GLfloat knots[NURP*2] = {0.0, 0.0, 0.0, 1.0, 1.0, 1.0}; void display(void) { int i, j; int lr; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLightfv(GL_LIGHT0, GL_POSITION, pos); glLightfv(GL_LIGHT1, GL_POSITION, pos2); glLightfv(GL_LIGHT2, GL_POSITION, pos2); glPushMatrix(); glTranslatef(0, -2, spin_z - 10); glRotatef(spin_x+30, 0, 1, 0); glRotatef(spin_y-30, 1, 0, 0); glRotatef(rot, 0, 0, 1); for(lr = 0; lr < LAYERS; lr++) { gluBeginSurface(theNurb[lr]); gluNurbsSurface(theNurb[lr], NURP*2, knots, NURP*2, knots, NURP * 3, 3, &ctl[lr][0][0][0], NURP, NURP, GL_MAP2_VERTEX_3); gluNurbsSurface(theNurb[lr], NURP*2, knots, NURP*2, knots, NURP * 3, 3, &ctl[lr][0][0][0], NURP, NURP, GL_MAP2_TEXTURE_COORD_4); gluEndSurface(theNurb[lr]); } if(showPoints) { glDisable(GL_LIGHTING); glColor3f(0, 1, 1); glBegin(GL_POINTS); for(lr = 0; lr < LAYERS; lr++) for (i = 0; i < NURP; i++) for (j = 0; j < NURP; j++) glVertex3fv(ctl[lr][i][j]); glEnd(); glEnable(GL_LIGHTING); } 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) { int lr; switch (value) { case 0: for(lr = 0; lr < LAYERS; lr++) gluNurbsProperty(theNurb[lr], GLU_DISPLAY_MODE, (fill ? GLU_FILL : GLU_OUTLINE_POLYGON)); fill = !fill; break; // case 1: // showPoints = !showPoints; // break; } glutPostRedisplay(); } int down = 0, lastx; int flagmov[4] = {0, 0, 0, 0}; float zeta = 0.9f; void mouse(int button, int state, int x, int y) { switch(button) { case 0: old_x = x - spin_x; old_y = y - spin_y; break; case 2: old_y = y - spin_z; move_z = (move_z ? 0 : 1); } glutPostRedisplay(); } void motion(x, y) { if(!move_z) { spin_x = x - old_x; spin_y = y - old_y; } else { spin_z = y - old_y; } glutPostRedisplay(); } int num = 1; int lr = 0, ld = 0; int movup = 0; void idle(void) { if(movup && ctl[lr][0][0][2] < 6) { ctl[lr][0][0][2] += 0.04f; ctl[lr][2][0][2] += 0.04f; ctl[lr][0][2][2] += 0.04f; ctl[lr][2][2][2] += 0.04f; ctl[lr][1][1][2] -= 0.04f; } else if(ctl[lr][0][0][2] > 2 && !ld) { if(movup) movup = 0; ctl[lr][0][0][2] -=0.04f; ctl[lr][0][0][0] -= 0.02f; ctl[lr][0][0][1] -= 0.02f; ctl[lr][2][0][2] -=0.04f; ctl[lr][2][0][0] += 0.02f; ctl[lr][2][0][1] -= 0.02f; ctl[lr][0][2][2] -=0.04f; ctl[lr][0][2][0] -= 0.02f; ctl[lr][0][2][1] += 0.02f; ctl[lr][2][2][2] -=0.04f; ctl[lr][2][2][0] += 0.02f; ctl[lr][2][2][1] += 0.02f; ctl[lr][1][1][2] += 0.04f; } else if(ctl[lr][0][0][2] > 2 && ld) { if(movup) movup = 0; ctl[lr][0][0][2] -=0.04f; ctl[lr][0][0][0] += 0.02f; ctl[lr][0][0][1] += 0.02f; ctl[lr][2][0][2] -=0.04f; ctl[lr][2][0][0] -= 0.02f; ctl[lr][2][0][1] += 0.02f; ctl[lr][0][2][2] -=0.04f; ctl[lr][0][2][0] += 0.02f; ctl[lr][0][2][1] -= 0.02f; ctl[lr][2][2][2] -=0.04f; ctl[lr][2][2][0] -= 0.02f; ctl[lr][2][2][1] -= 0.02f; ctl[lr][1][1][2] += 0.04f; } else if(ctl[lr][0][0][2] > -2 && !ld) { ctl[lr][0][0][2] -=0.04f; ctl[lr][0][0][0] += 0.02f; ctl[lr][0][0][1] += 0.02f; ctl[lr][2][0][2] -=0.04f; ctl[lr][2][0][0] -= 0.02f; ctl[lr][2][0][1] += 0.02f; ctl[lr][0][2][2] -= 0.04f; ctl[lr][0][2][0] += 0.02f; ctl[lr][0][2][1] -= 0.02f; ctl[lr][2][2][2] -=0.04f; ctl[lr][2][2][0] -= 0.02f; ctl[lr][2][2][1] -= 0.02f; ctl[lr][1][1][2] +=0.04f; } else if(ctl[lr][0][0][2] > -2 && ld) { ctl[lr][0][0][2] -=0.04f; ctl[lr][0][0][0] -= 0.02f; ctl[lr][0][0][1] -= 0.02f; ctl[lr][2][0][2] -=0.04f; ctl[lr][2][0][0] += 0.02f; ctl[lr][2][0][1] -= 0.02f; ctl[lr][0][2][2] -= 0.04f; ctl[lr][0][2][0] -= 0.02f; ctl[lr][0][2][1] += 0.02f; ctl[lr][2][2][2] -=0.04f; ctl[lr][2][2][0] += 0.02f; ctl[lr][2][2][1] += 0.02f; ctl[lr][1][1][2] +=0.04f; } else { ld = !ld; movup = 1; } glutPostRedisplay(); } 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/wire", 0); // glutAddMenuEntry("control on/off", 1); glutAttachMenu(GLUT_MIDDLE_BUTTON); glutMouseFunc(mouse); glutMotionFunc(motion); glutKeyboardFunc(key); glutIdleFunc(idle); glutMainLoop(); return 0; /* ANSI C requires main to return int. */ }