#include "..\glut.h" #include #include #include #include "button.cpp" /* Global object declarations */ bool tri_clicked = false; int tri_color_seq_num = 0; GLfloat tri_red[5] = { 1.0, 0.8, 0.5, 0.0, 0.0 }; GLfloat tri_green[5] = { 1.0, 0.2, 0.5, 1.0, 0.0 }; GLfloat tri_blue[5] = { 1.0, 0.0, 0.2, 0.3, 0.4 }; //global Button objects created with appropriate location and text PushButton colorChangeButton(20, 150, 80, 30, "Change Color"); PushButton quitButton(20,50, 50, 30, "Quit", 1.0, 0.0, 0.0); void myInit() { glClearColor(0.6, 0.6, 0.2, 1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 500.0, 0.0, 500.0); glMatrixMode(GL_MODELVIEW); } // end of my_init() void myDisplay() { glClear(GL_COLOR_BUFFER_BIT); colorChangeButton.Draw(); // calls the draw quitButton.Draw(); // method for each // of the buttons glColor3f(tri_red[tri_color_seq_num], tri_green[tri_color_seq_num], tri_blue[tri_color_seq_num]); glBegin(GL_POLYGON); //gives the attributes of the triangle glVertex2i(100, 500); glVertex2i(500, 300); glVertex2i(400, 50); glEnd(); glFlush(); } // end of myDisplay() void myMouseCallback(int mouse_btn, int state, int x, int y) { if (mouse_btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN && colorChangeButton.WasClicked(x, y)) { colorChangeButton.Depress(); } if (mouse_btn == GLUT_LEFT_BUTTON && state == GLUT_UP && colorChangeButton.WasClicked(x, y)) { // this means that the triangle tri_clicked=true; // button was clicked last tri_color_seq_num ++; if (tri_color_seq_num > 4) tri_color_seq_num = 0; colorChangeButton.Draw(); myDisplay(); } if (mouse_btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN && quitButton.WasClicked(x, y)) { quitButton.Depress(); } if (mouse_btn == GLUT_LEFT_BUTTON && state == GLUT_UP && quitButton.WasClicked(x, y)) { quitButton.Draw(); exit(0); } } // end of myMouseCallback() void main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB ); glutInitWindowSize(500, 500); glutInitWindowPosition(0, 0); glutCreateWindow("OpenGL PushButton Example"); glutDisplayFunc(myDisplay); glutMouseFunc(myMouseCallback); myInit(); glutMainLoop(); }