/* prac1b.c */ /* Purpose: Allow keyboard input to change background colour of screen. */ #ifdef __APPLE__ #include #else #include #endif #include #include #include #define KEY_ESC 27 /* GLUT doesn't supply this */ int myrandom(int m) { return rand()%m; } GLvoid keyboard(GLubyte key, GLint x, GLint y) { switch (key) { case ' ': glClearColor(myrandom(10)/10.0, myrandom(10)/10.0, myrandom(10)/10.0,1.0); glutPostRedisplay(); break; case KEY_ESC: exit(0); /* exit when ESC pressed */ } } GLvoid display( GLvoid ) { /* Do all your OpenGL rendering here */ glClear(GL_COLOR_BUFFER_BIT); glFlush(); } void init( void ) { /* set background color */ glClearColor(1.0,1.0,0.0,1.0); } int main(int argc, char* argv[]) { GLint width; GLint height; glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); width = glutGet(GLUT_SCREEN_WIDTH); height = glutGet(GLUT_SCREEN_HEIGHT); glutInitWindowPosition(width/4, height/4); glutInitWindowSize(width/2, height/2); glutCreateWindow(argv[0]); glutDisplayFunc(display); glutKeyboardFunc(keyboard); init(); glutMainLoop(); return 0; }