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