curvedraw

view src/app.cc @ 16:7f795f7fecd6

readme and COPYING
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 20 Dec 2015 10:55:57 +0200
parents 37ab3a4c02f8
children
line source
1 /*
2 curvedraw - a simple program to draw curves
3 Copyright (C) 2015 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <stdlib.h>
19 #include <float.h>
20 #include <assert.h>
21 #include <vector>
22 #include <algorithm>
23 #include "opengl.h"
24 #include "app.h"
25 #include "curve.h"
26 #include "widgets.h"
27 #include "curvefile.h"
29 enum SnapMode {
30 SNAP_NONE,
31 SNAP_GRID,
32 SNAP_POINT
33 };
35 int win_width, win_height;
36 float win_aspect;
38 static void draw_grid(float sz, float sep, float alpha = 1.0f);
39 static void draw_curve(const Curve *curve);
40 static void on_click(int bn, float u, float v);
42 // viewport control
43 static Vector2 view_pan;
44 static float view_scale = 0.2f;
45 static Matrix4x4 view_matrix;
47 static float grid_size = 1.0;
48 static SnapMode snap_mode;
50 static bool show_bounds;
52 static std::vector<Curve*> curves;
53 static Curve *sel_curve; // selected curve being edited
54 static Curve *new_curve; // new curve being entered
55 static Curve *hover_curve; // curve the mouse is hovering over (click to select)
56 static int sel_pidx = -1; // selected point of the selected curve
57 static int hover_pidx = -1; // hovered over point
59 static Label *weight_label; // floating label for the cp weight
61 static Vector2 mouse_pointer;
64 bool app_init(int argc, char **argv)
65 {
66 glewInit();
68 glEnable(GL_MULTISAMPLE);
69 glEnable(GL_CULL_FACE);
71 glEnable(GL_BLEND);
72 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
73 return true;
74 }
76 void app_cleanup()
77 {
78 for(size_t i=0; i<curves.size(); i++) {
79 delete curves[i];
80 }
81 curves.clear();
82 }
84 void app_draw()
85 {
86 glClearColor(0.1, 0.1, 0.1, 1);
87 glClear(GL_COLOR_BUFFER_BIT);
89 glMatrixMode(GL_MODELVIEW);
90 glLoadIdentity();
91 glTranslatef(view_pan.x * view_scale, view_pan.y * view_scale, 0);
92 glScalef(view_scale, view_scale, view_scale);
94 float max_aspect = std::max(win_aspect, 1.0f / win_aspect);
95 draw_grid(max_aspect, grid_size);
97 for(size_t i=0; i<curves.size(); i++) {
98 draw_curve(curves[i]);
99 }
100 if(new_curve) {
101 draw_curve(new_curve);
102 }
104 #ifdef DRAW_MOUSE_POINTER
105 glPointSize(6.0);
106 glBegin(GL_POINTS);
107 glColor3f(0, 0, 1);
108 glVertex2f(mouse_pointer.x, mouse_pointer.y);
109 glEnd();
110 #endif
112 glMatrixMode(GL_MODELVIEW);
113 glLoadIdentity();
115 if(weight_label) {
116 weight_label->draw();
117 }
118 }
120 static void draw_grid(float sz, float sep, float alpha)
121 {
122 float x = 0.0f;
123 float s = 1.0 / view_scale;
125 sz *= s;
126 sz += sep; // one more step for when we have non-zero fractional pan
127 float end = std::min(sz, 100.0f * sep);
129 // fractional pan
130 Vector2 pan = view_pan;
131 Vector2 fpan = Vector2(fmod(pan.x, sep), fmod(pan.y, sep));
132 Vector2 offset = fpan - pan;
134 glMatrixMode(GL_MODELVIEW);
135 glPushMatrix();
136 glTranslatef(offset.x, offset.y, 0);
138 glBegin(GL_LINES);
139 glColor4f(0.35, 0.35, 0.35, alpha);
140 while(x <= end) {
141 glVertex2f(-end, x);
142 glVertex2f(end, x);
143 glVertex2f(-end, -x);
144 glVertex2f(end, -x);
145 glVertex2f(x, -end);
146 glVertex2f(x, end);
147 glVertex2f(-x, -end);
148 glVertex2f(-x, end);
149 x += sep;
150 }
151 glEnd();
152 glPopMatrix();
155 glLineWidth(1.0);
156 glBegin(GL_LINES);
157 glColor4f(0.6, 0.3, 0.2, alpha);
158 glVertex2f(-sz + offset.x, 0);
159 glVertex2f(sz + offset.x, 0);
160 glColor4f(0.2, 0.3, 0.6, alpha);
161 glVertex2f(0, -sz + offset.y);
162 glVertex2f(0, sz + offset.y);
163 glEnd();
165 }
167 static void draw_curve(const Curve *curve)
168 {
169 int numpt = curve->size();
170 int segm = numpt * 16;
172 if(show_bounds) {
173 Vector3 bmin, bmax;
174 curve->get_bbox(&bmin, &bmax);
176 glLineWidth(1.0);
177 glColor3f(0, 1, 0);
178 glBegin(GL_LINE_LOOP);
179 glVertex2f(bmin.x, bmin.y);
180 glVertex2f(bmax.x, bmin.y);
181 glVertex2f(bmax.x, bmax.y);
182 glVertex2f(bmin.x, bmax.y);
183 glEnd();
184 }
186 glLineWidth(curve == hover_curve ? 4.0 : 2.0);
187 if(curve == sel_curve) {
188 glColor3f(0.3, 0.4, 1.0);
189 } else if(curve == new_curve) {
190 glColor3f(1.0, 0.75, 0.3);
191 } else {
192 glColor3f(0.6, 0.6, 0.6);
193 }
194 glBegin(GL_LINE_STRIP);
195 for(int i=0; i<segm; i++) {
196 float t = (float)i / (float)(segm - 1);
197 Vector3 v = curve->interpolate(t);
198 glVertex2f(v.x, v.y);
199 }
200 glEnd();
201 glLineWidth(1.0);
203 glPointSize(curve == hover_curve ? 10.0 : 7.0);
204 glBegin(GL_POINTS);
205 if(curve == new_curve) {
206 glColor3f(1.0, 0.0, 0.0);
207 } else {
208 glColor3f(0.6, 0.3, 0.2);
209 }
210 for(int i=0; i<numpt; i++) {
211 if(curve == sel_curve) {
212 if(i == sel_pidx) {
213 glColor3f(1.0, 0.2, 0.1);
214 } else {
215 glColor3f(0.2, 1.0, 0.2);
216 }
217 }
218 Vector2 pt = curve->get_point2(i);
219 glVertex2f(pt.x, pt.y);
220 }
221 glEnd();
223 // draw the projected mouse point on the selected curve
224 /*
225 if(curve == sel_curve) {
226 Vector3 pp = curve->proj_point(Vector3(mouse_pointer.x, mouse_pointer.y, 0.0));
228 glPointSize(5.0);
229 glBegin(GL_POINTS);
230 glColor3f(1, 0.8, 0.2);
231 glVertex2f(pp.x, pp.y);
232 glEnd();
233 }
234 */
235 glPointSize(1.0);
236 }
238 void app_reshape(int x, int y)
239 {
240 win_width = x;
241 win_height = y;
242 win_aspect = (float)x / (float)y;
244 glViewport(0, 0, x, y);
245 glMatrixMode(GL_PROJECTION);
246 glLoadIdentity();
247 glOrtho(-win_aspect, win_aspect, -1, 1, -1, 1);
248 }
250 void app_keyboard(int key, bool pressed)
251 {
252 if(pressed) {
253 switch(key) {
254 case 'q':
255 case 'Q':
256 exit(0);
258 case 27:
259 if(new_curve) {
260 delete new_curve;
261 new_curve = 0;
262 post_redisplay();
263 }
264 break;
266 case '1':
267 case '2':
268 case '3':
269 if(sel_curve) {
270 sel_curve->set_type((CurveType)((int)CURVE_LINEAR + key - '1'));
271 post_redisplay();
272 }
273 if(new_curve) {
274 new_curve->set_type((CurveType)((int)CURVE_LINEAR + key - '1'));
275 post_redisplay();
276 }
277 break;
279 case 'b':
280 case 'B':
281 show_bounds = !show_bounds;
282 post_redisplay();
283 break;
285 case 'n':
286 case 'N':
287 if(sel_curve) {
288 sel_curve->normalize();
289 post_redisplay();
290 }
291 break;
293 case 'e':
294 case 'E':
295 // TODO: GUI for filename at least
296 if(!save_curves("test.curves", &curves[0], (int)curves.size())) {
297 fprintf(stderr, "failed to export curves\n");
298 }
299 printf("exported %d curves\n", (int)curves.size());
300 break;
302 case 'l':
303 case 'L':
304 {
305 std::list<Curve*> clist = load_curves("test.curves");
306 if(clist.empty()) {
307 fprintf(stderr, "failed to import curves\n");
308 }
310 for(size_t i=0; i<curves.size(); i++) {
311 delete curves[i];
312 }
313 curves.clear();
315 int num = 0;
316 std::list<Curve*>::iterator it = clist.begin();
317 while(it != clist.end()) {
318 curves.push_back(*it++);
319 ++num;
320 }
321 printf("imported %d curves\n", num);
322 }
323 post_redisplay();
324 break;
325 }
326 }
329 switch(key) {
330 case 's':
331 snap_mode = pressed ? SNAP_GRID : SNAP_NONE;
332 break;
334 case 'S':
335 snap_mode = pressed ? SNAP_POINT : SNAP_NONE;
336 break;
338 default:
339 break;
340 }
341 }
343 static void calc_view_matrix()
344 {
345 view_matrix.reset_identity();
346 view_matrix.scale(Vector3(view_scale, view_scale, view_scale));
347 view_matrix.translate(Vector3(view_pan.x, view_pan.y, 0.0));
348 }
350 static Vector2 pixel_to_uv(int x, int y)
351 {
352 float u = win_aspect * (2.0 * (float)x / (float)win_width - 1.0);
353 float v = 1.0 - 2.0 * (float)y / (float)win_height;
355 u = u / view_scale - view_pan.x;
356 v = v / view_scale - view_pan.y;
357 return Vector2(u, v);
358 /*
359 Matrix4x4 inv_view_matrix = view_matrix.inverse();
360 Vector4 res = Vector4(u, v, 0.0, 1.0).transformed(inv_view_matrix);
362 return Vector2(res.x, res.y);
363 */
364 }
366 static int prev_x, prev_y;
367 static int click_pos[8][2];
368 static unsigned int bnstate;
370 #define BNBIT(x) (1 << (x))
372 void app_mouse_button(int bn, bool pressed, int x, int y)
373 {
374 prev_x = x;
375 prev_y = y;
376 if(pressed) {
377 bnstate |= BNBIT(bn);
378 } else {
379 bnstate &= ~BNBIT(bn);
380 }
382 if(pressed) {
383 click_pos[bn][0] = x;
384 click_pos[bn][1] = y;
385 } else {
386 int dx = x - click_pos[bn][0];
387 int dy = y - click_pos[bn][1];
389 if(abs(dx) + abs(dy) < 3) {
390 Vector2 uv = pixel_to_uv(x, y);
391 on_click(bn, uv.x, uv.y);
392 }
394 if(!(bnstate & BNBIT(2))) {
395 delete weight_label;
396 weight_label = 0;
397 post_redisplay();
398 }
399 }
400 }
402 static bool point_hit_test(const Vector2 &pos, Curve **curveret, int *pidxret)
403 {
404 float thres = 0.02 / view_scale;
406 for(size_t i=0; i<curves.size(); i++) {
407 int pidx = curves[i]->nearest_point(pos);
408 if(pidx == -1) continue;
410 Vector2 cp = curves[i]->get_point2(pidx);
411 if((cp - pos).length_sq() < thres * thres) {
412 *curveret = curves[i];
413 *pidxret = pidx;
414 return true;
415 }
416 }
417 *curveret = 0;
418 *pidxret = -1;
419 return false;
420 }
422 static bool hit_test(const Vector2 &pos, Curve **curveret, int *pidxret)
423 {
424 float thres = 0.02 / view_scale;
426 if(point_hit_test(pos, curveret, pidxret)) {
427 return true;
428 }
430 Vector3 pos3 = Vector3(pos.x, pos.y, 0.0f);
431 for(size_t i=0; i<curves.size(); i++) {
432 float x;
433 if((x = curves[i]->distance_sq(pos3)) < thres * thres) {
434 *curveret = curves[i];
435 *pidxret = -1;
436 return true;
437 }
438 }
439 *curveret = 0;
440 *pidxret = -1;
441 return false;
442 }
444 static Vector2 snap(const Vector2 &p)
445 {
446 switch(snap_mode) {
447 case SNAP_GRID:
448 return Vector2(round(p.x / grid_size) * grid_size, round(p.y / grid_size) * grid_size);
449 case SNAP_POINT:
450 {
451 Curve *nearest_curve = 0;
452 int nearest_curve_pidx = -1;
453 float nearest_dist_sq = FLT_MAX;
455 if(new_curve) {
456 // find the closest point, ignoring the last
457 for(int i=0; i<new_curve->size() - 1; i++) {
458 Vector2 cp = new_curve->get_point(i);
459 float distsq = (cp - p).length_sq();
460 if(distsq < nearest_dist_sq) {
461 nearest_curve = new_curve;
462 nearest_dist_sq = distsq;
463 nearest_curve_pidx = i;
464 }
465 }
466 }
469 for(size_t i=0; i<curves.size(); i++) {
470 int pidx = curves[i]->nearest_point(p);
471 Vector2 cp = curves[i]->get_point(pidx);
472 float dist_sq = (cp - p).length_sq();
473 if(dist_sq < nearest_dist_sq) {
474 nearest_curve = curves[i];
475 nearest_curve_pidx = pidx;
476 nearest_dist_sq = dist_sq;
477 }
478 }
480 if(nearest_curve) {
481 return nearest_curve->get_point(nearest_curve_pidx);
482 }
483 }
484 break;
486 default:
487 break;
488 }
489 return p;
490 }
492 void app_mouse_motion(int x, int y)
493 {
494 Vector2 prev_uv = pixel_to_uv(prev_x, prev_y);
496 int dx = x - prev_x;
497 int dy = y - prev_y;
498 prev_x = x;
499 prev_y = y;
501 if(!dx && !dy) return;
503 Vector2 uv = pixel_to_uv(x, y);
504 mouse_pointer = uv;
505 //post_redisplay();
507 /* when entering a new curve, have the last (extra) point following
508 * the mouse until it's entered by a click (see on_click).
509 */
510 if(new_curve) {
511 new_curve->move_point(new_curve->size() - 1, snap(uv));
512 post_redisplay();
513 }
515 if(!new_curve && !bnstate) {
516 // not dragging, highlight curve under mouse
517 hit_test(uv, &hover_curve, &hover_pidx);
518 if(hover_curve == sel_curve) {
519 sel_pidx = hover_pidx;
520 }
521 post_redisplay();
523 } else {
524 // we're dragging with one or more buttons held down
526 if(sel_curve && sel_pidx != -1) {
527 // we have a curve and a point of the curve selected
529 if(bnstate & BNBIT(0)) {
530 // dragging point with left button: move it
531 sel_curve->move_point(sel_pidx, snap(uv));
532 post_redisplay();
533 }
535 if(bnstate & BNBIT(2)) {
536 // dragging point with right button: change weight
537 float w = sel_curve->get_weight(sel_pidx);
538 w -= dy * 0.01;
539 if(w < FLT_MIN) w = FLT_MIN;
540 sel_curve->set_weight(sel_pidx, w);
542 // popup floating weight label if not already there
543 if(!weight_label) {
544 weight_label = new Label;
545 }
546 weight_label->set_position(uv);
547 weight_label->set_textf("w=%g", w);
548 post_redisplay();
549 }
550 } else {
551 // no selection, we're dragging in empty space: manipulate viewport
552 Vector2 dir = uv - prev_uv;
554 if(bnstate & (BNBIT(0) | BNBIT(1))) {
555 // panning
556 view_pan += dir;
557 calc_view_matrix();
558 post_redisplay();
559 }
560 if(bnstate & BNBIT(2)) {
561 // zooming
562 view_scale -= ((float)dy / (float)win_height) * view_scale * 5.0;
563 if(view_scale < 1e-4) view_scale = 1e-4;
564 calc_view_matrix();
565 post_redisplay();
566 }
567 }
568 }
569 }
571 static void on_click(int bn, float u, float v)
572 {
573 Vector2 uv = Vector2(u, v);
575 switch(bn) {
576 case 0: // ------- LEFT CLICK ------
577 if(hover_curve) {
578 // if we're hovering: click selects
579 sel_curve = hover_curve;
580 sel_pidx = hover_pidx;
581 hover_curve = 0;
582 } else if(sel_curve) {
583 // if we have a selected curve: click adds point (enter new_curve mode)
584 std::vector<Curve*>::iterator it = std::find(curves.begin(), curves.end(), sel_curve);
585 assert(it != curves.end());
586 curves.erase(it, it + 1);
588 new_curve = sel_curve;
589 sel_curve = 0;
590 sel_pidx = -1;
592 new_curve->add_point(uv);
593 } else {
594 // otherwise, click starts a new curve
595 if(!new_curve) {
596 new_curve = new Curve;
597 new_curve->add_point(uv);
598 }
599 new_curve->add_point(uv);
600 }
601 post_redisplay();
602 break;
604 case 2: // ------- RIGHT CLICK ------
605 if(new_curve) {
606 // in new-curve mode: finish curve (cancels last floating segment)
607 new_curve->remove_point(new_curve->size() - 1);
608 if(new_curve->empty()) {
609 delete new_curve;
610 } else {
611 curves.push_back(new_curve);
612 }
613 new_curve = 0;
615 } else if(sel_curve) {
616 // in selected curve mode: delete control point or unselect
617 Curve *hit_curve;
618 int hit_pidx;
619 if(hit_test(uv, &hit_curve, &hit_pidx) && hit_curve == sel_curve) {
620 if(hit_pidx != -1) {
621 hit_curve->remove_point(hit_pidx);
622 sel_pidx = -1;
623 }
624 } else {
625 sel_curve = 0;
626 sel_pidx = -1;
627 }
628 }
629 post_redisplay();
630 break;
632 default:
633 break;
634 }
635 }