#include #include #include #include using namespace std; // typedefs typedef unsigned char uchar; // constants const int BOXSIZE = 20; const int NUMCOLORS = 7; // struct struct s_point{ int x, y, color; }; // static vars static s_point *points = NULL; static int cur_color = 0; static int num_points = 0; static int height; // callbacks void keyb(uchar key, int x, int y); void disp(void); void resize(int x, int y); void mouse(int button, int state, int x, int y); // prototypes void drawQuad(int x, int y,int delta); //////////////////////////////// // main int main(int argc,char **argv){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); glutInitWindowSize(500,500); glutInitWindowPosition(100,100); glutCreateWindow("Polygon.cpp"); glClearColor(0.0,0.0,0.0,0.0); glutKeyboardFunc(keyb); glutDisplayFunc(disp); glutReshapeFunc(resize); glutMouseFunc(mouse); glutMainLoop(); return 0; } //////////////////////////////////////////////// // mouse void mouse(int button, int state, int x, int y){ // if left mouse down if(button == GLUT_LEFT && state == GLUT_DOWN){ // are we clicking in the pallette ? if((x < BOXSIZE) && (abs(y-height) < (NUMCOLORS * BOXSIZE))){ // if we are in the pallette, change the current color cur_color = abs(y-height)/BOXSIZE; // or have we clicked in the screen ? }else{ s_point * temp = new s_point[num_points+1]; // copy the old to a temporary for(int i=0;i<(num_points);i++){ temp[i].x = points[i].x; temp[i].y = points[i].y; temp[i].color = points[i].color; } // save the new one temp[num_points].x = x; temp[num_points].y = height - y; temp[num_points].color = cur_color; // increment the number of points num_points++; // delete the old delete [] points; // make the temporary the current one points = temp; // ask for a redisplay glutPostRedisplay(); } } } /////////////////////////////////// // keyb void keyb(uchar key, int x, int y){ switch(key){ case 'q': delete [] points; exit(0); break; case 'w': cout << "Wireframe mode on " << endl; glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); glutPostRedisplay(); break; case 'p': cout << "Point mode on " << endl; glPolygonMode(GL_FRONT_AND_BACK,GL_POINT); glutPostRedisplay(); break; case 'f': cout << "Fill mode on " << endl; glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glutPostRedisplay(); break; case 's': cout << "Smooth shading " << endl; glShadeModel(GL_SMOOTH); glutPostRedisplay(); break; case 'b': cout << "Flat shading " << endl; glShadeModel(GL_FLAT); glutPostRedisplay(); break; } }; //////////////// // disp void disp(void){ // define colors static float colorarray[NUMCOLORS][3] = { {1.0,1.0,1.0}, {0.0,1.0,1.0}, {0.0,0.0,1.0}, {1.0,1.0,0.0}, {1.0,0.0,0.0}, {0.0,1.0,0.0}, {1.0,0.0,1.0} }; glClear(GL_COLOR_BUFFER_BIT); // draw the palette int ypos = 0; for(int i=0;i