include glut include Windows include cstdio include iostream GLUnurbs

  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
#include "glut.h"
#include <Windows.h>
#include <cstdio>
#include <iostream>
GLUnurbs* nrbsObject;
float ps[4] = {10, 20, 30, 0};
float dfvl[4] = {1, 1, 1, 1};
void init()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_AUTO_NORMAL);
glLightfv(GL_LIGHT0, GL_POSITION, ps);
glLightfv(GL_LIGHT0, GL_DIFFUSE, dfvl);
glShadeModel(GL_SMOOTH);
glColor3f(1, 1, 1);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nrbsObject = gluNewNurbsRenderer();
gluNurbsProperty(nrbsObject, GLU_SAMPLING_TOLERANCE, 25.0);
glPointSize(10);
}
void generateKnot(int pointCount, int degree, float* knot, int& knotSize)
{
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;
}
}
knotSize = n + k + 1;
}
float knot1[30];
float knot2[30];
int pnt1 = 4;
int pnt2 = 4;
float ctlarray[4][4][4] = {0.10, 0.0, 0.0, 1.0,
0.20, 0.0, 0.0, 1.0,
0.30, 0.0, 0.0, 1.0,
0.40, 0.0, 0.0, 1.0,
0.10, 0.10, 0.0, 1.0,
0.20, 0.1, 0.5, 1.0,
0.30, 0.1, 0.0, 1.0,
0.40, 0.1, 0.0, 1.0,
0.10, 0.2, 0.0, 1.0,
0.20, 0.2, 0.0, 1.0,
0.30, 0.2, 0.0, 1.0,
0.40, 0.2, 0.0, 1.0,
};
int step = 2;
void Display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int knot1size = 0;
int knot2size = 0;
generateKnot(pnt1, step, knot1, knot1size);
generateKnot(pnt2, step, knot2, knot2size);
for(int i = 0; i < pnt1; ++i)
for (int j = 0; j < pnt2; ++j)
{
glBegin(GL_POINTS);
glVertex4f(ctlarray[i][j][0], ctlarray[i][j][1], ctlarray[i][j][2], ctlarray[i][j][3]);
glEnd();
}
gluBeginSurface(nrbsObject);
gluNurbsSurface(nrbsObject, knot1size, (GLfloat*)knot1, knot2size, (GLfloat*)knot2, 4 * 4, 4, &ctlarray[0][0][0], step + 1, step + 1, GL_MAP2_VERTEX_4);
gluEndSurface(nrbsObject);
glRotatef(0.1, 1.0, 1.0, 1.0);
glutPostRedisplay();
glutSwapBuffers();
}
void main()
{
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(480, 480);
glutInitWindowPosition(100, 100);
glutCreateWindow(" dfa");
init();
glutDisplayFunc(Display);
glutMainLoop();
}