opengl - How to implement adaptive subdivision algorithm for curve in C -
my homework write c program opengl/glut which, after getting groups of 4 points mouse click (points 3 coordinates), should draw bezier curve adaptive algorithm. @ theoretical level it's clear how algorithm works don't know how put in c code. mean @ lesson saw 4 control points have shape similar "trapeze" , algorithm calculates 2 "heights" , checks if satisfy tollerance. problem user might click everywhere in screen , points might not have trapeze-like shape...so, can start from? have
this cole have written, called each time control point added:
if (beziermode == casteljau_adaptive) { glcolor3f (0.0f, 0.8f, 0.4f); /* draw adaptive casteljau curve in green */ for(i=0; i+3<numcv; += 3) adaptivedecasteljau3(cv, i, 0.01); } void adaptivedecasteljau3(float cv[max_cv][3], int position, float tolerance) { float x01 = (cv[position][0] + cv[position+1][0]) / 2; float y01 = (cv[position][1] + cv[position+1][1]) / 2; float x12 = (cv[position+1][0] + cv[position+2][0]) / 2; float y12 = (cv[position+1][1] + cv[position+2][1]) / 2; float x23 = (cv[position+2][0] + cv[position+3][0]) / 2; float y23 = (cv[position+2][1] + cv[position+3][1]) / 2; float x012 = (x01 + x12) / 2; float y012 = (y01 + y12) / 2; float x123 = (x12 + x23) / 2; float y123 = (y12 + y23) / 2; float x0123 = (x012 + x123) / 2; float y0123 = (y012 + y123) / 2; float dx = cv[3][0] - cv[0][0]; float dy = cv[3][1] - cv[0][1]; float d2 = fabs(((cv[1][0] - cv[3][0]) * dy - (cv[1][1] - cv[3][1]) * dx)); float d3 = fabs(((cv[2][0] - cv[3][0]) * dy - (cv[2][1] - cv[3][1]) * dx)); if((d2 + d3)*(d2 + d3) < tolerance * (dx*dx + dy*dy)) { glbegin(gl_line_strip); glvertex2f(x0123, y0123); glend(); return; } float tmpleft[4][3]; float tmpright[4][3]; tmpleft[0][0] = cv[0][0]; tmpleft[0][1] = cv[0][1]; tmpleft[1][0] = x01; tmpleft[1][1] = y01; tmpleft[2][0] = x012; tmpleft[2][1] = y012; tmpleft[3][0] = x0123; tmpleft[3][1] = y0123; tmpright[0][0] = x0123; tmpright[0][1] = y0123; tmpright[1][0] = x123; tmpright[1][1] = y123; tmpright[2][0] = x23; tmpright[2][1] = y23; tmpright[3][0] = cv[3][0]; tmpright[3][1] = cv[3][1]; adaptivedecasteljau3(tmpleft, 0, tolerance); adaptivedecasteljau3(tmpright, 0, tolerance); }
and nothing drawn. have idea?
the begin / end should engulf whole loop, not being inside each isolated vertex !
Comments
Post a Comment