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