curvedraw

view src/app.cc @ 2:ce7aa9a0594c

improved curve editing
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 17 Dec 2015 05:13:25 +0200
parents 7dcd0f6113e5
children bf78387a9925
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 int win_width, win_height;
12 float win_aspect;
14 static void draw_grid(float sz, float sep, float alpha = 1.0f);
15 static void draw_curve(const Curve *curve);
16 static void on_click(int bn, float u, float v);
18 // viewport control
19 static Vector2 view_pan;
20 static float view_scale = 1.0f;
22 static std::vector<Curve*> curves;
23 static Curve *sel_curve; // selected curve being edited
24 static Curve *new_curve; // new curve being entered
25 static Curve *hover_curve; // curve the mouse is hovering over (click to select)
26 static int sel_pidx = -1; // selected point of the selected or hovered-over curve
28 static Label *weight_label; // floating label for the cp weight
31 bool app_init(int argc, char **argv)
32 {
33 glEnable(GL_MULTISAMPLE);
34 glEnable(GL_CULL_FACE);
36 glEnable(GL_BLEND);
37 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
38 return true;
39 }
41 void app_cleanup()
42 {
43 for(size_t i=0; i<curves.size(); i++) {
44 delete curves[i];
45 }
46 curves.clear();
47 }
49 void app_draw()
50 {
51 glClearColor(0.1, 0.1, 0.1, 1);
52 glClear(GL_COLOR_BUFFER_BIT);
54 glMatrixMode(GL_MODELVIEW);
55 glLoadIdentity();
56 glTranslatef(-view_pan.x, -view_pan.y, 0);
57 glScalef(view_scale, view_scale, view_scale);
59 draw_grid(std::max(win_aspect, 1.0f / win_aspect), 0.1);
61 for(size_t i=0; i<curves.size(); i++) {
62 draw_curve(curves[i]);
63 }
64 if(new_curve) {
65 draw_curve(new_curve);
66 }
67 if(weight_label) {
68 weight_label->draw();
69 }
70 }
72 static void draw_grid(float sz, float sep, float alpha)
73 {
74 float x = 0.0f;
76 glLineWidth(1.0);
77 glBegin(GL_LINES);
78 glColor4f(0.6, 0.3, 0.2, alpha);
79 glVertex2f(-sz, 0);
80 glVertex2f(sz, 0);
81 glColor4f(0.2, 0.3, 0.6, alpha);
82 glVertex2f(0, -sz);
83 glVertex2f(0, sz);
84 glColor4f(0.35, 0.35, 0.35, alpha);
85 while(x < sz) {
86 x += sep;
87 glVertex2f(-sz, x);
88 glVertex2f(sz, x);
89 glVertex2f(-sz, -x);
90 glVertex2f(sz, -x);
91 glVertex2f(x, -sz);
92 glVertex2f(x, sz);
93 glVertex2f(-x, -sz);
94 glVertex2f(-x, sz);
95 }
96 glEnd();
97 }
99 static void draw_curve(const Curve *curve)
100 {
101 int numpt = curve->size();
102 int segm = numpt * 16;
104 /*if(curve == hover_curve) {
105 glLineWidth(3.0);
106 glColor4f(0.8, 0.8, 0.0, 1.0);
108 glBegin(GL_LINE_STRIP);
109 for(int i=0; i<segm; i++) {
110 float t = (float)i / (float)(segm - 1);
111 Vector2 v = curve->interpolate(t);
112 glVertex2f(v.x, v.y);
113 }
114 glEnd();
115 }
116 */
118 glLineWidth(curve == hover_curve ? 4.0 : 2.0);
119 if(curve == sel_curve) {
120 glColor3f(0.3, 0.4, 1.0);
121 } else if(curve == new_curve) {
122 glColor3f(1.0, 0.75, 0.3);
123 } else {
124 glColor3f(0.6, 0.6, 0.6);
125 }
126 glBegin(GL_LINE_STRIP);
127 for(int i=0; i<segm; i++) {
128 float t = (float)i / (float)(segm - 1);
129 Vector2 v = curve->interpolate(t);
130 glVertex2f(v.x, v.y);
131 }
132 glEnd();
133 glLineWidth(1.0);
135 glPointSize(curve == hover_curve ? 10.0 : 7.0);
136 glBegin(GL_POINTS);
137 if(curve == new_curve) {
138 glColor3f(1.0, 0.0, 0.0);
139 } else {
140 glColor3f(0.6, 0.3, 0.2);
141 }
142 for(int i=0; i<numpt; i++) {
143 if(curve == sel_curve) {
144 if(i == sel_pidx) {
145 glColor3f(1.0, 0.2, 0.1);
146 } else {
147 glColor3f(0.2, 1.0, 0.2);
148 }
149 }
150 Vector2 pt = curve->get_point(i);
151 glVertex2f(pt.x, pt.y);
152 }
153 glEnd();
154 glPointSize(1.0);
155 }
157 void app_reshape(int x, int y)
158 {
159 win_width = x;
160 win_height = y;
161 win_aspect = (float)x / (float)y;
163 glViewport(0, 0, x, y);
164 glMatrixMode(GL_PROJECTION);
165 glLoadIdentity();
166 glOrtho(-win_aspect, win_aspect, -1, 1, -1, 1);
167 }
169 void app_keyboard(int key, bool pressed)
170 {
171 if(pressed) {
172 switch(key) {
173 case 'q':
174 case 'Q':
175 exit(0);
177 case 27:
178 if(new_curve) {
179 delete new_curve;
180 new_curve = 0;
181 post_redisplay();
182 }
183 break;
185 case 'l':
186 case 'L':
187 if(sel_curve) {
188 sel_curve->set_type(CURVE_LINEAR);
189 post_redisplay();
190 }
191 if(new_curve) {
192 new_curve->set_type(CURVE_LINEAR);
193 post_redisplay();
194 }
195 break;
197 case 'b':
198 case 'B':
199 if(sel_curve) {
200 sel_curve->set_type(CURVE_BSPLINE);
201 post_redisplay();
202 }
203 if(new_curve) {
204 new_curve->set_type(CURVE_BSPLINE);
205 post_redisplay();
206 }
207 break;
209 case 'h':
210 case 'H':
211 if(sel_curve) {
212 sel_curve->set_type(CURVE_HERMITE);
213 post_redisplay();
214 }
215 if(new_curve) {
216 new_curve->set_type(CURVE_HERMITE);
217 post_redisplay();
218 }
219 break;
220 }
221 }
222 }
224 static Vector2 pixel_to_uv(int x, int y)
225 {
226 float u = win_aspect * (2.0 * (float)x / (float)win_width - 1.0);
227 float v = 1.0 - 2.0 * (float)y / (float)win_height;
228 return Vector2(u, v);
229 }
231 static int prev_x, prev_y;
232 static int click_pos[8][2];
233 static unsigned int bnstate;
235 #define BNBIT(x) (1 << (x))
237 void app_mouse_button(int bn, bool pressed, int x, int y)
238 {
239 prev_x = x;
240 prev_y = y;
241 if(pressed) {
242 bnstate |= BNBIT(bn);
243 } else {
244 bnstate &= ~BNBIT(bn);
245 }
247 if(pressed) {
248 click_pos[bn][0] = x;
249 click_pos[bn][1] = y;
250 } else {
251 int dx = x - click_pos[bn][0];
252 int dy = y - click_pos[bn][1];
254 if(abs(dx) + abs(dy) < 3) {
255 Vector2 uv = pixel_to_uv(x, y);
256 on_click(bn, uv.x, uv.y);
257 }
259 if(!(bnstate & BNBIT(2))) {
260 delete weight_label;
261 weight_label = 0;
262 post_redisplay();
263 }
264 }
265 }
267 static bool point_hit_test(const Vector2 &pos, Curve **curveret, int *pidxret)
268 {
269 float thres = 0.02;
271 for(size_t i=0; i<curves.size(); i++) {
272 int pidx = curves[i]->nearest_point(pos);
273 if(pidx == -1) continue;
275 Vector2 cp = curves[i]->get_point(pidx);
276 if((cp - pos).length_sq() < thres * thres) {
277 *curveret = curves[i];
278 *pidxret = pidx;
279 return true;
280 }
281 }
282 *curveret = 0;
283 *pidxret = -1;
284 return false;
285 }
287 void app_mouse_motion(int x, int y)
288 {
289 int dx = x - prev_x;
290 int dy = y - prev_y;
291 prev_x = x;
292 prev_y = y;
294 if(!dx && !dy) return;
296 Vector2 uv = pixel_to_uv(x, y);
298 /* when entering a new curve, have the last (extra) point following
299 * the mouse until it's entered by a click (see on_click).
300 */
301 if(new_curve && !new_curve->empty()) {
302 new_curve->move_point(new_curve->size() - 1, uv);
303 post_redisplay();
304 }
306 if(!new_curve && !bnstate) {
307 point_hit_test(uv, &hover_curve, &sel_pidx);
308 post_redisplay();
309 }
311 if(sel_curve && sel_pidx != -1) {
312 if(bnstate & BNBIT(0)) {
313 float w = sel_curve->get_weight(sel_pidx);
314 sel_curve->set_point(sel_pidx, uv, w);
315 post_redisplay();
316 }
318 if(bnstate & BNBIT(2)) {
319 float w = sel_curve->get_weight(sel_pidx);
320 w -= dy * 0.01;
321 if(w < FLT_MIN) w = FLT_MIN;
322 sel_curve->set_weight(sel_pidx, w);
324 if(!weight_label) {
325 weight_label = new Label;
326 }
327 weight_label->set_position(uv);
328 weight_label->set_textf("w=%g", w);
329 post_redisplay();
330 }
331 }
332 }
334 static void on_click(int bn, float u, float v)
335 {
336 Vector2 uv = Vector2(u, v);
338 switch(bn) {
339 case 0: // ------- LEFT CLICK ------
340 if(hover_curve) {
341 // if we're hovering: click selects
342 sel_curve = hover_curve;
343 hover_curve = 0;
344 } else if(sel_curve) {
345 // if we have a selected curve: click adds point (enter new_curve mode)
346 std::vector<Curve*>::iterator it = std::find(curves.begin(), curves.end(), sel_curve);
347 assert(it != curves.end());
348 curves.erase(it, it + 1);
350 new_curve = sel_curve;
351 sel_curve = 0;
352 sel_pidx = -1;
354 new_curve->add_point(uv);
355 } else {
356 // otherwise, click starts a new curve
357 if(!new_curve) {
358 new_curve = new Curve;
359 new_curve->add_point(uv);
360 }
361 new_curve->add_point(uv);
362 }
363 post_redisplay();
364 break;
366 case 2: // ------- RIGHT CLICK ------
367 if(new_curve) {
368 // in new-curve mode: finish curve (cancels last floating segment)
369 new_curve->remove_point(new_curve->size() - 1);
370 if(new_curve->empty()) {
371 delete new_curve;
372 } else {
373 curves.push_back(new_curve);
374 }
375 new_curve = 0;
377 } else if(sel_curve) {
378 // in selected curve mode: delete control point or unselect
379 Curve *hit_curve;
380 int hit_pidx;
381 if(point_hit_test(uv, &hit_curve, &hit_pidx) && hit_curve == sel_curve) {
382 hit_curve->remove_point(hit_pidx);
383 sel_pidx = -1;
384 } else {
385 sel_curve = 0;
386 sel_pidx = -1;
387 }
388 }
389 post_redisplay();
390 break;
392 default:
393 break;
394 }
395 }