rendered paste body#include <GL/gl.h>#include <GL/glu.h>#include <GL/glut.h>#include <math.h>#include <vector>using namespace std;typedef struct _vec3{ double x, y, z;} vec3;int wwidth, wheight;vector<vec3> bcontrols;vector<vec3> bpoints;vector<double> bknots;void affichageGL(){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluOrtho2D(0, wwidth, 0, wheight);*/ glColor3f(1, 1, 1); glBegin(GL_LINE_STRIP); for(unsigned int i(0); i < bpoints.size(); i++) glVertex3d(bpoints[i].x, bpoints[i].y, bpoints[i].z); glEnd(); glPointSize(4.0); glColor3f(0.5, 0, 0); glBegin(GL_POINTS); for(unsigned int i(0); i < bcontrols.size(); i++) glVertex3d(bcontrols[i].x, bcontrols[i].y, bcontrols[i].z); glEnd(); glPointSize(1.0); glutSwapBuffers();}void redimGL(int _w, int _h){ wwidth = _w; wheight = _h; glViewport(0, 0, _w, _h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, wwidth, 0, wheight); glMatrixMode(GL_MODELVIEW); glLoadIdentity();}double CoeffCoxDeBoor(int _i, int _k, double _t){ if(fabs(bknots[_i] - bknots[_i + _k]) < 1e-06) return 0; return (_t - bknots[_i]) / (bknots[_i + _k] - bknots[_i]);}#include <iostream>double CoxDeBoor(int _i, int _k, double _t){ if(_k==1) { if(bknots[_i] <= _t && _t <= bknots[_i+1] ) { return 1.0; } return 0.0; } float Den1 = bknots[_i+_k-1] - bknots[_i]; float Den2 = bknots[_i+_k] - bknots[_i+1]; float Eq1=0,Eq2=0; if(Den1>0) { Eq1 = ((_t-bknots[_i]) / Den1) * CoxDeBoor(_i,_k-1,_t); } if(Den2>0) { Eq2 = (bknots[_i+_k]-_t) / Den2 * CoxDeBoor(_i+1,_k-1,_t); } return Eq1+Eq2; /*if(bknots.size() == 0) return 0.0; //if(_i == if(_k == 1) { if(_t >= bknots[_i] && _t <= bknots[_i + 1]) return 1.0; return 0.0; } return CoxDeBoor(_i, _k - 1, _t) * CoeffCoxDeBoor(_i, _k, _t) + (1.0 - CoxDeBoor(_i + 1, _k - 1, _t)) * CoeffCoxDeBoor(_i + 1, _k, _t);*/ //return CoxDeBoor(_i, _k - 1, _t) * (_t - bknots[_i]) / (bknots[_i + _k - 1] - bknots[_i]) + CoxDeBoor(_i + 1, _k - 1, _t) * (bknots[_i + _k] - _t) / (bknots[_i + _k] - bknots[_i + 1]);}void initGL(){ vec3 p; glClearColor(0.5, 0.5, 0.5, 1.0); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); bcontrols.clear(); bpoints.clear(); bknots.clear(); p.x = 50; p.y = 50; p.z = 0; bcontrols.push_back(p); p.x = 100; p.y = 350; p.z = 0; bcontrols.push_back(p); p.x = 150; p.y = 450; p.z = 0; bcontrols.push_back(p); p.x = 200; p.y = 250; p.z = 0; bcontrols.push_back(p); p.x = 250; p.y = 350; p.z = 0; bcontrols.push_back(p); p.x = 300; p.y = 250; p.z = 0; bcontrols.push_back(p); p.x = 450; p.y = 450; p.z = 0; bcontrols.push_back(p); bknots.push_back(0); bknots.push_back(0); bknots.push_back(0); bknots.push_back(0); bknots.push_back(1); bknots.push_back(2); bknots.push_back(3); bknots.push_back(4); bknots.push_back(4); bknots.push_back(4); bknots.push_back(4); double c; for(int i(0); i < 20; i++) { p.x = 0; p.y = 0; p.z = 0; for(int j(0); j < bcontrols.size(); j++) { c = CoxDeBoor(j, 4, (i * bknots.back() / 19.0)); p.x += bcontrols[j].x * c; p.y += bcontrols[j].y * c; p.z += bcontrols[j].z * c; } bpoints.push_back(p); }}int main(int _argc, char ** _argv){ int win_x, win_y; wwidth = 500; wheight = 500; glutInit(&_argc, _argv); win_x = (glutGet(GLUT_SCREEN_WIDTH) - wwidth)/2; win_y = (glutGet(GLUT_SCREEN_HEIGHT) - wheight)/2; glutInitWindowSize(wwidth, wheight); glutInitWindowPosition(win_x, win_y); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("BSpline"); glutDisplayFunc(affichageGL); glutReshapeFunc(redimGL); initGL(); glutMainLoop(); return 0;}