/* * 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 2 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; unsigned *teximage; int t = 0, f = 1; void LoadTexture() { int texwid, texht; int texcomps; char fn[]="rose.rgb"; teximage = read_texture(fn, &texwid, &texht, &texcomps); if (!teximage) { printf("Sorry, can't read texture file..."); exit(0); } glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glGenTextures(2, &texname); glBindTexture(GL_TEXTURE_2D, texname); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texwid, texht, 0, GL_RGBA, GL_UNSIGNED_BYTE, teximage); glEnable(GL_TEXTURE_2D); glEnable(GL_DEPTH_TEST); } 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.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_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.9; 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; /* whose turn it is to move */ int movup[] = {0, 0}; void idle(void) { int i, j; if(movup[lr] && ctl[lr][0][0][2] < 6) { ctl[lr][0][0][2] += 0.008; ctl[lr][2][0][2] += 0.008; ctl[lr][0][2][2] += 0.008; ctl[lr][2][2][2] += 0.008; } else if(ctl[lr][0][0][2] > 1) { if(movup[lr]) movup[lr] = 0; ctl[lr][0][0][2] -= 0.008; ctl[lr][0][0][0] -= 0.004; ctl[lr][0][0][1] -= 0.004; ctl[lr][2][0][2] -= 0.008; ctl[lr][2][0][0] += 0.004; ctl[lr][2][0][1] -= 0.004; ctl[lr][0][2][2] -= 0.008; ctl[lr][0][2][0] -= 0.004; ctl[lr][0][2][1] += 0.004; ctl[lr][2][2][2] -= 0.008; ctl[lr][2][2][0] += 0.004; ctl[lr][2][2][1] += 0.004; ctl[lr][1][1][2] += 0.008; } else { if(lr) { init_surface(0); move_down(0); lr = 0; movup[0] = 1; } else { init_surface(1); move_down(1); lr = 1; movup[1] = 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); LoadTexture(); glutMouseFunc(mouse); glutMotionFunc(motion); glutKeyboardFunc(key); glutIdleFunc(idle); glutMainLoop(); return 0; /* ANSI C requires main to return int. */ }