Add stencil control for mask overlay. Key combinations are the same as
[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_image.h"
47 #include "BKE_paint.h"
48 #include "BKE_colortools.h"
49
50 #include "WM_api.h"
51
52 #include "BIF_gl.h"
53 #include "BIF_glutil.h"
54
55 #include "ED_view3d.h"
56
57 #include "paint_intern.h"
58 /* still needed for sculpt_stroke_get_location, should be
59  * removed eventually (TODO) */
60 #include "sculpt_intern.h"
61
62 /* TODOs:
63  *
64  * Some of the cursor drawing code is doing non-draw stuff
65  * (e.g. updating the brush rake angle). This should be cleaned up
66  * still.
67  *
68  * There is also some ugliness with sculpt-specific code.
69  */
70
71 typedef struct TexSnapshot {
72         GLuint overlay_texture;
73         int winx;
74         int winy;
75         bool init;
76         int old_size;
77         int old_zoom;
78         bool old_col;
79 } TexSnapshot;
80
81 typedef struct CurveSnapshot {
82         int BKE_brush_size_get;
83         int curve_changed_timestamp;
84         bool init;
85 } CurveSnapshot;
86
87 static int same_tex_snap(TexSnapshot *snap, MTex *mtex, ViewContext *vc, bool col, float zoom)
88 {
89         return (/* 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
93                 (mtex->brush_map_mode != MTEX_MAP_MODE_TILED ||
94                 (vc->ar->winx == snap->winx &&
95                 vc->ar->winy == snap->winy)) &&
96                 snap->old_zoom == zoom &&
97                         snap->old_col == col
98                 );
99 }
100
101 static void make_tex_snap(TexSnapshot *snap, ViewContext *vc, float zoom)
102 {
103         snap->old_zoom = zoom;
104         snap->winx = vc->ar->winx;
105         snap->winy = vc->ar->winy;
106 }
107
108 static int load_tex(Brush *br, ViewContext *vc, float zoom, bool col, bool primary)
109 {
110         static int init = 0;
111         static TexSnapshot primary_snap = {0};
112         static TexSnapshot secondary_snap  = {0};
113         TexSnapshot *target;
114
115         MTex *mtex = (primary) ? &br->mtex : &br->mask_mtex;
116         OverlayControlFlags overlay_flags = BKE_paint_get_overlay_flags();
117         GLubyte *buffer = NULL;
118
119         int size;
120         int j;
121         int refresh;
122         GLenum format = col ? GL_RGBA : GL_ALPHA;
123         OverlayControlFlags invalid = (primary) ? (overlay_flags & PAINT_INVALID_OVERLAY_TEXTURE_PRIMARY) :
124                                    (overlay_flags & PAINT_INVALID_OVERLAY_TEXTURE_SECONDARY);
125
126         target = (primary) ? &primary_snap : &secondary_snap;
127         
128         if (mtex->brush_map_mode != MTEX_MAP_MODE_VIEW && !mtex->tex) return 0;
129         
130         refresh = 
131             !target->overlay_texture ||
132             (invalid != 0) ||
133             !same_tex_snap(target, mtex, vc, col, zoom);
134
135         if (refresh) {
136                 struct ImagePool *pool = NULL;
137                 /* stencil is rotated later */
138                 const float rotation = (mtex->brush_map_mode != MTEX_MAP_MODE_STENCIL) ?
139                                        -mtex->rot : 0;
140
141                 float radius = BKE_brush_size_get(vc->scene, br) * zoom;
142
143                 make_tex_snap(target, vc, zoom);
144
145                 if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
146                         int s = BKE_brush_size_get(vc->scene, br);
147                         int r = 1;
148
149                         for (s >>= 1; s > 0; s >>= 1)
150                                 r++;
151
152                         size = (1 << r);
153
154                         if (size < 256)
155                                 size = 256;
156
157                         if (size < target->old_size)
158                                 size = target->old_size;
159                 }
160                 else
161                         size = 512;
162
163                 if (target->old_size != size) {
164                         if (target->overlay_texture) {
165                                 glDeleteTextures(1, &target->overlay_texture);
166                                 target->overlay_texture = 0;
167                         }
168
169                         init = 0;
170
171                         target->old_size = size;
172                 }
173                 if (col)
174                         buffer = MEM_mallocN(sizeof(GLubyte) * size * size * 4, "load_tex");
175                 else
176                         buffer = MEM_mallocN(sizeof(GLubyte) * size * size, "load_tex");
177
178                 if (mtex->tex)
179                         pool = BKE_image_pool_new();
180
181                 #pragma omp parallel for schedule(static)
182                 for (j = 0; j < size; j++) {
183                         int i;
184                         float y;
185                         float len;
186
187                         for (i = 0; i < size; i++) {
188
189                                 // largely duplicated from tex_strength
190
191                                 int index = j * size + i;
192                                 float x;
193
194                                 x = (float)i / size;
195                                 y = (float)j / size;
196
197                                 if (mtex->brush_map_mode == MTEX_MAP_MODE_TILED) {
198                                         x *= vc->ar->winx / radius;
199                                         y *= vc->ar->winy / radius;
200                                 }
201                                 else {
202                                         x -= 0.5f;
203                                         y -= 0.5f;
204
205                                         x *= 2;
206                                         y *= 2;
207                                 }
208
209                                 len = sqrtf(x * x + y * y);
210
211                                 if (ELEM(mtex->brush_map_mode, MTEX_MAP_MODE_TILED, MTEX_MAP_MODE_STENCIL) || len <= 1) {
212                                         /* it is probably worth optimizing for those cases where 
213                                          * the texture is not rotated by skipping the calls to
214                                          * atan2, sqrtf, sin, and cos. */
215                                         if (mtex->tex && (rotation > 0.001f || rotation < -0.001f)) {
216                                                 const float angle = atan2f(y, x) + rotation;
217
218                                                 x = len * cosf(angle);
219                                                 y = len * sinf(angle);
220                                         }
221
222                                         x *= mtex->size[0];
223                                         y *= mtex->size[1];
224
225                                         x += mtex->ofs[0];
226                                         y += mtex->ofs[1];
227
228                                         if (col) {
229                                                 float rgba[4];
230
231                                                 if (mtex->tex)
232                                                         paint_get_tex_pixel_col(mtex, x, y, rgba, pool);
233
234                                                 buffer[index * 4]     = rgba[0] * 255;
235                                                 buffer[index * 4 + 1] = rgba[1] * 255;
236                                                 buffer[index * 4 + 2] = rgba[2] * 255;
237                                                 buffer[index * 4 + 3] = rgba[3] * 255;
238                                         }
239                                         else {
240                                                 float avg = mtex->tex ? paint_get_tex_pixel(mtex, x, y, pool) : 1;
241
242                                                 avg += br->texture_sample_bias;
243
244                                                 /* clamp to avoid precision overflow */
245                                                 CLAMP(avg, 0.0f, 1.0f);
246                                                 buffer[index] = 255 - (GLubyte)(255 * avg);
247                                         }
248                                 }
249                                 else {
250                                         if (col) {
251                                                 buffer[index * 4]     = 0;
252                                                 buffer[index * 4 + 1] = 0;
253                                                 buffer[index * 4 + 2] = 0;
254                                                 buffer[index * 4 + 3] = 0;
255                                         }
256                                         else {
257                                                 buffer[index] = 0;
258                                         }
259                                 }
260                         }
261                 }
262
263                 if (pool)
264                         BKE_image_pool_free(pool);
265
266                 if (!target->overlay_texture)
267                         glGenTextures(1, &target->overlay_texture);
268         }
269         else {
270                 size = target->old_size;
271         }
272
273         glBindTexture(GL_TEXTURE_2D, target->overlay_texture);
274
275         if (refresh) {
276                 if (!init || (target->old_col != col)) {
277                         glTexImage2D(GL_TEXTURE_2D, 0, format, size, size, 0, format, GL_UNSIGNED_BYTE, buffer);
278                         init = 1;
279                 }
280                 else {
281                         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, size, size, format, GL_UNSIGNED_BYTE, buffer);
282                 }
283
284                 if (buffer)
285                         MEM_freeN(buffer);
286
287                 target->old_col = col;
288         }
289
290         glEnable(GL_TEXTURE_2D);
291
292         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
293         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
294         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
295
296         if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
297                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
298                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
299         }
300
301         BKE_paint_reset_overlay_invalid(invalid);
302
303         return 1;
304 }
305
306 static int load_tex_cursor(Brush *br, ViewContext *vc, float zoom)
307 {
308         static GLuint overlay_texture = 0;
309         static int init = 0;
310         static int old_size = -1;
311         static int old_zoom = -1;
312
313         OverlayControlFlags overlay_flags = BKE_paint_get_overlay_flags();
314         GLubyte *buffer = NULL;
315
316         int size;
317         int j;
318         int refresh;
319
320         refresh =
321             !overlay_texture ||
322             (overlay_flags & PAINT_INVALID_OVERLAY_CURVE) ||
323             old_zoom != zoom;
324
325         if (refresh) {
326                 int s, r;
327
328                 old_zoom = zoom;
329
330                 s = BKE_brush_size_get(vc->scene, br);
331                 r = 1;
332
333                 for (s >>= 1; s > 0; s >>= 1)
334                         r++;
335
336                 size = (1 << r);
337
338                 if (size < 256)
339                         size = 256;
340
341                 if (size < old_size)
342                         size = old_size;
343
344                 if (old_size != size) {
345                         if (overlay_texture) {
346                                 glDeleteTextures(1, &overlay_texture);
347                                 overlay_texture = 0;
348                         }
349
350                         init = 0;
351
352                         old_size = size;
353                 }
354                 buffer = MEM_mallocN(sizeof(GLubyte) * size * size, "load_tex");
355
356                 curvemapping_initialize(br->curve);
357
358                 #pragma omp parallel for schedule(static)
359                 for (j = 0; j < size; j++) {
360                         int i;
361                         float y;
362                         float len;
363
364                         for (i = 0; i < size; i++) {
365
366                                 // largely duplicated from tex_strength
367
368                                 int index = j * size + i;
369                                 float x;
370
371                                 x = (float)i / size;
372                                 y = (float)j / size;
373
374                                 x -= 0.5f;
375                                 y -= 0.5f;
376
377                                 x *= 2;
378                                 y *= 2;
379
380                                 len = sqrtf(x * x + y * y);
381
382                                 if (len <= 1) {
383                                                 float avg = BKE_brush_curve_strength(br, len, 1.0f);  /* Falloff curve */
384
385                                                 /* clamp to avoid precision overflow */
386                                                 CLAMP(avg, 0.0f, 1.0f);
387                                                 buffer[index] = 255 - (GLubyte)(255 * avg);
388
389                                 }
390                                 else {
391                                         buffer[index] = 0;
392                                 }
393                         }
394                 }
395
396                 if (!overlay_texture)
397                         glGenTextures(1, &overlay_texture);
398         }
399         else {
400                 size = old_size;
401         }
402
403         glBindTexture(GL_TEXTURE_2D, overlay_texture);
404
405         if (refresh) {
406                 if (!init) {
407                         glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, size, size, 0, GL_ALPHA, GL_UNSIGNED_BYTE, buffer);
408                         init = 1;
409                 }
410                 else {
411                         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, size, size, GL_ALPHA, GL_UNSIGNED_BYTE, buffer);
412                 }
413
414                 if (buffer)
415                         MEM_freeN(buffer);
416         }
417
418         glEnable(GL_TEXTURE_2D);
419
420         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
421         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
422         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
423
424         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
425         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
426
427         BKE_paint_reset_overlay_invalid(PAINT_INVALID_OVERLAY_CURVE);
428
429         return 1;
430 }
431
432
433
434 static int project_brush_radius(ViewContext *vc,
435                                 float radius,
436                                 const float location[3])
437 {
438         float view[3], nonortho[3], ortho[3], offset[3], p1[2], p2[2];
439
440         ED_view3d_global_to_vector(vc->rv3d, location, view);
441
442         /* create a vector that is not orthogonal to view */
443
444         if (fabsf(view[0]) < 0.1f) {
445                 nonortho[0] = view[0] + 1.0f;
446                 nonortho[1] = view[1];
447                 nonortho[2] = view[2];
448         }
449         else if (fabsf(view[1]) < 0.1f) {
450                 nonortho[0] = view[0];
451                 nonortho[1] = view[1] + 1.0f;
452                 nonortho[2] = view[2];
453         }
454         else {
455                 nonortho[0] = view[0];
456                 nonortho[1] = view[1];
457                 nonortho[2] = view[2] + 1.0f;
458         }
459
460         /* get a vector in the plane of the view */
461         cross_v3_v3v3(ortho, nonortho, view);
462         normalize_v3(ortho);
463
464         /* make a point on the surface of the brush tagent to the view */
465         mul_v3_fl(ortho, radius);
466         add_v3_v3v3(offset, location, ortho);
467
468         /* project the center of the brush, and the tangent point to the view onto the screen */
469         if ((ED_view3d_project_float_global(vc->ar, location, p1, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) &&
470             (ED_view3d_project_float_global(vc->ar, offset,   p2, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK))
471         {
472                 /* the distance between these points is the size of the projected brush in pixels */
473                 return len_v2v2(p1, p2);
474         }
475         else {
476                 BLI_assert(0);  /* assert because the code that sets up the vectors should disallow this */
477                 return 0;
478         }
479 }
480
481 static int sculpt_get_brush_geometry(bContext *C, ViewContext *vc,
482                                      int x, int y, int *pixel_radius,
483                                      float location[3])
484 {
485         Scene *scene = CTX_data_scene(C);
486         Paint *paint = BKE_paint_get_active_from_context(C);
487         float mouse[2];
488         int hit;
489
490         mouse[0] = x;
491         mouse[1] = y;
492
493         if (vc->obact->sculpt && vc->obact->sculpt->pbvh &&
494             sculpt_stroke_get_location(C, location, mouse))
495         {
496                 Brush *brush = BKE_paint_brush(paint);
497                 *pixel_radius =
498                     project_brush_radius(vc,
499                                          BKE_brush_unprojected_radius_get(scene, brush),
500                                          location);
501
502                 if (*pixel_radius == 0)
503                         *pixel_radius = BKE_brush_size_get(scene, brush);
504
505                 mul_m4_v3(vc->obact->obmat, location);
506
507                 hit = 1;
508         }
509         else {
510                 Sculpt *sd    = CTX_data_tool_settings(C)->sculpt;
511                 Brush *brush = BKE_paint_brush(&sd->paint);
512
513                 *pixel_radius = BKE_brush_size_get(scene, brush);
514                 hit = 0;
515         }
516
517         return hit;
518 }
519
520 /* Draw an overlay that shows what effect the brush's texture will
521  * have on brush strength */
522 static void paint_draw_tex_overlay(UnifiedPaintSettings *ups, Brush *brush,
523                                      ViewContext *vc, int x, int y, float zoom, bool col, bool primary)
524 {
525         rctf quad;
526         /* check for overlay mode */
527
528         MTex *mtex = (primary) ? &brush->mtex : &brush->mask_mtex;
529         bool valid = (primary) ? (brush->overlay_flags & BRUSH_OVERLAY_PRIMARY) != 0 :
530                                  (brush->overlay_flags & BRUSH_OVERLAY_SECONDARY) != 0;
531         int overlay_alpha = (primary) ? brush->texture_overlay_alpha : brush->mask_overlay_alpha;
532
533         if (!((mtex->brush_map_mode == MTEX_MAP_MODE_STENCIL && mtex->tex) ||
534             (valid &&
535             ELEM(mtex->brush_map_mode, MTEX_MAP_MODE_VIEW, MTEX_MAP_MODE_TILED))))
536         {
537                 return;
538         }
539
540         if (load_tex(brush, vc, zoom, col, primary)) {
541                 glEnable(GL_BLEND);
542
543                 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
544                 glDepthMask(GL_FALSE);
545                 glDepthFunc(GL_ALWAYS);
546
547                 glMatrixMode(GL_TEXTURE);
548                 glPushMatrix();
549                 glLoadIdentity();
550
551                 if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
552                         /* brush rotation */
553                         glTranslatef(0.5, 0.5, 0);
554                         glRotatef((double)RAD2DEGF(ups->brush_rotation),
555                                   0.0, 0.0, 1.0);
556                         glTranslatef(-0.5f, -0.5f, 0);
557
558                         /* scale based on tablet pressure */
559                         if (primary && ups->draw_pressure && BKE_brush_use_size_pressure(vc->scene, brush)) {
560                                 glTranslatef(0.5f, 0.5f, 0);
561                                 glScalef(1.0f / ups->pressure_value, 1.0f / ups->pressure_value, 1);
562                                 glTranslatef(-0.5f, -0.5f, 0);
563                         }
564
565                         if (ups->draw_anchored) {
566                                 const float *aim = ups->anchored_initial_mouse;
567                                 quad.xmin = aim[0] - ups->anchored_size;
568                                 quad.ymin = aim[1] - ups->anchored_size;
569                                 quad.xmax = aim[0] + ups->anchored_size;
570                                 quad.ymax = aim[1] + ups->anchored_size;
571                         }
572                         else {
573                                 const int radius = BKE_brush_size_get(vc->scene, brush) * zoom;
574                                 quad.xmin = x - radius;
575                                 quad.ymin = y - radius;
576                                 quad.xmax = x + radius;
577                                 quad.ymax = y + radius;
578                         }
579                 }
580                 else if (mtex->brush_map_mode == MTEX_MAP_MODE_TILED) {
581                         quad.xmin = 0;
582                         quad.ymin = 0;
583                         quad.xmax = BLI_rcti_size_x(&vc->ar->winrct);
584                         quad.ymax = BLI_rcti_size_y(&vc->ar->winrct);
585                 }
586                 /* Stencil code goes here */
587                 else {
588                         if (primary) {
589                                 quad.xmin = -brush->stencil_dimension[0];
590                                 quad.ymin = -brush->stencil_dimension[1];
591                                 quad.xmax = brush->stencil_dimension[0];
592                                 quad.ymax = brush->stencil_dimension[1];
593                         }
594                         else {
595                                 quad.xmin = -brush->mask_stencil_dimension[0];
596                                 quad.ymin = -brush->mask_stencil_dimension[1];
597                                 quad.xmax = brush->mask_stencil_dimension[0];
598                                 quad.ymax = brush->mask_stencil_dimension[1];
599                         }
600                         glMatrixMode(GL_MODELVIEW);
601                         glPushMatrix();
602                         if (primary)
603                                 glTranslatef(brush->stencil_pos[0], brush->stencil_pos[1], 0);
604                         else
605                                 glTranslatef(brush->mask_stencil_pos[0], brush->mask_stencil_pos[1], 0);
606                         glRotatef(RAD2DEGF(mtex->rot), 0, 0, 1);
607                         glMatrixMode(GL_TEXTURE);
608                 }
609
610                 /* set quad color. Colored overlay does not get blending */
611                 if (col)
612                         glColor4f(1.0,
613                                       1.0,
614                                       1.0,
615                                       overlay_alpha / 100.0f);
616                 else
617                         glColor4f(U.sculpt_paint_overlay_col[0],
618                                       U.sculpt_paint_overlay_col[1],
619                                       U.sculpt_paint_overlay_col[2],
620                                       overlay_alpha / 100.0f);
621
622                 /* draw textured quad */
623                 glBegin(GL_QUADS);
624                 glTexCoord2f(0, 0);
625                 glVertex2f(quad.xmin, quad.ymin);
626                 glTexCoord2f(1, 0);
627                 glVertex2f(quad.xmax, quad.ymin);
628                 glTexCoord2f(1, 1);
629                 glVertex2f(quad.xmax, quad.ymax);
630                 glTexCoord2f(0, 1);
631                 glVertex2f(quad.xmin, quad.ymax);
632                 glEnd();
633
634                 glPopMatrix();
635
636                 if (mtex->brush_map_mode == MTEX_MAP_MODE_STENCIL) {
637                         glMatrixMode(GL_MODELVIEW);
638                         glPopMatrix();
639                 }
640         }
641 }
642
643 /* Draw an overlay that shows what effect the brush's texture will
644  * have on brush strength */
645 static void paint_draw_cursor_overlay(UnifiedPaintSettings *ups, Brush *brush,
646                                      ViewContext *vc, int x, int y, float zoom)
647 {
648         rctf quad;
649         /* check for overlay mode */
650
651         if (!(brush->overlay_flags & BRUSH_OVERLAY_CURSOR))
652         {
653                 return;
654         }
655
656         if (load_tex_cursor(brush, vc, zoom)) {
657                 glEnable(GL_BLEND);
658
659                 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
660                 glDepthMask(GL_FALSE);
661                 glDepthFunc(GL_ALWAYS);
662
663                 /* scale based on tablet pressure */
664                 if (ups->draw_pressure && BKE_brush_use_size_pressure(vc->scene, brush)) {
665                         glTranslatef(0.5f, 0.5f, 0);
666                         glScalef(1.0f / ups->pressure_value, 1.0f / ups->pressure_value, 1);
667                         glTranslatef(-0.5f, -0.5f, 0);
668                 }
669
670                 if (ups->draw_anchored) {
671                         const float *aim = ups->anchored_initial_mouse;
672                         quad.xmin = aim[0] - ups->anchored_size;
673                         quad.ymin = aim[1] - ups->anchored_size;
674                         quad.xmax = aim[0] + ups->anchored_size;
675                         quad.ymax = aim[1] + ups->anchored_size;
676                 }
677                 else {
678                         const int radius = BKE_brush_size_get(vc->scene, brush) * zoom;
679                         quad.xmin = x - radius;
680                         quad.ymin = y - radius;
681                         quad.xmax = x + radius;
682                         quad.ymax = y + radius;
683                 }
684
685                 glColor4f(U.sculpt_paint_overlay_col[0],
686                         U.sculpt_paint_overlay_col[1],
687                         U.sculpt_paint_overlay_col[2],
688                         brush->cursor_overlay_alpha / 100.0f);
689
690                 /* draw textured quad */
691                 glBegin(GL_QUADS);
692                 glTexCoord2f(0, 0);
693                 glVertex2f(quad.xmin, quad.ymin);
694                 glTexCoord2f(1, 0);
695                 glVertex2f(quad.xmax, quad.ymin);
696                 glTexCoord2f(1, 1);
697                 glVertex2f(quad.xmax, quad.ymax);
698                 glTexCoord2f(0, 1);
699                 glVertex2f(quad.xmin, quad.ymax);
700                 glEnd();
701         }
702 }
703
704 static void paint_draw_alpha_overlay(UnifiedPaintSettings *ups, Brush *brush,
705                                      ViewContext *vc, int x, int y, float zoom, PaintMode mode)
706 {
707         /* color means that primary brush texture is colured and secondary is used for alpha/mask control */
708         bool col = ELEM3(mode, PAINT_TEXTURE_PROJECTIVE, PAINT_TEXTURE_2D, PAINT_VERTEX) ? true: false;
709
710         /* save lots of GL state
711          * TODO: check on whether all of these are needed? */
712         glPushAttrib(GL_COLOR_BUFFER_BIT |
713                      GL_CURRENT_BIT |
714                      GL_DEPTH_BUFFER_BIT |
715                      GL_ENABLE_BIT |
716                      GL_LINE_BIT |
717                      GL_POLYGON_BIT |
718                      GL_STENCIL_BUFFER_BIT |
719                      GL_TRANSFORM_BIT |
720                      GL_VIEWPORT_BIT |
721                      GL_TEXTURE_BIT);
722
723
724         /* coloured overlay should be drawn separately */
725         if (col) {
726                 paint_draw_tex_overlay(ups, brush, vc, x, y, zoom, true, true);
727                 paint_draw_tex_overlay(ups, brush, vc, x, y, zoom, false, false);
728                 paint_draw_cursor_overlay(ups, brush, vc, x, y, zoom);
729         } else {
730                 paint_draw_tex_overlay(ups, brush, vc, x, y, zoom, false, true);
731                 paint_draw_cursor_overlay(ups, brush, vc, x, y, zoom);
732         }
733
734         glPopAttrib();
735 }
736
737 /* Special actions taken when paint cursor goes over mesh */
738 /* TODO: sculpt only for now */
739 static void paint_cursor_on_hit(UnifiedPaintSettings *ups, Brush *brush, ViewContext *vc,
740                                 const float location[3])
741 {
742         float unprojected_radius, projected_radius;
743
744         /* update the brush's cached 3D radius */
745         if (!BKE_brush_use_locked_size(vc->scene, brush)) {
746                 /* get 2D brush radius */
747                 if (ups->draw_anchored)
748                         projected_radius = ups->anchored_size;
749                 else {
750                         if (brush->flag & BRUSH_ANCHORED)
751                                 projected_radius = 8;
752                         else
753                                 projected_radius = BKE_brush_size_get(vc->scene, brush);
754                 }
755         
756                 /* convert brush radius from 2D to 3D */
757                 unprojected_radius = paint_calc_object_space_radius(vc, location,
758                                                                     projected_radius);
759
760                 /* scale 3D brush radius by pressure */
761                 if (ups->draw_pressure && BKE_brush_use_size_pressure(vc->scene, brush))
762                         unprojected_radius *= ups->pressure_value;
763
764                 /* set cached value in either Brush or UnifiedPaintSettings */
765                 BKE_brush_unprojected_radius_set(vc->scene, brush, unprojected_radius);
766         }
767 }
768
769 static void paint_draw_cursor(bContext *C, int x, int y, void *UNUSED(unused))
770 {
771         Scene *scene = CTX_data_scene(C);
772         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
773         Paint *paint = BKE_paint_get_active_from_context(C);
774         Brush *brush = BKE_paint_brush(paint);
775         ViewContext vc;
776         PaintMode mode;
777         float final_radius;
778         float translation[2];
779         float outline_alpha, *outline_col;
780         float zoomx, zoomy;
781         
782         /* check that brush drawing is enabled */
783         if (!(paint->flags & PAINT_SHOW_BRUSH))
784                 return;
785
786         /* can't use stroke vc here because this will be called during
787          * mouse over too, not just during a stroke */
788         view3d_set_viewcontext(C, &vc);
789
790         get_imapaint_zoom(C, &zoomx, &zoomy);
791         zoomx = max_ff(zoomx, zoomy);
792         mode = BKE_paintmode_get_active_from_context(C);
793
794         /* set various defaults */
795         translation[0] = x;
796         translation[1] = y;
797         outline_alpha = 0.5;
798         outline_col = brush->add_col;
799         final_radius = BKE_brush_size_get(scene, brush) * zoomx;
800
801         if (brush->flag & BRUSH_RAKE)
802                 /* here, translation contains the mouse coordinates. */
803                 paint_calculate_rake_rotation(ups, translation);
804         else if (!(brush->flag & BRUSH_ANCHORED))
805                 ups->brush_rotation = 0.0;
806
807         /* draw overlay */
808         if (!BKE_paint_get_overlay_override())
809                 paint_draw_alpha_overlay(ups, brush, &vc, x, y, zoomx, mode);
810
811         /* TODO: as sculpt and other paint modes are unified, this
812          * special mode of drawing will go away */
813         if (vc.obact->sculpt) {
814                 float location[3];
815                 int pixel_radius, hit;
816
817                 /* test if brush is over the mesh */
818                 hit = sculpt_get_brush_geometry(C, &vc, x, y, &pixel_radius, location);
819
820                 if (BKE_brush_use_locked_size(scene, brush))
821                         BKE_brush_size_set(scene, brush, pixel_radius);
822
823                 /* check if brush is subtracting, use different color then */
824                 /* TODO: no way currently to know state of pen flip or
825                  * invert key modifier without starting a stroke */
826                 if ((!(brush->flag & BRUSH_INVERTED) ^
827                      !(brush->flag & BRUSH_DIR_IN)) &&
828                     ELEM5(brush->sculpt_tool, SCULPT_TOOL_DRAW,
829                           SCULPT_TOOL_INFLATE, SCULPT_TOOL_CLAY,
830                           SCULPT_TOOL_PINCH, SCULPT_TOOL_CREASE))
831                 {
832                         outline_col = brush->sub_col;
833                 }
834
835                 /* only do if brush is over the mesh */
836                 if (hit)
837                         paint_cursor_on_hit(ups, brush, &vc, location);
838
839                 if (ups->draw_anchored) {
840                         final_radius = ups->anchored_size;
841                         translation[0] = ups->anchored_initial_mouse[0];
842                         translation[1] = ups->anchored_initial_mouse[1];
843                 }
844         }
845
846         /* make lines pretty */
847         glEnable(GL_BLEND);
848         glEnable(GL_LINE_SMOOTH);
849
850         /* set brush color */
851         glColor4f(outline_col[0], outline_col[1], outline_col[2], outline_alpha);
852
853         /* draw brush outline */
854         glTranslatef(translation[0], translation[1], 0);
855
856         /* draw an inner brush */
857         if (ups->draw_pressure && BKE_brush_use_size_pressure(scene, brush)) {
858                 /* inner at full alpha */
859                 glutil_draw_lined_arc(0.0, M_PI * 2.0, final_radius * ups->pressure_value, 40);
860                 /* outer at half alpha */
861                 glColor4f(outline_col[0], outline_col[1], outline_col[2], outline_alpha * 0.5f);
862         }
863         glutil_draw_lined_arc(0.0, M_PI * 2.0, final_radius, 40);
864         glTranslatef(-translation[0], -translation[1], 0);
865
866         /* restore GL state */
867         glDisable(GL_BLEND);
868         glDisable(GL_LINE_SMOOTH);
869 }
870
871 /* Public API */
872
873 void paint_cursor_start(bContext *C, int (*poll)(bContext *C))
874 {
875         Paint *p = BKE_paint_get_active_from_context(C);
876
877         if (p && !p->paint_cursor)
878                 p->paint_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), poll, paint_draw_cursor, NULL);
879
880         /* invalidate the paint cursors */
881         BKE_paint_invalidate_overlay_all();
882 }
883
884 void paint_cursor_start_explicit(Paint *p, wmWindowManager *wm, int (*poll)(bContext *C))
885 {
886         if (p && !p->paint_cursor)
887                 p->paint_cursor = WM_paint_cursor_activate(wm, poll, paint_draw_cursor, NULL);
888 }