/* demoSweepVol.c */ /* Purpose: Construct a shape and sweep it around the y-axis */ #include #include #include #include #define KEY_ESC 27 /* GLUT doesn't supply this */ #define MAXOUTLINES 30 /* maximum number of discrete steps */ #define MAXPOINTS 30 /* maximum number of points in outline */ /* Global Definitions */ struct mPoint3D { GLfloat x; GLfloat y; GLfloat z; }; /* This contains the vertex data for the lamp outline */ struct mPoint3D mV[MAXPOINTS] = { {0.0, 0.0, 0.0}, {0.1, 0.0, 0.0}, {0.2, 0.0, 0.0}, {0.3, 0.0, 0.0}, {0.4, 0.0, 0.0}, {0.5, 0.0, 0.0}, {0.5, 0.3, 0.0}, {0.5, 0.4, 0.0}, {0.4, 0.55, 0.0}, {0.3, 0.7, 0.0}, {0.2, 0.8, 0.0}, {0.2, 1.0, 0.0}, {0.3, 1.0, 0.0}, {0.4, 1.0, 0.0}, {0.5, 1.0, 0.0}, {0.6, 1.0, 0.0}, {0.7, 1.0, 0.0}, {0.8, 1.0, 0.0}, {0.9, 1.0, 0.0}, {1.0, 1.0, 0.0}, {0.9, 1.05, 0.0}, {0.8, 1.1, 0.0}, {0.7, 1.15, 0.0}, {0.6, 1.2, 0.0}, {0.5, 1.25, 0.0}, {0.4, 1.3, 0.0}, {0.3, 1.35, 0.0}, {0.2, 1.4, 0.0}, {0.1, 1.45, 0.0}, {0.0, 1.5, 0.0} }; void printHelp( void ) { fprintf(stdout, "\ndemoSweepVol - 3D shape from 2D outline\n\n" "Escape key - exit the program\n\n"); } GLvoid keyboard( GLubyte key, GLint x, GLint y) { switch (key) { case KEY_ESC: /* exit when escape key is pressed */ exit(0); } } GLvoid checkError( const char* const label ) { GLenum error; error = glGetError(); while (GL_NO_ERROR != error ) { fprintf(stderr,"%s: %s\n", label, gluErrorString(error) ); error = glGetError(); } } /* draw outline of lamp */ GLvoid drawOutline( GLvoid ) { int loop; glBegin(GL_LINE_STRIP); for( loop = 0; loop < MAXPOINTS; ++loop) { glVertex3f( mV[loop].x, mV[loop].y, mV[loop].z ); } glEnd(); } /* Sweep the outline around the y axis */ GLvoid sweepOutline( GLvoid ) { GLint loop; glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); for( loop = 0; loop < MAXOUTLINES ; ++loop ) { float frac = (float)loop / MAXOUTLINES; /* frac will run from 0 to a bit less than 1 */ glLoadIdentity(); glRotatef( frac * 360, 0.0, 1.0, 0.0 ); glColor3f(1.0 - frac, 0.0, frac); /* change colour to show up separate outlines */ drawOutline(); } checkError( "sweepOutline" ); glFlush(); } GLvoid init( GLvoid ) { glClearColor( 1.0, 1.0, 1.0, 1.0); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0); glMatrixMode( GL_MODELVIEW ); glLineWidth(3.0); glEnable(GL_DEPTH_TEST); } int main( int argc, char *argv[] ) { glutInit( &argc, argv ); glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH ); glutCreateWindow( argv[0] ); init(); glutKeyboardFunc( keyboard ); glutDisplayFunc( sweepOutline ); printHelp( ); glutMainLoop(); return 0; }