#include #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 */ float px; /* Horizontal position of the mouse cursor */ float colorr[91]; /* R Color Values of Rainbow Gradient */ float colorg[91]; /* G Color Values of Rainbow Gradient */ float colorb[91]; /* B Color Values of Rainbow Gradient */ int xindex = 0; /* Horizontal Mouse position relative to window origin as Rainbow Gradient index */ int yindex = 0; /* Vertical Mouse position relative to window origin as Rainbow Gradient index */ void display(void) { /* OpenGL Rendering Function */ /* Reset all pixels in buffer to clear color */ glClearColor(colorr[yindex], colorg[yindex], colorb[yindex], 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(colorr[xindex], colorg[xindex], colorb[xindex]); 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 '+': angle += 1.0; break; case '-': angle -= 1.0; break; } } void motion(int x, int y) { /* Mouse Event Handler */ xindex = (int) (91.0 * (float) x / (float) 400.0); /* Map Horizontal Mouse Movement to Rainbow Gradient */ yindex = (int) (91.0 * (float) y / (float) 400.0); /* Map Vertical Mouse Movement to Rainbow Gradient */ printf("The mouse is at %d and %d (indicies at %d and %d)\n",x,y,xindex,yindex); /* Print x,y coordinates of mouse location relative to window origin */ } void myinit(void) { /* Initialize OpenGL default values */ int w,h; int i; FILE *fp; /* 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; px = 0.0; /* Read R G B Color Values From File (Rainbow Gradient) */ fp = fopen("COLOR_PATTERNS","r"); for (i=0;i<91;i++) { fscanf(fp, "%f%f%f", &colorr[i], &colorg[i], &colorb[i]); } fclose(fp); /* 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(); }