Use OpenGL to dynamically draw a cubic bezr curve with the mouse

Source: Internet
Author: User

The three-step curve has four control nodes. The one-dimensional calculator principle of OpenGL is used.

 

Program introduction:

For the first time, the left mouse button down is set to 0 control nodes. Press and hold the left mouse button to move dynamically to determine 1 control node,

The second time you left the mouse to go down to 3 control nodes, press and hold the left button to move to confirm 2 control nodes.

Program running:

 

The code just draws a piece of bezeiser as follows:

# Include <Gl/glut. h> <br/> # include <iostream> <br/> # include <vector> <br/> using namespace STD; </P> <p> # define key_point_num 6 <br/> # define control_point_num 4 <br/> # define winwidth1024 <br/> # define winheight768 </P> <p> # define drawoneline (x1, y1, Z1, X2, Y2, Z2) glbegin (gl_lines);/<br/> glvertex3d (X1), (Y1), (Z1 )); glvertex3d (X2), (Y2), (Z2); glend (); </P> <p> enumdraw_flag {<br/> first_line = 0, <Br/> second_line = 1, <br/> end = 2 <br/>}; <br/> intg_iflag = first_line; <br/> intg_viewport [4]; <br/> boolg_bisdown = false; <br/> doubleg_modelmatrix [16]; <br/> doubleg_projmatrix [16]; <br/> double g_vertex [key_point_num] [3]; // six key points, four of which are control nodes. <Br/> doubleg_controlvertex [control_point_num + 3] [3]; </P> <p> ///////////////////////////////// //////////////////////////////////////// /<br/> void Init (); <br/> void display (); <br/> void reshape (int w, int H); <br/> void keyboard (unsigned char key, int X, int y); <br/> void mouse (INT button, int state, int X, int y); <br/> void motion (int x, int y ); </P> <p> int main (INT argc, char ** argv) <br/> {<br/> glutin It (& argc, argv); <br/> gluinitdisplaymode (glu_double | glu_rgb); <br/> gluinitwindowsize (winwidth, winheight); <br/> gluinitwindowposition (100,100 ); <br/> glucreatewindow (argv [0]); </P> <p> Init (); <br/> gludisplayfunc (Display ); <br/> glureshapefunc (reshape); <br/> glukeyboardfunc (keyboard); <br/> glumousefunc (Mouse); <br/> glumotionfunc (motion ); </P> <p> glumainloop (); </P> <p> return 0; <br/>}< /P> <p> void Init (void) <br/>{< br/> glclearcolor (0.0, 0.0, 0.0, 0.0); <br/> glshademodel (gl_smooth ); <br/>}</P> <p> void display (void) <br/>{< br/> glclear (gl_color_buffer_bit ); </P> <p> glpointsize (5.0); <br/> glcolor3f (1.0, 1.0, 0.0); </P> <p> glable (gl_line_stipple ); <br/>{< br/> gllinestipple (1, 0x0101); <br/> If (first_line = g_iflag) <br/> {<br/> drawoneline (g_vertex [0] [0], g_vertex [0] [1], g_vertex [0] [2 ], <Br/> g_vertex [1] [0], g_vertex [1] [1], g_vertex [1] [2]); <br/> drawoneline (g_vertex [0] [0], g_vertex [0] [1], g_vertex [0] [2], <br/> g_vertex [2] [0], g_vertex [2] [1], g_vertex [2] [2]); </P> <p> glbegin (gl_points); <br/>{< br/> glvertex3dv (g_vertex [0]); <br/> glvertex3dv (g_vertex [1]); <br/> glvertex3dv (g_vertex [2]); <br/>} glend (); <br/>}< br/> else if (second_line = g_iflag) <br/>{< br/> drawoneline (g_vertex [3] [0], g_vertex [3] [1], g_vertex [3] [2], <br/> g_vertex [4] [0], g_vertex [4] [1], g_vertex [4] [2]); <br/> drawoneline (g_vertex [3] [0], g_vertex [3] [1], g_vertex [3] [2], <br/> g_vertex [5] [0], g_vertex [5] [1], g_vertex [5] [2]); </P> <p> glbegin (gl_points); <br/>{< br/> glvertex3dv (g_vertex [0]); <br/> glvertex3dv (g_vertex [3]); <br/> glvertex3dv (g_vertex [4]); <br/> glvertex3dv (g_vertex [5]); <br/>} glend (); <br/>}< br />} Gldisable (gl_line_stipple); </P> <p> // extract the control node and draw the bezr curve. <Br/> //////////////////////////////////// /// // <br /> glbegin (gl_points ); <br/> {<br/> for (INT I = 0; I <control_point_num; I ++) <br/> glvertex3dv (& g_controlvertex [I] [0]); <br/>} glend (); </P> <p> glcolor3f (1.0f, 1.0f, 1.0f); <br/> glmap1d (gl_map1_vertex_3, 0.0, 1.0, 3, control_point_num, & g_controlvertex [0] [0]); <br/> glable (gl_map1_vertex_3); <br/> glbegin (gl_line_stri P); <br/>{< br/> for (INT I = 0; I <= 30; I ++) <br/> glevalcoord1f (glfloat) i/30366f); <br/>} glend (); <br/> //////////////////////////////////// /// // </ p> <p> fig (); <br/>}</P> <p> void reshape (int w, int h) <br/> {<br/> glviewport (0, 0, (glsizei) w, (glsizei) H); <br/> glmatrixmode (gl_projection); <br/> glloadidentity (); <br/> If (W <= H) <br/> glortho (-5.0, 5.0,-5.0 * (glfloat) h/(glfloat) W, <br/> 5.0 * (glfloat) h/(glfloat) W,-5.0, 5.0 ); <br/> else <br/> glortho (-5.0 * (glfloat) W/(glfloat) h, <br/> 5.0 * (glfloat) W/(glfloat) H, -5.0, 5.0,-5.0, 5.0); <br/> glmatrixmode (gl_modelview); <br/> glloadidentity (); <br/>}</P> <p> void keyboard (unsigned char key, int X, int y) <br/>{< br/> switch (key) {<br/> Case 27: <br/> exit (0); <br/> break; <br/>}</P> <p> voi D mouse (INT button, int state, int X, int y) <br/>{< br/> // obtain matrix Information <br/> glgetintegerv (gl_viewport, g_viewport ); <br/> glgetdoublev( gl_modelview_matrix, g_modelmatrix); <br/> glgetdoublev( gl_projection_matrix, g_projmatrix ); </P> <p> If (Button = maid & State = maid) <br/>{< br/> g_bisdown = true; </P> <p> double _ 3 dpoint [3]; <br/> Y = g_viewport [3]-y; </P> <p> If (first_line = g_iflag) <br/>{< br/> Glu Unproject (X, Y, 0, <br/> g_modelmatrix, g_projmatrix, g_viewport, <br/> & _ 3 dpoint [0], & _ 3 dpoint [1], & _ 3 dpoint [2]); </P> <p> // all initialization <br/> for (INT I = 0; I <3; I ++) <br/> g_controlvertex [0] [I] = g_vertex [0] [I] = _ 3 dpoint [I]; <br/>}< br/> else if (second_line = g_iflag) <br/>{< br/> gluunproject (X, Y, 0, <br/> g_modelmatrix, g_projmatrix, g_viewport, <br/> & _ 3 dpoint [0], & _ 3 dpoint [1], & _ 3 dpoint [2]); </P> <p> // Save V (3) <br/> for (INT I = 0; I <3; I ++) <br/> g_controlvertex [3] [I] = g_vertex [3] [I] = _ 3 dpoint [I]; <br/>}</P> <p> If (Button = fig & State = fig) <br/>{< br/> g_bisdown = false; </P> <p> g_iflag = (g_iflag + 1) % 3; </P> <p> glusetcursor (glu_cursor_right_arrow ); <br/>}</P> <p> /////////////////////// //////////////////////////////////////// ///////// <br/> // computing control node <br/> void motion (in T x, int y) <br/>{< br/> If (! G_bisdown) <br/> return; </P> <p> maid (FIG); <br/> Y = g_viewport [3]-y; </P> <p> If (first_line = g_iflag) <br/>{< br/> gluunproject (X, Y, 0, <br/> g_modelmatrix, g_projmatrix, g_viewport, <br/> & g_vertex [1] [0], & g_vertex [1] [1], & g_vertex [1] [2]); </P> <p> for (INT I = 0; I <3; I ++) <br/> {<br/> // record control node <br/> g_controlvertex [1] [I] = g_vertex [1] [I]; </P> <p> // v0 is the midpoint of V1 and V2 <br/> g_vertex [2] [I] = 2 * g_vertex [0] [I]-g_vertex [1] [I]; <br/>}< br/> else if (second_line = g_iflag) <br/>{< br/> gluunproject (X, Y, 0, <br/> g_modelmatrix, g_projmatrix, g_viewport, <br/> & g_vertex [5] [0], & g_vertex [5] [1], & g_vertex [5] [2]); </P> <p> for (INT I = 0; I <3; I ++) <br/> {<br/> // V3 is the midpoint of V4 and V5 <br/> g_vertex [4] [I] = 2 * g_vertex [3] [I]- g_vertex [5] [I]; </P> <p> // record control node <br/> g_controlvertex [2] [I] = g_vertex [4] [I]; <br/>}</P> <p> glupostredisplay (); <br/>}< br/>

 

You can splice multiple segments in time.

 

 

 

 

 

 

 

 

 

 

 

 

 

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.