Fix part of #20090: boolean modifier would lose link to object
[blender.git] / source / blender / blenkernel / intern / brush.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <math.h>
31 #include <string.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "DNA_brush_types.h"
36 #include "DNA_color_types.h"
37 #include "DNA_image_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_texture_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_windowmanager_types.h"
42
43 #include "WM_types.h"
44
45 #include "RNA_access.h"
46
47 #include "BLI_math.h"
48 #include "BLI_blenlib.h"
49 #include "BLI_rand.h"
50
51 #include "BKE_brush.h"
52 #include "BKE_colortools.h"
53 #include "BKE_global.h"
54 #include "BKE_image.h"
55 #include "BKE_library.h"
56 #include "BKE_main.h"
57 #include "BKE_paint.h"
58 #include "BKE_texture.h"
59 #include "BKE_utildefines.h"
60
61
62
63 #include "IMB_imbuf.h"
64 #include "IMB_imbuf_types.h"
65
66 #include "RE_render_ext.h" /* externtex */
67 #include "RE_shader_ext.h"
68
69 /* Datablock add/copy/free/make_local */
70
71 Brush *add_brush(const char *name)
72 {
73         Brush *brush;
74
75         brush= alloc_libblock(&G.main->brush, ID_BR, name);
76
77         brush->rgb[0]= 1.0f;
78         brush->rgb[1]= 1.0f;
79         brush->rgb[2]= 1.0f;
80         brush->alpha= 0.2f;
81         brush->size= 25;
82         brush->spacing= 7.5f;
83         brush->smooth_stroke_radius= 75;
84         brush->smooth_stroke_factor= 0.9;
85         brush->rate= 0.1f;
86         brush->jitter= 0.0f;
87         brush->clone.alpha= 0.5;
88         brush->sculpt_tool = SCULPT_TOOL_DRAW;
89         brush->flag |= BRUSH_SPACE;
90
91         brush_curve_preset(brush, CURVE_PRESET_SMOOTH);
92         
93         default_mtex(&brush->mtex);
94
95         /* enable fake user by default */
96         brush->id.flag |= LIB_FAKEUSER;
97         brush_toggled_fake_user(brush);
98         
99         return brush;   
100 }
101
102 Brush *copy_brush(Brush *brush)
103 {
104         Brush *brushn;
105         
106         brushn= copy_libblock(brush);
107
108         if(brush->mtex.tex) id_us_plus((ID*)brush->mtex.tex);
109         
110         brushn->curve= curvemapping_copy(brush->curve);
111
112         /* enable fake user by default */
113         if (!(brushn->id.flag & LIB_FAKEUSER)) {
114                 brushn->id.flag |= LIB_FAKEUSER;
115                 brush_toggled_fake_user(brushn);
116         }
117         
118         return brushn;
119 }
120
121 /* not brush itself */
122 void free_brush(Brush *brush)
123 {
124         if(brush->mtex.tex) brush->mtex.tex->id.us--;
125         
126         curvemapping_free(brush->curve);
127 }
128
129 void make_local_brush(Brush *brush)
130 {
131         /* - only lib users: do nothing
132                 * - only local users: set flag
133                 * - mixed: make copy
134                 */
135         
136         Brush *brushn;
137         Scene *scene;
138         int local= 0, lib= 0;
139
140         if(brush->id.lib==0) return;
141
142         if(brush->clone.image) {
143                 /* special case: ima always local immediately */
144                 brush->clone.image->id.lib= 0;
145                 brush->clone.image->id.flag= LIB_LOCAL;
146                 new_id(0, (ID *)brush->clone.image, 0);
147         }
148
149         for(scene= G.main->scene.first; scene; scene=scene->id.next)
150                 if(paint_brush(&scene->toolsettings->imapaint.paint)==brush) {
151                         if(scene->id.lib) lib= 1;
152                         else local= 1;
153                 }
154         
155         if(local && lib==0) {
156                 brush->id.lib= 0;
157                 brush->id.flag= LIB_LOCAL;
158                 new_id(0, (ID *)brush, 0);
159
160                 /* enable fake user by default */
161                 if (!(brush->id.flag & LIB_FAKEUSER)) {
162                         brush->id.flag |= LIB_FAKEUSER;
163                         brush_toggled_fake_user(brush);
164                 }
165         }
166         else if(local && lib) {
167                 brushn= copy_brush(brush);
168                 brushn->id.us= 1; /* only keep fake user */
169                 brushn->id.flag |= LIB_FAKEUSER;
170                 
171                 for(scene= G.main->scene.first; scene; scene=scene->id.next)
172                         if(paint_brush(&scene->toolsettings->imapaint.paint)==brush)
173                                 if(scene->id.lib==0) {
174                                         paint_brush_set(&scene->toolsettings->imapaint.paint, brushn);
175                                         brushn->id.us++;
176                                         brush->id.us--;
177                                 }
178         }
179 }
180
181 /* Library Operations */
182
183 int brush_set_nr(Brush **current_brush, int nr, const char *name)
184 {
185         ID *idtest, *id;
186         
187         id= (ID*)(*current_brush);
188         idtest= (ID*)BLI_findlink(&G.main->brush, nr-1);
189         
190         if(idtest==0) { /* new brush */
191                 if(id) idtest= (ID *)copy_brush((Brush *)id);
192                 else idtest= (ID *)add_brush(name);
193                 idtest->us--;
194         }
195         if(idtest!=id) {
196                 brush_delete(current_brush);
197                 *current_brush= (Brush *)idtest;
198                 id_us_plus(idtest);
199
200                 return 1;
201         }
202
203         return 0;
204 }
205
206 int brush_delete(Brush **current_brush)
207 {
208         if (*current_brush) {
209                 (*current_brush)->id.us--;
210                 *current_brush= NULL;
211
212                 return 1;
213         }
214
215         return 0;
216 }
217
218 void brush_toggled_fake_user(Brush *brush)
219 {
220         ID *id= (ID*)brush;
221         if(id) {
222                 if(id->flag & LIB_FAKEUSER) {
223                         id_us_plus(id);
224                 } else {
225                         id->us--;
226                 }
227         }
228 }
229
230 void brush_curve_preset(Brush *b, /*CurveMappingPreset*/int preset)
231 {
232         CurveMap *cm = NULL;
233
234         if(!b->curve)
235                 b->curve = curvemapping_add(1, 0, 0, 1, 1);
236
237         cm = b->curve->cm;
238         cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
239
240         curvemap_reset(cm, &b->curve->clipr, preset);
241         curvemapping_changed(b->curve, 0);
242 }
243
244 static MTex *brush_active_texture(Brush *brush)
245 {
246         if(brush)
247                 return &brush->mtex;
248         return NULL;
249 }
250
251 int brush_texture_set_nr(Brush *brush, int nr)
252 {
253         ID *idtest, *id=NULL;
254
255         id= (ID *)brush->mtex.tex;
256
257         idtest= (ID*)BLI_findlink(&G.main->tex, nr-1);
258         if(idtest==0) { /* new tex */
259                 if(id) idtest= (ID *)copy_texture((Tex *)id);
260                 else idtest= (ID *)add_texture("Tex");
261                 idtest->us--;
262         }
263         if(idtest!=id) {
264                 brush_texture_delete(brush);
265
266                 brush->mtex.tex= (Tex*)idtest;
267                 id_us_plus(idtest);
268
269                 return 1;
270         }
271
272         return 0;
273 }
274
275 int brush_texture_delete(Brush *brush)
276 {
277         if(brush->mtex.tex)
278                 brush->mtex.tex->id.us--;
279
280         return 1;
281 }
282
283 int brush_clone_image_set_nr(Brush *brush, int nr)
284 {
285         if(brush && nr > 0) {
286                 Image *ima= (Image*)BLI_findlink(&G.main->image, nr-1);
287
288                 if(ima) {
289                         brush_clone_image_delete(brush);
290                         brush->clone.image= ima;
291                         id_us_plus(&ima->id);
292                         brush->clone.offset[0]= brush->clone.offset[1]= 0.0f;
293
294                         return 1;
295                 }
296         }
297
298         return 0;
299 }
300
301 int brush_clone_image_delete(Brush *brush)
302 {
303         if (brush && brush->clone.image) {
304                 brush->clone.image->id.us--;
305                 brush->clone.image= NULL;
306                 return 1;
307         }
308
309         return 0;
310 }
311
312 void brush_check_exists(Brush **brush, const char *name)
313 {
314         if(*brush==NULL)
315                 brush_set_nr(brush, 1, name);
316 }
317
318 /* Brush Sampling */
319 void brush_sample_tex(Brush *brush, float *xy, float *rgba)
320 {
321         MTex *mtex= &brush->mtex;
322
323         if (mtex && mtex->tex) {
324                 float co[3], tin, tr, tg, tb, ta;
325                 int hasrgb;
326                 
327                 co[0]= xy[0]/(brush->size >> 1);
328                 co[1]= xy[1]/(brush->size >> 1);
329                 co[2]= 0.0f;
330
331                 hasrgb= externtex(mtex, co, &tin, &tr, &tg, &tb, &ta);
332
333                 if (hasrgb) {
334                         rgba[0]= tr;
335                         rgba[1]= tg;
336                         rgba[2]= tb;
337                         rgba[3]= ta;
338                 }
339                 else {
340                         rgba[0]= tin;
341                         rgba[1]= tin;
342                         rgba[2]= tin;
343                         rgba[3]= 1.0f;
344                 }
345         }
346         else if (rgba)
347                 rgba[0]= rgba[1]= rgba[2]= rgba[3]= 1.0f;
348 }
349
350
351 void brush_imbuf_new(Brush *brush, short flt, short texfall, int size, ImBuf **outbuf)
352 {
353         ImBuf *ibuf;
354         float xy[2], dist, rgba[4], *dstf;
355         int x, y, rowbytes, xoff, yoff, imbflag;
356         int maxsize = brush->size >> 1;
357         char *dst, crgb[3];
358
359         imbflag= (flt)? IB_rectfloat: IB_rect;
360         xoff = -size/2.0f + 0.5f;
361         yoff = -size/2.0f + 0.5f;
362         rowbytes= size*4;
363
364         if (*outbuf)
365                 ibuf= *outbuf;
366         else
367                 ibuf= IMB_allocImBuf(size, size, 32, imbflag, 0);
368
369         if (flt) {
370                 for (y=0; y < ibuf->y; y++) {
371                         dstf = ibuf->rect_float + y*rowbytes;
372
373                         for (x=0; x < ibuf->x; x++, dstf+=4) {
374                                 xy[0] = x + xoff;
375                                 xy[1] = y + yoff;
376
377                                 if (texfall == 0) {
378                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
379
380                                         VECCOPY(dstf, brush->rgb);
381                                         dstf[3]= brush->alpha*brush_curve_strength_clamp(brush, dist, maxsize);
382                                 }
383                                 else if (texfall == 1) {
384                                         brush_sample_tex(brush, xy, dstf);
385                                 }
386                                 else {
387                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
388
389                                         brush_sample_tex(brush, xy, rgba);
390
391                                         dstf[0] = rgba[0]*brush->rgb[0];
392                                         dstf[1] = rgba[1]*brush->rgb[1];
393                                         dstf[2] = rgba[2]*brush->rgb[2];
394                                         dstf[3] = rgba[3]*brush->alpha*brush_curve_strength_clamp(brush, dist, maxsize);
395                                 }
396                         }
397                 }
398         }
399         else {
400                 crgb[0]= FTOCHAR(brush->rgb[0]);
401                 crgb[1]= FTOCHAR(brush->rgb[1]);
402                 crgb[2]= FTOCHAR(brush->rgb[2]);
403
404                 for (y=0; y < ibuf->y; y++) {
405                         dst = (char*)ibuf->rect + y*rowbytes;
406
407                         for (x=0; x < ibuf->x; x++, dst+=4) {
408                                 xy[0] = x + xoff;
409                                 xy[1] = y + yoff;
410
411                                 if (texfall == 0) {
412                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
413
414                                         dst[0]= crgb[0];
415                                         dst[1]= crgb[1];
416                                         dst[2]= crgb[2];
417                                         dst[3]= FTOCHAR(brush->alpha*brush_curve_strength(brush, dist, maxsize));
418                                 }
419                                 else if (texfall == 1) {
420                                         brush_sample_tex(brush, xy, rgba);
421                                         dst[0]= FTOCHAR(rgba[0]);
422                                         dst[1]= FTOCHAR(rgba[1]);
423                                         dst[2]= FTOCHAR(rgba[2]);
424                                         dst[3]= FTOCHAR(rgba[3]);
425                                 }
426                                 else {
427                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
428
429                                         brush_sample_tex(brush, xy, rgba);
430                                         dst[0] = FTOCHAR(rgba[0]*brush->rgb[0]);
431                                         dst[1] = FTOCHAR(rgba[1]*brush->rgb[1]);
432                                         dst[2] = FTOCHAR(rgba[2]*brush->rgb[2]);
433                                         dst[3] = FTOCHAR(rgba[3]*brush->alpha*brush_curve_strength_clamp(brush, dist, maxsize));
434                                 }
435                         }
436                 }
437         }
438
439         *outbuf= ibuf;
440 }
441
442 /* Brush Painting */
443
444 typedef struct BrushPainterCache {
445         short enabled;
446
447         int size;                       /* size override, if 0 uses brush->size */
448         short flt;                      /* need float imbuf? */
449         short texonly;          /* no alpha, color or fallof, only texture in imbuf */
450
451         int lastsize;
452         float lastalpha;
453         float lastjitter;
454
455         ImBuf *ibuf;
456         ImBuf *texibuf;
457         ImBuf *maskibuf;
458 } BrushPainterCache;
459
460 struct BrushPainter {
461         Brush *brush;
462
463         float lastmousepos[2];  /* mouse position of last paint call */
464
465         float accumdistance;    /* accumulated distance of brush since last paint op */
466         float lastpaintpos[2];  /* position of last paint op */
467         float startpaintpos[2]; /* position of first paint */
468
469         double accumtime;               /* accumulated time since last paint op (airbrush) */
470         double lasttime;                /* time of last update */
471
472         float lastpressure;
473
474         short firsttouch;               /* first paint op */
475
476         float startsize;
477         float startalpha;
478         float startjitter;
479         float startspacing;
480
481         BrushPainterCache cache;
482 };
483
484 BrushPainter *brush_painter_new(Brush *brush)
485 {
486         BrushPainter *painter= MEM_callocN(sizeof(BrushPainter), "BrushPainter");
487
488         painter->brush= brush;
489         painter->firsttouch= 1;
490         painter->cache.lastsize= -1; /* force ibuf create in refresh */
491
492         painter->startsize = brush->size;
493         painter->startalpha = brush->alpha;
494         painter->startjitter = brush->jitter;
495         painter->startspacing = brush->spacing;
496
497         return painter;
498 }
499
500 void brush_painter_require_imbuf(BrushPainter *painter, short flt, short texonly, int size)
501 {
502         if ((painter->cache.flt != flt) || (painter->cache.size != size) ||
503                 ((painter->cache.texonly != texonly) && texonly)) {
504                 if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
505                 if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
506                 painter->cache.ibuf= painter->cache.maskibuf= NULL;
507                 painter->cache.lastsize= -1; /* force ibuf create in refresh */
508         }
509
510         if (painter->cache.flt != flt) {
511                 if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
512                 painter->cache.texibuf= NULL;
513                 painter->cache.lastsize= -1; /* force ibuf create in refresh */
514         }
515
516         painter->cache.size= size;
517         painter->cache.flt= flt;
518         painter->cache.texonly= texonly;
519         painter->cache.enabled= 1;
520 }
521
522 void brush_painter_free(BrushPainter *painter)
523 {
524         Brush *brush = painter->brush;
525
526         brush->size = painter->startsize;
527         brush->alpha = painter->startalpha;
528         brush->jitter = painter->startjitter;
529         brush->spacing = painter->startspacing;
530
531         if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
532         if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
533         if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
534         MEM_freeN(painter);
535 }
536
537 static void brush_painter_do_partial(BrushPainter *painter, ImBuf *oldtexibuf, int x, int y, int w, int h, int xt, int yt, float *pos)
538 {
539         Brush *brush= painter->brush;
540         ImBuf *ibuf, *maskibuf, *texibuf;
541         float *bf, *mf, *tf, *otf=NULL, xoff, yoff, xy[2], rgba[4];
542         char *b, *m, *t, *ot= NULL;
543         int dotexold, origx= x, origy= y;
544
545         xoff = -brush->size/2.0f + 0.5f;
546         yoff = -brush->size/2.0f + 0.5f;
547         xoff += (int)pos[0] - (int)painter->startpaintpos[0];
548         yoff += (int)pos[1] - (int)painter->startpaintpos[1];
549
550         ibuf = painter->cache.ibuf;
551         texibuf = painter->cache.texibuf;
552         maskibuf = painter->cache.maskibuf;
553
554         dotexold = (oldtexibuf != NULL);
555
556         if (painter->cache.flt) {
557                 for (; y < h; y++) {
558                         bf = ibuf->rect_float + (y*ibuf->x + origx)*4;
559                         tf = texibuf->rect_float + (y*texibuf->x + origx)*4;
560                         mf = maskibuf->rect_float + (y*maskibuf->x + origx)*4;
561
562                         if (dotexold)
563                                 otf = oldtexibuf->rect_float + ((y - origy + yt)*oldtexibuf->x + xt)*4;
564
565                         for (x=origx; x < w; x++, bf+=4, mf+=4, tf+=4) {
566                                 if (dotexold) {
567                                         VECCOPY(tf, otf);
568                                         tf[3] = otf[3];
569                                         otf += 4;
570                                 }
571                                 else {
572                                         xy[0] = x + xoff;
573                                         xy[1] = y + yoff;
574
575                                         brush_sample_tex(brush, xy, tf);
576                                 }
577
578                                 bf[0] = tf[0]*mf[0];
579                                 bf[1] = tf[1]*mf[1];
580                                 bf[2] = tf[2]*mf[2];
581                                 bf[3] = tf[3]*mf[3];
582                         }
583                 }
584         }
585         else {
586                 for (; y < h; y++) {
587                         b = (char*)ibuf->rect + (y*ibuf->x + origx)*4;
588                         t = (char*)texibuf->rect + (y*texibuf->x + origx)*4;
589                         m = (char*)maskibuf->rect + (y*maskibuf->x + origx)*4;
590
591                         if (dotexold)
592                                 ot = (char*)oldtexibuf->rect + ((y - origy + yt)*oldtexibuf->x + xt)*4;
593
594                         for (x=origx; x < w; x++, b+=4, m+=4, t+=4) {
595                                 if (dotexold) {
596                                         t[0] = ot[0];
597                                         t[1] = ot[1];
598                                         t[2] = ot[2];
599                                         t[3] = ot[3];
600                                         ot += 4;
601                                 }
602                                 else {
603                                         xy[0] = x + xoff;
604                                         xy[1] = y + yoff;
605
606                                         brush_sample_tex(brush, xy, rgba);
607                                         t[0]= FTOCHAR(rgba[0]);
608                                         t[1]= FTOCHAR(rgba[1]);
609                                         t[2]= FTOCHAR(rgba[2]);
610                                         t[3]= FTOCHAR(rgba[3]);
611                                 }
612
613                                 b[0] = t[0]*m[0]/255;
614                                 b[1] = t[1]*m[1]/255;
615                                 b[2] = t[2]*m[2]/255;
616                                 b[3] = t[3]*m[3]/255;
617                         }
618                 }
619         }
620 }
621
622 static void brush_painter_fixed_tex_partial_update(BrushPainter *painter, float *pos)
623 {
624         Brush *brush= painter->brush;
625         BrushPainterCache *cache= &painter->cache;
626         ImBuf *oldtexibuf, *ibuf;
627         int imbflag, destx, desty, srcx, srcy, w, h, x1, y1, x2, y2;
628
629         imbflag= (cache->flt)? IB_rectfloat: IB_rect;
630         if (!cache->ibuf)
631                 cache->ibuf= IMB_allocImBuf(brush->size, brush->size, 32, imbflag, 0);
632         ibuf= cache->ibuf;
633
634         oldtexibuf= cache->texibuf;
635         cache->texibuf= IMB_allocImBuf(brush->size, brush->size, 32, imbflag, 0);
636
637         if (oldtexibuf) {
638                 srcx= srcy= 0;
639                 destx= (int)painter->lastpaintpos[0] - (int)pos[0];
640                 desty= (int)painter->lastpaintpos[1] - (int)pos[1];
641                 w= oldtexibuf->x;
642                 h= oldtexibuf->y;
643
644                 IMB_rectclip(cache->texibuf, oldtexibuf, &destx, &desty, &srcx, &srcy, &w, &h);
645         }
646         else {
647                 srcx= srcy= 0;
648                 destx= desty= 0;
649                 w= h= 0;
650         }
651         
652         x1= destx;
653         y1= desty;
654         x2= destx+w;
655         y2= desty+h;
656
657         /* blend existing texture in new position */
658         if ((x1 < x2) && (y1 < y2))
659                 brush_painter_do_partial(painter, oldtexibuf, x1, y1, x2, y2, srcx, srcy, pos);
660
661         if (oldtexibuf)
662                 IMB_freeImBuf(oldtexibuf);
663
664         /* sample texture in new areas */
665         if ((0 < x1) && (0 < ibuf->y))
666                 brush_painter_do_partial(painter, NULL, 0, 0, x1, ibuf->y, 0, 0, pos);
667         if ((x2 < ibuf->x) && (0 < ibuf->y))
668                 brush_painter_do_partial(painter, NULL, x2, 0, ibuf->x, ibuf->y, 0, 0, pos);
669         if ((x1 < x2) && (0 < y1))
670                 brush_painter_do_partial(painter, NULL, x1, 0, x2, y1, 0, 0, pos);
671         if ((x1 < x2) && (y2 < ibuf->y))
672                 brush_painter_do_partial(painter, NULL, x1, y2, x2, ibuf->y, 0, 0, pos);
673 }
674
675 static void brush_painter_refresh_cache(BrushPainter *painter, float *pos)
676 {
677         Brush *brush= painter->brush;
678         BrushPainterCache *cache= &painter->cache;
679         MTex *mtex= &brush->mtex;
680         int size;
681         short flt;
682
683         if ((brush->size != cache->lastsize) || (brush->alpha != cache->lastalpha)
684             || (brush->jitter != cache->lastjitter)) {
685                 if (cache->ibuf) {
686                         IMB_freeImBuf(cache->ibuf);
687                         cache->ibuf= NULL;
688                 }
689                 if (cache->maskibuf) {
690                         IMB_freeImBuf(cache->maskibuf);
691                         cache->maskibuf= NULL;
692                 }
693
694                 flt= cache->flt;
695                 size= (cache->size)? cache->size: brush->size;
696
697                 if (!(mtex && mtex->tex) || (mtex->tex->type==0)) {
698                         brush_imbuf_new(brush, flt, 0, size, &cache->ibuf);
699                 }
700                 else if (brush->flag & BRUSH_FIXED_TEX) {
701                         brush_imbuf_new(brush, flt, 0, size, &cache->maskibuf);
702                         brush_painter_fixed_tex_partial_update(painter, pos);
703                 }
704                 else
705                         brush_imbuf_new(brush, flt, 2, size, &cache->ibuf);
706
707                 cache->lastsize= brush->size;
708                 cache->lastalpha= brush->alpha;
709                 cache->lastjitter= brush->jitter;
710         }
711         else if ((brush->flag & BRUSH_FIXED_TEX) && mtex && mtex->tex) {
712                 int dx = (int)painter->lastpaintpos[0] - (int)pos[0];
713                 int dy = (int)painter->lastpaintpos[1] - (int)pos[1];
714
715                 if ((dx != 0) || (dy != 0))
716                         brush_painter_fixed_tex_partial_update(painter, pos);
717         }
718 }
719
720 void brush_painter_break_stroke(BrushPainter *painter)
721 {
722         painter->firsttouch= 1;
723 }
724
725 static void brush_apply_pressure(BrushPainter *painter, Brush *brush, float pressure)
726 {
727         if (brush->flag & BRUSH_ALPHA_PRESSURE) 
728                 brush->alpha = MAX2(0.0, painter->startalpha*pressure);
729         if (brush->flag & BRUSH_SIZE_PRESSURE)
730                 brush->size = MAX2(1.0, painter->startsize*pressure);
731         if (brush->flag & BRUSH_JITTER_PRESSURE)
732                 brush->jitter = MAX2(0.0, painter->startjitter*pressure);
733         if (brush->flag & BRUSH_SPACING_PRESSURE)
734                 brush->spacing = MAX2(1.0, painter->startspacing*(1.5f-pressure));
735 }
736
737 static void brush_jitter_pos(Brush *brush, float *pos, float *jitterpos)
738 {
739         if(brush->jitter){
740                 jitterpos[0] = pos[0] + ((BLI_frand()-0.5f) * brush->size * brush->jitter * 2);
741                 jitterpos[1] = pos[1] + ((BLI_frand()-0.5f) * brush->size * brush->jitter * 2);
742         }
743         else {
744                 VECCOPY2D(jitterpos, pos);
745         }
746 }
747
748 int brush_painter_paint(BrushPainter *painter, BrushFunc func, float *pos, double time, float pressure, void *user)
749 {
750         Brush *brush= painter->brush;
751         int totpaintops= 0;
752
753         if (pressure == 0.0f) {
754                 if(painter->lastpressure) // XXX - hack, operator misses
755                         pressure= painter->lastpressure;
756                 else
757                         pressure = 1.0f;        /* zero pressure == not using tablet */
758         }
759         if (painter->firsttouch) {
760                 /* paint exactly once on first touch */
761                 painter->startpaintpos[0]= pos[0];
762                 painter->startpaintpos[1]= pos[1];
763
764                 brush_apply_pressure(painter, brush, pressure);
765                 if (painter->cache.enabled)
766                         brush_painter_refresh_cache(painter, pos);
767                 totpaintops += func(user, painter->cache.ibuf, pos, pos);
768                 
769                 painter->lasttime= time;
770                 painter->firsttouch= 0;
771                 painter->lastpaintpos[0]= pos[0];
772                 painter->lastpaintpos[1]= pos[1];
773         }
774 #if 0
775         else if (painter->brush->flag & BRUSH_AIRBRUSH) {
776                 float spacing, step, paintpos[2], dmousepos[2], len;
777                 double starttime, curtime= time;
778
779                 /* compute brush spacing adapted to brush size */
780                 spacing= brush->rate; //brush->size*brush->spacing*0.01f;
781
782                 /* setup starting time, direction vector and accumulated time */
783                 starttime= painter->accumtime;
784                 sub_v2_v2v2(dmousepos, pos, painter->lastmousepos);
785                 len= normalize_v2(dmousepos);
786                 painter->accumtime += curtime - painter->lasttime;
787
788                 /* do paint op over unpainted time distance */
789                 while (painter->accumtime >= spacing) {
790                         step= (spacing - starttime)*len;
791                         paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
792                         paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
793
794                         if (painter->cache.enabled)
795                                 brush_painter_refresh_cache(painter);
796                         totpaintops += func(user, painter->cache.ibuf,
797                                 painter->lastpaintpos, paintpos);
798
799                         painter->lastpaintpos[0]= paintpos[0];
800                         painter->lastpaintpos[1]= paintpos[1];
801                         painter->accumtime -= spacing;
802                         starttime -= spacing;
803                 }
804                 
805                 painter->lasttime= curtime;
806         }
807 #endif
808         else {
809                 float startdistance, spacing, step, paintpos[2], dmousepos[2], finalpos[2];
810                 float t, len, press;
811
812                 /* compute brush spacing adapted to brush size, spacing may depend
813                    on pressure, so update it */
814                 brush_apply_pressure(painter, brush, painter->lastpressure);
815                 spacing= MAX2(1.0f, brush->size)*brush->spacing*0.01f;
816
817                 /* setup starting distance, direction vector and accumulated distance */
818                 startdistance= painter->accumdistance;
819                 sub_v2_v2v2(dmousepos, pos, painter->lastmousepos);
820                 len= normalize_v2(dmousepos);
821                 painter->accumdistance += len;
822
823                 /* do paint op over unpainted distance */
824                 while ((len > 0.0f) && (painter->accumdistance >= spacing)) {
825                         step= spacing - startdistance;
826                         paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
827                         paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
828
829                         t = step/len;
830                         press= (1.0f-t)*painter->lastpressure + t*pressure;
831                         brush_apply_pressure(painter, brush, press);
832                         spacing= MAX2(1.0f, brush->size)*brush->spacing*0.01f;
833
834                         brush_jitter_pos(brush, paintpos, finalpos);
835
836                         if (painter->cache.enabled)
837                                 brush_painter_refresh_cache(painter, finalpos);
838
839                         totpaintops +=
840                                 func(user, painter->cache.ibuf, painter->lastpaintpos, finalpos);
841
842                         painter->lastpaintpos[0]= paintpos[0];
843                         painter->lastpaintpos[1]= paintpos[1];
844                         painter->accumdistance -= spacing;
845                         startdistance -= spacing;
846                 }
847
848                 /* do airbrush paint ops, based on the number of paint ops left over
849                    from regular painting. this is a temporary solution until we have
850                    accurate time stamps for mouse move events */
851                 if (brush->flag & BRUSH_AIRBRUSH) {
852                         double curtime= time;
853                         double painttime= brush->rate*totpaintops;
854
855                         painter->accumtime += curtime - painter->lasttime;
856                         if (painter->accumtime <= painttime)
857                                 painter->accumtime= 0.0;
858                         else
859                                 painter->accumtime -= painttime;
860
861                         while (painter->accumtime >= brush->rate) {
862                                 brush_apply_pressure(painter, brush, pressure);
863
864                                 brush_jitter_pos(brush, pos, finalpos);
865
866                                 if (painter->cache.enabled)
867                                         brush_painter_refresh_cache(painter, finalpos);
868
869                                 totpaintops +=
870                                         func(user, painter->cache.ibuf, painter->lastmousepos, finalpos);
871                                 painter->accumtime -= brush->rate;
872                         }
873
874                         painter->lasttime= curtime;
875                 }
876         }
877
878         painter->lastmousepos[0]= pos[0];
879         painter->lastmousepos[1]= pos[1];
880         painter->lastpressure= pressure;
881
882         brush->alpha = painter->startalpha;
883         brush->size = painter->startsize;
884         brush->jitter = painter->startjitter;
885         brush->spacing = painter->startspacing;
886
887         return totpaintops;
888 }
889
890 /* Uses the brush curve control to find a strength value between 0 and 1 */
891 float brush_curve_strength_clamp(Brush *br, float p, const float len)
892 {
893         if(p >= len)    p= 1.0f;
894         else                    p= p/len;
895
896         p= curvemapping_evaluateF(br->curve, 0, p);
897         if(p < 0.0)                     p= 0.0f;
898         else if(p > 1.0f)       p= 1.0f;
899         return p;
900 }
901 /* same as above but can return negative values if the curve enables
902  * used for sculpt only */
903 float brush_curve_strength(Brush *br, float p, const float len)
904 {
905         if(p >= len)    p= 1.0f;
906         else                    p= p/len;
907         return curvemapping_evaluateF(br->curve, 0, p);
908 }
909
910 /* TODO: should probably be unified with BrushPainter stuff? */
911 unsigned int *brush_gen_texture_cache(Brush *br, int half_side)
912 {
913         unsigned int *texcache = NULL;
914         MTex *mtex = &br->mtex;
915         TexResult texres;
916         int hasrgb, ix, iy;
917         int side = half_side * 2;
918
919         memset(&texres, 0, sizeof(TexResult));
920         
921         if(mtex && mtex->tex) {
922                 float x, y, step = 2.0 / side, co[3];
923
924                 texcache = MEM_callocN(sizeof(int) * side * side, "Brush texture cache");
925
926                 BKE_image_get_ibuf(mtex->tex->ima, NULL);
927                 
928                 /*do normalized cannonical view coords for texture*/
929                 for (y=-1.0, iy=0; iy<side; iy++, y += step) {
930                         for (x=-1.0, ix=0; ix<side; ix++, x += step) {
931                                 co[0]= x;
932                                 co[1]= y;
933                                 co[2]= 0.0f;
934                                 
935                                 /* This is copied from displace modifier code */
936                                 hasrgb = multitex_ext(mtex->tex, co, NULL, NULL, 1, &texres);
937                         
938                                 /* if the texture gave an RGB value, we assume it didn't give a valid
939                                  * intensity, so calculate one (formula from do_material_tex).
940                                  * if the texture didn't give an RGB value, copy the intensity across
941                                  */
942                                 if(hasrgb & TEX_RGB)
943                                         texres.tin = (0.35 * texres.tr + 0.45 *
944                                                       texres.tg + 0.2 * texres.tb);
945
946                                 texres.tin = texres.tin * 255.0;
947                                 ((char*)texcache)[(iy*side+ix)*4] = (char)texres.tin;
948                                 ((char*)texcache)[(iy*side+ix)*4+1] = (char)texres.tin;
949                                 ((char*)texcache)[(iy*side+ix)*4+2] = (char)texres.tin;
950                                 ((char*)texcache)[(iy*side+ix)*4+3] = (char)texres.tin;
951                         }
952                 }
953         }
954
955         return texcache;
956 }
957
958 /**** Radial Control ****/
959 static struct ImBuf *brush_gen_radial_control_imbuf(Brush *br)
960 {
961         ImBuf *im = MEM_callocN(sizeof(ImBuf), "radial control texture");
962         unsigned int *texcache;
963         int side = 128;
964         int half = side / 2;
965         int i, j;
966
967         texcache = brush_gen_texture_cache(br, half);
968         im->rect_float = MEM_callocN(sizeof(float) * side * side, "radial control rect");
969         im->x = im->y = side;
970
971         for(i=0; i<side; ++i) {
972                 for(j=0; j<side; ++j) {
973                         float magn= sqrt(pow(i - half, 2) + pow(j - half, 2));
974                         im->rect_float[i*side + j]= brush_curve_strength_clamp(br, magn, half);
975                 }
976         }
977
978         /* Modulate curve with texture */
979         if(texcache) {
980                 for(i=0; i<side; ++i) {
981                         for(j=0; j<side; ++j) {
982                                 const int col= texcache[i*side+j];
983                                 im->rect_float[i*side+j]*= (((char*)&col)[0]+((char*)&col)[1]+((char*)&col)[2])/3.0f/255.0f;
984                         }
985                 }
986
987                 MEM_freeN(texcache);
988         }
989
990         return im;
991 }
992
993 void brush_radial_control_invoke(wmOperator *op, Brush *br, float size_weight)
994 {
995         int mode = RNA_int_get(op->ptr, "mode");
996         float original_value= 0;
997
998         if(mode == WM_RADIALCONTROL_SIZE)
999                 original_value = br->size * size_weight;
1000         else if(mode == WM_RADIALCONTROL_STRENGTH)
1001                 original_value = br->alpha;
1002         else if(mode == WM_RADIALCONTROL_ANGLE) {
1003                 MTex *mtex = brush_active_texture(br);
1004                 if(mtex)
1005                         original_value = mtex->rot;
1006         }
1007
1008         RNA_float_set(op->ptr, "initial_value", original_value);
1009         op->customdata = brush_gen_radial_control_imbuf(br);
1010 }
1011
1012 int brush_radial_control_exec(wmOperator *op, Brush *br, float size_weight)
1013 {
1014         int mode = RNA_int_get(op->ptr, "mode");
1015         float new_value = RNA_float_get(op->ptr, "new_value");
1016         const float conv = 0.017453293;
1017
1018         if(mode == WM_RADIALCONTROL_SIZE)
1019                 br->size = new_value * size_weight;
1020         else if(mode == WM_RADIALCONTROL_STRENGTH)
1021                 br->alpha = new_value;
1022         else if(mode == WM_RADIALCONTROL_ANGLE) {
1023                 MTex *mtex = brush_active_texture(br);
1024                 if(mtex)
1025                         mtex->rot = new_value * conv;
1026         }
1027
1028         return OPERATOR_FINISHED;
1029 }