include iostream include glut include vector 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
110
111
112
113
#include <iostream>
#include "glut.h"
#include <vector>
using namespace std;
GLUnurbsObj* nobj = gluNewNurbsRenderer();
void calcKnots(int n, int k, vector<float> &knots, bool isOpen)
{
knots.resize(n + k);
if (isOpen)
{
int temp = 0;
for (int i = 0; i < knots.size(); ++i)
{
if (i >= k && knots.size() - i >= k)
temp++;
knots[i] = temp;
}
}
else
{
for (int i = 0; i < knots.size(); ++i)
{
knots[i] = i;
}
}
}
void displayFunc()
{
gluNurbsProperty(nobj, GLU_SAMPLING_TOLERANCE, 25);
GLfloat ctlarray[4][3] = { { -0.9, -0.8, 0 }, { -0.2, 0.8, 0 }, { 0.2, -0.5, 0 }, { 0.9, 0.8, 0 } };
vector<float> knot;
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
calcKnots(4, 2, knot, true);
glColor3f(0.7, 0.7, 0);
gluNurbsCurve(nobj, knot.size(), &knot[0],3, &ctlarray[0][0], 2, GL_MAP1_VERTEX_3);
calcKnots(4, 3, knot, true);
glColor3f(0.7, 0.7, 0.0);
gluNurbsCurve(nobj, knot.size(), &knot[0], 3, &ctlarray[0][0], 3, GL_MAP1_VERTEX_3);
calcKnots(4, 4, knot, true);
glColor3f(0.7, 0.7, 0.0);
gluNurbsCurve(nobj, knot.size(), &knot[0], 3, &ctlarray[0][0], 4, GL_MAP1_VERTEX_3);
calcKnots(4, 2, knot, false);
glColor3f(0.2, 0, 0);
gluNurbsCurve(nobj, knot.size(), &knot[0], 3, &ctlarray[0][0], 2, GL_MAP1_VERTEX_3);
calcKnots(4, 3, knot, false);
glColor3f(0, 0.2, 0);
gluNurbsCurve(nobj, knot.size(), &knot[0], 3, &ctlarray[0][0], 3, GL_MAP1_VERTEX_3);
calcKnots(4, 4, knot, false);
glColor3f(0, 0, 0.2);
gluNurbsCurve(nobj, knot.size(), &knot[0], 3, &ctlarray[0][0], 4, GL_MAP1_VERTEX_3);
glColor3f(0,0,0);
glPointSize(8);
glBegin(GL_POINTS);
for (int i = 0; i < 4; ++i)
{
glVertex3f(ctlarray[i][0], ctlarray[i][1], ctlarray[i][2]);
}
glEnd();
glutPostRedisplay();
glutSwapBuffers();
}
void displayFunc2()
{
gluNurbsProperty(nobj, GLU_SAMPLING_TOLERANCE, 25);
GLfloat ctlarray1[4][3] = { { -0.9, -0.8, 0 }, { -0.2, 0.8, 0 }, { 0.2, -0.5, 0 }, { 0.9, 0.8, 0 } };
GLfloat ctlarray2[5][3] = { { -0.9f, -0.8f, 0 }, { -0.2f, 0.8f, 0 }, { -0.2f, 0.8f, 0 }, { 0.2f, -0.5f, 0 }, { 0.9f, 0.8f, 0 } };
GLfloat ctlarray3[6][3] = { { -0.9f, -0.8f, 0 }, { -0.2f, 0.8f, 0 }, { -0.2f, 0.8f, 0 }, { -0.2f, 0.8f, 0 }, { 0.2f, -0.5f, 0 }, { 0.9f, 0.8f, 0 } };
vector<float> knot;
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
calcKnots(4, 4, knot, true);
glColor3f(0.7, 0.7, 0);
gluNurbsCurve(nobj, knot.size(), &knot[0], 3, &ctlarray1[0][0], 4, GL_MAP1_VERTEX_3);
calcKnots(5, 4, knot, true);
glColor3f(0.7, 0.7, 0.0);
gluNurbsCurve(nobj, knot.size(), &knot[0], 3, &ctlarray2[0][0], 4, GL_MAP1_VERTEX_3);
calcKnots(6, 4, knot, true);
glColor3f(0.7, 0.7, 0.0);
gluNurbsCurve(nobj, knot.size(), &knot[0], 3, &ctlarray3[0][0], 4, GL_MAP1_VERTEX_3);
GLfloat points[4][3]{{ -0.9f, -0.8f, 0 }, { -0.2f, 0.8f, 0 }, { 0.2f, -0.5f, 0 }, { 0.9f, 0.8f, 0 } };
glColor3f(0, 0, 0);
glPointSize(8);
glBegin(GL_POINTS);
for (int i = 0; i < 4; ++i)
{
glVertex3f(points[i][0], points[i][1], points[i][2]);
}
glEnd();
glutPostRedisplay();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(480, 480);
glutInitWindowPosition(100, 100);
glutCreateWindow(" ");
glutDisplayFunc(displayFunc2);
glutMainLoop();
}