#include <windows.h> #include <glut.h> #include<iostream.

h> //////////////// Global Variables //////////////////// GLint ScreenWidth=640; GLint ScreenHeight=480; #define SQUARE 1 #define TRIANGLE 2 #define RED 3 #define BLUE 4 #define GREEN 5 #define MAX 50 GLint shape, color; // used for drawing in selected shape and color bool draw=false; //used to diable drawind on the panel bool objectselected=false; // used to check object is selected or not GLint NO_OF_SQUARES=0; // used for counting squares and indexing in the array of squares GLint NO_OF_TRIANGLES=0; // used for counting triangles and indexing in the array of triangles

struct GLPoint { GLint x; GLint y; };

// structure for storing a point

GLPoint first; // these two variabes are used to draw the square GLPoint last; GLPoint bottomLeft; // these three points are used to draw triangle GLPoint top; GLPoint bottomRight; GLPoint temp; // used as a reference point to draw triangle, helpful for drawing triangle while dragging the mouse

// array of square objects struct Triangle { // structure to store triangle objects GLPoint bottomLeft.0).0. GLint clr.0).1. if(shape==SQUARE) { glBegin(GL_LINE_STRIP).0. } squares[MAX].1. glVertex2i(630.ScreenHeight). glEnd(). GLint clr. glVertex2i(580. GLPoint end.ScreenHeight-20). glBegin(GL_LINE_STRIP). //panel backgound glColor3f(1. glVertex2i(600. glVertex2i(580.0.0. glVertex2i(630. glVertex2i(600. GLPoint top. GLPoint bottomRight.0. glVertex2i(ScreenWidth. glEnd(). .0.ScreenHeight-50).ScreenHeight-20).0. //Square button glColor3f(0. //Selected Square button glColor3f(1.0).struct Square { // structure to store square objects GLPoint start. } triangles[MAX].0).0.ScreenHeight-20).0.1. // array of triangle objects void drawpanel() { glBegin(GL_QUADS). glVertex2i(ScreenWidth. glVertex2i(600.0).ScreenHeight).ScreenHeight-50).

glVertex2i(600. glVertex2i(580. glVertex2i(ScreenWidth. glVertex2i(630.ScreenHeight-110). glVertex2i(615. glVertex2i(600.0.0.1. glEnd().ScreenHeight-20).ScreenHeight-80).ScreenHeight-50).0.ScreenHeight-110).0. glVertex2i(615.ScreenHeight-110).1.0.0. glEnd().ScreenHeight-50). glEnd().0. glVertex2i(630.ScreenHeight-80).ScreenHeight-130). } glBegin(GL_LINE_STRIP). glBegin(GL_QUADS). glVertex2i(615.ScreenHeight-20).0). glVertex2i(600. glVertex2i(630. glEnd().0.ScreenHeight-20). //Red button glColor3f(1.0).ScreenHeight-130).0.0.ScreenHeight-80). glVertex2i(600. .0. } glBegin(GL_LINES). glVertex2i(615.ScreenHeight-180). glVertex2i(630.0. glVertex2i(600.ScreenHeight-110).0).0). //Triangle button glColor3f(0. //Separation line glColor3f(0.0.ScreenHeight-80).glVertex2i(600. //Selected Trangle button glColor3f(1.0. if(shape==TRIANGLE) { glBegin(GL_LINE_STRIP).

} glBegin(GL_QUADS).ScreenHeight-150).ScreenHeight-180). //Selected Green button glColor3f(0.0.0.0). if(color==RED) { glBegin(GL_QUADS). glEnd(). glVertex2i(630.ScreenHeight-240).ScreenHeight-270).ScreenHeight-300). //Green button glColor3f(0.ScreenHeight-210). glVertex2i(600. glVertex2i(600. //Selected Red button glColor3f(0.ScreenHeight-150).0).7. glVertex2i(600.ScreenHeight-180).0.ScreenHeight-150).0). glVertex2i(630.ScreenHeight-210).ScreenHeight-270).0. glVertex2i(630. glEnd().0. glVertex2i(600. //Blue button glColor3f(0.1. glVertex2i(630. glEnd().0. glVertex2i(630. glEnd(). glVertex2i(600.0).0. glVertex2i(630.1.ScreenHeight-300).0. glVertex2i(630.ScreenHeight-150). glVertex2i(600. if(color==GREEN) { glBegin(GL_QUADS).ScreenHeight-240).0.7.ScreenHeight-210). glEnd().0. glVertex2i(630.0. glVertex2i(630.0. . glVertex2i(630. } glBegin(GL_QUADS). glVertex2i(600.ScreenHeight-210). glVertex2i(600.ScreenHeight-240).ScreenHeight-240).ScreenHeight-180).glVertex2i(600.

