/* melite1b.c - visualizza un pianeta e un satellite artificiale in 2D * usando le primitive viste * * Autore: Andrea Fusiello, Andrea Colombari 2004 * * arrow keys - move planet and artificial satellite coherently * s key - commuta tra modalita' con shading e flat * Escape Key - esce dal programma */ #include #include #include #include /* includes gl.h, glu.h */ /* Function Prototypes */ void initgfx( void ); void keyboard( GLubyte, GLint, GLint ); void specialkeys( GLint, GLint, GLint ); void drawScene( void ); void checkError( char * ); void printHelp( char * ); /* Global Definitions */ GLboolean flat = GL_TRUE; GLdouble x; GLdouble y; GLdouble g_v; #define KEY_ESC 27 /* ascii value for the escape key */ int main( int argc, char *argv[] ) { GLsizei width, height; glutInit( &argc, argv ); width = glutGet( GLUT_SCREEN_WIDTH ); height = glutGet( GLUT_SCREEN_HEIGHT ); glutInitWindowPosition( width / 4, height / 4 ); glutInitWindowSize( width / 2, height / 2 ); glutInitDisplayMode( GLUT_RGBA ); glutCreateWindow( argv[0] ); initgfx(); glutKeyboardFunc( keyboard ); glutSpecialFunc( specialkeys ); glutDisplayFunc( drawScene ); printHelp( argv[0] ); glutMainLoop(); return(0); } void printHelp( char *progname ) { fprintf(stdout, "\n%s - elite in 2D with moving objects\n\n" "arrow keys - move planet and artificial satellite coherently\n" " key - toggle smooth/flat shading\n" "Escape Key - exit the program\n\n", progname); } void initgfx( void ) { /* set clear color to black */ glClearColor( 0.0, 0.0, 0.0, 1.0 ); glOrtho( -4.0f, 4.0f, -3.0f, 3.0f, -3.0f, 3.0f ); y = 0; x = 0; g_v = 0.2; } void checkError( char *label ) { GLenum error; while ( (error = glGetError()) != GL_NO_ERROR ) printf( "%s: %s\n", label, gluErrorString(error) ); } void keyboard( GLubyte key, GLint x, GLint y ) { switch (key) { case 's': /* s key */ if (flat) glShadeModel( GL_FLAT ); else glShadeModel( GL_SMOOTH ); glutPostRedisplay(); /* toggle between smooth and flat shading */ flat = !flat; break; case KEY_ESC: /* Exit when the Escape key is pressed */ exit(0); } } void specialkeys( GLint key, GLint u, GLint v ) { /* arrow keys */ /* move the point of view */ switch (key) { case GLUT_KEY_UP: y += g_v; glutPostRedisplay(); break; case GLUT_KEY_DOWN: y -= g_v; glutPostRedisplay(); break; case GLUT_KEY_LEFT: x -= g_v; glutPostRedisplay(); break; case GLUT_KEY_RIGHT: x += g_v; glutPostRedisplay(); break; } } void drawScene( void ) { static GLfloat yellow[] = { 1.0f, 1.0f, 0.0f }; static GLfloat darkgreen[] = { 0.0f, 0.25f, 0.0f }; static GLfloat blue[] = { 0.0f, 0.0f, 1.0f }; static GLfloat grey[] = { 0.5f, 0.5f, 0.5f }; glClear( GL_COLOR_BUFFER_BIT ); //glOrtho( -4.0f, 4.0f, -3.0f, 3.0f, -3.0f, 3.0f ); /* Do all your OpenGL rendering here */ /* Draw a triangle fan for the planet; make * the center yellow, and the edges darkgreen */ glBegin( GL_TRIANGLE_FAN ); glColor3fv( yellow ); glVertex2f( x, y ); glColor3fv( darkgreen ); glVertex2f( x, y-1.5f ); glVertex2f( x+1.5f, y-0.8f ); glVertex2f( x+1.5f, y+0.8f ); glVertex2f( x, y+1.5f ); glVertex2f( x-1.5f, y+0.8f ); glVertex2f( x-1.5f, y-0.8f ); glVertex2f( x, y-1.5f ); glEnd(); /* Draw a triangle strip for the spaceship; * use your fantasy to give it a shape * and use two different color (e.g. blue and * grey) for shading */ glBegin( GL_TRIANGLE_FAN ); glColor3fv( blue ); glVertex2f( x+2.1f, y+2.0f ); glColor3fv( grey ); glVertex2f( x+2.2f, y+2.2f ); glVertex2f( x+1.8f, y+2.0f ); glVertex2f( x+2.2f, y+1.8f ); glVertex2f( x+2.1f, y+2.0f ); glEnd(); checkError( "drawScene" ); glFlush(); }