#include #include #define X 300 #define Y 300 float color[3] = {0.5,0.5,0.5}; void setColor(int i){ switch (i){ case 0: color[0]=1.0; color[1]=0.0; color[2]=0.0; break; case 1: color[0]=0.0; color[1]=1.0; color[2]=0.0; break; case 2: color[0]=0.0; color[1]=0.0; color[2]=1.0; break; case 3: color[0]=0.0; color[1]=0.0; color[2]=0.0; break; case 4: color[0]=1.0; color[1]=1.0; color[2]=1.0; break; default: break; } } void init(){ //set the background color to black (RGBA) glClearColor(0.0,0.0,0.0,0.0); //Make a menu (pass in the func name, the func takes an int) glutCreateMenu(setColor); //add entries (char *,int) glutAddMenuEntry("Red",0); glutAddMenuEntry("Green",1); glutAddMenuEntry("Blue",2); glutAddMenuEntry("Black",3); glutAddMenuEntry("White",4); //make the menu show up upon right mouse button (hold down) glutAttachMenu(GLUT_RIGHT_BUTTON); } void display(){ glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POINTS); //put the real work in between here for (int i=0;i<50;i++){ glColor3f((float)i*1.0/(float)50,1.0,0.0); glVertex2i(10+i,20+i); } // glEnd(); glFlush(); } void mouse(int button, int state, int x, int y){ y = Y - y; //this converts coordinates properly switch(button){ case GLUT_LEFT_BUTTON: if (state==GLUT_DOWN){ glBegin(GL_POINTS); //3fv takes an array of 3 floats glColor3fv(color); glVertex2i(x,y); glEnd(); glFlush(); }break; default:break; } } void keyboard(unsigned char key, int x, int y){ y = Y - y; //this also converts coordinates properly switch (key){ case 'q':case 'Q': exit(0); break; case ' ': glBegin(GL_POINTS); glColor3f(1.0,1.0,1.0); glVertex2i(x,y); glEnd(); glFlush(); break; default: display(); } } int main (int argc,char **argv){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(X,Y); //your window size glutInitWindowPosition(100,100);//your window position glutCreateWindow(argv[0]); //your window name glOrtho(0,X-1,0,Y-1,-1,1); //the coords that will show up in your window init(); glutDisplayFunc(display); glutMouseFunc(mouse); glutKeyboardFunc(keyboard); glutMainLoop(); //note: this will NEVER return. return 0; }