#include #include #include #include #ifdef __APPLE__ #include #else #include #endif void display(void); void keypress(unsigned char key, int x, int y); float polar1(float theta, float r); float polar2(float theta, float r); void draw_polygon(float dx, float dy); void main_menu(int value); void off_menu(int value); GLfloat red[3] = {1.0, 0.0, 0.0}; GLfloat green[3] = {0.0, 1.0, 0.0}; GLfloat blue[3] = {0.0, 0.0, 1.0}; GLfloat black[3] = {0.0, 0.0, 0.0}; GLfloat white[3] = {1.0, 1.0, 1.0}; GLfloat gray[3] = {0.5, 0.5, 0.5}; GLfloat yellow[3] = {1.0, 1.0, 0.0}; GLfloat magenta[3] = {1.0, 0.0, 1.0}; GLfloat cyan[3] = {0.0, 1.0, 1.0}; GLfloat *curCol = white; GLint num_balls = 4; GLint fillmode = 0; GLint shapep = 50; GLint *shape; int main (int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutInitWindowPosition(100,100); glutCreateWindow(argv[0]); glClearColor(0,0,0,0); glColor3f(1.0, 1.0, 1.0); shape = & shapep; glutDisplayFunc(display); glutKeyboardFunc(keypress); glutCreateMenu(main_menu); glutAddMenuEntry("red", 1); glutAddMenuEntry("green", 2); glutAddMenuEntry("blue", 3); glutAddMenuEntry("black", 4); glutAddMenuEntry("white", 5); glutAddMenuEntry("gray", 6); glutAddMenuEntry("yellow", 7); glutAddMenuEntry("magenta", 8); glutAddMenuEntry("cyan", 9); glutAddMenuEntry("Quit", 10); glutAttachMenu(GLUT_RIGHT_BUTTON); glutCreateMenu(off_menu); glutAddMenuEntry("Triangle", 3); glutAddMenuEntry("Square", 4); glutAddMenuEntry("Pentagon", 5); glutAddMenuEntry("Hexagon", 6); glutAddMenuEntry("circle", 7); glutAddMenuEntry("auto", 8); glutAttachMenu(GLUT_LEFT_BUTTON); glutMainLoop(); return 0; } void display(void) { float i = 0; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); for(i = 0; i < 2*M_PI; i = i + (2*M_PI)/num_balls) { glColor3f(curCol[0] + i/(2*M_PI), curCol[1] + i/(2*M_PI), curCol[2] + i/(2*M_PI)); draw_polygon(polar1(i,.75), polar2(i,.75)); } glFlush(); } float polar1(float theta, float r) { return r * sin(theta); } float polar2(float theta, float r) { return r * cos(theta); } void draw_polygon(float dx, float dy) { float i = 0; if(fillmode) { glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); } else { glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); } glBegin(GL_POLYGON); for(i = 0; i < 2*M_PI; i = i+(2*M_PI/(*shape))) { glVertex2f(dx + polar1(i,.1),dy + polar2(i,.1)); } glEnd(); } void keypress(unsigned char key, int x, int y) { switch(key) { case 'q': exit(0); break; case '+': num_balls++; break; case '-': if(num_balls > 0) num_balls--; break; case 'g': fillmode = !fillmode; break; } glutPostRedisplay(); } void off_menu(int value) { switch (value) { case 7: *shape = 50; break; case 8: shape = &num_balls; break; default: *shape = (GLint)value; } glutPostRedisplay(); } void main_menu(int value) { switch (value) { case 1: curCol = red; break; case 2: curCol = green; break; case 3: curCol = blue; break; case 4: curCol = black; break; case 5: curCol = white; break; case 6: curCol = gray; break; case 7: curCol = yellow; break; case 8: curCol = magenta; break; case 9: curCol = cyan; break; case 10: exit(0); } glutPostRedisplay(); }