draw the brush size with pressure applied (when the tablet is used), nice to see...
[blender.git] / source / blender / editors / sculpt_paint / paint_cursor.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software  Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2009 by Nicholas Bishop
19  * All rights reserved.
20  *
21  * Contributor(s): Jason Wilkins, Tom Musgrove.
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  *
25  */
26
27 /** \file blender/editors/sculpt_paint/paint_cursor.c
28  *  \ingroup edsculpt
29  */
30
31 #include "MEM_guardedalloc.h"
32
33 #include "BLI_math.h"
34 #include "BLI_rect.h"
35 #include "BLI_utildefines.h"
36
37 #include "DNA_brush_types.h"
38 #include "DNA_color_types.h"
39 #include "DNA_object_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_userdef_types.h"
43
44 #include "BKE_brush.h"
45 #include "BKE_context.h"
46 #include "BKE_paint.h"
47
48 #include "WM_api.h"
49
50 #include "BIF_gl.h"
51 #include "BIF_glutil.h"
52
53 #include "ED_view3d.h"
54
55 #include "paint_intern.h"
56 /* still needed for sculpt_stroke_get_location, should be
57  * removed eventually (TODO) */
58 #include "sculpt_intern.h"
59
60 /* TODOs:
61  *
62  * Some of the cursor drawing code is doing non-draw stuff
63  * (e.g. updating the brush rake angle). This should be cleaned up
64  * still.
65  *
66  * There is also some ugliness with sculpt-specific code.
67  */
68
69 typedef struct Snapshot {
70         float size[3];
71         float ofs[3];
72         float rot;
73         int BKE_brush_size_get;
74         int winx;
75         int winy;
76         int brush_map_mode;
77         int curve_changed_timestamp;
78 } Snapshot;
79
80 static int same_snap(Snapshot *snap, Brush *brush, ViewContext *vc)
81 {
82         MTex *mtex = &brush->mtex;
83
84         return (((mtex->tex) &&
85                  equals_v3v3(mtex->ofs, snap->ofs) &&
86                  equals_v3v3(mtex->size, snap->size) &&
87                  mtex->rot == snap->rot) &&
88
89                 /* make brush smaller shouldn't cause a resample */
90                 ((mtex->brush_map_mode == MTEX_MAP_MODE_VIEW &&
91                   (BKE_brush_size_get(vc->scene, brush) <= snap->BKE_brush_size_get)) ||
92                  (BKE_brush_size_get(vc->scene, brush) == snap->BKE_brush_size_get)) &&
93
94                 (mtex->brush_map_mode == snap->brush_map_mode) &&
95                 (vc->ar->winx == snap->winx) &&
96                 (vc->ar->winy == snap->winy));
97 }
98
99 static void make_snap(Snapshot *snap, Brush *brush, ViewContext *vc)
100 {
101         if (brush->mtex.tex) {
102                 snap->brush_map_mode = brush->mtex.brush_map_mode;
103                 copy_v3_v3(snap->ofs, brush->mtex.ofs);
104                 copy_v3_v3(snap->size, brush->mtex.size);
105                 snap->rot = brush->mtex.rot;
106         }
107         else {
108                 snap->brush_map_mode = -1;
109                 snap->ofs[0] = snap->ofs[1] = snap->ofs[2] = -1;
110                 snap->size[0] = snap->size[1] = snap->size[2] = -1;
111                 snap->rot = -1;
112         }
113
114         snap->BKE_brush_size_get = BKE_brush_size_get(vc->scene, brush);
115         snap->winx = vc->ar->winx;
116         snap->winy = vc->ar->winy;
117 }
118
119 static int load_tex(Brush *br, ViewContext *vc)
120 {
121         static GLuint overlay_texture = 0;
122         static int init = 0;
123         static int tex_changed_timestamp = -1;
124         static int curve_changed_timestamp = -1;
125         static Snapshot snap;
126         static int old_size = -1;
127
128         GLubyte *buffer = NULL;
129
130         int size;
131         int j;
132         int refresh;
133         
134         if (br->mtex.brush_map_mode == MTEX_MAP_MODE_TILED && !br->mtex.tex) return 0;
135         
136         refresh = 
137             !overlay_texture ||
138             (br->mtex.tex &&
139              (!br->mtex.tex->preview ||
140               br->mtex.tex->preview->changed_timestamp[0] != tex_changed_timestamp)) ||
141             !br->curve ||
142             br->curve->changed_timestamp != curve_changed_timestamp ||
143             !same_snap(&snap, br, vc);
144
145         if (refresh) {
146                 if (br->mtex.tex && br->mtex.tex->preview)
147                         tex_changed_timestamp = br->mtex.tex->preview->changed_timestamp[0];
148
149                 if (br->curve)
150                         curve_changed_timestamp = br->curve->changed_timestamp;
151
152                 make_snap(&snap, br, vc);
153
154                 if (br->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW) {
155                         int s = BKE_brush_size_get(vc->scene, br);
156                         int r = 1;
157
158                         for (s >>= 1; s > 0; s >>= 1)
159                                 r++;
160
161                         size = (1 << r);
162
163                         if (size < 256)
164                                 size = 256;
165
166                         if (size < old_size)
167                                 size = old_size;
168                 }
169                 else
170                         size = 512;
171
172                 if (old_size != size) {
173                         if (overlay_texture) {
174                                 glDeleteTextures(1, &overlay_texture);
175                                 overlay_texture = 0;
176                         }
177
178                         init = 0;
179
180                         old_size = size;
181                 }
182
183                 buffer = MEM_mallocN(sizeof(GLubyte) * size * size, "load_tex");
184
185                 #pragma omp parallel for schedule(static)
186                 for (j = 0; j < size; j++) {
187                         int i;
188                         float y;
189                         float len;
190
191                         for (i = 0; i < size; i++) {
192
193                                 // largely duplicated from tex_strength
194
195                                 const float rotation = -br->mtex.rot;
196                                 float radius = BKE_brush_size_get(vc->scene, br);
197                                 int index = j * size + i;
198                                 float x;
199                                 float avg;
200
201                                 x = (float)i / size;
202                                 y = (float)j / size;
203
204                                 x -= 0.5f;
205                                 y -= 0.5f;
206
207                                 if (br->mtex.brush_map_mode == MTEX_MAP_MODE_TILED) {
208                                         x *= vc->ar->winx / radius;
209                                         y *= vc->ar->winy / radius;
210                                 }
211                                 else {
212                                         x *= 2;
213                                         y *= 2;
214                                 }
215
216                                 len = sqrtf(x * x + y * y);
217
218                                 if ((br->mtex.brush_map_mode == MTEX_MAP_MODE_TILED) || len <= 1) {
219                                         /* it is probably worth optimizing for those cases where 
220                                          * the texture is not rotated by skipping the calls to
221                                          * atan2, sqrtf, sin, and cos. */
222                                         if (br->mtex.tex && (rotation > 0.001f || rotation < -0.001f)) {
223                                                 const float angle    = atan2f(y, x) + rotation;
224
225                                                 x = len * cosf(angle);
226                                                 y = len * sinf(angle);
227                                         }
228
229                                         x *= br->mtex.size[0];
230                                         y *= br->mtex.size[1];
231
232                                         x += br->mtex.ofs[0];
233                                         y += br->mtex.ofs[1];
234
235                                         avg = br->mtex.tex ? paint_get_tex_pixel(br, x, y) : 1;
236
237                                         avg += br->texture_sample_bias;
238
239                                         if (br->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW)
240                                                 avg *= BKE_brush_curve_strength(br, len, 1);  /* Falloff curve */
241
242                                         buffer[index] = 255 - (GLubyte)(255 * avg);
243                                 }
244                                 else {
245                                         buffer[index] = 0;
246                                 }
247                         }
248                 }
249
250                 if (!overlay_texture)
251                         glGenTextures(1, &overlay_texture);
252         }
253         else {
254                 size = old_size;
255         }
256
257         glBindTexture(GL_TEXTURE_2D, overlay_texture);
258
259         if (refresh) {
260                 if (!init) {
261                         glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, size, size, 0, GL_ALPHA, GL_UNSIGNED_BYTE, buffer);
262                         init = 1;
263                 }
264                 else {
265                         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, size, size, GL_ALPHA, GL_UNSIGNED_BYTE, buffer);
266                 }
267
268                 if (buffer)
269                         MEM_freeN(buffer);
270         }
271
272         glEnable(GL_TEXTURE_2D);
273
274         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
275         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
276         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
277
278         if (br->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW) {
279                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
280                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
281         }
282
283         return 1;
284 }
285
286 static int project_brush_radius(ViewContext *vc,
287                                 float radius,
288                                 const float location[3])
289 {
290         float view[3], nonortho[3], ortho[3], offset[3], p1[2], p2[2];
291
292         ED_view3d_global_to_vector(vc->rv3d, location, view);
293
294         /* create a vector that is not orthogonal to view */
295
296         if (fabsf(view[0]) < 0.1f) {
297                 nonortho[0] = view[0] + 1.0f;
298                 nonortho[1] = view[1];
299                 nonortho[2] = view[2];
300         }
301         else if (fabsf(view[1]) < 0.1f) {
302                 nonortho[0] = view[0];
303                 nonortho[1] = view[1] + 1.0f;
304                 nonortho[2] = view[2];
305         }
306         else {
307                 nonortho[0] = view[0];
308                 nonortho[1] = view[1];
309                 nonortho[2] = view[2] + 1.0f;
310         }
311
312         /* get a vector in the plane of the view */
313         cross_v3_v3v3(ortho, nonortho, view);
314         normalize_v3(ortho);
315
316         /* make a point on the surface of the brush tagent to the view */
317         mul_v3_fl(ortho, radius);
318         add_v3_v3v3(offset, location, ortho);
319
320         /* project the center of the brush, and the tangent point to the view onto the screen */
321         if ((ED_view3d_project_float_global(vc->ar, location, p1, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) &&
322             (ED_view3d_project_float_global(vc->ar, offset,   p2, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK))
323         {
324                 /* the distance between these points is the size of the projected brush in pixels */
325                 return len_v2v2(p1, p2);
326         }
327         else {
328                 BLI_assert(0);  /* assert because the code that sets up the vectors should disallow this */
329                 return 0;
330         }
331 }
332
333 static int sculpt_get_brush_geometry(bContext *C, ViewContext *vc,
334                                      int x, int y, int *pixel_radius,
335                                      float location[3])
336 {
337         Scene *scene = CTX_data_scene(C);
338         Paint *paint = paint_get_active_from_context(C);
339         float window[2];
340         int hit;
341
342         window[0] = x + vc->ar->winrct.xmin;
343         window[1] = y + vc->ar->winrct.ymin;
344
345         if (vc->obact->sculpt && vc->obact->sculpt->pbvh &&
346             sculpt_stroke_get_location(C, location, window))
347         {
348                 Brush *brush = paint_brush(paint);
349                 *pixel_radius =
350                     project_brush_radius(vc,
351                                          BKE_brush_unprojected_radius_get(scene, brush),
352                                          location);
353
354                 if (*pixel_radius == 0)
355                         *pixel_radius = BKE_brush_size_get(scene, brush);
356
357                 mul_m4_v3(vc->obact->obmat, location);
358
359                 hit = 1;
360         }
361         else {
362                 Sculpt *sd    = CTX_data_tool_settings(C)->sculpt;
363                 Brush *brush = paint_brush(&sd->paint);
364
365                 *pixel_radius = BKE_brush_size_get(scene, brush);
366                 hit = 0;
367         }
368
369         return hit;
370 }
371
372 /* Draw an overlay that shows what effect the brush's texture will
373  * have on brush strength */
374 /* TODO: sculpt only for now */
375 static void paint_draw_alpha_overlay(UnifiedPaintSettings *ups, Brush *brush,
376                                      ViewContext *vc, int x, int y)
377 {
378         rctf quad;
379
380         /* check for overlay mode */
381         if (!(brush->flag & BRUSH_TEXTURE_OVERLAY) ||
382             !(ELEM(brush->mtex.brush_map_mode, MTEX_MAP_MODE_VIEW, MTEX_MAP_MODE_TILED)))
383         {
384                 return;
385         }
386
387         /* save lots of GL state
388          * TODO: check on whether all of these are needed? */
389         glPushAttrib(GL_COLOR_BUFFER_BIT |
390                      GL_CURRENT_BIT |
391                      GL_DEPTH_BUFFER_BIT |
392                      GL_ENABLE_BIT |
393                      GL_LINE_BIT |
394                      GL_POLYGON_BIT |
395                      GL_STENCIL_BUFFER_BIT |
396                      GL_TRANSFORM_BIT |
397                      GL_VIEWPORT_BIT |
398                      GL_TEXTURE_BIT);
399
400         if (load_tex(brush, vc)) {
401                 glEnable(GL_BLEND);
402
403                 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
404                 glDepthMask(GL_FALSE);
405                 glDepthFunc(GL_ALWAYS);
406
407                 glMatrixMode(GL_TEXTURE);
408                 glPushMatrix();
409                 glLoadIdentity();
410
411                 if (brush->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW) {
412                         /* brush rotation */
413                         glTranslatef(0.5, 0.5, 0);
414                         glRotatef((double)RAD2DEGF((brush->flag & BRUSH_RAKE) ?
415                                                    ups->last_angle : ups->special_rotation),
416                                   0.0, 0.0, 1.0);
417                         glTranslatef(-0.5f, -0.5f, 0);
418
419                         /* scale based on tablet pressure */
420                         if (ups->draw_pressure && BKE_brush_use_size_pressure(vc->scene, brush)) {
421                                 glTranslatef(0.5f, 0.5f, 0);
422                                 glScalef(1.0f / ups->pressure_value, 1.0f / ups->pressure_value, 1);
423                                 glTranslatef(-0.5f, -0.5f, 0);
424                         }
425
426                         if (ups->draw_anchored) {
427                                 const float *aim = ups->anchored_initial_mouse;
428                                 const rcti *win = &vc->ar->winrct;
429                                 quad.xmin = aim[0] - ups->anchored_size - win->xmin;
430                                 quad.ymin = aim[1] - ups->anchored_size - win->ymin;
431                                 quad.xmax = aim[0] + ups->anchored_size - win->xmin;
432                                 quad.ymax = aim[1] + ups->anchored_size - win->ymin;
433                         }
434                         else {
435                                 const int radius = BKE_brush_size_get(vc->scene, brush);
436                                 quad.xmin = x - radius;
437                                 quad.ymin = y - radius;
438                                 quad.xmax = x + radius;
439                                 quad.ymax = y + radius;
440                         }
441                 }
442                 else {
443                         quad.xmin = 0;
444                         quad.ymin = 0;
445                         quad.xmax = BLI_rcti_size_x(&vc->ar->winrct);
446                         quad.ymax = BLI_rcti_size_y(&vc->ar->winrct);
447                 }
448
449                 /* set quad color */
450                 glColor4f(U.sculpt_paint_overlay_col[0],
451                           U.sculpt_paint_overlay_col[1],
452                           U.sculpt_paint_overlay_col[2],
453                           brush->texture_overlay_alpha / 100.0f);
454
455                 /* draw textured quad */
456                 glBegin(GL_QUADS);
457                 glTexCoord2f(0, 0);
458                 glVertex2f(quad.xmin, quad.ymin);
459                 glTexCoord2f(1, 0);
460                 glVertex2f(quad.xmax, quad.ymin);
461                 glTexCoord2f(1, 1);
462                 glVertex2f(quad.xmax, quad.ymax);
463                 glTexCoord2f(0, 1);
464                 glVertex2f(quad.xmin, quad.ymax);
465                 glEnd();
466
467                 glPopMatrix();
468         }
469
470         glPopAttrib();
471 }
472
473 /* Special actions taken when paint cursor goes over mesh */
474 /* TODO: sculpt only for now */
475 static void paint_cursor_on_hit(UnifiedPaintSettings *ups, Brush *brush, ViewContext *vc,
476                                 const float location[3])
477 {
478         float unprojected_radius, projected_radius;
479
480         /* update the brush's cached 3D radius */
481         if (!BKE_brush_use_locked_size(vc->scene, brush)) {
482                 /* get 2D brush radius */
483                 if (ups->draw_anchored)
484                         projected_radius = ups->anchored_size;
485                 else {
486                         if (brush->flag & BRUSH_ANCHORED)
487                                 projected_radius = 8;
488                         else
489                                 projected_radius = BKE_brush_size_get(vc->scene, brush);
490                 }
491         
492                 /* convert brush radius from 2D to 3D */
493                 unprojected_radius = paint_calc_object_space_radius(vc, location,
494                                                                     projected_radius);
495
496                 /* scale 3D brush radius by pressure */
497                 if (ups->draw_pressure && BKE_brush_use_size_pressure(vc->scene, brush))
498                         unprojected_radius *= ups->pressure_value;
499
500                 /* set cached value in either Brush or UnifiedPaintSettings */
501                 BKE_brush_unprojected_radius_set(vc->scene, brush, unprojected_radius);
502         }
503 }
504
505 static void paint_draw_cursor(bContext *C, int x, int y, void *UNUSED(unused))
506 {
507         Scene *scene = CTX_data_scene(C);
508         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
509         Paint *paint = paint_get_active_from_context(C);
510         Brush *brush = paint_brush(paint);
511         ViewContext vc;
512         float final_radius;
513         float translation[2];
514         float outline_alpha, *outline_col;
515         
516         /* set various defaults */
517         translation[0] = x;
518         translation[1] = y;
519         outline_alpha = 0.5;
520         outline_col = brush->add_col;
521         final_radius = BKE_brush_size_get(scene, brush);
522
523         /* check that brush drawing is enabled */
524         if (!(paint->flags & PAINT_SHOW_BRUSH))
525                 return;
526
527         /* can't use stroke vc here because this will be called during
528          * mouse over too, not just during a stroke */
529         view3d_set_viewcontext(C, &vc);
530
531         /* TODO: as sculpt and other paint modes are unified, this
532          * special mode of drawing will go away */
533         if (vc.obact->sculpt) {
534                 float location[3];
535                 int pixel_radius, hit;
536
537                 /* this is probably here so that rake takes into
538                  * account the brush movements before the stroke
539                  * starts, but this doesn't really belong in draw code
540                  *  TODO) */
541                 {
542                         const float u = 0.5f;
543                         const float v = 1 - u;
544                         const float r = 20;
545
546                         const float dx = ups->last_x - x;
547                         const float dy = ups->last_y - y;
548
549                         if (dx * dx + dy * dy >= r * r) {
550                                 ups->last_angle = atan2(dx, dy);
551
552                                 ups->last_x = u * ups->last_x + v * x;
553                                 ups->last_y = u * ups->last_y + v * y;
554                         }
555                 }
556
557                 /* test if brush is over the mesh */
558                 hit = sculpt_get_brush_geometry(C, &vc, x, y, &pixel_radius, location);
559
560                 /* draw overlay */
561                 paint_draw_alpha_overlay(ups, brush, &vc, x, y);
562
563                 if (BKE_brush_use_locked_size(scene, brush))
564                         BKE_brush_size_set(scene, brush, pixel_radius);
565
566                 /* check if brush is subtracting, use different color then */
567                 /* TODO: no way currently to know state of pen flip or
568                  * invert key modifier without starting a stroke */
569                 if ((!(brush->flag & BRUSH_INVERTED) ^
570                      !(brush->flag & BRUSH_DIR_IN)) &&
571                     ELEM5(brush->sculpt_tool, SCULPT_TOOL_DRAW,
572                           SCULPT_TOOL_INFLATE, SCULPT_TOOL_CLAY,
573                           SCULPT_TOOL_PINCH, SCULPT_TOOL_CREASE))
574                 {
575                         outline_col = brush->sub_col;
576                 }
577
578                 /* only do if brush is over the mesh */
579                 if (hit)
580                         paint_cursor_on_hit(ups, brush, &vc, location);
581
582                 if (ups->draw_anchored) {
583                         final_radius = ups->anchored_size;
584                         translation[0] = ups->anchored_initial_mouse[0] - vc.ar->winrct.xmin;
585                         translation[1] = ups->anchored_initial_mouse[1] - vc.ar->winrct.ymin;
586                 }
587         }
588
589         /* make lines pretty */
590         glEnable(GL_BLEND);
591         glEnable(GL_LINE_SMOOTH);
592
593         /* set brush color */
594         glColor4f(outline_col[0], outline_col[1], outline_col[2], outline_alpha);
595
596         /* draw brush outline */
597         glTranslatef(translation[0], translation[1], 0);
598
599         /* draw an inner brush */
600         if (BKE_brush_use_size_pressure(scene, brush)) {
601                 const wmWindow *win = CTX_wm_window(C);
602                 const float pressure = WM_cursor_pressure(win);
603                 if (pressure != -1.0f && pressure != 0.0f) {
604                         /* inner at full alpha */
605                         glutil_draw_lined_arc(0.0, M_PI * 2.0, final_radius * pressure, 40);
606                         /* outer at half alpha */
607                         glColor4f(outline_col[0], outline_col[1], outline_col[2], outline_alpha * 0.5f);
608                 }
609         }
610         glutil_draw_lined_arc(0.0, M_PI * 2.0, final_radius, 40);
611         glTranslatef(-translation[0], -translation[1], 0);
612
613         /* restore GL state */
614         glDisable(GL_BLEND);
615         glDisable(GL_LINE_SMOOTH);
616 }
617
618 /* Public API */
619
620 void paint_cursor_start(bContext *C, int (*poll)(bContext *C))
621 {
622         Paint *p = paint_get_active_from_context(C);
623
624         if (p && !p->paint_cursor)
625                 p->paint_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), poll, paint_draw_cursor, NULL);
626 }