Repository navigation
Expand file tree
/
Copy pathbezCurves.c
More file actions
211 lines (192 loc) · 4.7 KB
/
Copy pathbezCurves.c
File metadata and controls
211 lines (192 loc) · 4.7 KB
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
/*
* bezCurves.c
*
* This program shows Bezier curves of orders (= number of
* control points) from 2 to 6.
*
* Interaction:
* Press the up/down arrows to choose the order and then press enter.
* Press shift to select (highlight) a control point and then up/down
* and left/right arrows to move it.
* Press delete to reset.
*/
#include <GL/glut.h>
#include <stdlib.h>
static int count = 0, order = 2, askOrder = 1;
int font=(int)GLUT_BITMAP_8_BY_13;
GLfloat ctrlpoints[6][3] = {
{ -4.0, 0.0, 0.0}, { -3.0, 0.0, 0.0}, { -1.0, 0.0, 0.0},
{0.0, 0.0, 0.0}, {3.0, 0.0, 0.0}, { 4.0, 0.0, 0.0}};
// Control point values stored unchanged for use on reset.
GLfloat originalCtrlpoints[6][3] = {
{ -4.0, 0.0, 0.0}, { -3.0, 0.0, 0.0}, { -1.0, 0.0, 0.0},
{0.0, 0.0, 0.0}, {3.0, 0.0, 0.0}, { 4.0, 0.0, 0.0}};
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}
// Restore control points to original values.
void restoreCtrlpoints(void)
{
int i, j;
for (i=0; i<6; i++)
for (j=0; j<3; j++)
ctrlpoints[i][j] = originalCtrlpoints[i][j];
}
// 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++;
}
}
void display(void)
{
char theStringShowBezCurve[] = "Show Bezier curve order";
char theString2[] = "2";
char theString3[] = "3";
char theString4[] = "4";
char theString5[] = "5";
char theString6[] = "6";
float rx = 1.7, ry = 3.5;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
if (askOrder == 1)
// First screen.
{
writeBitmapString((void*)font, theStringShowBezCurve, -1.7, ry);
glColor3f(1.0, 0.0, 0.0);
switch (order)
{
case 2:
writeBitmapString((void*)font, theString2, rx, ry);
break;
case 3:
writeBitmapString((void*)font, theString3, rx, ry);
break;
case 4:
writeBitmapString((void*)font, theString4, rx, ry);
break;
case 5:
writeBitmapString((void*)font, theString5, rx, ry);
break;
case 6:
writeBitmapString((void*)font, theString6, rx, ry);
break;
default:
break;
}
}
else
// Second screen.
{
int i;
glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, order, &ctrlpoints[0][0]);
glEnable(GL_MAP1_VERTEX_3);
// Define a one-dimensional grid and evaluate on it.
glMapGrid1f(100, 0.0, 1.0);
glEvalMesh1(GL_LINE, 0, 100);
/* Draw the control points as dots. */
glPointSize(5.0);
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_POINTS);
for (i = 0; i < order; i++)
glVertex3fv(ctrlpoints[i]);
glEnd();
// Highlight selected control point,
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
glVertex3fv(ctrlpoints[count]);
glEnd();
}
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 13:
askOrder = 0;
glutPostRedisplay();
break;
case 127:
askOrder = 1;
order = 2;
restoreCtrlpoints();
glutPostRedisplay();
break;
case ' ':
if (askOrder == 0)
{
if (count < order-1) count++;
else count = 0;
}
glutPostRedisplay();
break;
default:
break;
}
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
{
if (askOrder == 1)
{
if (order < 6) order += 1;
}
else
ctrlpoints[count][1] += 0.1f;
}
if(key == GLUT_KEY_DOWN)
{
if (askOrder == 1)
{
if (order > 2) order -= 1;
}
else
ctrlpoints[count][1] -= 0.1f;
}
if(key == GLUT_KEY_LEFT)
ctrlpoints[count][0] -= 0.1f;
if(key == GLUT_KEY_RIGHT)
ctrlpoints[count][0] += 0.1f;
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (600, 600);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc (keyboard);
glutSpecialFunc(SpecialKeys);
glutMainLoop();
return 0;
}