/////////////////////////////////// // square.cpp // // OpenGL program to draw a square. // // Sumanta Guha. /////////////////////////////////// #include #include #include #include using namespace std; // global variables for callbacks // Color_Mode controls how the square will be colored. int Color_Mode; GLfloat Color[4][3]; // Drawing routine. void drawScene(void) { glClear(GL_COLOR_BUFFER_BIT); if (Color_Mode == 0) { glColor3f(0.0, 0.0, 0.0); } // Draw a polygon with specified vertices. glBegin(GL_POLYGON); if (Color_Mode == 1) { glColor3fv(Color[0]); } glVertex3f(20.0, 20.0, 0.0); if (Color_Mode == 1) { glColor3fv(Color[1]); } glVertex3f(80.0, 20.0, 0.0); if (Color_Mode == 1) { glColor3fv(Color[2]); } glVertex3f(80.0, 80.0, 0.0); if (Color_Mode == 1) { glColor3fv(Color[3]); } glVertex3f(20.0, 80.0, 0.0); glEnd(); glFlush(); } void reset(void) { int i,j; // color mode setting Color_Mode = 0; // color setting for(j=0;j<4;j++) { for(i=0;i<3;i++) { Color[j][i] = 0.0; } } // red green blue by default for(i=0;i<3;i++) { Color[i][i] = 1; } // something for color 4 Color[3][0] = 1.0; Color[3][2] = 1.0; return; } // Initialization routine. void setup(void) { reset(); glClearColor(1.0, 1.0, 1.0, 0.0); } // OpenGL window reshape routine. void resize(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 100.0, 0.0, 100.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void RotateColor(GLfloat color[3]) { float tmp; tmp = color[0]; color[0] = color[1]; color[1] = color[2]; color[2] = tmp; return; } // Keyboard input processing routine. void keyInput(unsigned char key, int x, int y) { bool redisplay = true; int i; switch(key) { case 'c': Color_Mode ++; if (Color_Mode > 1) { Color_Mode = 0; } cout << "Color_Mode is now " << Color_Mode << endl; break; case 'C': for(i=0;i<4;i++) { RotateColor(Color[i]); } break; case 'q': case 'Q': case 27: redisplay = false; exit(0); break; default: redisplay = false; cout << "Keypress " << key << " does not have an assigned action " << endl; break; } if (redisplay) { glutPostRedisplay(); } } void idle() { static int count =0; if (count == 10000000 ) { cout << "Idle Function, nothing to report" << endl; count = 0; } else { count++; } return; } void mouse(int button, int state, int x, int y) { cout << "Button Press, it was " ; switch(button) { case GLUT_LEFT_BUTTON: cout << "the left "; break; case GLUT_MIDDLE_BUTTON: cout << "the middle "; break; case GLUT_RIGHT_BUTTON: cout << "the right "; break; default: cout << "an unknown "; } cout << "button" << endl; cout << "It was "; switch(state) { case GLUT_DOWN: cout << "pressed "; break; case GLUT_UP: cout << "released "; break; } cout << endl; cout << " At position (" << x << ", " << y << ") " << endl; } void motion(int x, int y) { cout << "Motion x=" << x << ", y = " << y << endl; return; } void passiveMotion(int x, int y) { cout << "Passive motion x=" << x << ", y = " << y << endl; return; } void wheel(int wheel, int direction, int x, int y) { cout << "Mouse Wheel number " << wheel << endl; cout << "\twas spun " << direction << " by x= " << x << ", y = " << y << endl; return; } // Main routine. int main(int argc, char **argv) { glutInit(&argc, argv); glutInitContextVersion(3,0); glutInitContextProfile(GLUT_CORE_PROFILE); //glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow("square.cpp"); glutDisplayFunc(drawScene); glutReshapeFunc(resize); glutIdleFunc(idle); glutMotionFunc(motion); glutMouseFunc(mouse); glutPassiveMotionFunc(passiveMotion); glutMouseWheelFunc(wheel); glutKeyboardFunc(keyInput); glewExperimental = GL_TRUE; glewInit(); setup(); glutMainLoop(); }