/* simple2.c draws a rectangle with vertices of different colors; note the color blending done by OpenGL, this is called smooth shading, and it's the default. The user can switch between glShadeModel(GL_SMOOTH); //smooth shading or glShadeModel(GL_FLAT); //pick last color and draw all polygon with that color OpenGL 1.x Laura Toma */ #include #include #include #include #ifdef __APPLE__ #include #else #include #endif GLfloat red[3] = {1.0, 0.0, 0.0}; GLfloat green[3] = {0.0, 1.0, 0.0}; GLfloat blue[3] = {0.0, 0.0, 1.0}; GLfloat black[3] = {0.0, 0.0, 0.0}; GLfloat white[3] = {1.0, 1.0, 1.0}; GLfloat gray[3] = {0.5, 0.5, 0.5}; GLfloat yellow[3] = {1.0, 1.0, 0.0}; GLfloat magenta[3] = {1.0, 0.0, 1.0}; GLfloat cyan[3] = {0.0, 1.0, 1.0}; GLint fillmode = 1; /* forward declarations of functions */ void display(void); void keypress(unsigned char key, int x, int y); int main(int argc, char** argv) { /* open a window */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutInitWindowPosition(100,100); glutCreateWindow(argv[0]); /* OpenGL init */ glClearColor(0, 0, 0, 0); /* set background color black*/ glEnable(GL_DEPTH_TEST); //not necessary in 2D /* callback functions */ glutDisplayFunc(display); glutKeyboardFunc(keypress); /* event handler */ glutMainLoop(); return 0; } /* draw a polygon with vertices in [-1,1]x[-1,1]*/ void draw_polygon(){ if (fillmode) { glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); } else { glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); } /* draw a polygon */ glBegin(GL_POLYGON); glColor3fv(blue); glVertex2f(-0.5,-0.5); glColor3fv(white); glVertex2f(-0.5,0.5); glColor3fv(red); glVertex2f(0.5,0.5); glColor3fv(gray); glVertex2f(0.5,-0.5); glEnd(); } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); draw_polygon(); /* execute the drawing commands */ glFlush(); } void keypress(unsigned char key, int x, int y) { switch(key) { case 'q': exit(0); break; } }