curvedraw

view src/app.cc @ 1:7dcd0f6113e5

some ui and feedback stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 16 Dec 2015 04:49:16 +0200
parents 8e524989c904
children ce7aa9a0594c
line source
1 #include <stdlib.h>
2 #include <float.h>
3 #include <vector>
4 #include <algorithm>
5 #include "opengl.h"
6 #include "app.h"
7 #include "curve.h"
8 #include "widgets.h"
10 int win_width, win_height;
11 float win_aspect;
13 static void draw_grid(float sz, float sep, float alpha = 1.0f);
14 static void draw_curve(const Curve *curve);
15 static void on_click(int bn, float u, float v);
17 static float view_pan_x, view_pan_y;
18 static float view_scale = 1.0f;
20 static std::vector<Curve*> curves;
21 static Curve *sel_curve, *new_curve;
22 static int sel_pidx = -1;
24 static Label *weight_label;
27 bool app_init(int argc, char **argv)
28 {
29 glEnable(GL_MULTISAMPLE);
30 glEnable(GL_CULL_FACE);
31 return true;
32 }
34 void app_cleanup()
35 {
36 for(size_t i=0; i<curves.size(); i++) {
37 delete curves[i];
38 }
39 curves.clear();
40 }
42 void app_draw()
43 {
44 glClearColor(0.1, 0.1, 0.1, 1);
45 glClear(GL_COLOR_BUFFER_BIT);
47 glMatrixMode(GL_MODELVIEW);
48 glLoadIdentity();
49 glTranslatef(-view_pan_x, -view_pan_y, 0);
50 glScalef(view_scale, view_scale, view_scale);
52 draw_grid(std::max(win_aspect, 1.0f / win_aspect), 0.1);
54 for(size_t i=0; i<curves.size(); i++) {
55 draw_curve(curves[i]);
56 }
57 if(new_curve) {
58 // TODO special drawing with more feedback
59 draw_curve(new_curve);
60 }
61 if(weight_label) {
62 weight_label->draw();
63 }
64 }
66 static void draw_grid(float sz, float sep, float alpha)
67 {
68 float x = 0.0f;
70 glLineWidth(1.0);
71 glBegin(GL_LINES);
72 glColor4f(0.6, 0.3, 0.2, alpha);
73 glVertex2f(-sz, 0);
74 glVertex2f(sz, 0);
75 glColor4f(0.2, 0.3, 0.6, alpha);
76 glVertex2f(0, -sz);
77 glVertex2f(0, sz);
78 glColor4f(0.35, 0.35, 0.35, alpha);
79 while(x < sz) {
80 x += sep;
81 glVertex2f(-sz, x);
82 glVertex2f(sz, x);
83 glVertex2f(-sz, -x);
84 glVertex2f(sz, -x);
85 glVertex2f(x, -sz);
86 glVertex2f(x, sz);
87 glVertex2f(-x, -sz);
88 glVertex2f(-x, sz);
89 }
90 glEnd();
91 }
93 static void draw_curve(const Curve *curve)
94 {
95 int numpt = curve->get_point_count();
96 int segm = numpt * 16.0f;
98 glLineWidth(2.0);
99 if(curve == sel_curve) {
100 glColor3f(0.3, 0.4, 1.0);
101 } else if(curve == new_curve) {
102 glColor3f(1.0, 0.75, 0.3);
103 } else {
104 glColor3f(0.75, 0.75, 0.75);
105 }
106 glBegin(GL_LINE_STRIP);
107 for(int i=0; i<segm; i++) {
108 float t = (float)i / (float)(segm - 1);
109 Vector2 v = curve->interpolate(t);
110 glVertex2f(v.x, v.y);
111 }
112 glEnd();
113 glLineWidth(1.0);
115 glPointSize(7.0);
116 glBegin(GL_POINTS);
117 if(curve == new_curve) {
118 glColor3f(1.0, 0.0, 0.0);
119 } else {
120 glColor3f(0.6, 0.3, 0.2);
121 }
122 for(int i=0; i<numpt; i++) {
123 if(curve == sel_curve) {
124 if(i == sel_pidx) {
125 glColor3f(1.0, 0.2, 0.1);
126 } else {
127 glColor3f(0.2, 1.0, 0.2);
128 }
129 }
130 Vector2 pt = curve->get_point(i);
131 glVertex2f(pt.x, pt.y);
132 }
133 glEnd();
134 glPointSize(1.0);
135 }
137 void app_reshape(int x, int y)
138 {
139 win_width = x;
140 win_height = y;
141 win_aspect = (float)x / (float)y;
143 glViewport(0, 0, x, y);
144 glMatrixMode(GL_PROJECTION);
145 glLoadIdentity();
146 glOrtho(-win_aspect, win_aspect, -1, 1, -1, 1);
147 }
149 void app_keyboard(int key, bool pressed)
150 {
151 if(pressed) {
152 switch(key) {
153 case 'q':
154 case 'Q':
155 exit(0);
157 case 27:
158 if(new_curve) {
159 delete new_curve;
160 new_curve = 0;
161 post_redisplay();
162 }
163 break;
165 case 'l':
166 case 'L':
167 if(sel_curve) {
168 sel_curve->set_type(CURVE_LINEAR);
169 post_redisplay();
170 }
171 if(new_curve) {
172 new_curve->set_type(CURVE_LINEAR);
173 post_redisplay();
174 }
175 break;
177 case 'b':
178 case 'B':
179 if(sel_curve) {
180 sel_curve->set_type(CURVE_BSPLINE);
181 post_redisplay();
182 }
183 if(new_curve) {
184 new_curve->set_type(CURVE_BSPLINE);
185 post_redisplay();
186 }
187 break;
189 case 'h':
190 case 'H':
191 if(sel_curve) {
192 sel_curve->set_type(CURVE_HERMITE);
193 post_redisplay();
194 }
195 if(new_curve) {
196 new_curve->set_type(CURVE_HERMITE);
197 post_redisplay();
198 }
199 break;
200 }
201 }
202 }
204 static Vector2 pixel_to_uv(int x, int y)
205 {
206 float u = win_aspect * (2.0 * (float)x / (float)win_width - 1.0);
207 float v = 1.0 - 2.0 * (float)y / (float)win_height;
208 return Vector2(u, v);
209 }
211 static int prev_x, prev_y;
212 static int click_pos[8][2];
213 static unsigned int bnstate;
215 #define BNBIT(x) (1 << (x))
217 void app_mouse_button(int bn, bool pressed, int x, int y)
218 {
219 prev_x = x;
220 prev_y = y;
221 if(pressed) {
222 bnstate |= BNBIT(bn);
223 } else {
224 bnstate &= ~BNBIT(bn);
225 }
227 if(pressed) {
228 click_pos[bn][0] = x;
229 click_pos[bn][1] = y;
230 } else {
231 int dx = x - click_pos[bn][0];
232 int dy = y - click_pos[bn][1];
234 if(abs(dx) + abs(dy) < 3) {
235 Vector2 uv = pixel_to_uv(x, y);
236 on_click(bn, uv.x, uv.y);
237 }
239 if(!(bnstate & BNBIT(2))) {
240 delete weight_label;
241 weight_label = 0;
242 post_redisplay();
243 }
244 }
245 }
247 static void hover(int x, int y)
248 {
249 float thres = 0.02;
251 Vector2 uv = pixel_to_uv(x, y);
252 for(size_t i=0; i<curves.size(); i++) {
253 int pidx = curves[i]->nearest_point(uv);
254 if(pidx == -1) continue;
256 Vector2 cp = curves[i]->get_point(pidx);
257 if((cp - uv).length_sq() < thres * thres) {
258 sel_curve = curves[i];
259 sel_pidx = pidx;
260 return;
261 }
262 }
264 sel_curve = 0;
265 sel_pidx = -1;
266 }
268 void app_mouse_motion(int x, int y)
269 {
270 int dx = x - prev_x;
271 int dy = y - prev_y;
272 prev_x = x;
273 prev_y = y;
275 if(!dx && !dy) return;
277 if(!new_curve && !bnstate) {
278 hover(x, y);
279 post_redisplay();
280 }
282 if(sel_curve && sel_pidx != -1) {
283 if(bnstate & BNBIT(0)) {
284 float w = sel_curve->get_weight(sel_pidx);
285 sel_curve->set_point(sel_pidx, pixel_to_uv(x, y), w);
286 post_redisplay();
287 }
289 if(bnstate & BNBIT(2)) {
290 float w = sel_curve->get_weight(sel_pidx);
291 w -= dy * 0.01;
292 if(w < FLT_MIN) w = FLT_MIN;
293 sel_curve->set_weight(sel_pidx, w);
295 if(!weight_label) {
296 weight_label = new Label;
297 }
298 weight_label->set_position(pixel_to_uv(x, y));
299 weight_label->set_textf("w=%g", w);
300 post_redisplay();
301 }
302 }
303 }
305 static void on_click(int bn, float u, float v)
306 {
307 switch(bn) {
308 case 0:
309 if(!new_curve) {
310 new_curve = new Curve;
311 }
312 new_curve->add_point(Vector2(u, v));
313 post_redisplay();
314 break;
316 case 2:
317 curves.push_back(new_curve);
318 new_curve = 0;
319 post_redisplay();
320 break;
322 default:
323 break;
324 }
325 }