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