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