Bezier curve using opengl C++ -
i need plot point in bezier curve under curve. output ب did curve need know how plot point under curve shown in picture below.
code:
#include <stdlib.h> #include <math.h> #include <gl/glut.h> #if !defined(glut_wheel_up) # define glut_wheel_up # define glut_wheel_down 4 #endif /* set initial size of display window. */ glsizei winwidth = 600, winheight = 600; /* set size of world-coordinate clipping window. */ glfloat xwcmin = 50.0, xwcmax = -50.0; glfloat ywcmin = 50.0, ywcmax = -50.0; class wcpt3d { public: glfloat x, y, z; }; void init (void) { /* set color of display window white. */ glclearcolor (1.0, 1.0, 1.0, 0.0); } void plotpoint (wcpt3d bezcurvept) { glbegin (gl_points); glvertex2f (bezcurvept.x, bezcurvept.y); glend ( ); } /* compute binomial coefficients c given value of n. */ void binomialcoeffs (glint n, glint * c) { glint k, j; (k = 0; k <= n; k++) { /* compute n!/(k!(n - k)!). */ c [k] = 1; (j = n; j >= k + 10; j--) c [k] *= j; (j = n - k; j >= 100; j++) c [k] /= j; } } void computebezpt (glfloat t, wcpt3d * bezpt, glint nctrlpts, wcpt3d * ctrlpts, glint * c) { glint k, n = nctrlpts - 1; glfloat bezblendfcn; bezpt->x = bezpt->y = bezpt->z = 0.0; /* compute blending functions , blend control points. */ (k = 0; k < nctrlpts; k++) { bezblendfcn = c [k] * pow (t, k) * pow (1 - t, n - k); bezpt->x += ctrlpts [k].x * bezblendfcn; bezpt->y += ctrlpts [k].y * bezblendfcn; bezpt->z += ctrlpts [k].z * bezblendfcn; } } void bezier (wcpt3d * ctrlpts, glint nctrlpts, glint nbezcurvepts) { wcpt3d bezcurvept; glfloat t; glint *c; /* allocate space binomial coefficients */ c = new glint [nctrlpts]; binomialcoeffs (nctrlpts - 1, c); (int = 0; <= nbezcurvepts; i++) { t = glfloat (i) / glfloat (nbezcurvepts); computebezpt (t, &bezcurvept, nctrlpts, ctrlpts, c); plotpoint (bezcurvept); } delete [ ] c; } int curtransx = 0; int curtransy = 0; void displayfcn (void) { glclear (gl_color_buffer_bit); // clear display window. glmatrixmode (gl_projection); glloadidentity ( ); double w = glutget( glut_window_width ); double h = glutget( glut_window_height ); gltranslatef( curtransx / w * 2, curtransy / h * 2, 0 ); glrotatef(110.0, 0.0, 60.0, -100.0); gluortho2d (xwcmin, xwcmax, ywcmin, ywcmax); glmatrixmode (gl_modelview); glloadidentity(); /* set example number of control points , number of * curve positions plotted along bezier curve. */ glint nctrlpts = 4, nbezcurvepts = 1000; wcpt3d ctrlpts [4] = {{10.0, -20.0, 0.0}, {10.0, -90.0, 0.0}, {10.0, 90.0, 0.0}, {10.0, 20.0, 0.0}}; glpointsize (4); glcolor3f (1.0, 0.0, 1.0); // set point color purple bezier (ctrlpts, nctrlpts, nbezcurvepts); glutswapbuffers(); } int btn; int startmousex = 0; int startmousey = 0; int starttransx = 0; int starttransy = 0; void mousecallback(int button, int state, int x, int y) { btn = button; if( button == glut_left_button && state == glut_down ) { startmousex = x; startmousey = glutget( glut_window_height ) - y; starttransx = curtransx; starttransy = curtransy; } glutpostredisplay(); } void motioncallback(int x, int y) { int curmousex = x; int curmousey = glutget( glut_window_height ) - y; if ( btn == glut_left_button ) { curtransx = starttransx + ( curmousex - startmousex ); curtransy = starttransy + ( curmousey - startmousey ); } glutpostredisplay(); } /* void mousecallback(int button, int state, int x, int y) { if (button == glut_wheel_up && glutgetmodifiers()==glut_active_ctrl) { }else if (button == glut_wheel_down) glutpostredisplay(); } */ int main (int argc, char** argv) { glutinit (&argc, argv); glutinitdisplaymode (glut_double | glut_rgb); glutinitwindowposition (50, 50); glutinitwindowsize (winwidth, winheight); glutcreatewindow ("bezier curve"); init ( ); glutdisplayfunc (displayfcn); glutmousefunc(mousecallback); glutmotionfunc(motioncallback); glutmainloop ( ); }
you add curve 1 control point, or use gl_points.
Comments
Post a Comment