curvedraw

view src/app.cc @ 12:84a647283237

added all the extra functionality to the curve class first pass at a project-to-curve function (needs more work)
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 20 Dec 2015 07:21:32 +0200
parents 95fada20c638
children 4da693339d99
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"
10 #include "curvefile.h"
12 enum SnapMode {
13 SNAP_NONE,
14 SNAP_GRID,
15 SNAP_POINT
16 };
18 int win_width, win_height;
19 float win_aspect;
21 static void draw_grid(float sz, float sep, float alpha = 1.0f);
22 static void draw_curve(const Curve *curve);
23 static void on_click(int bn, float u, float v);
25 // viewport control
26 static Vector2 view_pan;
27 static float view_scale = 0.2f;
28 static Matrix4x4 view_matrix;
30 static float grid_size = 1.0;
31 static SnapMode snap_mode;
33 static bool show_bounds;
35 static std::vector<Curve*> curves;
36 static Curve *sel_curve; // selected curve being edited
37 static Curve *new_curve; // new curve being entered
38 static Curve *hover_curve; // curve the mouse is hovering over (click to select)
39 static int sel_pidx = -1; // selected point of the selected or hovered-over curve
41 static Label *weight_label; // floating label for the cp weight
43 static Vector2 mouse_pointer;
46 bool app_init(int argc, char **argv)
47 {
48 glewInit();
50 glEnable(GL_MULTISAMPLE);
51 glEnable(GL_CULL_FACE);
53 glEnable(GL_BLEND);
54 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
55 return true;
56 }
58 void app_cleanup()
59 {
60 for(size_t i=0; i<curves.size(); i++) {
61 delete curves[i];
62 }
63 curves.clear();
64 }
66 void app_draw()
67 {
68 glClearColor(0.1, 0.1, 0.1, 1);
69 glClear(GL_COLOR_BUFFER_BIT);
71 glMatrixMode(GL_MODELVIEW);
72 glLoadIdentity();
73 glTranslatef(view_pan.x * view_scale, view_pan.y * view_scale, 0);
74 glScalef(view_scale, view_scale, view_scale);
76 float max_aspect = std::max(win_aspect, 1.0f / win_aspect);
77 draw_grid(max_aspect, grid_size);
79 for(size_t i=0; i<curves.size(); i++) {
80 draw_curve(curves[i]);
81 }
82 if(new_curve) {
83 draw_curve(new_curve);
84 }
86 #ifdef DRAW_MOUSE_POINTER
87 glPointSize(6.0);
88 glBegin(GL_POINTS);
89 glColor3f(0, 0, 1);
90 glVertex2f(mouse_pointer.x, mouse_pointer.y);
91 glEnd();
92 #endif
94 glMatrixMode(GL_MODELVIEW);
95 glLoadIdentity();
97 if(weight_label) {
98 weight_label->draw();
99 }
100 }
102 static void draw_grid(float sz, float sep, float alpha)
103 {
104 float x = 0.0f;
105 float s = 1.0 / view_scale;
107 sz *= s;
108 sz += sep; // one more step for when we have non-zero fractional pan
109 float end = std::min(sz, 100.0f * sep);
111 // fractional pan
112 Vector2 pan = view_pan;
113 Vector2 fpan = Vector2(fmod(pan.x, sep), fmod(pan.y, sep));
114 Vector2 offset = fpan - pan;
116 glMatrixMode(GL_MODELVIEW);
117 glPushMatrix();
118 glTranslatef(offset.x, offset.y, 0);
120 glBegin(GL_LINES);
121 glColor4f(0.35, 0.35, 0.35, alpha);
122 while(x <= end) {
123 glVertex2f(-end, x);
124 glVertex2f(end, x);
125 glVertex2f(-end, -x);
126 glVertex2f(end, -x);
127 glVertex2f(x, -end);
128 glVertex2f(x, end);
129 glVertex2f(-x, -end);
130 glVertex2f(-x, end);
131 x += sep;
132 }
133 glEnd();
134 glPopMatrix();
137 glLineWidth(1.0);
138 glBegin(GL_LINES);
139 glColor4f(0.6, 0.3, 0.2, alpha);
140 glVertex2f(-sz + offset.x, 0);
141 glVertex2f(sz + offset.x, 0);
142 glColor4f(0.2, 0.3, 0.6, alpha);
143 glVertex2f(0, -sz + offset.y);
144 glVertex2f(0, sz + offset.y);
145 glEnd();
147 }
149 static void draw_curve(const Curve *curve)
150 {
151 int numpt = curve->size();
152 int segm = numpt * 16;
154 if(show_bounds) {
155 Vector3 bmin, bmax;
156 curve->get_bbox(&bmin, &bmax);
158 glLineWidth(1.0);
159 glColor3f(0, 1, 0);
160 glBegin(GL_LINE_LOOP);
161 glVertex2f(bmin.x, bmin.y);
162 glVertex2f(bmax.x, bmin.y);
163 glVertex2f(bmax.x, bmax.y);
164 glVertex2f(bmin.x, bmax.y);
165 glEnd();
166 }
168 glLineWidth(curve == hover_curve ? 4.0 : 2.0);
169 if(curve == sel_curve) {
170 glColor3f(0.3, 0.4, 1.0);
171 } else if(curve == new_curve) {
172 glColor3f(1.0, 0.75, 0.3);
173 } else {
174 glColor3f(0.6, 0.6, 0.6);
175 }
176 glBegin(GL_LINE_STRIP);
177 for(int i=0; i<segm; i++) {
178 float t = (float)i / (float)(segm - 1);
179 Vector3 v = curve->interpolate(t);
180 glVertex2f(v.x, v.y);
181 }
182 glEnd();
183 glLineWidth(1.0);
185 glPointSize(curve == hover_curve ? 10.0 : 7.0);
186 glBegin(GL_POINTS);
187 if(curve == new_curve) {
188 glColor3f(1.0, 0.0, 0.0);
189 } else {
190 glColor3f(0.6, 0.3, 0.2);
191 }
192 for(int i=0; i<numpt; i++) {
193 if(curve == sel_curve) {
194 if(i == sel_pidx) {
195 glColor3f(1.0, 0.2, 0.1);
196 } else {
197 glColor3f(0.2, 1.0, 0.2);
198 }
199 }
200 Vector2 pt = curve->get_point2(i);
201 glVertex2f(pt.x, pt.y);
202 }
203 glEnd();
205 // draw the projected mouse point on the selected curve
206 /*
207 if(curve == sel_curve) {
208 Vector3 pp = curve->proj_point(Vector3(mouse_pointer.x, mouse_pointer.y, 0.0));
210 glPointSize(5.0);
211 glBegin(GL_POINTS);
212 glColor3f(1, 0.8, 0.2);
213 glVertex2f(pp.x, pp.y);
214 glEnd();
215 }
216 */
217 glPointSize(1.0);
218 }
220 void app_reshape(int x, int y)
221 {
222 win_width = x;
223 win_height = y;
224 win_aspect = (float)x / (float)y;
226 glViewport(0, 0, x, y);
227 glMatrixMode(GL_PROJECTION);
228 glLoadIdentity();
229 glOrtho(-win_aspect, win_aspect, -1, 1, -1, 1);
230 }
232 void app_keyboard(int key, bool pressed)
233 {
234 if(pressed) {
235 switch(key) {
236 case 'q':
237 case 'Q':
238 exit(0);
240 case 27:
241 if(new_curve) {
242 delete new_curve;
243 new_curve = 0;
244 post_redisplay();
245 }
246 break;
248 case '1':
249 case '2':
250 case '3':
251 if(sel_curve) {
252 sel_curve->set_type((CurveType)((int)CURVE_LINEAR + key - '1'));
253 post_redisplay();
254 }
255 if(new_curve) {
256 new_curve->set_type((CurveType)((int)CURVE_LINEAR + key - '1'));
257 post_redisplay();
258 }
259 break;
261 case 'b':
262 case 'B':
263 show_bounds = !show_bounds;
264 post_redisplay();
265 break;
267 case 'n':
268 case 'N':
269 if(sel_curve) {
270 sel_curve->normalize();
271 post_redisplay();
272 }
273 break;
275 case 'e':
276 case 'E':
277 // TODO: GUI for filename at least
278 if(!save_curves("test.curves", &curves[0], (int)curves.size())) {
279 fprintf(stderr, "failed to export curves\n");
280 }
281 printf("exported %d curves\n", (int)curves.size());
282 break;
284 case 'l':
285 case 'L':
286 {
287 std::list<Curve*> clist = load_curves("test.curves");
288 if(clist.empty()) {
289 fprintf(stderr, "failed to import curves\n");
290 }
292 for(size_t i=0; i<curves.size(); i++) {
293 delete curves[i];
294 }
295 curves.clear();
297 int num = 0;
298 std::list<Curve*>::iterator it = clist.begin();
299 while(it != clist.end()) {
300 curves.push_back(*it++);
301 ++num;
302 }
303 printf("imported %d curves\n", num);
304 }
305 post_redisplay();
306 break;
307 }
308 }
311 switch(key) {
312 case 's':
313 snap_mode = pressed ? SNAP_GRID : SNAP_NONE;
314 break;
316 case 'S':
317 snap_mode = pressed ? SNAP_POINT : SNAP_NONE;
318 break;
320 default:
321 break;
322 }
323 }
325 static void calc_view_matrix()
326 {
327 view_matrix.reset_identity();
328 view_matrix.scale(Vector3(view_scale, view_scale, view_scale));
329 view_matrix.translate(Vector3(view_pan.x, view_pan.y, 0.0));
330 }
332 static Vector2 pixel_to_uv(int x, int y)
333 {
334 float u = win_aspect * (2.0 * (float)x / (float)win_width - 1.0);
335 float v = 1.0 - 2.0 * (float)y / (float)win_height;
337 u = u / view_scale - view_pan.x;
338 v = v / view_scale - view_pan.y;
339 return Vector2(u, v);
340 /*
341 Matrix4x4 inv_view_matrix = view_matrix.inverse();
342 Vector4 res = Vector4(u, v, 0.0, 1.0).transformed(inv_view_matrix);
344 return Vector2(res.x, res.y);
345 */
346 }
348 static int prev_x, prev_y;
349 static int click_pos[8][2];
350 static unsigned int bnstate;
352 #define BNBIT(x) (1 << (x))
354 void app_mouse_button(int bn, bool pressed, int x, int y)
355 {
356 prev_x = x;
357 prev_y = y;
358 if(pressed) {
359 bnstate |= BNBIT(bn);
360 } else {
361 bnstate &= ~BNBIT(bn);
362 }
364 if(pressed) {
365 click_pos[bn][0] = x;
366 click_pos[bn][1] = y;
367 } else {
368 int dx = x - click_pos[bn][0];
369 int dy = y - click_pos[bn][1];
371 if(abs(dx) + abs(dy) < 3) {
372 Vector2 uv = pixel_to_uv(x, y);
373 on_click(bn, uv.x, uv.y);
374 }
376 if(!(bnstate & BNBIT(2))) {
377 delete weight_label;
378 weight_label = 0;
379 post_redisplay();
380 }
381 }
382 }
384 static bool point_hit_test(const Vector2 &pos, Curve **curveret, int *pidxret)
385 {
386 float thres = 0.02 / view_scale;
388 for(size_t i=0; i<curves.size(); i++) {
389 int pidx = curves[i]->nearest_point(pos);
390 if(pidx == -1) continue;
392 Vector2 cp = curves[i]->get_point(pidx);
393 if((cp - pos).length_sq() < thres * thres) {
394 *curveret = curves[i];
395 *pidxret = pidx;
396 return true;
397 }
398 }
399 *curveret = 0;
400 *pidxret = -1;
401 return false;
402 }
404 static Vector2 snap(const Vector2 &p)
405 {
406 switch(snap_mode) {
407 case SNAP_GRID:
408 return Vector2(round(p.x / grid_size) * grid_size, round(p.y / grid_size) * grid_size);
409 case SNAP_POINT:
410 // TODO
411 default:
412 break;
413 }
414 return p;
415 }
417 void app_mouse_motion(int x, int y)
418 {
419 Vector2 prev_uv = pixel_to_uv(prev_x, prev_y);
421 int dx = x - prev_x;
422 int dy = y - prev_y;
423 prev_x = x;
424 prev_y = y;
426 if(!dx && !dy) return;
428 Vector2 uv = pixel_to_uv(x, y);
429 mouse_pointer = uv;
430 post_redisplay();
432 /* when entering a new curve, have the last (extra) point following
433 * the mouse until it's entered by a click (see on_click).
434 */
435 if(new_curve) {
436 new_curve->move_point(new_curve->size() - 1, snap(uv));
437 post_redisplay();
438 }
440 if(!new_curve && !bnstate) {
441 // not dragging, highlight curve under mouse
442 point_hit_test(uv, &hover_curve, &sel_pidx);
443 post_redisplay();
445 } else {
446 // we're dragging with one or more buttons held down
448 if(sel_curve && sel_pidx != -1) {
449 // we have a curve and a point of the curve selected
451 if(bnstate & BNBIT(0)) {
452 // dragging point with left button: move it
453 sel_curve->move_point(sel_pidx, snap(uv));
454 post_redisplay();
455 }
457 if(bnstate & BNBIT(2)) {
458 // dragging point with right button: change weight
459 float w = sel_curve->get_weight(sel_pidx);
460 w -= dy * 0.01;
461 if(w < FLT_MIN) w = FLT_MIN;
462 sel_curve->set_weight(sel_pidx, w);
464 // popup floating weight label if not already there
465 if(!weight_label) {
466 weight_label = new Label;
467 }
468 weight_label->set_position(uv);
469 weight_label->set_textf("w=%g", w);
470 post_redisplay();
471 }
472 } else {
473 // no selection, we're dragging in empty space: manipulate viewport
474 Vector2 dir = uv - prev_uv;
476 if(bnstate & (BNBIT(0) | BNBIT(1))) {
477 // panning
478 view_pan += dir;
479 calc_view_matrix();
480 post_redisplay();
481 }
482 if(bnstate & BNBIT(2)) {
483 // zooming
484 view_scale -= ((float)dy / (float)win_height) * view_scale * 5.0;
485 if(view_scale < 1e-4) view_scale = 1e-4;
486 calc_view_matrix();
487 post_redisplay();
488 }
489 }
490 }
491 }
493 static void on_click(int bn, float u, float v)
494 {
495 Vector2 uv = Vector2(u, v);
497 switch(bn) {
498 case 0: // ------- LEFT CLICK ------
499 if(hover_curve) {
500 // if we're hovering: click selects
501 sel_curve = hover_curve;
502 hover_curve = 0;
503 } else if(sel_curve) {
504 // if we have a selected curve: click adds point (enter new_curve mode)
505 std::vector<Curve*>::iterator it = std::find(curves.begin(), curves.end(), sel_curve);
506 assert(it != curves.end());
507 curves.erase(it, it + 1);
509 new_curve = sel_curve;
510 sel_curve = 0;
511 sel_pidx = -1;
513 new_curve->add_point(uv);
514 } else {
515 // otherwise, click starts a new curve
516 if(!new_curve) {
517 new_curve = new Curve;
518 new_curve->add_point(uv);
519 }
520 new_curve->add_point(uv);
521 }
522 post_redisplay();
523 break;
525 case 2: // ------- RIGHT CLICK ------
526 if(new_curve) {
527 // in new-curve mode: finish curve (cancels last floating segment)
528 new_curve->remove_point(new_curve->size() - 1);
529 if(new_curve->empty()) {
530 delete new_curve;
531 } else {
532 curves.push_back(new_curve);
533 }
534 new_curve = 0;
536 } else if(sel_curve) {
537 // in selected curve mode: delete control point or unselect
538 Curve *hit_curve;
539 int hit_pidx;
540 if(point_hit_test(uv, &hit_curve, &hit_pidx) && hit_curve == sel_curve) {
541 hit_curve->remove_point(hit_pidx);
542 sel_pidx = -1;
543 } else {
544 sel_curve = 0;
545 sel_pidx = -1;
546 }
547 }
548 post_redisplay();
549 break;
551 default:
552 break;
553 }
554 }