//Selected Blue button glColor3f(0.ScreenWidth.y).0).if(color==BLUE) { glBegin(GL_QUADS).clr=color. glVertex2i(bottomRight.0.y. glVertex2i(600.0. last.0.bottomRight. first.0.ScreenHeight-300).0. glVertex2i(600.y). glVertex2i(630. glMatrixMode(GL_PROJECTION). } } void drawTriangle(void) { // method to draw triangle if(draw && !objectselected) { triangles[NO_OF_TRIANGLES]. last. .ScreenHeight-270).0.0.ScreenHeight-270). } void drawSquare(void) { // method to draw square if(draw && !objectselected) { // not on the panel and no object is selected glRecti(first. glVertex2i(630. glEnd(). glBegin(GL_POLYGON).0.ScreenHeight).0.x. } } void initialize(void) { glClearColor(0. glLoadIdentity().7). glFlush().ScreenHeight-300).0.x. gluOrtho2D(0.0.0.x.0.

int bl_x. int P3x.click_y.b3. } bool firstTime=true.br_x.bl_y)<1. return ((b1==b2) && (b2==b3)). int P2y. // used as a flag to clear the screen only first time void Display(void) { if(firstTime) { // to clear the screen only first time glClear(GL_COLOR_BUFFER_BIT).x.x. // Draw the panel // drawing color selections in following three conditional statements . } drawpanel(). int P1y. int P3y) { return (P1x-P3x)*(P2y-P3y) .top_y)<1. b2=sign(click_x. glVertex2i(bottomLeft.bl_y.top_y.bottomLeft.br_y. } } // A helping function to calculate whether the clicked point lies inside any triangle object int sign(int P1x.top_x. b3=sign(click_x. int br_y.bl_x. glFlush(). b1=sign(click_x.top. int bl_y) { bool b1.y). } // A helping function to calculate whether the clicked point lies inside any triangle object bool checkPoint(int click_x.click_y.br_y)<1.y).click_y.bl_x.br_x. int click_y.b2. int top_y.(P2x-P3x)*(P1y-P3y). int br_x. int top_x.top_x. firstTime=false.glVertex2i(top. glEnd(). int P2x.

// drawing shape selections in following two conditional statements if(shape==SQUARE) drawSquare(). if(shape==TRIANGLE) drawTriangle().clr=color. if(color==BLUE) glColor3f(0. // stores the x of . glFlush().y. if(color==GREEN) glColor3f(0. starting point squares[NO_OF_SQUARES].0.start.start. int state.x. // stores the y of starting point squares[NO_OF_SQUARES]. //setting the color data member first.0).0).if(color==RED) glColor3f(1.0. int y) { if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { if(x<580 && x>0 && !objectselected) { // checking that mouse is not on the panel and no object is selected draw=true. // assigning values to global variables for drawing square first.start.0. of objects squares[NO_OF_SQUARES]. } // method to handle left mouse click void mouseHandler(int button.0.y=ScreenHeight-y.0.0. if(shape==SQUARE) { NO_OF_SQUARES++.1. int x.start. // increment the no.0.0.x=squares[NO_OF_SQUARES].1.x=x.0.y=squares[NO_OF_SQUARES].0.0).

// saving the reference point temp. triangles[NO_OF_TRIANGLES]. shape=SQUARE.} if(shape==TRIANGLE) { NO_OF_TRIANGLES++. } } ////////// following five conditional statements enable the desired action to be performed when the respective button is clicked if(x>600 && x<630 && ScreenHeight-y>ScreenHeight-180 && ScreenHeight-y<ScreenHeight-150) { draw=false. . glutPostRedisplay().clr=color.x=x. } if(x>600 && x<630 && ScreenHeight-y>ScreenHeight-300 && ScreenHeight-y<ScreenHeight-270) { draw=false. glutPostRedisplay(). color=BLUE. } if(x>600 && x<630 && ScreenHeight-y>ScreenHeight-50 && ScreenHeight-y<ScreenHeight-20) { draw=false. color=GREEN. color=RED. glutPostRedisplay().y=y. temp. } if(x>600 && x<630 && ScreenHeight-y>ScreenHeight-250 && ScreenHeight-y<ScreenHeight-210) { draw=false.

