#include #include #include #include #ifdef __APPLE__ #include #else #include #endif GLdouble PI; GLfloat blue[3] = {0.0, 0.0, 1.0}; GLfloat white[3] = {1.0, 1.0, 1.0}; GLfloat gray[3] = {0.5, 0.5, 0.5}; GLint rad = 60; GLint filled = 1; GLint axes = 0; GLint MOVE1 = 1, MOVE2 = 0, moveit = 0; GLdouble howfar = 0; //position to move GLdouble endpos[3] = {100, 100, -100}; void drawAxes(); void init(){ PI = 2 * asin(1); GLfloat light_position[] = {1.0, 1.0, -10.0, 0.0}; glLightfv(GL_LIGHT0, GL_POSITION, light_position); GLfloat light_ambient[] = {0.6, 0, 1, 0.0}; glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); glClearColor(0, 0, 0, 0); /* Background color = Black*/ glShadeModel(GL_SMOOTH); /* Smooth shading */ } void polar(GLdouble r, GLdouble fi, GLdouble* v) { v[0] = r * cos(fi); v[1] = r * sin(fi); } void drawAxes(GLint scale) { glBegin(GL_LINES); glColor3fv(blue); glVertex2i(-1*scale,0); glVertex2i(1*scale,0); glVertex2i(0,1*scale); glVertex2i(0,-1*scale); glEnd(); } void drawObject(){ GLdouble v[2]; int i; if (filled) { glutSolidSphere(rad, 30, 30); for (i=0;i<6;i++) { glPushMatrix(); glRotated(60*i, 0,0,1); glTranslated(rad*cos(PI/6),0,0); glRotated(90,0,1,0); glutSolidCone(rad/2,40,20,10); glPopMatrix(); } } else { glutWireSphere(rad, 30, 30); for (i=0;i<6;i++) { glPushMatrix(); glRotated(60*i, 0,0,1); glTranslated(rad*cos(PI/6),0,0); glRotated(90,0,1,0); glutWireCone(rad/2,40,20,10); glPopMatrix(); } } } void drawObjectOld(){ if (filled) { glutSolidCube(25); } else { glutWireCube(25); } } void moveObject() { //increment translation/rotation if (howfar < 1){ howfar +=0.01; glutPostRedisplay(); } } void keyPress(unsigned char key, int x, int y) { switch (key){ case 13: //enter moveit = 1; //start the idle func glutIdleFunc(moveObject); break; case 'd': //debug printf("MOVE1 = %d, MOVE2 = %d\n", MOVE1, MOVE2); break; case 'q': exit(1); break; } } void move1(GLdouble howfar) { glTranslated(howfar*endpos[0], howfar*endpos[1], howfar*endpos[2]); drawObject(); } void move2(GLdouble howfar) { GLdouble center[3]; center[0]= 0.5*endpos[0]; center[1]= 0.5*endpos[1]; center[2]= 0.5*endpos[2]; //move around center of cube in diagonal plan (1,0,1) //translate origin in center of cube glTranslated(center[0], center[1], center[2]); //rotate in y,z plane with 180*howfar clockwise glRotatef(180*howfar,1,0,1); //translate origin back glTranslated(-center[0], -center[1], -center[2]); //align image back (otherwise its face back and it flips..) glRotatef(-180*howfar,1,0,1); //draw the object drawObject(); } void display(void) { glClear(GL_COLOR_BUFFER_BIT); /* Set the background color */ glColor3f(1.0,1.0,1.0); glLoadIdentity(); /* viewing */ gluLookAt(0.0, 0.0, 150.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); // glScalef(1.0, 2.0, 1.0); if (!moveit) { //just draw the object drawObject(); } // select if do translation or rotation if (moveit && MOVE1) { move1(howfar); } if (moveit && MOVE2) { move2(1-howfar); } if (axes) drawAxes(30); //check fif done if (howfar > 1) { //move done MOVE1 = !MOVE1; MOVE2 = !MOVE2; moveit = 0; howfar = 0; //disable the idle function glutIdleFunc(NULL); } //glFlush(); glutSwapBuffers(); } void reshape(int w, int h) { glViewport(0,0, (GLsizei)w, (GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); //glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); gluPerspective( 80, 1,1, 350); glMatrixMode(GL_MODELVIEW); } void main_menu(int value) { switch (value){ case 1: filled = !filled; break; case 2: exit(0); } glutPostRedisplay(); } int main(int argc, char **argv) { int width_menu_id, fill_menu_id; glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(100,100); glutCreateWindow(argv[0]); init(); glutKeyboardFunc(keyPress); glutDisplayFunc(display); glutReshapeFunc(reshape); glutCreateMenu(main_menu); glutAddMenuEntry("filled/wired", 1); glutAddMenuEntry("Quit", 2); glutAttachMenu(GLUT_RIGHT_BUTTON); glutMainLoop(); return 0; }