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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <iostream>
#include "glut.h"
#include <vector>
using namespace std;
GLUnurbsObj* nobj = gluNewNurbsRenderer();
GLfloat ctlarrayTemp[3][2][4] = { 0.5, 0.0, -0.3, 1.0,
0.5, 0.0, 0.3, 1.0,
0.0, 0.866*0.55, -0.3*0.55, 1.0*0.55,
0.0, 0.866*0.55, 0.3*0.55, 1.0*0.55,
-0.5, 0.0, -0.3, 1.0,
-0.5, 0.0, 0.3, 1.0 };
GLfloat TexP[] = { 1.0, 0.0, 0.0, 0.0 };
GLfloat TexP1[] = { 0.0, 1.0, 0.0, 0.0 };
GLfloat TexP2[] = { 1.0, 0.0, 1.0, 0.0 };
GLfloat TexI[] = { 255, 0, 0, 255, 0, 0, 255, 255, 0, 255, 255, 0, 0, 255, 0, 0, 255, 0, 0, 0, 255, 0, 0, 255 };
GLfloat texpts[2][2][2] = { 0, 0, 0, 2, 2, 0, 2, 2 };
GLUnurbsObj* theNurb;
GLUquadricObj* theqw;
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();
}
void init()
{
glClearColor(0.5, 0.75, 0.75, 1);
theNurb = gluNewNurbsRenderer();
theqw = gluNewQuadric();
glEnable(GL_DEPTH_TEST);
gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage1D(GL_TEXTURE_1D, 0, 3, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, TexI);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glEnable(GL_TEXTURE_1D);
}
void displayFunc3()
{
GLfloat knot[] = { 0, 0, 0, 1, 1, 1 };
GLfloat knot1[] = { 0, 0, 1, 1 };
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glRotatef(1, 1, 1, 1);
glColor3f(1, 0, 0);
glEnable(GL_TEXTURE_GEN_S);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGenfv(GL_S, GL_OBJECT_PLANE, TexP1);
gluSphere(theqw, 0.3, 50, 50);
gluBeginSurface(theNurb);
gluNurbsSurface(theNurb, 6, knot, 4, knot1, 8, 4, &ctlarrayTemp[0][0][0], 3, 2, GL_MAP2_VERTEX_4);
gluEndSurface(theNurb);
glutPostRedisplay();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(480, 480);
glutInitWindowPosition(100, 100);
glutCreateWindow(" ");
init();
glutDisplayFunc(displayFunc3);
glutMainLoop();
}