#include "glut.h" #include #include using namespace std; GLUnurbsObj* nobj; struct pos { float x, y, z; }; int degree = 3; int knotSize = 7; GLfloat knot[20]; // int pointCount = 4; //GLfloat ctlarray[][3] = {{-0.9, -0.8, 0.0}, {-0.2, 0.8, 0.0}, {0.2, -0.5, 0.0}, {0.9, 0.8, 0.0}}; int pointCount = 8; GLfloat ctlarray[][3] = {{-0.9, 0.3, 0.0}, {-0.8, 0.8, 0.0}, {-0.5, 0.6, 0.0}, {-0.4, -0.3, 0.0}, {0.0, -0.6, 0.0}, {0.4, -0.3, 0.0}, {0.7, 0.8, 0.0}, {0.9, 0.8, 0.0}}; void generateKnot() { int k = degree + 1; int n = pointCount - 1; for (int i = 1; i <= n + k + 1; ++i) { if (i <= k) { knot[i - 1] = 0; } else if(i <= n + 1) { knot[i - 1] = i - k; } else { knot[i - 1] = n - k + 2; } std::cout << knot[i] << " "; } cout << endl; knotSize = n + k + 1; } void init(void) { glClearColor(1.0, 1.0, 1.0, 1.0); nobj = gluNewNurbsRenderer(); gluNurbsProperty( nobj, GLU_SAMPLING_TOLERANCE, 25.0); } void motFunction(int x, int y) { ctlarray[4][0] = x / 100.0 - 4; ctlarray[4][1] = 4 - y / 100.0; glutPostRedisplay(); } void Display() { glClear(GL_COLOR_BUFFER_BIT); glLineWidth(3.0); glColor3f(0.0, 0.3, 1.0); degree = 1; generateKnot(); gluNurbsCurve(nobj, knotSize, knot, 3, &ctlarray[0][0], degree + 1, GL_MAP1_VERTEX_3); glColor3f(1.0, 0.3, 1.0); degree = 3; generateKnot(); gluNurbsCurve(nobj, knotSize, knot, 3, &ctlarray[0][0], degree + 1, GL_MAP1_VERTEX_3); glPointSize(4.0); glColor3f(0.0, 0.0, 1.0); glBegin(GL_POINTS); for (int i = 0; i < pointCount; ++i) glVertex3fv(ctlarray[i]); glEnd(); glFlush(); } int main() { glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE); glutInitWindowSize(800, 800); glutInitWindowPosition(0,0); glutCreateWindow("bezier"); init(); glutDisplayFunc(Display); glutMotionFunc(motFunction); glutMainLoop(); return 0; }