// Demo for ECE660, 9/21/98, F.S.Hill,Jr. // bare bones Canvas class.. /* Draw a rotating rosette of numSides sides. Increase/decrease numSides with 'n"/'N', and increase/decrease rotation speed with 'r'/'R' */ #include #include #include #include #include #include typedef struct { float x, y;} tRealPoint; //<<<<<<<<<<<<<<<<<<<<<<<<<<< Canvas class definition >>>>>>>>>>> class Canvas { private: tRealPoint CP; // current position in world public: Canvas(){CP.x = CP.y = 0.0;};// constructor void lineTo(float x, float y); void moveTo(float x, float y){ CP.x = x; CP.y = y;} }; //<<<<<<<<<<<<<<<<<<<<<<< lineTo >>>>>>>>>>>>>>>>>>>>>>> void Canvas:: lineTo(float x, float y) { glBegin(GL_LINES); glVertex2f((GLfloat)CP.x, (GLfloat)CP.y); CP.x = x; CP.y = y; glVertex2f((GLfloat)CP.x, (GLfloat)CP.y); glEnd(); glFlush(); } //######################## GLOBALS ################################# int screenWidth = 480, screenHeight = 480; Canvas cvs; // create a global instance int numSides = 11; float angle = 0.0, delAngle = 0; //<<<<<<<<<<<<<<<<<<<<<<< Rosette >>>>>>>>>>>>>>>>>>>> void Rosette(int N, float radius) { #define MaxNum 97 // can draw up to 97 vertex rosette tRealPoint pt[MaxNum]; // vertices of the N-gon float angle = 2 * 3.14159265 / N; if(N < 3 || N >= MaxNum) return; for (int j = 0; j < N; j++) // create the vertices { pt[j].x = radius * cos(angle * j); pt[j].y = radius * sin(angle * j); } for(int i = 0; i < N -1;i++) for(int j = i+1; j < N ; j++) { cvs.moveTo(pt[i].x,pt[i].y); cvs.lineTo(pt[j].x, pt[j].y); } } //<<<<<<<<<<<<<<<<<<<<<<<<<<<<< display >>>>>>>>>>>>>>>>>>>>>> void display(void) { glClear(GL_COLOR_BUFFER_BIT); // clear screen glMatrixMode(GL_MODELVIEW); // set up rotation glLoadIdentity(); glRotatef(angle,0,0,1); Rosette(numSides, 8); // draw the rosette glutSwapBuffers(); // swap buffers for smooth animation } //<<<<<<<<<<<<<<<<<<<<<<< myKeys >>>>>>>>>>>>>>>>>>>>>>>> void myKeys(unsigned char key, int x, int y) { switch(key) { case 'q': exit(0); // quit case 'n': numSides++; cout << numSides << endl; break; case 'N': numSides--; cout << numSides << endl; break; case 'r': delAngle +=1.0; break; //increase rotation speed case 'R': delAngle -=1.0; break; } display(); } //<<<<<<<<<<<<<<<<<<<<<<<<<<< spinner >>>>>>>>>>>>>>> void spinner(void) // the idle function { angle += delAngle; // increase rotation angle display(); } //<<<<<<<<<<<<<<<<<<<<< myInit >>>>>>>>>>>>>>>>>> void myInit() { glClearColor(1.0f,1.0f,1.0f,1.0f); // background is white glColor3f(0.0f,0.0f,0.0f); // set color of stuff glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-10, 10,-10,10); // set window glViewport(0,0,screenWidth,screenHeight); //the default } //<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> void main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(screenWidth, screenHeight); glutInitWindowPosition(100, 100); glutCreateWindow("here we go"); glutKeyboardFunc(myKeys); glutDisplayFunc(display); glutIdleFunc(spinner); myInit(); glutMainLoop(); }