/* * Smiley.c * * A first attempt at OpenGL programming * */ /* #include // needed for Windows programming only */ #include #include #include #include #include #include // Global variables representing state of the graphics window int rgb, doubleBuffer; GLenum windType; GLint windW, windH; // Values & functions for setting colors #include "tkmap.c" // Used to draw circles GLUquadricObj *face; // Initialize variables that do not change static void Init(void) { glClearColor(0.0, 0.0, 0.0, 0.0); // black background /* Create quadric object for Smiley's face */ face = gluNewQuadric(); gluQuadricDrawStyle(face, GLU_FILL); gluQuadricNormals(face, GLU_FLAT); } //This is called when the window appears or changes shape static void Reshape(int width, int height) { windW = (GLint)width; windH = (GLint)height; // Draw in all of the screen window glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-1, 1, -1, 1); // Range of coordinates in the viewport glMatrixMode(GL_MODELVIEW); } // Respond to special keys static void Key2(int key, int x, int y) { switch (key) { case GLUT_KEY_LEFT: // left arrow break; case GLUT_KEY_RIGHT: // right arrow break; case GLUT_KEY_UP: // up arrow break; case GLUT_KEY_DOWN: // down arrow break; default: return; } glutPostRedisplay(); } // Respond to regular keys static void Key(unsigned char key, int x, int y) { switch (key) { case 27: // ESC key exit(1); default: return; } glutPostRedisplay(); } // Draw the smiley face: called whenever the screen window changes static void Draw(void) { float one_eighth, one_sixteenth, one_third; one_eighth = 2.0/8.0; // one eighth of the visible area one_sixteenth = 2.0/16.0; // one sixteenth of the visible area one_third = 2.0/3.0; // one third of the visible area /* Clear the screen */ glClear(GL_COLOR_BUFFER_BIT); /* Draw a yellow face */ SetColor(COLOR_YELLOW); gluDisk(face, 0.0, one_third, 20, 1); /* Draw black eyes */ SetColor(COLOR_BLACK); glBegin(GL_POLYGON); /* right eye */ glVertex2f(one_eighth, one_eighth); glVertex2f(one_eighth,one_sixteenth); glVertex2f(one_sixteenth, one_sixteenth); glVertex2f(one_sixteenth, one_eighth); glEnd(); glBegin(GL_POLYGON); /* left eye */ glVertex2f(-one_eighth, one_eighth); glVertex2f(-one_eighth,one_sixteenth); glVertex2f(-one_sixteenth, one_sixteenth); glVertex2f(-one_sixteenth, one_eighth); glEnd(); /* Draw the mouth */ glLineWidth(5); glBegin(GL_LINE_STRIP); glVertex2f(-one_eighth, -one_eighth); glVertex2f(one_eighth,-one_eighth); glEnd(); glFlush(); if (doubleBuffer) { glutSwapBuffers(); } } static GLenum Args(int argc, char **argv) // command line arguments { GLint i; rgb = GL_TRUE; doubleBuffer = GL_FALSE; for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-ci") == 0) { rgb = GL_FALSE; } else if (strcmp(argv[i], "-rgb") == 0) { rgb = GL_TRUE; } else if (strcmp(argv[i], "-sb") == 0) { doubleBuffer = GL_FALSE; } else if (strcmp(argv[i], "-db") == 0) { doubleBuffer = GL_TRUE; } else { printf("%s (Bad option).\n", argv[i]); return GL_FALSE; } } return GL_TRUE; } int main(int argc, char **argv) { glutInit(&argc, argv); if (Args(argc, argv) == GL_FALSE) { exit(1); } windW = 300; windH = 300; glutInitWindowPosition(0, 0); glutInitWindowSize( windW, windH); windType = (rgb) ? GLUT_RGB : GLUT_INDEX; windType |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE; glutInitDisplayMode(windType); if (glutCreateWindow("Smiley Face") == GL_FALSE) { exit(1); } InitMap(rgb); Init(); glutReshapeFunc(Reshape); glutKeyboardFunc(Key); glutSpecialFunc(Key2); glutDisplayFunc(Draw); glutMainLoop(); }