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