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