// Computer Graphics - Amir Hesami // This programs shows the effect of using glutMouseFunc() // rotatingrectangle3.cpp #include #include #pragma comment(lib, "opengl32.lib") // Link OpenGL32.lib #pragma comment(lib, "glu32.lib") // Link Glu32.lib #pragma comment(lib, "glut32.lib") // Link Glut32.lib #pragma comment(linker, "/entry:\"mainCRTStartup\"" ) // set the entry point to be main() #pragma comment(linker, "/subsystem:\"Windows\"" ) // set the entry point to be main() #define DEG_TO_RAD 57.29578 //degree to radian void display(); void idle(); void mouse(int button, int state, int x, int y); void init(); void reshape(int, int); GLfloat theta = 0.0; int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE| GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(100,100); glutCreateWindow("Rotating Rectangle 3"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouse); init(); glutMainLoop(); return 0; } void reshape(int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-2.0, 2.0, -2.0, 2.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void display() { //Clear the window glClear(GL_COLOR_BUFFER_BIT); //Set the drawing color to red glColor3f(1.0,0.0,0.0); GLfloat theta1 = theta * DEG_TO_RAD; //draw unit square polygon glBegin(GL_POLYGON); glVertex2f(cos(theta1),sin(theta1)); glVertex2f(-sin(theta1),cos(theta1)); glVertex2f(-cos(theta1),-sin(theta1)); glVertex2f(sin(theta1),-cos(theta1)); glEnd(); glutSwapBuffers(); } void init() { //Set the background color glClearColor(1.0,1.0,1.0,1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-2.0,2.0,-2.0,2.0); } void idle() { theta +=2.0; if(theta >360.0) theta -=360; glutPostRedisplay(); } void mouse(int button, int state, int x, int y) { switch (button) { case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) glutIdleFunc(idle); break; case GLUT_MIDDLE_BUTTON: case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) glutIdleFunc(NULL); break; default: break; } }