Merged changes in the trunk up to revision 51126.
[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(Sculpt *sd, 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 #ifndef _OPENMP
135         (void)sd; /* quied unused warning */
136 #endif
137         
138         if (br->mtex.brush_map_mode == MTEX_MAP_MODE_TILED && !br->mtex.tex) return 0;
139         
140         refresh = 
141             !overlay_texture ||
142             (br->mtex.tex &&
143              (!br->mtex.tex->preview ||
144               br->mtex.tex->preview->changed_timestamp[0] != tex_changed_timestamp)) ||
145             !br->curve ||
146             br->curve->changed_timestamp != curve_changed_timestamp ||
147             !same_snap(&snap, br, vc);
148
149         if (refresh) {
150                 if (br->mtex.tex && br->mtex.tex->preview)
151                         tex_changed_timestamp = br->mtex.tex->preview->changed_timestamp[0];
152
153                 if (br->curve)
154                         curve_changed_timestamp = br->curve->changed_timestamp;
155
156                 make_snap(&snap, br, vc);
157
158                 if (br->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW) {
159                         int s = BKE_brush_size_get(vc->scene, br);
160                         int r = 1;
161
162                         for (s >>= 1; s > 0; s >>= 1)
163                                 r++;
164
165                         size = (1 << r);
166
167                         if (size < 256)
168                                 size = 256;
169
170                         if (size < old_size)
171                                 size = old_size;
172                 }
173                 else
174                         size = 512;
175
176                 if (old_size != size) {
177                         if (overlay_texture) {
178                                 glDeleteTextures(1, &overlay_texture);
179                                 overlay_texture = 0;
180                         }
181
182                         init = 0;
183
184                         old_size = size;
185                 }
186
187                 buffer = MEM_mallocN(sizeof(GLubyte) * size * size, "load_tex");
188
189                 #pragma omp parallel for schedule(static) if (sd->flags & SCULPT_USE_OPENMP)
190                 for (j = 0; j < size; j++) {
191                         int i;
192                         float y;
193                         float len;
194
195                         for (i = 0; i < size; i++) {
196
197                                 // largely duplicated from tex_strength
198
199                                 const float rotation = -br->mtex.rot;
200                                 float radius = BKE_brush_size_get(vc->scene, br);
201                                 int index = j * size + i;
202                                 float x;
203                                 float avg;
204
205                                 x = (float)i / size;
206                                 y = (float)j / size;
207
208                                 x -= 0.5f;
209                                 y -= 0.5f;
210
211                                 if (br->mtex.brush_map_mode == MTEX_MAP_MODE_TILED) {
212                                         x *= vc->ar->winx / radius;
213                                         y *= vc->ar->winy / radius;
214                                 }
215                                 else {
216                                         x *= 2;
217                                         y *= 2;
218                                 }
219
220                                 len = sqrtf(x * x + y * y);
221
222                                 if ((br->mtex.brush_map_mode == MTEX_MAP_MODE_TILED) || len <= 1) {
223                                         /* it is probably worth optimizing for those cases where 
224                                          * the texture is not rotated by skipping the calls to
225                                          * atan2, sqrtf, sin, and cos. */
226                                         if (br->mtex.tex && (rotation > 0.001f || rotation < -0.001f)) {
227                                                 const float angle    = atan2f(y, x) + rotation;
228
229                                                 x = len * cosf(angle);
230                                                 y = len * sinf(angle);
231                                         }
232
233                                         x *= br->mtex.size[0];
234                                         y *= br->mtex.size[1];
235
236                                         x += br->mtex.ofs[0];
237                                         y += br->mtex.ofs[1];
238
239                                         avg = br->mtex.tex ? paint_get_tex_pixel(br, x, y) : 1;
240
241                                         avg += br->texture_sample_bias;
242
243                                         if (br->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW)
244                                                 avg *= BKE_brush_curve_strength(br, len, 1);  /* Falloff curve */
245
246                                         buffer[index] = 255 - (GLubyte)(255 * avg);
247                                 }
248                                 else {
249                                         buffer[index] = 0;
250                                 }
251                         }
252                 }
253
254                 if (!overlay_texture)
255                         glGenTextures(1, &overlay_texture);
256         }
257         else {
258                 size = old_size;
259         }
260
261         glBindTexture(GL_TEXTURE_2D, overlay_texture);
262
263         if (refresh) {
264                 if (!init) {
265                         glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, size, size, 0, GL_ALPHA, GL_UNSIGNED_BYTE, buffer);
266                         init = 1;
267                 }
268                 else {
269                         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, size, size, GL_ALPHA, GL_UNSIGNED_BYTE, buffer);
270                 }
271
272                 if (buffer)
273                         MEM_freeN(buffer);
274         }
275
276         glEnable(GL_TEXTURE_2D);
277
278         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
279         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
280         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
281
282         if (br->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW) {
283                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
284                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
285         }
286
287         return 1;
288 }
289
290 static int project_brush_radius(ViewContext *vc,
291                                 float radius,
292                                 const float location[3])
293 {
294         float view[3], nonortho[3], ortho[3], offset[3], p1[2], p2[2];
295
296         ED_view3d_global_to_vector(vc->rv3d, location, view);
297
298         /* create a vector that is not orthogonal to view */
299
300         if (fabsf(view[0]) < 0.1f) {
301                 nonortho[0] = view[0] + 1.0f;
302                 nonortho[1] = view[1];
303                 nonortho[2] = view[2];
304         }
305         else if (fabsf(view[1]) < 0.1f) {
306                 nonortho[0] = view[0];
307                 nonortho[1] = view[1] + 1.0f;
308                 nonortho[2] = view[2];
309         }
310         else {
311                 nonortho[0] = view[0];
312                 nonortho[1] = view[1];
313                 nonortho[2] = view[2] + 1.0f;
314         }
315
316         /* get a vector in the plane of the view */
317         cross_v3_v3v3(ortho, nonortho, view);
318         normalize_v3(ortho);
319
320         /* make a point on the surface of the brush tagent to the view */
321         mul_v3_fl(ortho, radius);
322         add_v3_v3v3(offset, location, ortho);
323
324         /* project the center of the brush, and the tangent point to the view onto the screen */
325         if ((ED_view3d_project_float_global(vc->ar, location, p1, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_SUCCESS) &&
326             (ED_view3d_project_float_global(vc->ar, offset,   p2, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_SUCCESS))
327         {
328                 /* the distance between these points is the size of the projected brush in pixels */
329                 return len_v2v2(p1, p2);
330         }
331         else {
332                 BLI_assert(0);  /* assert because the code that sets up the vectors should disallow this */
333                 return 0;
334         }
335 }
336
337 static int sculpt_get_brush_geometry(bContext *C, ViewContext *vc,
338                                      int x, int y, int *pixel_radius,
339                                      float location[3])
340 {
341         Scene *scene = CTX_data_scene(C);
342         Paint *paint = paint_get_active_from_context(C);
343         Brush *brush = paint_brush(paint);
344         float window[2];
345         int hit;
346
347         window[0] = x + vc->ar->winrct.xmin;
348         window[1] = y + vc->ar->winrct.ymin;
349
350         if (vc->obact->sculpt && vc->obact->sculpt->pbvh &&
351             sculpt_stroke_get_location(C, location, window))
352         {
353                 *pixel_radius =
354                     project_brush_radius(vc,
355                                          BKE_brush_unprojected_radius_get(scene, brush),
356                                          location);
357
358                 if (*pixel_radius == 0)
359                         *pixel_radius = BKE_brush_size_get(scene, brush);
360
361                 mul_m4_v3(vc->obact->obmat, location);
362
363                 hit = 1;
364         }
365         else {
366                 Sculpt *sd    = CTX_data_tool_settings(C)->sculpt;
367                 Brush *brush = paint_brush(&sd->paint);
368
369                 *pixel_radius = BKE_brush_size_get(scene, brush);
370                 hit = 0;
371         }
372
373         return hit;
374 }
375
376 /* Draw an overlay that shows what effect the brush's texture will
377  * have on brush strength */
378 /* TODO: sculpt only for now */
379 static void paint_draw_alpha_overlay(Sculpt *sd, Brush *brush,
380                                      ViewContext *vc, int x, int y)
381 {
382         rctf quad;
383
384         /* check for overlay mode */
385         if (!(brush->flag & BRUSH_TEXTURE_OVERLAY) ||
386             !(ELEM(brush->mtex.brush_map_mode, MTEX_MAP_MODE_VIEW, MTEX_MAP_MODE_TILED)))
387         {
388                 return;
389         }
390
391         /* save lots of GL state
392          * TODO: check on whether all of these are needed? */
393         glPushAttrib(GL_COLOR_BUFFER_BIT |
394                      GL_CURRENT_BIT |
395                      GL_DEPTH_BUFFER_BIT |
396                      GL_ENABLE_BIT |
397                      GL_LINE_BIT |
398                      GL_POLYGON_BIT |
399                      GL_STENCIL_BUFFER_BIT |
400                      GL_TRANSFORM_BIT |
401                      GL_VIEWPORT_BIT |
402                      GL_TEXTURE_BIT);
403
404         if (load_tex(sd, brush, vc)) {
405                 glEnable(GL_BLEND);
406
407                 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
408                 glDepthMask(GL_FALSE);
409                 glDepthFunc(GL_ALWAYS);
410
411                 glMatrixMode(GL_TEXTURE);
412                 glPushMatrix();
413                 glLoadIdentity();
414
415                 if (brush->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW) {
416                         /* brush rotation */
417                         glTranslatef(0.5, 0.5, 0);
418                         glRotatef((double)RAD2DEGF((brush->flag & BRUSH_RAKE) ?
419                                                    sd->last_angle : sd->special_rotation),
420                                   0.0, 0.0, 1.0);
421                         glTranslatef(-0.5f, -0.5f, 0);
422
423                         /* scale based on tablet pressure */
424                         if (sd->draw_pressure && BKE_brush_use_size_pressure(vc->scene, brush)) {
425                                 glTranslatef(0.5f, 0.5f, 0);
426                                 glScalef(1.0f / sd->pressure_value, 1.0f / sd->pressure_value, 1);
427                                 glTranslatef(-0.5f, -0.5f, 0);
428                         }
429
430                         if (sd->draw_anchored) {
431                                 const float *aim = sd->anchored_initial_mouse;
432                                 const rcti *win = &vc->ar->winrct;
433                                 quad.xmin = aim[0] - sd->anchored_size - win->xmin;
434                                 quad.ymin = aim[1] - sd->anchored_size - win->ymin;
435                                 quad.xmax = aim[0] + sd->anchored_size - win->xmin;
436                                 quad.ymax = aim[1] + sd->anchored_size - win->ymin;
437                         }
438                         else {
439                                 const int radius = BKE_brush_size_get(vc->scene, brush);
440                                 quad.xmin = x - radius;
441                                 quad.ymin = y - radius;
442                                 quad.xmax = x + radius;
443                                 quad.ymax = y + radius;
444                         }
445                 }
446                 else {
447                         quad.xmin = 0;
448                         quad.ymin = 0;
449                         quad.xmax = BLI_rcti_size_x(&vc->ar->winrct);
450                         quad.ymax = BLI_rcti_size_y(&vc->ar->winrct);
451                 }
452
453                 /* set quad color */
454                 glColor4f(U.sculpt_paint_overlay_col[0],
455                           U.sculpt_paint_overlay_col[1],
456                           U.sculpt_paint_overlay_col[2],
457                           brush->texture_overlay_alpha / 100.0f);
458
459                 /* draw textured quad */
460                 glBegin(GL_QUADS);
461                 glTexCoord2f(0, 0);
462                 glVertex2f(quad.xmin, quad.ymin);
463                 glTexCoord2f(1, 0);
464                 glVertex2f(quad.xmax, quad.ymin);
465                 glTexCoord2f(1, 1);
466                 glVertex2f(quad.xmax, quad.ymax);
467                 glTexCoord2f(0, 1);
468                 glVertex2f(quad.xmin, quad.ymax);
469                 glEnd();
470
471                 glPopMatrix();
472         }
473
474         glPopAttrib();
475 }
476
477 /* Special actions taken when paint cursor goes over mesh */
478 /* TODO: sculpt only for now */
479 static void paint_cursor_on_hit(Sculpt *sd, Brush *brush, ViewContext *vc,
480                                 const float location[3])
481 {
482         float unprojected_radius, projected_radius;
483
484         /* update the brush's cached 3D radius */
485         if (!BKE_brush_use_locked_size(vc->scene, brush)) {
486                 /* get 2D brush radius */
487                 if (sd->draw_anchored)
488                         projected_radius = sd->anchored_size;
489                 else {
490                         if (brush->flag & BRUSH_ANCHORED)
491                                 projected_radius = 8;
492                         else
493                                 projected_radius = BKE_brush_size_get(vc->scene, brush);
494                 }
495         
496                 /* convert brush radius from 2D to 3D */
497                 unprojected_radius = paint_calc_object_space_radius(vc, location,
498                                                                     projected_radius);
499
500                 /* scale 3D brush radius by pressure */
501                 if (sd->draw_pressure && BKE_brush_use_size_pressure(vc->scene, brush))
502                         unprojected_radius *= sd->pressure_value;
503
504                 /* set cached value in either Brush or UnifiedPaintSettings */
505                 BKE_brush_unprojected_radius_set(vc->scene, brush, unprojected_radius);
506         }
507 }
508
509 static void paint_draw_cursor(bContext *C, int x, int y, void *UNUSED(unused))
510 {
511         Scene *scene = CTX_data_scene(C);
512         Paint *paint = paint_get_active_from_context(C);
513         Brush *brush = paint_brush(paint);
514         ViewContext vc;
515         float final_radius;
516         float translation[2];
517         float outline_alpha, *outline_col;
518         
519         /* set various defaults */
520         translation[0] = x;
521         translation[1] = y;
522         outline_alpha = 0.5;
523         outline_col = brush->add_col;
524         final_radius = BKE_brush_size_get(scene, brush);
525
526         /* check that brush drawing is enabled */
527         if (!(paint->flags & PAINT_SHOW_BRUSH))
528                 return;
529
530         /* can't use stroke vc here because this will be called during
531          * mouse over too, not just during a stroke */
532         view3d_set_viewcontext(C, &vc);
533
534         /* TODO: as sculpt and other paint modes are unified, this
535          * special mode of drawing will go away */
536         if (vc.obact->sculpt) {
537                 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
538                 float location[3];
539                 int pixel_radius, hit;
540
541                 /* this is probably here so that rake takes into
542                  * account the brush movements before the stroke
543                  * starts, but this doesn't really belong in draw code
544                  *  TODO) */
545                 {
546                         const float u = 0.5f;
547                         const float v = 1 - u;
548                         const float r = 20;
549
550                         const float dx = sd->last_x - x;
551                         const float dy = sd->last_y - y;
552
553                         if (dx * dx + dy * dy >= r * r) {
554                                 sd->last_angle = atan2(dx, dy);
555
556                                 sd->last_x = u * sd->last_x + v * x;
557                                 sd->last_y = u * sd->last_y + v * y;
558                         }
559                 }
560
561                 /* test if brush is over the mesh */
562                 hit = sculpt_get_brush_geometry(C, &vc, x, y, &pixel_radius, location);
563
564                 /* draw overlay */
565                 paint_draw_alpha_overlay(sd, brush, &vc, x, y);
566
567                 if (BKE_brush_use_locked_size(scene, brush))
568                         BKE_brush_size_set(scene, brush, pixel_radius);
569
570                 /* check if brush is subtracting, use different color then */
571                 /* TODO: no way currently to know state of pen flip or
572                  * invert key modifier without starting a stroke */
573                 if ((!(brush->flag & BRUSH_INVERTED) ^
574                      !(brush->flag & BRUSH_DIR_IN)) &&
575                     ELEM5(brush->sculpt_tool, SCULPT_TOOL_DRAW,
576                           SCULPT_TOOL_INFLATE, SCULPT_TOOL_CLAY,
577                           SCULPT_TOOL_PINCH, SCULPT_TOOL_CREASE))
578                 {
579                         outline_col = brush->sub_col;
580                 }
581
582                 /* only do if brush is over the mesh */
583                 if (hit)
584                         paint_cursor_on_hit(sd, brush, &vc, location);
585
586                 if (sd->draw_anchored) {
587                         final_radius = sd->anchored_size;
588                         translation[0] = sd->anchored_initial_mouse[0] - vc.ar->winrct.xmin;
589                         translation[1] = sd->anchored_initial_mouse[1] - vc.ar->winrct.ymin;
590                 }
591         }
592
593         /* make lines pretty */
594         glEnable(GL_BLEND);
595         glEnable(GL_LINE_SMOOTH);
596
597         /* set brush color */
598         glColor4f(outline_col[0], outline_col[1], outline_col[2], outline_alpha);
599
600         /* draw brush outline */
601         glTranslatef(translation[0], translation[1], 0);
602         glutil_draw_lined_arc(0.0, M_PI * 2.0, final_radius, 40);
603         glTranslatef(-translation[0], -translation[1], 0);
604
605         /* restore GL state */
606         glDisable(GL_BLEND);
607         glDisable(GL_LINE_SMOOTH);
608 }
609
610 /* Public API */
611
612 void paint_cursor_start(bContext *C, int (*poll)(bContext *C))
613 {
614         Paint *p = paint_get_active_from_context(C);
615
616         if (p && !p->paint_cursor)
617                 p->paint_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), poll, paint_draw_cursor, NULL);
618 }