[OpenGL] Computer Graphics experiment 02: polygon world

Source: Internet
Author: User
/*************************************** ********************************* // * Test02: polygon world/* Author: Wang Haiyang/* Date: 2012/11/20/* Email: wanghaiyang@139.me /************************************** * ********************************/# define max_polygons 8 # define max_vertices 10 # define editrange 30 # include <Gl/glut. h> # include <stdlib. h> # include <iostream> # include <fstream> # include <iostream> # include <String> using namespace STD; void mymouse (INT, Int, Int, INT); void mymotion (INT, INT); void mydisplay (); void myreshape (INT, INT ); void color_menu (INT); void main_menu (INT); int pick_polygon (int x, int y); void myinit (); // define the struct type typedef struct polygon {int color; // color index bool used; // returns true int xmin, xmax, ymin, and Ymax if the polygon has been defined; // enclose the box float XC, YC; // The Center int nvertices of the polygon; // The number of vertices int X [max_vertices]; // Vertex int y [max_vertices]; // vertex} polygon; // define the flag variable bool picking = false; // whether it is in the picking status bool moving = false; // whether the polygon is in the moving state int in_polygon =-1; // int present_color = 0 inside any polygon; // The default color int polygoncount = 0; // record the total number of polygon glfloat temparrayx [max_vertices] = {0}; glfloat temparrayy [max_vertices] = {0}; int tempcount = 0; // The number of temporary points of the currently drawn polygon bool tempdrawing = false; // determine whether the current polygon is being drawn glsizei wh = 500; // initialize the window size glsizei WW = 500; // initialization window size int draw_mode = 0; // drawing mode bool editpointing = false; // whether to edit the vertex glfloat colors [8] [3] ={{ 0.0, 0.0, 0.0 },{ 1.0, 0.0, 0.0 },{ 0.0, 1.0, 0.0 },{ 0.0, 0.0, 1.0 },{ 0.0, 1.0, 1.0 },{ 1.0, 0.0, 1.0 },{ 1.0, 1.0, 0.0}, {1.0, 1.0, 1.0 }}; polygon polygons [max_polygons]; void myreshape (int w, int h) {glmatrixmode (gl_projection); glloadidentity (); gluortho2d (0.0, (gldouble) W, 0. 0, (gldouble) H); glmatrixmode (gl_modelview); glloadidentity (); glviewport (0, 0, W, H); ww = W; Wh = H ;} void myinit () {glclearcolor (0.1, 0.1, 0.5, 1.0); // set the clear screen color to blue-blue for (INT I = 0; I <max_polygons; I ++) {polygons [I]. used = false; // mark all polygon as undefined} void mymouse (int btn, int state, int X, int y) {Y = wh-y; // Add the vertex if (BTN = maid & State = fig &&! Picking &&! Moving & tempdrawing) {moving = false; picking = false; cout <"added a vertex! "<Endl; cout <" in_polygon: "<in_polygon <Endl; If (polygons [in_polygon]. nvertices = max_vertices) {cout <"Maximum number of vertices exceeded" <Endl;} else {temparrayx [tempcount] = x; temparrayy [tempcount] = y; tempcount ++; glupostredisplay ();} If (in_polygon> = 0) {If (polygons [in_polygon]. nvertices = max_vertices) {cout <"exceeds the maximum number of vertices" <Endl;} else {int I = polygons [in_polygon]. nvertices; polygons [in_polygon]. X [I] = X; cout <"added a vertex! Polygons [in_polygon]. X [I]: "<polygons [in_polygon]. X [I] <Endl; polygons [in_polygon]. Y [I] = y; cout <"added a vertex! Polygons [in_polygon]. Y [I]: "<polygons [in_polygon]. Y [I] <Endl; polygons [in_polygon]. nvertices ++ }}} if (BTN = maid & State = maid & Picking &&! Moving) {moving = false; picking = false; Int J = pick_polygon (x, y); If (j> = 0) {polygons [J]. used = false; polygoncount --; in_polygon =-1; fig () ;}} int pick_polygon (int x, int y) {for (INT I = max_polygons; i> = 0; I --) {cout <"polygons [I]. used = "<polygons [I]. used <Endl; If (polygons [I]. used) {If (x> = polygons [I]. xmin) & (x <= polygons [I]. xmax) & (Y> = polygons [I]. ymin )&& (Y <= polygons [I]. ymax) {in_polygon = I; moving = true; return I ;}}cout <"not inside the polygon" <Endl; Return-1 ;} void mymotion (int x, int y) {float dx, Dy; y = wh-y; int I, j; If (editpointing) {for (INT I = 0; I <max_polygons; ++ I) {If (polygons [I]. used) {for (Int J = 0; j <polygons [I]. nvertices; ++ J) {If (x <polygons [I]. X [J] + editrange & x> polygons [I]. X [J]-editrange & Y <polygons [I]. Y [J] + editra Nge & Y> polygons [I]. Y [J]-editrange) {polygons [I]. X [J] = x; polygons [I]. Y [J] = y ;}}} glupostredisplay () ;}if (tempdrawing) {temparrayx [tempCount-1] = x; temparrayy [tempCount-1] = y; glupostredisplay ();} if (moving) {J = pick_polygon (x, y); If (j <0) {cout <"not inside the polygon" <Endl; return ;} DX = x-polygons [J]. XC; DY = Y-polygons [J]. YC; for (I = 0; I <polygons [J]. nvertices; I ++) {polygons [J]. X [I] + = DX; polygons [J]. Y [I] + = Dy;} polygons [J]. XC + = DX; polygons [J]. YC + = Dy; polygons [J]. xmax + = DX; polygons [J]. xmin + = DX; polygons [J]. ymax + = Dy; polygons [J]. ymin + = Dy; glupostredisplay () ;}} void color_menu (INT index) {present_color = index; If (in_polygon> = 0) {polygons [in_polygon]. color = index ;}} void main_menu (INT index) {int I = 0; Switch (INDEX) {Case 1: // create a new polygon {tempdrawing = True; tempcount = 0; moving = false; picking = false; editpointing = false; moving = false; for (I = 0; I <max_polygons; I ++) {If (polygons [I]. used = false) {break;} if (I = max_polygons) {cout <"has reached the maximum value \ n" <Endl; exit (0 );} cout <"I =" <I <Endl; polygons [I]. color = present_color; polygons [I]. used = true; polygons [I]. nvertices = 0; in_polygon = I; picking = false; break;} case 2: // define the ending polygon and calculate the bounding box of the polygon and its center position tempdrawing = false; tempcount = 0; moving = false; picking = false; editpointing = false; if (in_polygon> = 0) {polygons [in_polygon]. xmax = polygons [in_polygon]. xmin = polygons [in_polygon]. X [0]; polygons [in_polygon]. ymax = polygons [in_polygon]. ymin = polygons [in_polygon]. Y [0]; polygons [in_polygon]. XC = polygons [in_polygon]. X [0]; polygons [in_polygon]. YC = polygons [in _ Polygon]. Y [0]; for (INT I = 1; I <polygons [in_polygon]. nvertices; I ++) {If (polygons [in_polygon]. X [I] <polygons [in_polygon]. xmin) {polygons [in_polygon]. xmin = polygons [in_polygon]. X [I];} else if (polygons [in_polygon]. X [I]> polygons [in_polygon]. xmax) {polygons [in_polygon]. xmax = polygons [in_polygon]. X [I];} If (polygons [in_polygon]. Y [I] <polygons [in_polygon]. ymin) {polygons [in_polygon]. ym In = polygons [in_polygon]. Y [I];} else if (polygons [in_polygon]. Y [I]> polygons [in_polygon]. ymax) {polygons [in_polygon]. ymax = polygons [in_polygon]. Y [I];} polygons [in_polygon]. XC + = polygons [in_polygon]. X [I]; polygons [in_polygon]. YC ++ = polygons [in_polygon]. Y [I]; cout <"polygons [in_polygon]. X "<I <": "<polygons [in_polygon]. X [I] <Endl; cout <"polygons [in_polygon]. Y "<I <": "<polygons [in_polygon]. Y [I] <Endl;} polygons [in_polygon]. XC = polygons [in_polygon]. XC/polygons [in_polygon]. nvertices; polygons [in_polygon]. YC = polygons [in_polygon]. YC/polygons [in_polygon]. nvertices;} in_polygon =-1; polygoncount ++; glupostredisplay (); break; Case 3: // sets the picking mode tempdrawing = false; tempcount = 0; moving = false; picking = true; editpointing = false; break; Case 4: // whether to move tempdrawing = false; tempcou Nt = 0; moving = true; picking = false; editpointing = false; break; Case 5: // storage file {tempdrawing = false; tempcount = 0; moving = false; picking = false; editpointing = false; in_polygon =-1; STD: ofstream file ("polygon. dat ", STD: IOS: ate | STD: IOS: Binary); If (! File) {STD: cout <"file opening failed! "; Abort (); // equivalent to exit} // write the file for (INT I = 0; I <max_polygons; I ++) {cout <"polygons [" <I <"]. used "<polygons [I]. used <Endl; If (polygons [I]. used) {cout <"I wrote one !! "<Endl; file. write (char *) & polygons [I], sizeof (polygons [I]); cout <"& polygons [I], sizeof (polygons [I]) "<& polygons [I] <" size "<sizeof (polygons [I]) <Endl ;}// close the file. close (); break;} case 6: // read the file {in_polygon =-1; tempdrawing = false; tempcount = 0; moving = false; picking = false; editpointing = false; STD: ifstream rfile ("polygon. dat ", STD: IOS: Binary); // open the file // STD: ofstream file (" polygon. da T ", STD: IOS: ate | STD: IOS: Binary); If (! Rfile) {STD: cout <"file opening failed! "; Return; // equivalent to exit} // read the file for (INT I = 0; I <max_polygons; I ++) {cout <"polygons [" <I <"]. used "<polygons [I]. used <Endl; rfile. read (char *) & polygons [I], sizeof (polygons [I]); cout <"polygons [" <I <"]. used "<polygons [I]. used <Endl;} glupostredisplay (); // close the file rfile. close (); break;} case 7: in_polygon =-1; tempdrawing = false; tempcount = 0; moving = false; picking = false; editpointing = true; brea K ;}} void mydisplay () {cout <"mydisplay! "<Endl; glclear (gl_color_buffer_bit); For (INT I = 0; I <max_polygons; ++ I) {If (polygons [I]. Used & (I! = In_polygon | moving) {cout <"polygons [I]. used = "<polygons [I]. used <Endl; glcolor3fv (colors [polygons [I]. color]); cout <colors [polygons [I]. color] <Endl; glbegin (gl_polygon); For (Int J = 0; j <polygons [I]. nvertices; ++ J) {glvertex2i (polygons [I]. X [J], polygons [I]. Y [J]); cout <"X:" <polygons [I]. X [J] <"Y:" <polygons [I]. Y [J] <Endl;} glend () ;}if (tempdrawing) {glcolor3fv (colors [present_color]); glbegin (gl_line_strip); For (Int J = 0; j <tempcount; ++ J) {glvertex2f (temparrayx [J], temparrayy [J]);} glend ();} glflush ();} int main (INT argc, char ** argv) {int c_menu; gluinit (& argc, argv); gluinitdisplaymode (glu_single | glu_rgb); gluinitwindowposition (400,100); gluinitwindowsize (500,500 ); valley createwindow ("polygon test"); fig (mydisplay); myinit (); c_menu = fig (color_menu); fig ("black", 0); fig ("red ", 1); maid ("green", 2); maid ("blue", 3); maid ("blue", 4); maid ("Purple", 5 ); glumaddmenuentry ("yellow", 6); glumaddmenuentry ("white", 7); glumcreatemenu (main_menu); glumaddmenuentry ("", 1); glumaddmenuentry ", 2); maid ("delete", 3); maid ("move", 4); maid ("Archive Local file", 5 ); // The options for storing the Quadrilateral data to a local file ("read local file", 6); gluaddmenuentry ("Edit vertex", 7); gluaddsubmenu ("color ", c_menu); cout <"c_menu =" <c_menu <Endl; gluattachmenu (FIG); glureshapefunc (myreshape); glumousefunc (mymouse); glumotionfunc (mymotion ); glumainloop ();}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.