include glut include cstdio include iostream using namespace std GLUnu

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
#include "glut.h"
#include <cstdio>
#include <iostream>
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;
}