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