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