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