-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp4.c
383 lines (322 loc) · 9.07 KB
/
mp4.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
371
372
373
374
375
376
377
378
379
380
381
382
383
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include <string.h>
#include <ctype.h>
/*
* NAME : Mike Mesnier PROGRAM : MP4
* NET ID : mesnier COURSE : CS318 - Fall 1997
* DUE DATE : 11/7
*
* PURPOSE : The purpose of this program is to understand the
* various splines presented in the book, derive the
* cubic Bezier Spline basis-matrix, derive the equations
* for forward difference calculations, and implement the
* drawing of Bezier Splines using forward differences.
*
* INPUTS : Mouse/keyboard.
*/
/*************
* Constants *
*************/
#define MAX_POINTS 50
#define PRECISION 50
#define MAX_SPLINES 1 + (MAX_POINTS - 4)/3
#define true 1
#define false 0
/*********
* Types *
*********/
typedef int bool;
/*********************
* Globals Variables *
*********************/
int Px[MAX_POINTS]; /* x coords. of control points */
int Py[MAX_POINTS]; /* y coords. of control points */
int Sx[MAX_SPLINES][PRECISION+1]; /* x coords. of spline points */
int Sy[MAX_SPLINES][PRECISION+1]; /* y coords. of spline points */
int splineCount; /* num. of splines */
int numPoints = 0; /* num. control points */
bool firstOrder; /* first order continuity */
int windowWidth = 300; /* width of app. window */
int windowHeight = 300; /* height of app. window */
bool movingPoint = false; /* a point is currently being moved */
int pointBeingMoved; /* index of point being moved */
/************************
* Forward Declarations *
************************/
void Display(void);
void Mouse(int button, int state, int x, int y);
void Keyboard(unsigned char key, int x, int y);
void SetCameraPosition(int width, int height);
void beep(void);
void CalcSpline(int p0, int p1, int p2, int p3, int spline[]);
/****************
* Subroutines *
****************/
/*
* main() initializes GLUT, creates the main window,
* and loops for events.
*
* Inputs: argc, argv
* Outputs: none
* Side effects: main window created
*/
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (windowWidth, windowHeight);
glutCreateWindow ("mp4");
glutDisplayFunc(Display);
glutMouseFunc(Mouse);
glutKeyboardFunc(Keyboard);
glutReshapeFunc(SetCameraPosition);
SetCameraPosition(windowWidth, windowHeight);
glutMainLoop();
}
/*
* Keyboard() processes input from the keyboard. "q" and
* "Q" cause the application to exit.
*
* Inputs: key, x, y
* Outputs: none
* Side effects: terminates application on 'Q' and 'q'
* toggles first order cont. on ' '
*/
void Keyboard(unsigned char key, int x, int y) {
key = toupper(key);
if (key == 'Q') {
exit(0);
} else if (key == ' ') {
if (firstOrder) {
firstOrder = false;
} else {
firstOrder = true;
}
glutPostRedisplay();
} else {
beep();
}
}
/*
* Mouse() is called when there's input from the mouse.
*
* Inputs: button, state, x, y
* Outputs: none
* Side Effects: Either adds a control point, deletes all
* control points, or moves a control point.
* May also be a no-op if moving a point
* and any key other than the middle key was
* clicked.
*
* The left button is used for entering control points.
* The middle button is used for moving control points.
* The right button is used for clearing control points.
*
* The coordindates of the control points are stored
* in the arrays Px[] and Py[].
*
* The global variable movingPoint is a boolean telling
* whether or not a point is currently being moved. If
* a point is being moved, the next click must be from
* the middle button - all others are ignored.
*/
void Mouse(int button, int state, int x, int y) {
bool redraw = false;
int i;
y = windowHeight - y;
switch(button) {
case GLUT_LEFT_BUTTON:
switch(state) {
case GLUT_UP:
break;
case GLUT_DOWN:
if ((numPoints<MAX_POINTS)&(!movingPoint)) {
Px[numPoints] = x;
Py[numPoints] = y;
numPoints++;
redraw = true;
} else {
beep();
}
break;
}
break;
case GLUT_RIGHT_BUTTON:
switch(state) {
case GLUT_UP:
break;
case GLUT_DOWN:
if (!movingPoint) {
numPoints = 0;
movingPoint = false;
redraw = true;
} else {
beep();
}
break;
}
break;
case GLUT_MIDDLE_BUTTON:
switch(state) {
case GLUT_UP:
break;
case GLUT_DOWN:
if (!movingPoint) {
/* check if it's close to an existing points */
for (i=0;i<numPoints;i++) {
if ((abs(Px[i]-x)<4)&(abs(Py[i]-y)<4)) {
movingPoint = true;
pointBeingMoved = i;
redraw = true;
break;
}
}
} else {
/* store new coords. for control point */
Px[pointBeingMoved] = x;
Py[pointBeingMoved] = y;
movingPoint = false;
redraw = true;
}
break;
}
break;
}
if (redraw) {
glutPostRedisplay();
}
}
/*
* SetCameraPosition() sets up the viewing area for
* the window.
*
* Inputs: width, height
* Outputs: none
* Side effects: sets viewing area
*/
void SetCameraPosition(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble) width, 0.0, (GLdouble) height);
glMatrixMode(GL_MODELVIEW);
windowWidth = width;
windowHeight = height;
}
/*
* Display() is responsible for redrawing the window.
*
* Inputs: none
* Outputs: none
* Side effects: clears and draws the main window
*/
void Display(void)
{
int i, j;
/* clear window */
glClear(GL_COLOR_BUFFER_BIT);
/* draw control points */
glColor3f(0.0,1.0,1.0);
glPointSize(5.0);
if (movingPoint) {
/* draw control points up to point being moved in cyan */
for (i=0;i<pointBeingMoved;i++) {
glBegin(GL_POINTS);
glVertex2i(Px[i], Py[i]);
glEnd();
}
/* draw control point being moved in white */
glColor3f(1.0,1.0,1.0);
glBegin(GL_POINTS);
glVertex2i(Px[i], Py[i]);
glEnd();
/* draw remaining control point in cyan */
glColor3f(0.0,1.0,1.0);
for (i=pointBeingMoved+1;i<numPoints;i++) {
glBegin(GL_POINTS);
glVertex2i(Px[i], Py[i]);
glEnd();
}
} else {
/* draw all control point in cyan */
for (i=0;i<numPoints;i++) {
glBegin(GL_POINTS);
glVertex2i(Px[i], Py[i]);
glEnd();
}
}
/* connect control points */
glPointSize(1.0);
glBegin(GL_LINE_STRIP);
for (i=0;i<numPoints;i++) {
glVertex2i(Px[i], Py[i]);
}
glEnd();
/* calc splines */
splineCount = 0;
for (i=0;i<=numPoints-4;i+=3) {
if (firstOrder&(i>0)) {
CalcSpline(Px[i], Px[i]+Px[i]-Px[i-1], Px[i+2], Px[i+3], Sx[splineCount]);
CalcSpline(Py[i], Py[i]+Py[i]-Py[i-1], Py[i+2], Py[i+3], Sy[splineCount]);
} else {
CalcSpline(Px[i], Px[i+1], Px[i+2], Px[i+3], Sx[splineCount]);
CalcSpline(Py[i], Py[i+1], Py[i+2], Py[i+3], Sy[splineCount]);
}
splineCount++;
}
/* draw splines */
glColor3f(1.0,1.0,0.0);
for (i=0;i<splineCount;i++) {
glBegin(GL_LINE_STRIP);
for (j=0;j<=PRECISION;j++) {
glVertex2i(Sx[i][j], Sy[i][j]);
}
glEnd();
}
/* flush all commands */
glFlush();
}
/*
* beep() sends an audible character to the console
*
* Inputs: none
* Outputs: none
* Side effects: beep
*/
void beep() {
printf("\a");
fflush(stdout);
}
/*
* CalcSpline() calculates the coordinates of a spline from
* its 4 control points. These coordinates are saved in the
* global arrays Sx[][] and Sy[][]. The first dimension is
* the spline, and the second is a point within the spline.
* This subroutine accepts a pointer to the *second* dimension.
*
* The calculation used is a forward difference as described
* in the text.
*/
void CalcSpline(int p0, int p1, int p2, int p3, int spline[]) {
int i;
double a, b, c, d;
double deltaU = 1.0/PRECISION;
double Xk, deltaXk, delta2Xk;
a = -p0+3*p1-3*p2+p3;
b = 3*p0-6*p1+3*p2;
c = -3*p0+3*p1;
d = p0;
Xk = d;
deltaXk = a*pow(deltaU,3) + b*pow(deltaU,2) + c*deltaU;
delta2Xk = 6*a*pow(deltaU,3) + 2*b*pow(deltaU,2);
spline[0] = Xk;
for (i=1;i<=PRECISION;i++) {
Xk += deltaXk;
deltaXk += delta2Xk;
delta2Xk += 6*a*pow(deltaU,3);
spline[i] = Xk;
}
}