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