#include #include #include #include float spin = 0.0; float angle = 0.0; /* Spin increment angle */ float rb,gb,bb; /* Background R G B color */ float rs,gs,bs; /* Foreground (Square) R G B color */ void display(void) { /* OpenGL Rendering Function */ /* Reset all pixels in buffer to clear color */ glClearColor(rb,gb,bb, 1.0); glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); /* Rotate buffer x y z relative to horizontal mouse position */ glRotatef(spin, 0.0, 0.0, 1.0); /* Draw rectangle by coordinate pairs x1,y1 x2,y2 */ glColor3f(rs,gs,bs); glRectf(-25.0, -25.0, 25.0, 25.0); glPopMatrix(); /* Switch buffers displaying rendering to the screen */ glutSwapBuffers(); } void spinDisplay(void) { /* Increase the rotation angle and render */ spin += angle; if (spin >= 360.0) { /* Limit the rotation angle to [0,360) */ spin -= 360; } if (spin <= -360.0) { /* Limit the rotation angle to (-360,0] */ spin += 360; } display(); } void keyboard(unsigned char key, int x, int y) { /* Keyboard Event Handler */ switch (key) { case 'q': exit(0); /* Exit if 'q' is pressed */ break; case 'b': printf("What colors for the background?\n"); scanf("%f%f%f",&rb,&gb,&bb); /* Enter new Background R G B Color if 'b' is pressed */ break; case 's': printf("What colors for the square?\n"); scanf("%f%f%f",&rs,&gs,&bs); /* Enter new Foreground (Square) R G B Color if 's' is pressed */ break; case '+': angle += 1.0; break; case '-': angle -= 1.0; break; } } void motion(int x, int y) { /* Mouse Event Handler */ printf("The mouse is at %d and %d\n", x,y); /* Print x,y coordinates of mouse location relative to window origin */ } void myinit(void) { /* Initialize OpenGL default values */ int w,h; /* Window geometry - Width, Height */ w = 400; h = 400; /* Initial Forground and Background R G B Colors */ rb = 1.0; gb = 1.0; bb = 1.0; rs = 0.0; gs = 0.0; bs = 0.0; /* OpenGL defaults */ glClearColor(rb,gb,bb, 1.0); /* Background Color */ glColor3f(rs,gs,bs); /* Foreground Color */ glViewport(0,0,w,h); /* Viewable area includes entire window geometry */ /* Default orientation */ glOrtho(-50.0, 50.0, -50.0*(float)h/(float)w, 50.0*(float)h/(float)w, -1.0, 1.0); } int main(int argc, char** argv) { /* Initialize GLUT and processes any command line arguments */ glutInit(&argc, argv); /* Create a double buffer using the RGB color scheme */ glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ); /* Define Window Geometry */ glutInitWindowSize(400, 400); /* Create window with string argument as title */ glutCreateWindow("My Box"); /* Initialize OpenGL */ myinit(); /* Register the spinDisplay function to run while idle (continuously) */ glutIdleFunc(spinDisplay); /* Register Rendering function */ glutDisplayFunc(display); /* Register Keyboard Event Handler function */ glutKeyboardFunc(keyboard); /* Register Mouse Event Handler function */ glutMotionFunc(motion); /* Initialize GLUT Event Handler */ glutMainLoop(); }