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