//********************************************************************************/ // OpenGL program illustartes: animation, interaction. // // OpenGL program that draws a square centered at the orign // and spins it around the origin // keyboard calback to speed up and speed down the rotation // qiit with q/Q // File: spinSquare.cpp // Date: 9/24/2006 /*********************************************************************************/ #include #include #include #include // modeling constants #define PI 3.141592 // globals GLdouble theta = 0; // rotation angle GLsizei wnd_w = 500; // display window width GLsizei wnd_h = 500; // display window height GLfloat spin_speed = PI/180; // inceremt for angle in rad //prototypes void help(); //print instructions void myinit(); // OpenGL init void display(); // display callback void myreshape(GLsizei, GLsizei); // reshape callback void update_angle(); // idle callback void control_speed(GLubyte, GLint, GLint); // keyboard callback /****************/ /***** MAIN *****/ /****************/ int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(wnd_w,wnd_h); glutInitWindowPosition(0,0); glutCreateWindow("spinning square, speed control with menu"); myinit(); // register callbacks glutDisplayFunc(display); glutReshapeFunc(myreshape); glutIdleFunc(update_angle); glutKeyboardFunc(control_speed); glutMainLoop(); return 0; } /********************************/ /***** FUNCTION DEFINITIONS *****/ /********************************/ /******************************************************************************/ // Print instructions /******************************************************************************/ void help() { printf("Spin the square"); printf(" To speed up press s\n To slow down press d\n Press q to quit\n"); } /*********************************************************************************/ // OpenGL init /*********************************************************************************/ void myinit() { help(); glClearColor(0.0,0.0,0.0,1.0); // white glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-2, 2, -2, 2); // origin at center of 4x4 clip window glMatrixMode(GL_MODELVIEW); } /*********************************************************************************/ // display callback /*********************************************************************************/ void display() { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glColor3f(1.0, 0.0, 0.0); glVertex2d(cos(theta), sin(theta)); glColor3f(0.0, 0.1, 0.0); glVertex2d(-sin(theta), cos(theta)); glColor3f(1.0, 0.0, 1.0); glVertex2d(-cos(theta), -sin(theta)); glColor3f(1.0, 1.0, 0.0); glVertex2d(sin(theta), -cos(theta)); glEnd(); glutSwapBuffers(); } /*********************************************************************************/ // window events (reshape) callback. // keeps the viewport aspect ratio 1, so drawings do not get distorted /*********************************************************************************/ void myreshape(GLsizei w, GLsizei h) { wnd_w = w; wnd_h = h; if (h < w) w=h; // adjust viewport (to preserve the original aspect ratio), and clear. glViewport(0, 0, w, w); glutPostRedisplay(); } /*********************************************************************************/ // idle callback updates rotation angle for spinning /*********************************************************************************/ void update_angle(void) { theta += spin_speed; if (theta>360) theta-=360; glutPostRedisplay(); } /*****************************************************************************/ // keyboard callback increase spead, decrease spid or quit /*****************************************************************************/ void control_speed(GLubyte key, GLint x, GLint y) { switch (key) { case('q'): case('Q'): exit (0); break; case('s'): case( 'S'): spin_speed *=2; break; case( 'd'): case( 'D'): spin_speed /= 2; } glutPostRedisplay( ); }