All pastes #813195 Raw Edit

houlala

public cpp v1 · immutable
·published 2007-12-12 10:30 UTC
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);		    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]);}double CoxDeBoor(int _i, int _k, double _t){	double d1, d2, r;		if(_k == 1)	{		if(_t >= bknots[_i] && _t <= bknots[_i + 1])			return 1.0;				return 0.0;	}		r  = 0;	d1 = bknots[_i + _k - 1] - bknots[_i    ];	d2 = bknots[_i + _k    ] - bknots[_i + 1];		if(d1 > 0.0)		r += (_t - bknots[_i]) / d1 * CoxDeBoor(_i, _k - 1, _t);		if(d2 > 0.0)		r += (bknots[_i + _k] - _t) / d2 * CoxDeBoor(_i + 1, _k - 1, _t);		return r;	}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;}