// WormholeDemo.cpp // NKU CSC 480 Spring 2006 Kirby Program 1 // ----------------------------------------------------------- // Display some wormholes in wireframe. // Use keys 1 2 3 to choose which variety of wormhole to draw. // // EXTRA: Introduces display lists, so we don't have to completely // recalculate every wormhole vertex at every frame of animation! // // Link this program with Wormhole.cpp // ----------------------------------------------------------- #include #include #include #include "Geometry.h" #include "Stopwatch.h" #include "Wormhole.h" using namespace csc480 ; using namespace std ; Stopwatch G_stopwatch ; // for pausing animation int G_whichWormhole= 0 ; // user choice of wormholes 0,1,2 int G_wormhole[3] ; // IDs of three wormlists stored in display lists void makeWormholes() // Creates display lists storing 3 differently shaped wormholes, for later display. { // "Standard" wormhole. G_wormhole[0]= glGenLists(1) ; glNewList( G_wormhole[0], GL_COMPILE ) ; wormhole( 2.0, 1.0, 1.4, 0.2, 0.1 ) ; glEndList() ; // Showing that rectangles on floor need not be same size. G_wormhole[1]= glGenLists(1) ; glNewList( G_wormhole[1], GL_COMPILE ) ; wormhole( 1.6, 1.6, 0.70, 0.25, 0.05 ) ; glEndList() ; // Skinny wormhole. G_wormhole[2]= glGenLists(1) ; glNewList( G_wormhole[2], GL_COMPILE ) ; wormhole( 2.0, 1.0, 1.4, 0.2, 0.03 ) ; glEndList() ; } void display() // Called by glut whenever scene needs repainting. { // Prepare to draw glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ; glLoadIdentity() ; glScaled( 0.8, 0.8, 0.8 ) ; // Set angle based on time, for animation. double angle= G_stopwatch.getValue() / 100 ; glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ) ; glRotated( angle, 1,1,1 ) ; // Draw the wormhole selected by user via keyboard callback. glColor3d( 0.5,0,0 ) ; glCallList( G_wormhole[ G_whichWormhole ] ) ; glutSwapBuffers() ; } void animate() // Callback for one frame of animtation. { glutPostRedisplay() ; } void toggleAnimation() // Toggle animation on and off. { if ( G_stopwatch.isStopped() ) { G_stopwatch.start() ; glutIdleFunc( animate ) ; } else { G_stopwatch.stop() ; glutIdleFunc( NULL ) ; } } void kbDispatch( unsigned char ch, int x, int y ) // Respond to key presses (ASCII key values): toggle animation or select wormhole type. { if ( ch == ' ' ) toggleAnimation() ; else if ( ch >= '0' && ch <= '2' ) G_whichWormhole= ch - '0' ; glutPostRedisplay() ; } int main( int argc, char** argv ) { // Initialize OpenGL. glutInit( &argc, argv ) ; glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH ) ; // Window attributes. glutInitWindowSize( 600, 600 ) ; glutInitWindowPosition( 100, 100 ) ; glutCreateWindow( "Csc 480: Wormhole!" ) ; glClearColor( 1,1,1,1 ) ; // white background // Set projection matrix. glMatrixMode( GL_PROJECTION ) ; glLoadIdentity() ; gluPerspective( 20, 1, 4, 8 ) ; gluLookAt( 0,0,6, 0,0,0, 0,1,0 ) ; glMatrixMode( GL_MODELVIEW ) ; // Build display lists makeWormholes(); // Set callbacks glutKeyboardFunc( kbDispatch ) ; glutIdleFunc( animate ) ; glutDisplayFunc( display ) ; // Turn control over to event loop. G_stopwatch.start() ; glutMainLoop() ; return 0 ; }