#include #include GLsizei wh = 500, ww = 500; GLfloat size = 10.0; void drawSquare(int x, int y) { y = wh - y; glColor3ub( (char) rand()%256, (char) rand()%256, (char) rand()%256); glBegin(GL_POLYGON); glVertex2f(x+size, y+size); glVertex2f(x-size, y+size); glVertex2f(x-size, y-size); glVertex2f(x+size, y-size); glEnd(); glFlush(); } void mouse(int btn, int state, int x, int y) { if(btn==GLUT_RIGHT_BUTTON) exit(0); if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN) drawSquare(x, y); } void myReshape(GLsizei w, GLsizei h) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glViewport(0,0,w,h); glClearColor (0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT); glFlush(); ww = w; wh = h; } void myinit(void) { glViewport(0,0,ww,wh); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble) ww, 0.0, (GLdouble) wh, -1.0, 1.0); glClearColor (1.0, 1.0, 1.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glFlush(); } void display(void) {} void mymenu(int id) { if(id == 1) glClearColor(0.0,1.0,0.0,1.0); if(id == 2) exit(0); } int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutCreateWindow("square"); myinit (); glutReshapeFunc (myReshape); glutMouseFunc (mouse); glutMotionFunc(drawSquare); glutDisplayFunc(display); int menu_id = glutCreateMenu(mymenu); glutAddMenuEntry("clear Screen", 1); glutAddMenuEntry("exit", 2); glutAttachMenu(GLUT_RIGHT_BUTTON); glutMainLoop(); return 0; }