#include #include #ifndef NULL #define NULL (void *) 0 #endif /* * This needs to be global to communicate between the animation callback and * the display callback. */ float angle = -45.0; /* angle of rotation about the y-axis */ /* * Specify the colors on the 8 vertices of the color cube. Black is adjacent * to red, green and blue. White is opposite black and is adjacent to * yellow, cyan and magenta. */ GLfloat color[][3] = { { 1.0, 0.0, 0.0 }, /* red */ { 1.0, 1.0, 0.0 }, /* yellow */ { 1.0, 0.0, 1.0 }, /* magenta */ { 0.0, 1.0, 0.0 }, /* green */ { 0.0, 0.0, 0.0 }, /* cyan */ { 0.0, 0.0, 1.0 } /* blue */ }; /* * Vertices for cube centered at origin, oriented with edges (which are of * length 2) parallel to coordinate axes. */ GLfloat cube[][3] = { { -1.0, -1.0, 1.0 }, { 1.0, -1.0, 1.0 }, { 1.0, 1.0, 1.0 }, { -1.0, 1.0, 1.0 }, { -1.0, -1.0, -1.0 }, { 1.0, -1.0, -1.0 }, { 1.0, 1.0, -1.0 }, { -1.0, 1.0, -1.0 } }; /* * Specify vertices in counter-clockwise order for each face */ int face[][4] = { { 0, 1, 2, 3 }, { 1, 5, 6, 2 }, { 5, 4, 7, 6 }, { 4, 0, 3, 7 }, { 2, 6, 7, 3 }, { 4, 5, 1, 0 } }; /*--------------------------------------------------------------------------*/ const double pi2 = 6.28318530718; const double K_max = 3.5; const double K_min = 0.1; static double Delta_K = 0.005; static double K = 0.1; /*--------------------------------------------------------------------------*/ void NonlinearMap(double *x, double *y){ /* Standard Map */ *y += K * sin(*x); *x += *y; /* Angle x is modules 2Pi */ *x = fmod(*x, pi2); if (*x < 0.0) *x += pi2; }; /*--------------------------------------------------------------------------*/ void graphicsInit( void ) { glClearColor( 0.0, 0.0, 0.0, 0.0 ); /* dark gray background */ glEnable( GL_CULL_FACE ); } /*--------------------------------------------------------------------------*/ void cbReshape( int width, int height ) { GLfloat w[] = { -2.0, 2.0, -2.0, 2.0, -2.0, 2.0 }; GLfloat aspect; glMatrixMode( GL_PROJECTION ); glLoadIdentity(); #ifdef PERSPECTIVE gluPerspective( 40.0, (GLfloat) width / (GLfloat) height, 1.0, 10.0 ); glTranslatef( 0.0, 0.0, -6.0 ); #else aspect = (GLfloat) width / (GLfloat) height; if ( aspect <= 1.0 ) { glOrtho( w[0], w[1], w[2] / aspect, w[3] / aspect, w[4], w[5] ); } else { glOrtho( w[0] * aspect, w[1] * aspect, w[2], w[3], w[4], w[5] ); } #endif glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glViewport( 0, 0, width, height ); } /*--------------------------------------------------------------------------*/ void cbKeyboard( unsigned char key, int x, int y ) { if ( key == 'q' || key == 'Q' || key == 27 /* ESC */ ) { glutDestroyWindow( glutGetWindow() ); exit( 0 ); } } /*--------------------------------------------------------------------------*/ void cbAnimate( void ) { // angle += 2.0; K += Delta_K; if(K > K_max) K = K_min; angle +=2.0; glutPostRedisplay(); } /*--------------------------------------------------------------------------*/ void cbVisible( int vis ) { glutIdleFunc( ( vis == GLUT_VISIBLE ) ? cbAnimate : NULL ); } /*--------------------------------------------------------------------------*/ void cbDrawPicture( void ) { int i, j; const int NumberSteps = 1000; const int NumberOrbits = 50; const double Delta_x = pi2/(NumberOrbits-1); int step, orbit; glClear( GL_COLOR_BUFFER_BIT ); glPushMatrix(); glRotatef( 30.0, 1.0, 0.0, 0.0 ); /* tip cube 30 toward viewer */ glRotatef( angle, 0.0, 1.0, 0.0 ); glShadeModel(GL_FLAT); // dennis added so it won't blend colors. glBegin( GL_QUADS ); for ( i = 0; i < 6; i++ ) /* draw all six sides */ { glColor3fv( color[i] ); for ( j = 0; j < 4; j++ ) /* four vertices per side */ { // glColor3fv( color[face[i] ); glVertex3fv( cube[face[i][j]] ); } } glEnd(); // glBegin(GL_POINTS); // glColor3f(0.0,0.0,0.0); // glVertex3f(0.0,0.0,1.0); // glVertex3f(0.0,0.0,-1.0); // glVertex3f(0.0,1.0,0.0); // glVertex3f(0.0,-1.0,0.0); // glVertex3f(1.0,0.0,0.0); // glVertex3f(-1.0,0.0,0.0); // glEnd(); glBegin(GL_POINTS); glColor3f(1.0,1.0,1.0); for (orbit = 0; orbit < NumberOrbits; orbit++){ double x, y,xx,yy; y = 3.1415; x = Delta_x * orbit; for (step = 0; step < NumberSteps; step++){ NonlinearMap(&x, &y); xx=(2*x/pi2)-1.0; if(xx<-1.0)xx=-1.0; if(xx>1.0) xx=1.0; yy=(2*y/pi2)-1.0; if(yy<-1.0)yy=-1.0; if(yy>1.0) yy=1.0; glVertex3f(xx,1.0,yy); //glVertex3f((2*x/pi2)-1.0,1.0, (2*y/pi2)-1.0 ); }; }; for (orbit = 0; orbit < NumberOrbits; orbit++){ double x, y , xx, yy; x = 3.1415; y = Delta_x * orbit; for (step = 0; step < NumberSteps; step++){ NonlinearMap(&x, &y); xx=(2*x/pi2)-1.0; if(xx<-1.0)xx=-1.0; if(xx>1.0) xx=1.0; yy=(2*y/pi2)-1.0; if(yy<-1.0)yy=-1.0; if(yy>1.0) yy=1.0; glVertex3f(xx,1.0,yy); // glVertex2f(x, y); }; }; glEnd(); glPopMatrix(); glutSwapBuffers(); } /*--------------------------------------------------------------------------*/ int main( int argc, char *argv[] ) { glutInit( &argc, argv ); glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE ); glutInitWindowSize( 200, 200 ); glutInitWindowPosition( 0, 0 ); //glutCreateWindow( argv[0] ); glutCreateWindow("RotatingCubeWithChaosOnTop" ); glutReshapeFunc( cbReshape ); glutKeyboardFunc( cbKeyboard ); glutDisplayFunc( cbDrawPicture ); glutVisibilityFunc( cbVisible ); graphicsInit(); glutMainLoop(); return 0; }