Anda di halaman 1dari 13

#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 { // structure for storing a point

GLint x;
GLint y;

};

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
struct Square { // structure to store square objects

GLPoint start;
GLPoint end;
GLint clr;
} squares[MAX]; // array of square objects

struct Triangle { // structure to store triangle objects

GLPoint bottomLeft;
GLPoint top;
GLPoint bottomRight;
GLint clr;
} triangles[MAX]; // array of triangle objects

void drawpanel() {

glBegin(GL_QUADS); //panel backgound


glColor3f(1.0,1.0,1.0);
glVertex2i(580,0);
glVertex2i(580,ScreenHeight);
glVertex2i(ScreenWidth,ScreenHeight);
glVertex2i(ScreenWidth,0);

glEnd();

glBegin(GL_LINE_STRIP); //Square button


glColor3f(0.0,0.0,1.0);
glVertex2i(600,ScreenHeight-20);
glVertex2i(630,ScreenHeight-20);
glVertex2i(630,ScreenHeight-50);
glVertex2i(600,ScreenHeight-50);
glVertex2i(600,ScreenHeight-20);

glEnd();

if(shape==SQUARE) {

glBegin(GL_LINE_STRIP); //Selected Square button


glColor3f(1.0,0.0,0.0);
glVertex2i(600,ScreenHeight-20);
glVertex2i(630,ScreenHeight-20);
glVertex2i(630,ScreenHeight-50);
glVertex2i(600,ScreenHeight-50);
glVertex2i(600,ScreenHeight-20);

glEnd();
}

glBegin(GL_LINE_STRIP); //Triangle button


glColor3f(0.0,0.0,1.0);
glVertex2i(615,ScreenHeight-80);
glVertex2i(630,ScreenHeight-110);
glVertex2i(600,ScreenHeight-110);
glVertex2i(615,ScreenHeight-80);

glEnd();

if(shape==TRIANGLE) {

glBegin(GL_LINE_STRIP); //Selected Trangle button


glColor3f(1.0,0.0,0.0);
glVertex2i(615,ScreenHeight-80);
glVertex2i(630,ScreenHeight-110);
glVertex2i(600,ScreenHeight-110);
glVertex2i(615,ScreenHeight-80);

glEnd();
}

glBegin(GL_LINES); //Separation line


glColor3f(0.0,0.0,1.0);
glVertex2i(580,ScreenHeight-130);
glVertex2i(ScreenWidth,ScreenHeight-130);

glEnd();

glBegin(GL_QUADS); //Red button


glColor3f(1.0,0.0,0.0);
glVertex2i(600,ScreenHeight-180);
glVertex2i(600,ScreenHeight-150);
glVertex2i(630,ScreenHeight-150);
glVertex2i(630,ScreenHeight-180);

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();
}

glBegin(GL_QUADS); //Green button


glColor3f(0.0,1.0,0.0);
glVertex2i(600,ScreenHeight-240);
glVertex2i(600,ScreenHeight-210);
glVertex2i(630,ScreenHeight-210);
glVertex2i(630,ScreenHeight-240);

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();
}

glBegin(GL_QUADS); //Blue button


glColor3f(0.0,0.0,1.0);
glVertex2i(600,ScreenHeight-300);
glVertex2i(600,ScreenHeight-270);
glVertex2i(630,ScreenHeight-270);
glVertex2i(630,ScreenHeight-300);

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);
}

void drawSquare(void) { // method to draw square

if(draw && !objectselected) { // not on the panel and no object is selected

glRecti(first.x, first.y, last.x, last.y);

glFlush();
}

void drawTriangle(void) { // method to draw triangle

if(draw && !objectselected) {


triangles[NO_OF_TRIANGLES].clr=color;

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) {

return (P1x-P3x)*(P2y-P3y) - (P2x-P3x)*(P1y-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;

return ((b1==b2) && (b2==b3));


}

bool firstTime=true; // 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);

firstTime=false;
}

drawpanel(); // Draw the panel

// drawing color selections in following three conditional statements


if(color==RED)
glColor3f(1.0,0.0,0.0);

if(color==GREEN)
glColor3f(0.0,1.0,0.0);

if(color==BLUE)
glColor3f(0.0,0.0,1.0);

// drawing shape selections in following two conditional statements

if(shape==SQUARE)
drawSquare();

if(shape==TRIANGLE)
drawTriangle();

glFlush();

// method to handle left mouse click

void mouseHandler(int button, int state, int x, 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;
if(shape==SQUARE) {

NO_OF_SQUARES++; // increment the no. of objects

squares[NO_OF_SQUARES].start.x=x; // stores the x of


starting point
squares[NO_OF_SQUARES].start.y=ScreenHeight-y; //
stores the y of starting point
squares[NO_OF_SQUARES].clr=color; //setting the color
data member

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++;

temp.x=x; // saving the reference point


temp.y=y;
triangles[NO_OF_TRIANGLES].clr=color;
}

////////// 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;
color=RED;
glutPostRedisplay();
}

if(x>600 && x<630 && ScreenHeight-y>ScreenHeight-250 &&


ScreenHeight-y<ScreenHeight-210) {

draw=false;
color=GREEN;
glutPostRedisplay();
}

if(x>600 && x<630 && ScreenHeight-y>ScreenHeight-300 &&


ScreenHeight-y<ScreenHeight-270) {

draw=false;
color=BLUE;
glutPostRedisplay();
}

if(x>600 && x<630 && ScreenHeight-y>ScreenHeight-50 &&


ScreenHeight-y<ScreenHeight-20) {

draw=false;
shape=SQUARE;
glutPostRedisplay();
}

if(x>600 && x<630 && ScreenHeight-y>ScreenHeight-110 &&


ScreenHeight-y<ScreenHeight-80) {

draw=false;
shape=TRIANGLE;
glutPostRedisplay();
}

// mouse motion handler function

void motionHandler(int x, int y) {

if(shape==SQUARE) {

squares[NO_OF_SQUARES].end.x=x; // keep updating the x of


second vertex of square
squares[NO_OF_SQUARES].end.y=ScreenHeight-y; // keep updating the
y of second vertex of 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

void reDrawSquare(GLint square_no) {


first.x=squares[square_no].start.x;
first.y=squares[square_no].start.y;
last.x=squares[square_no].end.x;
last.y=squares[square_no].end.y;
color=squares[square_no].clr;

/// 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.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;

/// method to handle menu options

void processMenuEvents(int option) {

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

void processMenuStatus(int status, int x, int y) {

for(int i=0; i<=NO_OF_SQUARES; i++) { // for each of the square boject drawn

if(x>squares[i].start.x && x<squares[i].end.x && ScreenHeight-


y<squares[i].start.y && ScreenHeight-y>squares[i].end.y) { // if menu is popped of on
any square object

reDrawSquare(i); // update this object

squareSelected=true; // flag true so that triangle objects are not


checked

break;
}
glutPostRedisplay(); // redraw it
}

if(!squareSelected) { // if no square object is selected check triangle objects


for(int j=0; j<=NO_OF_TRIANGLES; j++) { // for all trangle objects
if(checkPoint(x,ScreenHeight-
y,triangles[j].top.x,triangles[j].top.y,triangles[j].bottomRight.x,triangles[j].bottomRight.y,
triangles[j].bottomLeft.x,triangles[j].bottomLeft.y )) { // if menu is popped of on any
triangle object

reDrawTriangle(j); // update this object

break;
}

glutPostRedisplay(); // redraw it
}
}

void main(int argc, char **argv) {

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();
}

Anda mungkin juga menyukai