h>
#include <glut.h>
#include<iostream.h>
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
GLint x;
GLint y;
};
GLPoint first; // these two variabes are used to draw the square
GLPoint last;
GLPoint temp; // used as a reference point to draw triangle, helpful for drawing
triangle while dragging the mouse
struct Square { // structure to store square objects
GLPoint start;
GLPoint end;
GLint clr;
} squares[MAX]; // array of square objects
GLPoint bottomLeft;
GLPoint top;
GLPoint bottomRight;
GLint clr;
} triangles[MAX]; // array of triangle objects
void drawpanel() {
glEnd();
glEnd();
if(shape==SQUARE) {
glEnd();
}
glEnd();
if(shape==TRIANGLE) {
glEnd();
}
glEnd();
glEnd();
if(color==RED) {
glBegin(GL_QUADS); //Selected Red button
glColor3f(0.7,0.0,0.0);
glVertex2i(600,ScreenHeight-180);
glVertex2i(600,ScreenHeight-150);
glVertex2i(630,ScreenHeight-150);
glVertex2i(630,ScreenHeight-180);
glEnd();
}
glEnd();
if(color==GREEN) {
glBegin(GL_QUADS); //Selected Green button
glColor3f(0.0,0.7,0.0);
glVertex2i(600,ScreenHeight-240);
glVertex2i(600,ScreenHeight-210);
glVertex2i(630,ScreenHeight-210);
glVertex2i(630,ScreenHeight-240);
glEnd();
}
glEnd();
if(color==BLUE) {
glBegin(GL_QUADS); //Selected Blue button
glColor3f(0.0,0.0,0.7);
glVertex2i(600,ScreenHeight-300);
glVertex2i(600,ScreenHeight-270);
glVertex2i(630,ScreenHeight-270);
glVertex2i(630,ScreenHeight-300);
glEnd();
}
void initialize(void) {
glClearColor(0.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,ScreenWidth,0.0,ScreenHeight);
}
glFlush();
}
glBegin(GL_POLYGON);
glVertex2i(bottomRight.x,bottomRight.y);
glVertex2i(top.x,top.y);
glVertex2i(bottomLeft.x,bottomLeft.y);
glEnd();
glFlush();
}
}
// A helping function to calculate whether the clicked point lies inside any triangle object
int sign(int P1x, int P1y, int P2x, int P2y, int P3x, int P3y) {
// A helping function to calculate whether the clicked point lies inside any triangle object
bool checkPoint(int click_x, int click_y, int top_x, int top_y, int br_x, int br_y, int bl_x,
int bl_y) {
bool b1,b2,b3;
b1=sign(click_x,click_y,top_x,top_y,br_x,br_y)<1;
b2=sign(click_x,click_y,br_x,br_y,bl_x,bl_y)<1;
b3=sign(click_x,click_y,bl_x,bl_y,top_x,top_y)<1;
bool firstTime=true; // used as a flag to clear the screen only first time
void Display(void) {
glClear(GL_COLOR_BUFFER_BIT);
firstTime=false;
}
if(color==GREEN)
glColor3f(0.0,1.0,0.0);
if(color==BLUE)
glColor3f(0.0,0.0,1.0);
if(shape==SQUARE)
drawSquare();
if(shape==TRIANGLE)
drawTriangle();
glFlush();
first.x=squares[NO_OF_SQUARES].start.x; // assigning
values to global variables for drawing square
first.y=squares[NO_OF_SQUARES].start.y;
}
if(shape==TRIANGLE) {
NO_OF_TRIANGLES++;
draw=false;
color=RED;
glutPostRedisplay();
}
draw=false;
color=GREEN;
glutPostRedisplay();
}
draw=false;
color=BLUE;
glutPostRedisplay();
}
draw=false;
shape=SQUARE;
glutPostRedisplay();
}
draw=false;
shape=TRIANGLE;
glutPostRedisplay();
}
if(shape==SQUARE) {
//// also save changes in global variable of second vertex to draw the square
last.x=x;
last.y=ScreenHeight-y;
}
if(shape==TRIANGLE) {
int yy=y-temp.y;
int xx=x-temp.x;
/// following statements keep updating the three verteces of traingle, icreasing
them in a constant fashion with respect to the single reference point while the mouse is
dragged
triangles[NO_OF_TRIANGLES].top.x=temp.x;
triangles[NO_OF_TRIANGLES].top.y=ScreenHeight-temp.y+yy+10;
triangles[NO_OF_TRIANGLES].bottomRight.x=temp.x+xx+10;
triangles[NO_OF_TRIANGLES].bottomRight.y=ScreenHeight-
(temp.y+yy)-10;
triangles[NO_OF_TRIANGLES].bottomLeft.x=temp.x-xx-10;
triangles[NO_OF_TRIANGLES].bottomLeft.y=ScreenHeight-
(temp.y+yy)-10;
// follwing statements keep updating the global variabl's values for drawing
triangle
top.x=triangles[NO_OF_TRIANGLES].top.x;
top.y=triangles[NO_OF_TRIANGLES].top.y;
bottomRight.x=triangles[NO_OF_TRIANGLES].bottomRight.x;
bottomRight.y=triangles[NO_OF_TRIANGLES].bottomRight.y;
bottomLeft.x=triangles[NO_OF_TRIANGLES].bottomLeft.x;
bottomLeft.y=triangles[NO_OF_TRIANGLES].bottomLeft.y;
}
if(draw && x<580)
glutPostRedisplay(); // 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
/// method to update the global variables of triangle to those of the current selected
triangle object for redrawing it in new color
top.x=triangles[triangle_no].top.x;
top.y=triangles[triangle_no].top.y;
bottomRight.x=triangles[triangle_no].bottomRight.x;
bottomRight.y=triangles[triangle_no].bottomRight.y;
bottomLeft.x=triangles[triangle_no].bottomLeft.x;
bottomLeft.y=triangles[triangle_no].bottomLeft.y;
color=triangles[triangle_no].clr;
switch (option) {
case RED : color=RED; break;
case GREEN : color=GREEN; break;
case BLUE : color=BLUE; break;
}
}
bool squareSelected=false; // flag for checking whether square object is selected or not
//// method to check that from which point the menu is popped
for(int i=0; i<=NO_OF_SQUARES; i++) { // for each of the square boject drawn
break;
}
glutPostRedisplay(); // redraw it
}
break;
}
glutPostRedisplay(); // redraw it
}
}
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(ScreenWidth,ScreenHeight);
glutInitWindowPosition(100,150);
glViewport(0, 0, ScreenWidth, ScreenHeight);
glutCreateWindow("My First Attempt");
cout<<"in main";
glutDisplayFunc(Display);
glutMouseFunc(mouseHandler);
glutMotionFunc(motionHandler);
glutMenuStatusFunc(processMenuStatus);
glutCreateMenu(processMenuEvents);
glutAddMenuEntry("Red",RED);
glutAddMenuEntry("Blue",BLUE);
glutAddMenuEntry("Green",GREEN);
glutAttachMenu(GLUT_RIGHT_BUTTON);
initialize();
glutMainLoop();
}