end.x. .end.y=ScreenHeight-temp.top. last.x=x.y=ScreenHeight-y.glutPostRedisplay(). int xx=x-temp. } if(shape==TRIANGLE) { int yy=y-temp.x=x. } } } // mouse motion handler function void motionHandler(int x.y+yy+10.x=temp.y.top. } if(x>600 && x<630 && ScreenHeight-y>ScreenHeight-110 && ScreenHeight-y<ScreenHeight-80) { draw=false. /// following statements keep updating the three verteces of traingle. glutPostRedisplay(). triangles[NO_OF_TRIANGLES].y=ScreenHeight-y. shape=TRIANGLE. int y) { if(shape==SQUARE) { squares[NO_OF_SQUARES].x. // keep updating the y of second vertex of square //// also save changes in global variable of second vertex to draw the square last. // keep updating the x of second vertex of square squares[NO_OF_SQUARES]. icreasing them in a constant fashion with respect to the single reference point while the mouse is dragged triangles[NO_OF_TRIANGLES].

y=ScreenHeight(temp.y. } /// method to update the global variables of triangle to those of the current selected triangle object for redrawing it in new color void reDrawTriangle(GLint triangle_no) { top. last.bottomLeft.end.x=squares[square_no].x=triangles[NO_OF_TRIANGLES].y=squares[square_no].y+yy)-10.y=triangles[NO_OF_TRIANGLES]. } if(draw && x<580) glutPostRedisplay().x.x+xx+10.start.y. bottomRight.bottomLeft.bottomRight.start.bottomLeft.y=squares[square_no]. .end.y=triangles[NO_OF_TRIANGLES].clr.x.bottomLeft.x. top.y+yy)-10.top.triangles[NO_OF_TRIANGLES].y. triangles[NO_OF_TRIANGLES].y=triangles[NO_OF_TRIANGLES].top. // follwing statements keep updating the global variabl's values for drawing triangle top.top.x=triangles[NO_OF_TRIANGLES]. bottomRight.y. last.x=temp. bottomLeft. triangles[NO_OF_TRIANGLES].x. // called to draw shapes while the mouse is dragged } /// method to update the global variables of squre to those of the current selected square object for redrawing it in new color void reDrawSquare(GLint square_no) { first.bottomRight.bottomRight.x-xx-10.y. triangles[NO_OF_TRIANGLES].x. first.x.x=squares[square_no].y=ScreenHeight(temp.x=triangles[triangle_no].bottomRight. bottomLeft.x=temp. color=squares[square_no].x=triangles[NO_OF_TRIANGLES].

bottomLeft. color=triangles[triangle_no].end.x.y.y && ScreenHeight-y>squares[i]. case GREEN : color=GREEN.y) { // if menu is popped of on any square object reDrawSquare(i). // flag true so that triangle objects are not checked break.x=triangles[triangle_no].x && ScreenHeighty<squares[i]. bottomRight. bottomLeft.clr. break.start. bottomLeft. // update this object squareSelected=true.end.bottomLeft. bottomRight.y.top. } } bool squareSelected=false.bottomRight.x && x<squares[i]. i<=NO_OF_SQUARES. break. } /// method to handle menu options void processMenuEvents(int option) { switch (option) { case RED : color=RED. case BLUE : color=BLUE.start. int y) { for(int i=0. break.top.x=triangles[triangle_no]. int x.bottomRight.y=triangles[triangle_no].y=triangles[triangle_no]. // flag for checking whether square object is selected or not //// method to check that from which point the menu is popped void processMenuStatus(int status.y=triangles[triangle_no].y.x. } . i++) { // for each of the square boject drawn if(x>squares[i].

glutAttachMenu(GLUT_RIGHT_BUTTON). // redraw it } } } void main(int argc. ScreenWidth.triangles[j].150). j++) { // for all trangle objects if(checkPoint(x. glutInitWindowPosition(100. j<=NO_OF_TRIANGLES.bottomRight.triangles[j]. glutAddMenuEntry("Green".triangles[j]. glutDisplayFunc(Display). ScreenHeight).top. glutAddMenuEntry("Blue".bottomLeft. glutCreateMenu(processMenuEvents). glutAddMenuEntry("Red". . 0.ScreenHeighty. glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB). glutMotionFunc(motionHandler).RED). } glutPostRedisplay().top.GREEN).BLUE).ScreenHeight). glutInitWindowSize(ScreenWidth. glutMouseFunc(mouseHandler). initialize().x.x.y. // update this object break.bottomLeft. triangles[j].triangles[j]. glutMainLoop(). glViewport(0. glutCreateWindow("My First Attempt").triangles[j].bottomRight. // redraw it } if(!squareSelected) { // if no square object is selected check triangle objects for(int j=0. char **argv) { glutInit(&argc.glutPostRedisplay().y. glutMenuStatusFunc(processMenuStatus).y )) { // if menu is popped of on any triangle object reDrawTriangle(j). cout<<"in main".argv).x.

} .

Sign up to vote on this title
UsefulNot useful