forked from tienpt/OpenGL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbSplines.c
370 lines (330 loc) · 9.39 KB
/
bSplines.c
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* bSplines.c
*
* This program displays B-spline functions of orders 1, 2, 3 and 4.
*
* Interaction:
* Press the up/down arrow keys to cycle between order 1 through 4.
* Press space to select a knot points.
* Press the left/right arrow keys to move the selected knot point.
* Press delete to reset.
*/
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
static int countKnots = 0, splineOrder = 1;
int font=(int)GLUT_BITMAP_8_BY_13;
GLfloat knots[9] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0};
// Separate integer array to store 10 times the knot values. The values in
// this array are then increased/decreased by 1 using the arrow keys. Using
// integer values avoids round-off error problems in the increaseKnot and
// decreaseKnot routines.
GLint knotsTimes10[9] ={ 0, 10, 20, 30, 40, 50, 60, 70, 80};
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
}
// Draw text.
void writeBitmapString(void *font, char *string, float rasterx, float rastery)
{
int i = 0;
float rx = rasterx, ry = rastery;
glRasterPos2f(rx, ry);
while (string[i] != '\0') {
glutBitmapCharacter(font, string[i]);
rx+=glutBitmapWidth(font, string[i]);
i++;
}
}
// Changes knots array values according to the knotTimes10 array values.
void changeKnots(void)
{
int i;
for (i = 0; i < 9; i++)
knots[i] = ((float)knotsTimes10[i])/10.0;
}
// Function to increase value of a knot.
// Increments are made first to knotsTimes10 to work with integers and
// avoid round-off error problems.
void increaseKnot(int i)
{
if ( (i < 8) )
{
if (knotsTimes10[i] < knotsTimes10[i+1]) knotsTimes10[i] += 1;
else
{
increaseKnot(i+1);
if (knotsTimes10[i] < knotsTimes10[i+1]) knotsTimes10[i] += 1;
}
}
if ( (i == 8 ) && ( knotsTimes10[i] < 80) ) knotsTimes10[i] += 1;
changeKnots();
}
// Function to decrease value of a knot.
// Decrements are made first to knotsTimes10 to work with integers and
// avoid round-off error problems.
void decreaseKnot(int i)
{
if ( (i > 0) )
{
if (knotsTimes10[i] > knotsTimes10[i-1]) knotsTimes10[i] -= 1;
else
{
decreaseKnot(i-1);
if (knotsTimes10[i] > knotsTimes10[i-1]) knotsTimes10[i] -= 1;
}
}
if ( (i == 0 ) && ( knotsTimes10[i] > 0) ) knotsTimes10[i] -= 1;
changeKnots();
}
void resetKnots(void)
{
int i;
for (i = 0; i < 9; i++)
knotsTimes10[i] = 10*i;
changeKnots();
countKnots = 0;
}
// Recursive computation of B-spline functions.
float Bspline(int i, int m, float u)
{
float coef1, coef2;
if ( m == 1 )
{
if ( ( knots[i] < u ) && ( u <= knots[i+1] ) ) return 1.0;
else return 0.0;
}
else
{
if ( knots[i+m-1] == knots[i] ) coef1 = 0;
else coef1 = (u - knots[i])/(knots[i+m-1] - knots[i]);
if ( knots[i+m] == knots[i+1] ) coef2 = 0;
else coef2 = (knots[i+m] - u)/(knots[i+m] - knots[i+1]);
return ( coef1 * Bspline(i, m-1, u) + coef2 * Bspline(i+1 ,m-1 ,u) );
}
}
// Draw a B-spline function
void drawSpline(int i, int m)
{
float x;
// Drawing are scaled by factor of 3 in the y-direction.
// Special case to handle order 1 to avoid vertical edges.
if (m == 1)
{
glBegin(GL_LINE_STRIP);
for ( x = knots[i]; x < knots[i+1]; x+=0.005 )
glVertex3f( -4.0 + x, 1.0, 0.0 );
glEnd();
}
else
{
glBegin(GL_LINE_STRIP);
for ( x = knots[i]; x <= knots[i+m]; x+=0.005 )
glVertex3f( -4.0 + x, 3*Bspline(i, m, x) - 2.0, 0.0 );
glEnd();
}
}
void display(void)
{
int i;
char theStringOrder1[] = "Order 1 B-splines";
char theStringOrder2[] = "Order 2 B-splines";
char theStringOrder3[] = "Order 3 B-splines";
char theStringOrder4[] = "Order 4 B-splines";
char theString0[] = "0";
char theString1[] = "1";
char theString2[] = "2";
char theString3[] = "3";
char theString4[] = "4";
char theString5[] = "5";
char theString6[] = "6";
char theString7[] = "7";
char theString8[] = "8";
char theStringKnotValues[] = "Knot Values";
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.0, 0.5, 0.0);
// Write spline order.
glColor3f(1.0, 1.0, 1.0);
switch (splineOrder)
{
case 1:
writeBitmapString((void*)font, theStringOrder1, -1.05, 3.5);
break;
case 2:
writeBitmapString((void*)font, theStringOrder2, -1.05, 3.5);
break;
case 3:
writeBitmapString((void*)font, theStringOrder3, -1.05, 3.5);
break;
case 4:
writeBitmapString((void*)font, theStringOrder4, -1.05, 3.5);
break;
default:
break;
}
// Draw successive B-spline functions for the chosen order.
glEnable (GL_LINE_SMOOTH);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
for (i = 0; i < 9 - splineOrder; i++ )
{
switch (i)
{
case 0:
glColor3f(1.0, 0.0, 0.0);
break;
case 1:
glColor3f(0.0, 1.0, 0.0);
break;
case 2:
glColor3f(0.0, 0.0, 1.0);
break;
case 3:
glColor3f(1.0, 0.0, 1.0);
break;
case 4:
glColor3f(0.0, 1.0, 1.0);
break;
case 5:
glColor3f(1.0, 1.0, 0.0);
break;
case 6:
glColor3f(1.0, 1.0, 1.0);
break;
case 7:
glColor3f(1.0, 0.0, 0.0);
break;
default:
break;
}
drawSpline(i, splineOrder);
}
// Draw the x-axis.
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex3f(-4.0, -2.0, 0.0);
glVertex3f( 4.0, -2.0, 0.0);
glEnd();
// Draw points on the x-axis.
glPointSize(5.0);
glBegin(GL_POINTS);
for (i = 0; i < 9; i++)
glVertex3f(-4.0 + (float)i, -2.0, 0.0);
glEnd();
// Label the points on the x-axis.
writeBitmapString((void*)font, theString0, -4.05, -2.3);
writeBitmapString((void*)font, theString1, -3.05, -2.3);
writeBitmapString((void*)font, theString2, -2.05, -2.3);
writeBitmapString((void*)font, theString3, -1.05, -2.3);
writeBitmapString((void*)font, theString4, -0.05, -2.3);
writeBitmapString((void*)font, theString5, 0.95, -2.3);
writeBitmapString((void*)font, theString6, 1.95, -2.3);
writeBitmapString((void*)font, theString7, 2.95, -2.3);
writeBitmapString((void*)font, theString8, 3.95, -2.3);
// Draw the y-axis.
glBegin(GL_LINES);
glVertex3f(-4.0, -2.0, 0.0);
glVertex3f(-4.0, 4.0, 0.0);
glEnd();
// Draw points on the y-axis.
glBegin(GL_POINTS);
glVertex3f(-4.0, 1.0, 0.0);
glVertex3f(-4.0, 4.0, 0.0);
glEnd();
// Label the points on the y-axis.
writeBitmapString((void*)font, theString0, -4.25, -2.05);
writeBitmapString((void*)font, theString1, -4.25, 0.95);
writeBitmapString((void*)font, theString2, -4.25, 3.95);
// Draw horizontal bars corresponding to knot points.
glBegin(GL_LINES);
for (i = 0; i < 9; i++)
{
glVertex3f(-4.0, -3.5 + (float)i * 0.1, 0.0);
glVertex3f( 4.0, -3.5 + (float)i * 0.1, 0.0);
}
glEnd();
// Draw the knot points as dots on their respective bars.
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_POINTS);
for (i = 0; i < 9; i++)
glVertex3f( -4.0 + knots[i], -3.5 + (float)i * 0.1, 0.0);
glEnd();
// Highlight the selected knot point.
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
glVertex3f( -4.0 + knots[countKnots],
-3.5 + (float)countKnots * 0.1, 0.0);
glEnd();
// Lable the knot bars.
glColor3f(1.0, 1.0, 1.0);
writeBitmapString((void*)font, theStringKnotValues, -0.7, -4.0);
glPopMatrix();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-5.0, 5.0, -5.0*(GLfloat)h/(GLfloat)w,
5.0*(GLfloat)h/(GLfloat)w, -5.0, 5.0);
else
glOrtho(-5.0*(GLfloat)w/(GLfloat)h,
5.0*(GLfloat)w/(GLfloat)h, -5.0, 5.0, -5.0, 5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
break;
case ' ':
if (countKnots < 8) countKnots++;
else countKnots = 0;
glutPostRedisplay();
break;
case 127:
resetKnots();
glutPostRedisplay();
break;
default:
break;
}
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_LEFT) decreaseKnot(countKnots);
if(key == GLUT_KEY_RIGHT) increaseKnot(countKnots);
if(key == GLUT_KEY_UP)
{
if (splineOrder < 4) splineOrder++;
}
if(key == GLUT_KEY_DOWN)
{
if (splineOrder > 1) splineOrder--;
}
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (600, 600);
glutInitWindowPosition (100, 100);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc (keyboard);
glutSpecialFunc(SpecialKeys);
glutMainLoop();
return 0;
}