/************************************************************** * srawsquare.c * This program demonstrates the drawing of a square * using OpenGL * **************************************************************/ #include << X11/Xlib.h> #include << GLUT/glut.h> #include << OpenGL/gl.h> #include << stdlib.h> #include << stdio.h> void myInit(void) { // set black background (clearing) color glClearColor (1.0, 1.0, 1.0, 0.0); // initialize viewing values glMatrixMode (GL_PROJECTION); glLoadIdentity (); // clear the matrix // viewing transformation glOrtho (0.0, 1.0, 0.0, 1.0, -1.0, 1.0); } void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); // clear the screen // draw black polygon (rectangle) with corners at // (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0) glColor3f (0.0, 0.0, 0.0); // set the drawing color glBegin(GL_POLYGON); glVertex3f(0.25, 0.25, 0.0); glVertex3f(0.75, 0.25, 0.0); glVertex3f(0.75, 0.75, 0.0); glVertex3f(0.25, 0.75, 0.0); glEnd(); // Don't wait! // start processing buffered OpenGL rountines glFlush (); } void myMouse(int button, int state, int x, int y) { switch (button) { case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) exit (-1); break; default: break; } } /* * Request double buffer display mode. * Register mouse input callback functions */ int main(int argc, char** argv) { glutInit(&argc, argv); // initialize the toolkit glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // set display mode glutInitWindowSize (250, 250); // set screen window size glutInitWindowPosition (100, 100); // set window position on screen glutCreateWindow (argv[0]); // open the screen window myInit (); glutDisplayFunc(myDisplay); // register redraw function glutMouseFunc(myMouse); // register myMouse function glutMainLoop(); // go into a perpetual loop return 0; }