-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
330 lines (286 loc) · 7.08 KB
/
main.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
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#define NOMINMAX
#include <windows.h>
#endif
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include "vec2.h"
#include "curve.h"
#include "util.h"
#define DEFAULT_PAN_X 0.5f
#define DEFAULT_PAN_Y 0.5f
#define DEFAULT_SCALE 0.25f
#define DEFAULT_SLICES (1 << 8)
static int width = 1024, height = 1024;
static enum { NONE, PANNING, SCALING, MOVING } cur_op = NONE;
static int last_x, last_y;
static float pan_x = DEFAULT_PAN_X;
static float pan_y = DEFAULT_PAN_Y;
static float scale = DEFAULT_SCALE;
static int slices = DEFAULT_SLICES;
static int selected_point = -1;
static int use_de_casteljau = 1;
static int render_control_polygon = 1;
#define POINT_SIZE 5
#define POINTS_NAME 1000
#define MAX_POINTS 64
static struct vec2 points[MAX_POINTS];
static int nr_points = 0;
static void unproject2(struct vec2 *res, float x, float y)
{
GLdouble pt_x, pt_y, pt_z;
GLdouble model[16];
GLdouble proj[16];
GLint viewport[4];
glGetDoublev(GL_MODELVIEW_MATRIX, model);
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetIntegerv(GL_VIEWPORT, viewport);
gluUnProject(x, viewport[3] - y, 0.0, model, proj, viewport, &pt_x, &pt_y, &pt_z);
res->x = pt_x;
res->y = pt_y;
}
static void gl_printf(void *font, const char *format, ...)
{
char str[512], *p;
va_list args;
va_start(args, format);
vsprintf(str, format, args);
va_end(args);
for (p = str; *p; p++)
glutBitmapCharacter(font, *p);
}
static void draw_frame(void)
{
/* Draw the x axis */
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_LINES);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glEnd();
/* Draw the y axis */
glColor3f(0.0f, 1.0f, 0.0f);
glBegin(GL_LINES);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glEnd();
}
static void display(void)
{
static clock_t ticks;
static int nr_frames, fps;
clock_t now;
int i;
/* Calculate frame rate */
nr_frames++;
now = clock();
if (now - ticks >= CLOCKS_PER_SEC) {
fps = nr_frames;
ticks = now;
nr_frames = 0;
}
/* Start rendering */
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(pan_x, pan_y, 0.0f);
glScalef(scale, scale, 1.0f);
/* Render control polygon */
if (render_control_polygon) {
glBegin(GL_LINE_STRIP);
glColor3f(0.0f, 0.0f, 1.0f);
for (i = 0; i < nr_points; i++)
glVertex2f(points[i].x, points[i].y);
glEnd();
}
/* Render control points */
glPointSize(POINT_SIZE);
for (i = 0; i < nr_points; i++) {
glColor3f(1.0f, 1.0f, 1.0f);
glPushName(POINTS_NAME + i);
glBegin(GL_POINTS);
glVertex2f(points[i].x, points[i].y);
glEnd();
glPopName();
}
/* Render the curve itself */
glColor3f(0.0f, 1.0f, 0.0f);
glBegin(GL_LINE_STRIP);
for (i = 0; i <= slices; i++) {
struct vec2 pt = { 0.0f, 0.0f };
if (use_de_casteljau)
bezier_casteljau(&pt, points, nr_points, (float) i / slices);
else
bezier_bernstein(&pt, points, nr_points, (float) i / slices);
glVertex2f(pt.x, pt.y);
}
glEnd();
draw_frame();
/* Render fps counter */
glPushMatrix();
glLoadIdentity();
glColor3f(1.0, 1.0f, 1.0f);
glRasterPos2f(0.925f, 0.975f);
gl_printf(GLUT_BITMAP_HELVETICA_18, "%d fps", fps);
glPopMatrix();
glutSwapBuffers();
glutPostRedisplay();
}
static void reshape(int w, int h)
{
width = w;
height = h;
/* Set nice antialiasing settings */
glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
/* Set viewport and projection matrices */
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f);
}
static int check_hits(int x, int y)
{
GLuint buffer[256], *ptr;
GLint hits;
GLint viewport[4];
GLdouble proj[16];
int i, j;
glSelectBuffer(ARRAY_SIZE(buffer), buffer);
glRenderMode(GL_SELECT);
glInitNames();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
/* Set proper projection matrix */
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetIntegerv(GL_VIEWPORT, viewport);
glLoadIdentity();
gluPickMatrix(x, viewport[3] - y, POINT_SIZE * 1.5f, POINT_SIZE * 1.5f, viewport);
glMultMatrixd(proj);
display();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
hits = glRenderMode(GL_RENDER);
/* Process hits */
ptr = buffer;
for (i = 0; i < hits; i++) {
GLuint nr_names = *ptr++;
ptr++; /* zmin */
ptr++; /* zmax */
for (j = 0; j < (int) nr_names; j++) {
GLuint name = *ptr++;
int idx = name - POINTS_NAME;
if (idx >= 0 && idx < nr_points)
return idx;
}
}
return -1;
}
static void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27: case 17: /* Esc or Ctrl-Q */
exit(0);
break;
case 'r':
pan_x = DEFAULT_PAN_X;
pan_y = DEFAULT_PAN_Y;
scale = DEFAULT_SCALE;
slices = DEFAULT_SLICES;
break;
case '-': case '_':
if (slices != 1)
slices >>= 1;
printf("Rendering %d slices\n", slices);
break;
case '=': case '+':
if (slices < (1 << 11))
slices <<= 1;
printf("Rendering %d slices\n", slices);
break;
case 'c':
use_de_casteljau = !use_de_casteljau;
if (use_de_casteljau)
printf("Switched to de Casteljau's algorithm\n");
else
printf("Switched to Bernstein polynomials\n");
break;
case 'p':
render_control_polygon = !render_control_polygon;
break;
}
}
static void mouse(int button, int state, int x, int y)
{
static const int cursor[] = {
GLUT_CURSOR_RIGHT_ARROW, /* NONE */
GLUT_CURSOR_CROSSHAIR, /* PANNING */
GLUT_CURSOR_UP_DOWN, /* SCALING */
GLUT_CURSOR_CROSSHAIR /* MOVING */
};
cur_op = NONE;
if ((glutGetModifiers() & GLUT_ACTIVE_ALT) && state == GLUT_DOWN) {
if (button == GLUT_LEFT_BUTTON)
cur_op = PANNING;
else if (button == GLUT_RIGHT_BUTTON)
cur_op = SCALING;
} else if (state == GLUT_DOWN) {
int active_point = check_hits(x, y);
if (button == GLUT_LEFT_BUTTON && active_point != -1) {
/* Moving point */
selected_point = active_point;
cur_op = MOVING;
} else if ((button == GLUT_MIDDLE_BUTTON || glutGetModifiers())
&& active_point == -1 && nr_points < MAX_POINTS) {
/* Add point */
unproject2(&points[nr_points++], x, y);
} else if (button == GLUT_RIGHT_BUTTON && active_point != -1 && nr_points > 0) {
/* Remove point */
memmove(points + active_point, points + active_point + 1,
sizeof(*points) * (nr_points - active_point + 1));
nr_points--;
}
}
glutSetCursor(cursor[cur_op]);
last_x = x;
last_y = y;
}
static void motion(int x, int y)
{
float dx = (float) (x - last_x) / width;
float dy = (float) (y - last_y) / height;
if (cur_op == PANNING) {
pan_x += dx;
pan_y -= dy;
} else if (cur_op == SCALING) {
scale += dy;
} else if (cur_op == MOVING) {
unproject2(&points[selected_point], x, y);
}
last_x = x;
last_y = y;
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(width, height);
glutCreateWindow("bezier");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
return 0;
}