c++ - opengl dynamic bezier curve has gaps - glEvalCoord1f(); -
i have program can pick ten points , bezier curve calculated points. seems work pretty perfect, shown curve has gaps. used gl_line_strip
how possible points not connected?
i figured out, small u
in glevalcoord1f(u);
makes gaps smaller. how u
depending on controlpoints , window propperties in glevalcoord1f(u);
?
[edit] added screen thicker lines:
#include <iostream> #include <stdlib.h> #include <string> #include <fstream> #include <math.h> #include <time.h> #include <gl/glut.h> //added gl prefix ubuntu compatibility #define maxp 10 glint nnumpoints = 0; glfloat ctrlpoints[10][3]; gldouble mouseogl[3] = {0.0,0.0,0.0}; glint mousex, mousey; bool mousepressed = false; void getoglpos(int x, int y); void init(void) { glclearcolor(1.0, 1.0, 1.0, 0.0); glshademodel(gl_flat); // enable evaluator glenable(gl_map1_vertex_3); glenable(gl_depth); } void display(void) { int i; glclear(gl_color_buffer_bit | gl_depth_buffer_bit); glcolor3f(0.0, 0.0, 1.0); if(mousepressed && nnumpoints < maxp){ getoglpos(mousex, mousey); std::cout << mouseogl[0] << " # " << mouseogl[1] << " # " << mouseogl[2] << " # " << std::endl; nnumpoints++; ctrlpoints[nnumpoints-1][0] = mouseogl[0]; ctrlpoints[nnumpoints-1][3] = mouseogl[1]; ctrlpoints[nnumpoints-1][2] = mouseogl[2]; } //curves if( nnumpoints == maxp ){ glmap1f(gl_map1_vertex_3, // type of data generated 0.0f, // lower u range 1.0f, // upper u range 3, // distance between points in data 3: ...z-x-y-z... nnumpoints, // number of control points &ctrlpoints[0][0]); // start point glbegin(gl_line_strip); float max = pow(maxp,4)*2; //accuracy of pint calulation? (i = 0; <= max; i++) glevalcoord1f((glfloat) (i/max)); //high value avoid gaps?!? glend(); } //controllpoints: glpointsize(5.0); glcolor3f(1.0, 0.0, 0.0); glbegin(gl_points); (i = 0; < nnumpoints; i++) glvertex3fv(&ctrlpoints[i][0]); glend(); //lines glcolor3f(0.0, 1.0, 0.0); glbegin(gl_line_strip); (i = 0; < nnumpoints; i++) glvertex3fv(&ctrlpoints[i][0]); glend(); if( nnumpoints == maxp ){nnumpoints = 0;} glflush(); } void reshape(int w, int h) { glviewport(0, 0, (glsizei) w, (glsizei) h); glmatrixmode(gl_projection); glloadidentity(); //keep aspect ratio: if (w <= h) glortho(-2.0, 2.0, -2.0*(glfloat)h/(glfloat)w, 2.0*(glfloat)h/(glfloat)w, -2.0, 2.0); else glortho(-2.0*(glfloat)w/(glfloat)h, 2.0*(glfloat)w/(glfloat)h, -2.0, 2.0, -2.0, 2.0); glmatrixmode(gl_modelview); glloadidentity(); } //handle click events of mouse void mymouse(int button, int state, int x, int y) { //mouse coords gl coords mousex = x; mousey = y; switch (button) { case glut_left_button: if(state == glut_up){ //on release left mouse button std::cout << x << " * "<< y << std::endl; mousepressed = true; glutpostredisplay(); //redisplay , calculate gl coords } else { mousepressed = false; } break; } } // detailed information: // http://nehe.gamedev.net/article/using_gluunproject/16013/ void getoglpos(int x, int y) { //init vars: glint viewport[4]; gldouble modelview[16]; gldouble projection[16]; glfloat winx, winy, winz; gldouble posx, posy, posz; //get gl specs glgetdoublev( gl_modelview_matrix, modelview ); //get modelmatrix glgetdoublev( gl_projection_matrix, projection ); //get projection matrix glgetintegerv( gl_viewport, viewport ); //get viewport values //calculate gl mouseposition winx = (float)x; winy = (float)viewport[3] - (float)y; glreadpixels( x, int(winy), 1, 1, gl_depth_component, gl_float, &winz ); gluunproject( winx, winy, winz, modelview, projection, viewport, &posx, &posy, &posz); mouseogl[0] = posx; mouseogl[1] = posy; mouseogl[2] = posz; } int main(int argc, char** argv) { glutinit(&argc, argv); glutinitdisplaymode (glut_single | glut_rgb); glutinitwindowsize (600, 600); glutinitwindowposition (100, 100); glutcreatewindow (argv[0]); init (); glutdisplayfunc(display); glutreshapefunc(reshape); glutmousefunc(mymouse); glutmainloop(); return 0; }
try zeroing out control point z coords:
#include <gl/glut.h> #include <cmath> const unsigned int maxp = 10; glint nnumpoints = 0; glfloat ctrlpoints[10][3]; gldouble mouseogl[3] = {0.0,0.0,0.0}; glint mousex, mousey; bool mousepressed = false; void getoglpos(int x, int y); void display(void) { glclearcolor(1.0, 1.0, 1.0, 0.0); glclear(gl_color_buffer_bit | gl_depth_buffer_bit); int w = glutget( glut_window_width ); int h = glutget( glut_window_height ); glviewport(0, 0, w, h); glmatrixmode(gl_projection); glloadidentity(); double ar = w / (double)h; glortho(-2.0*ar, 2.0*ar, -2.0, 2.0, -2.0, 2.0); glmatrixmode(gl_modelview); glloadidentity(); if(mousepressed && nnumpoints < maxp) { getoglpos(mousex, mousey); nnumpoints++; ctrlpoints[nnumpoints-1][0] = mouseogl[0]; ctrlpoints[nnumpoints-1][1] = mouseogl[1]; // magic happens: ctrlpoints[nnumpoints-1][2] = 0; } //curves glcolor3f(0.0, 0.0, 1.0); if( nnumpoints == maxp ) { glmap1f(gl_map1_vertex_3, // type of data generated 0.0f, // lower u range 1.0f, // upper u range 3, // distance between points in data 3: ...z-x-y-z... nnumpoints, // number of control points &ctrlpoints[0][0]); // start point glbegin(gl_line_strip); float max = pow(maxp,4.0)*2; //accuracy of pint calulation? (int = 0; <= max; i++) glevalcoord1f((glfloat) (i/max)); //high value avoid gaps?!? glend(); } //controllpoints: glpointsize(5.0); glcolor3f(1.0, 0.0, 0.0); glbegin(gl_points); (int = 0; < nnumpoints; i++) glvertex3fv(&ctrlpoints[i][0]); glend(); //lines glcolor3f(0.0, 1.0, 0.0); glbegin(gl_line_strip); (int = 0; < nnumpoints; i++) glvertex3fv(&ctrlpoints[i][0]); glend(); if( nnumpoints == maxp ){nnumpoints = 0;} glflush(); } //handle click events of mouse void mymouse(int button, int state, int x, int y) { //mouse coords gl coords mousex = x; mousey = y; switch (button) { case glut_left_button: if(state == glut_up){ //on release left mouse button mousepressed = true; glutpostredisplay(); //redisplay , calculate gl coords } else { mousepressed = false; } break; } } // detailed information: // http://nehe.gamedev.net/article/using_gluunproject/16013/ void getoglpos(int x, int y) { //init vars: glint viewport[4]; gldouble modelview[16]; gldouble projection[16]; glfloat winx, winy, winz; gldouble posx, posy, posz; //get gl specs glgetdoublev( gl_modelview_matrix, modelview ); //get modelmatrix glgetdoublev( gl_projection_matrix, projection ); //get projection matrix glgetintegerv( gl_viewport, viewport ); //get viewport values //calculate gl mouseposition winx = (float)x; winy = (float)viewport[3] - (float)y; glreadpixels( x, int(winy), 1, 1, gl_depth_component, gl_float, &winz ); gluunproject( winx, winy, winz, modelview, projection, viewport, &posx, &posy, &posz); mouseogl[0] = posx; mouseogl[1] = posy; mouseogl[2] = posz; } int main(int argc, char** argv) { glutinit(&argc, argv); glutinitdisplaymode (glut_single | glut_rgb); glutinitwindowsize (600, 600); glutinitwindowposition (100, 100); glutcreatewindow (argv[0]); glshademodel(gl_flat); // enable evaluator glenable(gl_map1_vertex_3); glenable(gl_depth); glutdisplayfunc(display); glutmousefunc(mymouse); glutmainloop(); return 0; }
Comments
Post a Comment