include glut include Windows include cstdio include iostream void gene

  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
#include "glut.h"
#include <Windows.h>
#include <cstdio>
#include <iostream>
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;
}
GLUnurbs* nrbsObject;
float ps[4] = {-100, -200, -300, 0};
float ps1[4] = {100, -200, 300, 0};
float dfvl[4] = {3, 0.5, 0.5, 1};
void init()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_AUTO_NORMAL);
glLightfv(GL_LIGHT0, GL_POSITION, ps);
glLightfv(GL_LIGHT0, GL_DIFFUSE, dfvl);
glLightfv(GL_LIGHT1, GL_POSITION, ps1);
glLightfv(GL_LIGHT1, GL_DIFFUSE, dfvl);
glShadeModel(GL_SMOOTH);
//glColor3f(1, 0.5, 0.5);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nrbsObject = gluNewNurbsRenderer();
gluNurbsProperty(nrbsObject, GLU_SAMPLING_TOLERANCE, 5.0);
glPointSize(5);
glTranslatef(-0.5, 0, 0);
glRotatef(75, 1.0, 0.0, 0.0);
}
float knot1[30] = {0.0f,0.0f,1.0f,2.0f,3.0f,4.0f,5.0f,5.0f};
float knot2[30] = {0.0f,0.0f,1.0f,1.0f};
int pnt1 = 6;
int pnt2 = 2;
GLfloat ctlarray[6][2][4] = {0.0, 0.0, 0.3, 1.0,
0.0, 0.2, 0.3, 1.0,
0.2, 0.0, 0.0, 1.0,
0.2, 0.2, 0.0, 1.0,
0.4, 0.0, 0.3, 1.0,
0.4, 0.2, 0.3, 1.0,
0.6, 0.0, 0.0, 1.0,
0.6, 0.2, 0.0, 1.0,
0.8, 0.0, 0.3, 1.0,
0.8, 0.2, 0.3, 1.0,
1.0, 0.0, 0.0, 1.0,
1.0, 0.2, 0.0, 1.0
};
int step = 2;
int step2 = 1;
void Display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int knot1size = 0;
int knot2size = 0;
generateKnot(pnt1, step, knot1, knot1size);
generateKnot(pnt2, step2, 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, pnt2 * 4, 4, &ctlarray[0][0][0], step + 1, step2 + 1, GL_MAP2_VERTEX_4);
gluEndSurface(nrbsObject);
glutPostRedisplay();
glutSwapBuffers();
glTranslatef(0.5, 0, 0);
glRotatef(0.1, 0.0, 0.0, 1.0);
glTranslatef(-0.5, 0, 0);
}
void main()
{
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(480, 480);
glutInitWindowPosition(100, 100);
glutCreateWindow(" ");
init();
glutDisplayFunc(Display);
glutMainLoop();
}