curvedraw

view src/app.cc @ 4:9f75208b81cd

allow panning while inserting control points don't scale text with the viewport
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 17 Dec 2015 07:22:14 +0200
parents bf78387a9925
children 2b7ae76c173f b625f0575d66
line source
1 #include <stdlib.h>
2 #include <float.h>
3 #include <assert.h>
4 #include <vector>
5 #include <algorithm>
6 #include "opengl.h"
7 #include "app.h"
8 #include "curve.h"
9 #include "widgets.h"
11 enum SnapMode {
12 SNAP_NONE,
13 SNAP_GRID,
14 SNAP_POINT
15 };
17 int win_width, win_height;
18 float win_aspect;
20 static void draw_grid(float sz, float sep, float alpha = 1.0f);
21 static void draw_curve(const Curve *curve);
22 static void on_click(int bn, float u, float v);
24 // viewport control
25 static Vector2 view_pan;
26 static float view_scale = 0.2f;
27 static Matrix4x4 view_matrix;
29 static float grid_size = 1.0;
30 static SnapMode snap_mode;
32 static std::vector<Curve*> curves;
33 static Curve *sel_curve; // selected curve being edited
34 static Curve *new_curve; // new curve being entered
35 static Curve *hover_curve; // curve the mouse is hovering over (click to select)
36 static int sel_pidx = -1; // selected point of the selected or hovered-over curve
38 static Label *weight_label; // floating label for the cp weight
40 #ifdef DRAW_MOUSE_POINTER
41 static Vector2 mouse_pointer;
42 #endif
45 bool app_init(int argc, char **argv)
46 {
47 glEnable(GL_MULTISAMPLE);
48 glEnable(GL_CULL_FACE);
50 glEnable(GL_BLEND);
51 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
52 return true;
53 }
55 void app_cleanup()
56 {
57 for(size_t i=0; i<curves.size(); i++) {
58 delete curves[i];
59 }
60 curves.clear();
61 }
63 void app_draw()
64 {
65 glClearColor(0.1, 0.1, 0.1, 1);
66 glClear(GL_COLOR_BUFFER_BIT);
68 glMatrixMode(GL_MODELVIEW);
69 glLoadIdentity();
70 glTranslatef(view_pan.x * view_scale, view_pan.y * view_scale, 0);
71 glScalef(view_scale, view_scale, view_scale);
73 float max_aspect = std::max(win_aspect, 1.0f / win_aspect);
74 draw_grid(max_aspect, grid_size);
76 for(size_t i=0; i<curves.size(); i++) {
77 draw_curve(curves[i]);
78 }
79 if(new_curve) {
80 draw_curve(new_curve);
81 }
83 #ifdef DRAW_MOUSE_POINTER
84 glPointSize(6.0);
85 glBegin(GL_POINTS);
86 glColor3f(0, 0, 1);
87 glVertex2f(mouse_pointer.x, mouse_pointer.y);
88 glEnd();
89 #endif
91 glMatrixMode(GL_MODELVIEW);
92 glLoadIdentity();
94 if(weight_label) {
95 weight_label->draw();
96 }
97 }
99 static void draw_grid(float sz, float sep, float alpha)
100 {
101 float x = 0.0f;
102 float s = 1.0 / view_scale;
104 sz *= s;
105 sz += sep; // one more step for when we have non-zero fractional pan
106 float end = std::min(sz, 100.0f * sep);
108 // fractional pan
109 Vector2 pan = view_pan;
110 Vector2 fpan = Vector2(fmod(pan.x, sep), fmod(pan.y, sep));
111 Vector2 offset = fpan - pan;
113 glMatrixMode(GL_MODELVIEW);
114 glPushMatrix();
115 glTranslatef(offset.x, offset.y, 0);
117 glBegin(GL_LINES);
118 glColor4f(0.35, 0.35, 0.35, alpha);
119 while(x <= end) {
120 glVertex2f(-end, x);
121 glVertex2f(end, x);
122 glVertex2f(-end, -x);
123 glVertex2f(end, -x);
124 glVertex2f(x, -end);
125 glVertex2f(x, end);
126 glVertex2f(-x, -end);
127 glVertex2f(-x, end);
128 x += sep;
129 }
130 glEnd();
131 glPopMatrix();
134 glLineWidth(1.0);
135 glBegin(GL_LINES);
136 glColor4f(0.6, 0.3, 0.2, alpha);
137 glVertex2f(-sz + offset.x, 0);
138 glVertex2f(sz + offset.x, 0);
139 glColor4f(0.2, 0.3, 0.6, alpha);
140 glVertex2f(0, -sz + offset.y);
141 glVertex2f(0, sz + offset.y);
142 glEnd();
144 }
146 static void draw_curve(const Curve *curve)
147 {
148 int numpt = curve->size();
149 int segm = numpt * 16;
151 glLineWidth(curve == hover_curve ? 4.0 : 2.0);
152 if(curve == sel_curve) {
153 glColor3f(0.3, 0.4, 1.0);
154 } else if(curve == new_curve) {
155 glColor3f(1.0, 0.75, 0.3);
156 } else {
157 glColor3f(0.6, 0.6, 0.6);
158 }
159 glBegin(GL_LINE_STRIP);
160 for(int i=0; i<segm; i++) {
161 float t = (float)i / (float)(segm - 1);
162 Vector2 v = curve->interpolate(t);
163 glVertex2f(v.x, v.y);
164 }
165 glEnd();
166 glLineWidth(1.0);
168 glPointSize(curve == hover_curve ? 10.0 : 7.0);
169 glBegin(GL_POINTS);
170 if(curve == new_curve) {
171 glColor3f(1.0, 0.0, 0.0);
172 } else {
173 glColor3f(0.6, 0.3, 0.2);
174 }
175 for(int i=0; i<numpt; i++) {
176 if(curve == sel_curve) {
177 if(i == sel_pidx) {
178 glColor3f(1.0, 0.2, 0.1);
179 } else {
180 glColor3f(0.2, 1.0, 0.2);
181 }
182 }
183 Vector2 pt = curve->get_point(i);
184 glVertex2f(pt.x, pt.y);
185 }
186 glEnd();
187 glPointSize(1.0);
188 }
190 void app_reshape(int x, int y)
191 {
192 win_width = x;
193 win_height = y;
194 win_aspect = (float)x / (float)y;
196 glViewport(0, 0, x, y);
197 glMatrixMode(GL_PROJECTION);
198 glLoadIdentity();
199 glOrtho(-win_aspect, win_aspect, -1, 1, -1, 1);
200 }
202 void app_keyboard(int key, bool pressed)
203 {
204 if(pressed) {
205 switch(key) {
206 case 'q':
207 case 'Q':
208 exit(0);
210 case 27:
211 if(new_curve) {
212 delete new_curve;
213 new_curve = 0;
214 post_redisplay();
215 }
216 break;
218 case 'l':
219 case 'L':
220 if(sel_curve) {
221 sel_curve->set_type(CURVE_LINEAR);
222 post_redisplay();
223 }
224 if(new_curve) {
225 new_curve->set_type(CURVE_LINEAR);
226 post_redisplay();
227 }
228 break;
230 case 'b':
231 case 'B':
232 if(sel_curve) {
233 sel_curve->set_type(CURVE_BSPLINE);
234 post_redisplay();
235 }
236 if(new_curve) {
237 new_curve->set_type(CURVE_BSPLINE);
238 post_redisplay();
239 }
240 break;
242 case 'h':
243 case 'H':
244 if(sel_curve) {
245 sel_curve->set_type(CURVE_HERMITE);
246 post_redisplay();
247 }
248 if(new_curve) {
249 new_curve->set_type(CURVE_HERMITE);
250 post_redisplay();
251 }
252 break;
253 }
254 }
257 switch(key) {
258 case 's':
259 snap_mode = pressed ? SNAP_GRID : SNAP_NONE;
260 break;
262 case 'S':
263 snap_mode = pressed ? SNAP_POINT : SNAP_NONE;
264 break;
266 default:
267 break;
268 }
269 }
271 static void calc_view_matrix()
272 {
273 view_matrix.reset_identity();
274 view_matrix.scale(Vector3(view_scale, view_scale, view_scale));
275 view_matrix.translate(Vector3(view_pan.x, view_pan.y, 0.0));
276 }
278 static Vector2 pixel_to_uv(int x, int y)
279 {
280 float u = win_aspect * (2.0 * (float)x / (float)win_width - 1.0);
281 float v = 1.0 - 2.0 * (float)y / (float)win_height;
283 u = u / view_scale - view_pan.x;
284 v = v / view_scale - view_pan.y;
285 return Vector2(u, v);
286 /*
287 Matrix4x4 inv_view_matrix = view_matrix.inverse();
288 Vector4 res = Vector4(u, v, 0.0, 1.0).transformed(inv_view_matrix);
290 return Vector2(res.x, res.y);
291 */
292 }
294 static int prev_x, prev_y;
295 static int click_pos[8][2];
296 static unsigned int bnstate;
298 #define BNBIT(x) (1 << (x))
300 void app_mouse_button(int bn, bool pressed, int x, int y)
301 {
302 prev_x = x;
303 prev_y = y;
304 if(pressed) {
305 bnstate |= BNBIT(bn);
306 } else {
307 bnstate &= ~BNBIT(bn);
308 }
310 if(pressed) {
311 click_pos[bn][0] = x;
312 click_pos[bn][1] = y;
313 } else {
314 int dx = x - click_pos[bn][0];
315 int dy = y - click_pos[bn][1];
317 if(abs(dx) + abs(dy) < 3) {
318 Vector2 uv = pixel_to_uv(x, y);
319 on_click(bn, uv.x, uv.y);
320 }
322 if(!(bnstate & BNBIT(2))) {
323 delete weight_label;
324 weight_label = 0;
325 post_redisplay();
326 }
327 }
328 }
330 static bool point_hit_test(const Vector2 &pos, Curve **curveret, int *pidxret)
331 {
332 float thres = 0.02 / view_scale;
334 for(size_t i=0; i<curves.size(); i++) {
335 int pidx = curves[i]->nearest_point(pos);
336 if(pidx == -1) continue;
338 Vector2 cp = curves[i]->get_point(pidx);
339 if((cp - pos).length_sq() < thres * thres) {
340 *curveret = curves[i];
341 *pidxret = pidx;
342 return true;
343 }
344 }
345 *curveret = 0;
346 *pidxret = -1;
347 return false;
348 }
350 static Vector2 snap(const Vector2 &p)
351 {
352 switch(snap_mode) {
353 case SNAP_GRID:
354 return Vector2(round(p.x / grid_size) * grid_size, round(p.y / grid_size) * grid_size);
355 case SNAP_POINT:
356 // TODO
357 default:
358 break;
359 }
360 return p;
361 }
363 void app_mouse_motion(int x, int y)
364 {
365 Vector2 prev_uv = pixel_to_uv(prev_x, prev_y);
367 int dx = x - prev_x;
368 int dy = y - prev_y;
369 prev_x = x;
370 prev_y = y;
372 if(!dx && !dy) return;
374 Vector2 uv = pixel_to_uv(x, y);
375 #ifdef DRAW_MOUSE_POINTER
376 mouse_pointer = uv;
377 post_redisplay();
378 #endif
380 /* when entering a new curve, have the last (extra) point following
381 * the mouse until it's entered by a click (see on_click).
382 */
383 if(new_curve) {
384 new_curve->move_point(new_curve->size() - 1, snap(uv));
385 post_redisplay();
386 }
388 if(!new_curve && !bnstate) {
389 // not dragging, highlight curve under mouse
390 point_hit_test(uv, &hover_curve, &sel_pidx);
391 post_redisplay();
393 } else {
394 // we're dragging with one or more buttons held down
396 if(sel_curve && sel_pidx != -1) {
397 // we have a curve and a point of the curve selected
399 if(bnstate & BNBIT(0)) {
400 // dragging point with left button: move it
401 sel_curve->move_point(sel_pidx, snap(uv));
402 post_redisplay();
403 }
405 if(bnstate & BNBIT(2)) {
406 // dragging point with right button: change weight
407 float w = sel_curve->get_weight(sel_pidx);
408 w -= dy * 0.01;
409 if(w < FLT_MIN) w = FLT_MIN;
410 sel_curve->set_weight(sel_pidx, w);
412 // popup floating weight label if not already there
413 if(!weight_label) {
414 weight_label = new Label;
415 }
416 weight_label->set_position(uv);
417 weight_label->set_textf("w=%g", w);
418 post_redisplay();
419 }
420 } else {
421 // no selection, we're dragging in empty space: manipulate viewport
422 Vector2 dir = uv - prev_uv;
424 if(bnstate & (BNBIT(0) | BNBIT(1))) {
425 // panning
426 view_pan += dir;
427 calc_view_matrix();
428 post_redisplay();
429 }
430 if(bnstate & BNBIT(2)) {
431 // zooming
432 view_scale -= ((float)dy / (float)win_height) * view_scale * 5.0;
433 if(view_scale < 1e-4) view_scale = 1e-4;
434 calc_view_matrix();
435 post_redisplay();
436 }
437 }
438 }
439 }
441 static void on_click(int bn, float u, float v)
442 {
443 Vector2 uv = Vector2(u, v);
445 switch(bn) {
446 case 0: // ------- LEFT CLICK ------
447 if(hover_curve) {
448 // if we're hovering: click selects
449 sel_curve = hover_curve;
450 hover_curve = 0;
451 } else if(sel_curve) {
452 // if we have a selected curve: click adds point (enter new_curve mode)
453 std::vector<Curve*>::iterator it = std::find(curves.begin(), curves.end(), sel_curve);
454 assert(it != curves.end());
455 curves.erase(it, it + 1);
457 new_curve = sel_curve;
458 sel_curve = 0;
459 sel_pidx = -1;
461 new_curve->add_point(uv);
462 } else {
463 // otherwise, click starts a new curve
464 if(!new_curve) {
465 new_curve = new Curve;
466 new_curve->add_point(uv);
467 }
468 new_curve->add_point(uv);
469 }
470 post_redisplay();
471 break;
473 case 2: // ------- RIGHT CLICK ------
474 if(new_curve) {
475 // in new-curve mode: finish curve (cancels last floating segment)
476 new_curve->remove_point(new_curve->size() - 1);
477 if(new_curve->empty()) {
478 delete new_curve;
479 } else {
480 curves.push_back(new_curve);
481 }
482 new_curve = 0;
484 } else if(sel_curve) {
485 // in selected curve mode: delete control point or unselect
486 Curve *hit_curve;
487 int hit_pidx;
488 if(point_hit_test(uv, &hit_curve, &hit_pidx) && hit_curve == sel_curve) {
489 hit_curve->remove_point(hit_pidx);
490 sel_pidx = -1;
491 } else {
492 sel_curve = 0;
493 sel_pidx = -1;
494 }
495 }
496 post_redisplay();
497 break;
499 default:
500 break;
501 }
502 }