/* simple5.c simple5.c + moving when press l/r keys Laura Toma */ #include #include #include #include #ifdef __APPLE__ #include #else #include #endif 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}; GLint fillmode = 0; /* we keep track globally of the coordinates of the center of the rectangle; they will be updated by the keys that implement move left and right */ GLfloat x_center = 0.0, y_center = 0.0; /* forward declarations of functions */ void display(void); void keypress(unsigned char key, int x, int y); void main_menu(int value); int main(int argc, char** argv) { /* open a window */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutInitWindowPosition(100,100); glutCreateWindow(argv[0]); /* OpenGL init */ glClearColor(0, 0, 0, 0); /* set background color black*/ /* callback functions */ glutDisplayFunc(display); glutKeyboardFunc(keypress); glutCreateMenu(main_menu); glutAddMenuEntry("Fill/Outline", 1); glutAddMenuEntry("Quit", 2); glutAttachMenu(GLUT_RIGHT_BUTTON); /* event handler */ glutMainLoop(); return 0; } void draw_polygon(){ if (fillmode) { glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); } else { glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); } /* draw a polygon centered at x_center, y_center */ glBegin(GL_POLYGON); glColor3fv(blue); glVertex2f(x_center - 0.5, y_center -0.5); glColor3fv(white); glVertex2f(x_center -0.5, y_center + 0.5); glColor3fv(red); glVertex2f(x_center + 0.5, y_center + 0.5); glColor3fv(gray); glVertex2f(x_center + 0.5, y_center -0.5); glEnd(); } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); draw_polygon(); /* execute the drawing commands */ glFlush(); } void keypress(unsigned char key, int x, int y) { switch(key) { case 'q': exit(0); break; case 'l': x_center -= 0.2; glutPostRedisplay(); break; case 'r': x_center += 0.2; glutPostRedisplay(); break; } } void main_menu(int value) { switch (value){ case 1: /* toggle outline/fill */ fillmode = !fillmode; break; case 2: exit(0); } glutPostRedisplay(); }