/* Author's name: Leyond Lin Author's Blog: http://yexin218.cublog.cn Student's ID:04009853F-I011-0032 declaration: These codes just for study. If you want to use my codes in your own program, please keep the information of author, thanks. Data:2005-3-5 */
#include "pointArray.h" #include "stdlib.h" #include <GL/glut.h> #include <GL/glu.h> #include <iostream> using namespace std;
int operation=0; //operation of begin, delete,move ,refresh, quit
int selection; //selecton of GL_LINE_LOOP¡¡or GL_LINER_STRIP
GLint WinHeight=480,WinWidth=480;//set the value of the Windows
static GLintPointArray poly;//creat an instantiation of GLintPointArray
int current=-1; //the current number of the points
int deleteIndex,moveIndex; //the index of deleting and moving point
int NUM=0; int F=0,flag[20]={0}; // an array stores the number of Starting points
void drawPolyLine(GLintPointArray p,int selection) { int v=0;//parameter of the number of the strating points
//using the array flag to store the index of the starting points
for(int i=0;i<=current;i++) { if(poly.pt[i].getFlag()==1) flag[++v]=i; }
//print the indexs
/*for(int i=0;i<20;i++) cout<<"the index of the strating points"< cout< //draw the integrated image with the beginning of the starting point
for(int k=0;k<NUM;k++){ glBegin(selection==5? GL_LINE_LOOP:GL_LINE_STRIP); for(int m=flag[k];m<flag[k+1];m++) glVertex2i(p.pt[m].getX(),p.pt[m].getY()); glEnd(); } //draw the image of the rest part
glBegin(selection==5? GL_LINE_LOOP:GL_LINE_STRIP); for(int n= flag[NUM];n<=current;n++) glVertex2i(p.pt[n].getX(),p.pt[n].getY()); glEnd();
//glFlush();
} void mykey(unsigned char key, int mouseX, int mouseY) { //GLint x= mouseX;
// GLint y = mouseY;
switch(key) { case 'b': // presents for creating a new polyLine
case 'B': operation = 1; cout<< "operation is Beginning"<<endl; break; case 'd': //presents for deleting the next point pointed to
case 'D': operation = 2; cout<< "operation is Deleting"<<endl; break; case 'm': //presents for moveing the point
case 'M': operation = 3; cout<< "operation is Moving"<<endl; break; case 'r'://refresh the screen and redraw the polylines
case 'R': glClear(GL_COLOR_BUFFER_BIT); current=-1; operation=0; F=0; NUM=0; cout<< "operation is erasing "<<endl; glFlush(); break; case 'l': //do the action of GL_LINE_LOOP
case 'L': selection = 5; cout<<"selecton is LOOP"<<endl; break; case 's'://do the actions of GL_LINE_STRIP
case 'S': selection = 6; cout<<"selecton is STRIP"<<endl; break; case 'f'://presents for a new staring point
case 'F': F=1; NUM++; cout<<"NUM is"<<NUM<<endl; break; case 'g'://reset the value of NUM to 0
case 'G': F=0; cout<<"NUM is"<<NUM<<endl; break; case 'q'://quit the programming
case 'Q': exit(-1); default: break; } //glFlush();
}
void mymouse(int button,int state,int mouseX,int mouseY) { //to creat a new polyline
if(button == GLUT_LEFT_BUTTON && state ==GLUT_DOWN && current< 60 && operation == 1) { if(current == 59 || NUM == 19){ cout<<"YOU COULD NOT ADD MORE POINTS!!!"<<endl; exit(-1); } poly.pt[++current].setX(mouseX); poly.pt[current].setY(WinHeight-mouseY); poly.pt[current].setFlag(F); cout<<"point begin ("<<poly.pt[current].getX()<<","<< poly.pt[current].getY()<<" ) with the flag of "<<poly.pt[current].getFlag()<<endl; glClear(GL_COLOR_BUFFER_BIT); drawPolyLine(poly,selection); } //to delete the point
if(button==GLUT_LEFT_BUTTON && state ==GLUT_DOWN && operation==2) { poly.CP.setX(mouseX); poly.CP.setY(WinHeight-mouseY); //cout<<"point to be deleted is:"< for(int i= 0;i<=current;i++) { int subX = poly.pt[i].getX() - poly.CP.getX(); int subY = poly.pt[i].getY() - poly.CP.getY(); if((subX < 8 && subX > -8)&&(subY < 8 && subY > -8)) { deleteIndex =i; break; } } cout<<"point ("<<poly.pt[deleteIndex].getX()<<","<<poly.pt[deleteIndex].getY()<<") be deleted"<<endl; //set the next point's attribute if current's flag =1
if(poly.pt[deleteIndex].getFlag()==1) poly.pt[deleteIndex+1].setFlag(1); //refresh the array
for(int k=deleteIndex; k<=current;k++) { poly.pt[k].setX(poly.pt[k+1].getX()); poly.pt[k].setY(poly.pt[k+1].getY()); poly.pt[k].setFlag(poly.pt[k+1].getFlag()); } current--; glClear(GL_COLOR_BUFFER_BIT); drawPolyLine(poly,selection); }
//to move the point
if(button==GLUT_LEFT_BUTTON && state ==GLUT_DOWN && operation==3) { poly.CP.setX(mouseX); poly.CP.setY(WinHeight-mouseY);
//cout<<"current to moving is"< for(int i= 0;i<=current;i++) { int subX =poly.pt[i].getX()-poly.CP.getX(); int subY = poly.pt[i].getY() - poly.CP.getY(); if((subX< 8 && subX> -8)&&(subY < 8 &&subY > -8)) { moveIndex =i; break; } } // to move a new one when click again
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN && operation==3) { poly.CP.setX(mouseX); poly.CP.setY(WinHeight-mouseY); poly.pt[moveIndex].setX(poly.CP.getX()); poly.pt[moveIndex].setY(poly.CP.getY()); //poly.pt[moveIndex].setFlag();
cout<<"destinaton to moving is ("<<poly.CP.getX()<<","<<poly.CP.getY()<<")"<<endl;
/*show a new polylines for(int i=0;i<=current;i++) { cout< }*/ } glClear(GL_COLOR_BUFFER_BIT); drawPolyLine(poly,selection); }
glFlush(); }
void reshape(GLsizei w, GLsizei h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, w , 0, h); glMatrixMode(GL_PROJECTION); //drawPolyLine(poly,selection);
glFlush();
} void display(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 0.0); drawPolyLine(poly,selection); glFlush(); }
void init(void) { glClearColor(0.0, 0.0, 1.0, 1.0); glColor3f(0.0f,0.0f,0.0f); glPointSize(2.0);
}
int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(480,480); glutInitWindowPosition(0, 0); glutCreateWindow("Drawing a House,0409853F-I011-0032"); glutReshapeFunc(reshape); glutDisplayFunc(display); glutKeyboardFunc(mykey); glutMouseFunc(mymouse); init(); glutMainLoop(); return 0; }
|