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
#include <iostream>
#include "glut.h"
#include <vector>
using namespace std;
GLUnurbsObj* nobj = gluNewNurbsRenderer();
void calcKnots(int n, int k, vector<float> &knots)
{
knots.resize(n + k);
int temp = 0;
for (int i = 0; i < knots.size(); ++i)
{
if (i >= k && knots.size() - i >= k)
temp++;
knots[i] = temp;
}
}
void displaySurface()
{
gluNurbsProperty(nobj, GLU_SAMPLING_TOLERANCE, 25);
GLfloat ctlarray[4][4][4] = {
{ { -0.5, 0.375, -0.5, 1 }, { -0.25, 0.5, -0.5, 1 }, { 0.25, 0.375, -0.5, 1}, { 0.5, 0.25, -0.5, 1 } },
{ { -0.5, 0.375, -0.25, 1 }, { -0.25, 1.0, -0.25, 1 }, { 0.25, 1.0, -0.25, 1 }, { 0.5, 0.25, -0.25, 1 } },
{ { -0.5, 0.375, 0.25, 1 }, { -0.25, 1.0, 0.25, 1 }, { 0.25, 1.0, 0.25, 1 }, { 0.5, 0.25, 0.25, 1 } },
{ { -0.5, 0.375, 0.5, 1 }, { -0.25, 0.375, 0.5, 1 }, { 0.25, 0.375, 0.5, 1 }, { 0.5, 0.25, 0.5, 1 } }
};
//GLfloat ctlarray[3][3][4] = {
// { { -0.25, -0.5, -0.5, 1 }, { 0, 0.5, -0.5, 1 }, { 0.25, -0.5, -0.5, 1 } },
// { { -0.25, -0.5, 0.5, 1 }, { 0, 0.5, 0.5, 1 }, { 0.25, -0.5, 0.5, 1 } },
// { { -0.25, -0.5, 0.75, 1 }, { 0, 0.5, 0.75, 1 }, { 0.25, -0.5, 0.75, 1 } },
//};
glEnable(GL_DEPTH_TEST);
vector<float> knot;
calcKnots(4, 4, knot);
vector<float> knot1;
calcKnots(4, 4, knot1);
glClearColor(0.5, 0.75, 0.75, 0.75);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(0, 0, 0);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glPushMatrix();
glTranslatef(0, -0.5, 0);
glRotatef(45, 0, 1, 0);
gluBeginSurface(nobj);
gluNurbsSurface(nobj, knot.size(), &knot[0], knot1.size(), &knot1[0], 4*4, 4, &ctlarray[0][0][0], 4,4, GL_MAP2_VERTEX_4);
gluEndSurface(nobj);
glPopMatrix();
glColor3f(1, 0, 0);
glPointSize(5);
glPushMatrix();
glRotatef(45, 0, 1, 0);
glBegin(GL_POINTS);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
glVertex3f(ctlarray[i][j][0], ctlarray[i][j][1], ctlarray[i][j][2]);
}
glEnd();
glPopMatrix();
glutPostRedisplay();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(480, 480);
glutInitWindowPosition(100, 100);
glutCreateWindow(" ");
glutDisplayFunc(displaySurface);
glutMainLoop();
}