Merging r58464 through r58474 from trunk into soc-2013-depsgraph_mt
[blender.git] / source / blender / blenkernel / intern / texture.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/texture.c
29  *  \ingroup bke
30  */
31
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <math.h>
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_dynlib.h"
42 #include "BLI_math.h"
43 #include "BLI_kdopbvh.h"
44 #include "BLI_utildefines.h"
45
46 #include "DNA_key_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_lamp_types.h"
49 #include "DNA_material_types.h"
50 #include "DNA_world_types.h"
51 #include "DNA_brush_types.h"
52 #include "DNA_node_types.h"
53 #include "DNA_color_types.h"
54 #include "DNA_particle_types.h"
55
56 #include "IMB_imbuf.h"
57
58 #include "BKE_global.h"
59 #include "BKE_main.h"
60 #include "BKE_ocean.h"
61
62 #include "BKE_library.h"
63 #include "BKE_image.h"
64 #include "BKE_material.h"
65 #include "BKE_texture.h"
66 #include "BKE_key.h"
67 #include "BKE_icons.h"
68 #include "BKE_node.h"
69 #include "BKE_animsys.h"
70 #include "BKE_colortools.h"
71
72 /* ****************** Mapping ******************* */
73
74 TexMapping *add_tex_mapping(void)
75 {
76         TexMapping *texmap = MEM_callocN(sizeof(TexMapping), "TexMapping");
77         
78         default_tex_mapping(texmap);
79         
80         return texmap;
81 }
82
83 void default_tex_mapping(TexMapping *texmap)
84 {
85         memset(texmap, 0, sizeof(TexMapping));
86
87         texmap->size[0] = texmap->size[1] = texmap->size[2] = 1.0f;
88         texmap->max[0] = texmap->max[1] = texmap->max[2] = 1.0f;
89         unit_m4(texmap->mat);
90
91         texmap->projx = PROJ_X;
92         texmap->projy = PROJ_Y;
93         texmap->projz = PROJ_Z;
94         texmap->mapping = MTEX_FLAT;
95 }
96
97 void init_tex_mapping(TexMapping *texmap)
98 {
99         float smat[3][3], rmat[3][3], mat[3][3], proj[3][3];
100
101         if (texmap->projx == PROJ_X && texmap->projy == PROJ_Y && texmap->projz == PROJ_Z &&
102             is_zero_v3(texmap->loc) && is_zero_v3(texmap->rot) && is_one_v3(texmap->size))
103         {
104                 unit_m4(texmap->mat);
105
106                 texmap->flag |= TEXMAP_UNIT_MATRIX;
107         }
108         else {
109                 /* axis projection */
110                 zero_m3(proj);
111
112                 if (texmap->projx != PROJ_N)
113                         proj[texmap->projx - 1][0] = 1.0f;
114                 if (texmap->projy != PROJ_N)
115                         proj[texmap->projy - 1][1] = 1.0f;
116                 if (texmap->projz != PROJ_N)
117                         proj[texmap->projz - 1][2] = 1.0f;
118
119                 /* scale */
120                 size_to_mat3(smat, texmap->size);
121                 
122                 /* rotation */
123                 /* TexMapping rotation are now in radians. */
124                 eul_to_mat3(rmat, texmap->rot);
125                 
126                 /* compose it all */
127                 mul_m3_m3m3(mat, rmat, smat);
128                 mul_m3_m3m3(mat, proj, mat);
129                 
130                 /* translation */
131                 copy_m4_m3(texmap->mat, mat);
132                 copy_v3_v3(texmap->mat[3], texmap->loc);
133
134                 texmap->flag &= ~TEXMAP_UNIT_MATRIX;
135         }
136 }
137
138 ColorMapping *add_color_mapping(void)
139 {
140         ColorMapping *colormap = MEM_callocN(sizeof(ColorMapping), "ColorMapping");
141         
142         default_color_mapping(colormap);
143         
144         return colormap;
145 }
146
147 void default_color_mapping(ColorMapping *colormap)
148 {
149         memset(colormap, 0, sizeof(ColorMapping));
150
151         init_colorband(&colormap->coba, true);
152
153         colormap->bright = 1.0;
154         colormap->contrast = 1.0;
155         colormap->saturation = 1.0;
156
157         colormap->blend_color[0] = 0.8f;
158         colormap->blend_color[1] = 0.8f;
159         colormap->blend_color[2] = 0.8f;
160         colormap->blend_type = MA_RAMP_BLEND;
161         colormap->blend_factor = 0.0f;
162 }
163
164 /* ****************** COLORBAND ******************* */
165
166 void init_colorband(ColorBand *coba, bool rangetype)
167 {
168         int a;
169         
170         coba->data[0].pos = 0.0;
171         coba->data[1].pos = 1.0;
172         
173         if (rangetype == 0) {
174                 coba->data[0].r = 0.0;
175                 coba->data[0].g = 0.0;
176                 coba->data[0].b = 0.0;
177                 coba->data[0].a = 0.0;
178
179                 coba->data[1].r = 1.0;
180                 coba->data[1].g = 1.0;
181                 coba->data[1].b = 1.0;
182                 coba->data[1].a = 1.0;
183         }
184         else {
185                 coba->data[0].r = 0.0;
186                 coba->data[0].g = 0.0;
187                 coba->data[0].b = 0.0;
188                 coba->data[0].a = 1.0;
189
190                 coba->data[1].r = 1.0;
191                 coba->data[1].g = 1.0;
192                 coba->data[1].b = 1.0;
193                 coba->data[1].a = 1.0;
194         }
195
196         for (a = 2; a < MAXCOLORBAND; a++) {
197                 coba->data[a].r = 0.5;
198                 coba->data[a].g = 0.5;
199                 coba->data[a].b = 0.5;
200                 coba->data[a].a = 1.0;
201                 coba->data[a].pos = 0.5;
202         }
203         
204         coba->tot = 2;
205         
206 }
207
208 ColorBand *add_colorband(bool rangetype)
209 {
210         ColorBand *coba;
211         
212         coba = MEM_callocN(sizeof(ColorBand), "colorband");
213         init_colorband(coba, rangetype);
214         
215         return coba;
216 }
217
218 /* ------------------------------------------------------------------------- */
219
220 int do_colorband(const ColorBand *coba, float in, float out[4])
221 {
222         const CBData *cbd1, *cbd2, *cbd0, *cbd3;
223         float fac, mfac, t[4];
224         int a;
225         
226         if (coba == NULL || coba->tot == 0) return 0;
227         
228         cbd1 = coba->data;
229         if (coba->tot == 1) {
230                 out[0] = cbd1->r;
231                 out[1] = cbd1->g;
232                 out[2] = cbd1->b;
233                 out[3] = cbd1->a;
234         }
235         else {
236                 if (in <= cbd1->pos && coba->ipotype < 2) {
237                         out[0] = cbd1->r;
238                         out[1] = cbd1->g;
239                         out[2] = cbd1->b;
240                         out[3] = cbd1->a;
241                 }
242                 else {
243                         CBData left, right;
244                         
245                         /* we're looking for first pos > in */
246                         for (a = 0; a < coba->tot; a++, cbd1++) if (cbd1->pos > in) break;
247                                 
248                         if (a == coba->tot) {
249                                 cbd2 = cbd1 - 1;
250                                 right = *cbd2;
251                                 right.pos = 1.0f;
252                                 cbd1 = &right;
253                         }
254                         else if (a == 0) {
255                                 left = *cbd1;
256                                 left.pos = 0.0f;
257                                 cbd2 = &left;
258                         }
259                         else {
260                                 cbd2 = cbd1 - 1;
261                         }
262                         
263                         if (in >= cbd1->pos && coba->ipotype < 2) {
264                                 out[0] = cbd1->r;
265                                 out[1] = cbd1->g;
266                                 out[2] = cbd1->b;
267                                 out[3] = cbd1->a;
268                         }
269                         else {
270                 
271                                 if (cbd2->pos != cbd1->pos)
272                                         fac = (in - cbd1->pos) / (cbd2->pos - cbd1->pos);
273                                 else {
274                                         /* was setting to 0.0 in 2.56 & previous, but this
275                                          * is incorrect for the last element, see [#26732] */
276                                         fac = (a != coba->tot) ? 0.0f : 1.0f;
277                                 }
278                                 
279                                 if (coba->ipotype == 4) {
280                                         /* constant */
281                                         out[0] = cbd2->r;
282                                         out[1] = cbd2->g;
283                                         out[2] = cbd2->b;
284                                         out[3] = cbd2->a;
285                                         return 1;
286                                 }
287                                 
288                                 if (coba->ipotype >= 2) {
289                                         /* ipo from right to left: 3 2 1 0 */
290                                         
291                                         if (a >= coba->tot - 1) cbd0 = cbd1;
292                                         else cbd0 = cbd1 + 1;
293                                         if (a < 2) cbd3 = cbd2;
294                                         else cbd3 = cbd2 - 1;
295                                         
296                                         CLAMP(fac, 0.0f, 1.0f);
297                                         
298                                         if (coba->ipotype == 3)
299                                                 key_curve_position_weights(fac, t, KEY_CARDINAL);
300                                         else
301                                                 key_curve_position_weights(fac, t, KEY_BSPLINE);
302
303                                         out[0] = t[3] * cbd3->r + t[2] * cbd2->r + t[1] * cbd1->r + t[0] * cbd0->r;
304                                         out[1] = t[3] * cbd3->g + t[2] * cbd2->g + t[1] * cbd1->g + t[0] * cbd0->g;
305                                         out[2] = t[3] * cbd3->b + t[2] * cbd2->b + t[1] * cbd1->b + t[0] * cbd0->b;
306                                         out[3] = t[3] * cbd3->a + t[2] * cbd2->a + t[1] * cbd1->a + t[0] * cbd0->a;
307                                         CLAMP(out[0], 0.0f, 1.0f);
308                                         CLAMP(out[1], 0.0f, 1.0f);
309                                         CLAMP(out[2], 0.0f, 1.0f);
310                                         CLAMP(out[3], 0.0f, 1.0f);
311                                 }
312                                 else {
313                                 
314                                         if (coba->ipotype == 1) { /* EASE */
315                                                 mfac = fac * fac;
316                                                 fac = 3.0f * mfac - 2.0f * mfac * fac;
317                                         }
318                                         mfac = 1.0f - fac;
319                                         
320                                         out[0] = mfac * cbd1->r + fac * cbd2->r;
321                                         out[1] = mfac * cbd1->g + fac * cbd2->g;
322                                         out[2] = mfac * cbd1->b + fac * cbd2->b;
323                                         out[3] = mfac * cbd1->a + fac * cbd2->a;
324                                 }
325                         }
326                 }
327         }
328         return 1;   /* OK */
329 }
330
331 void colorband_table_RGBA(ColorBand *coba, float **array, int *size)
332 {
333         int a;
334         
335         *size = CM_TABLE + 1;
336         *array = MEM_callocN(sizeof(float) * (*size) * 4, "ColorBand");
337
338         for (a = 0; a < *size; a++)
339                 do_colorband(coba, (float)a / (float)CM_TABLE, &(*array)[a * 4]);
340 }
341
342 static int vergcband(const void *a1, const void *a2)
343 {
344         const CBData *x1 = a1, *x2 = a2;
345
346         if (x1->pos > x2->pos) return 1;
347         else if (x1->pos < x2->pos) return -1;
348         return 0;
349 }
350
351 void colorband_update_sort(ColorBand *coba)
352 {
353         int a;
354         
355         if (coba->tot < 2)
356                 return;
357         
358         for (a = 0; a < coba->tot; a++)
359                 coba->data[a].cur = a;
360
361         qsort(coba->data, coba->tot, sizeof(CBData), vergcband);
362
363         for (a = 0; a < coba->tot; a++) {
364                 if (coba->data[a].cur == coba->cur) {
365                         coba->cur = a;
366                         break;
367                 }
368         }
369 }
370
371 CBData *colorband_element_add(struct ColorBand *coba, float position)
372 {
373         if (coba->tot == MAXCOLORBAND) {
374                 return NULL;
375         }
376         else if (coba->tot > 0) {
377                 CBData *xnew;
378                 float col[4];
379
380                 do_colorband(coba, position, col);
381
382                 xnew = &coba->data[coba->tot];
383                 xnew->pos = position;
384
385                 xnew->r = col[0];
386                 xnew->g = col[1];
387                 xnew->b = col[2];
388                 xnew->a = col[3];
389         }
390
391         coba->tot++;
392         coba->cur = coba->tot - 1;
393
394         colorband_update_sort(coba);
395
396         return coba->data + coba->cur;
397 }
398
399 int colorband_element_remove(struct ColorBand *coba, int index)
400 {
401         int a;
402
403         if (coba->tot < 2)
404                 return 0;
405
406         if (index < 0 || index >= coba->tot)
407                 return 0;
408
409         for (a = index; a < coba->tot; a++) {
410                 coba->data[a] = coba->data[a + 1];
411         }
412         if (coba->cur) coba->cur--;
413         coba->tot--;
414         return 1;
415 }
416
417 /* ******************* TEX ************************ */
418
419 void BKE_texture_free(Tex *tex)
420 {
421         if (tex->coba) MEM_freeN(tex->coba);
422         if (tex->env) BKE_free_envmap(tex->env);
423         if (tex->pd) BKE_free_pointdensity(tex->pd);
424         if (tex->vd) BKE_free_voxeldata(tex->vd);
425         if (tex->ot) BKE_free_oceantex(tex->ot);
426         BKE_free_animdata((struct ID *)tex);
427         
428         BKE_previewimg_free(&tex->preview);
429         BKE_icon_delete((struct ID *)tex);
430         tex->id.icon_id = 0;
431         
432         if (tex->nodetree) {
433                 ntreeFreeTree(tex->nodetree);
434                 MEM_freeN(tex->nodetree);
435         }
436 }
437
438 /* ------------------------------------------------------------------------- */
439
440 void default_tex(Tex *tex)
441 {
442         tex->type = TEX_CLOUDS;
443         tex->stype = 0;
444         tex->flag = TEX_CHECKER_ODD;
445         tex->imaflag = TEX_INTERPOL | TEX_MIPMAP | TEX_USEALPHA;
446         tex->extend = TEX_REPEAT;
447         tex->cropxmin = tex->cropymin = 0.0;
448         tex->cropxmax = tex->cropymax = 1.0;
449         tex->texfilter = TXF_EWA;
450         tex->afmax = 8;
451         tex->xrepeat = tex->yrepeat = 1;
452         tex->fie_ima = 2;
453         tex->sfra = 1;
454         tex->frames = 0;
455         tex->offset = 0;
456         tex->noisesize = 0.25;
457         tex->noisedepth = 2;
458         tex->turbul = 5.0;
459         tex->nabla = 0.025;  // also in do_versions
460         tex->bright = 1.0;
461         tex->contrast = 1.0;
462         tex->saturation = 1.0;
463         tex->filtersize = 1.0;
464         tex->rfac = 1.0;
465         tex->gfac = 1.0;
466         tex->bfac = 1.0;
467         /* newnoise: init. */
468         tex->noisebasis = 0;
469         tex->noisebasis2 = 0;
470         /* musgrave */
471         tex->mg_H = 1.0;
472         tex->mg_lacunarity = 2.0;
473         tex->mg_octaves = 2.0;
474         tex->mg_offset = 1.0;
475         tex->mg_gain = 1.0;
476         tex->ns_outscale = 1.0;
477         /* distnoise */
478         tex->dist_amount = 1.0;
479         /* voronoi */
480         tex->vn_w1 = 1.0;
481         tex->vn_w2 = tex->vn_w3 = tex->vn_w4 = 0.0;
482         tex->vn_mexp = 2.5;
483         tex->vn_distm = 0;
484         tex->vn_coltype = 0;
485
486         if (tex->env) {
487                 tex->env->stype = ENV_ANIM;
488                 tex->env->clipsta = 0.1;
489                 tex->env->clipend = 100;
490                 tex->env->cuberes = 600;
491                 tex->env->depth = 0;
492         }
493
494         if (tex->pd) {
495                 tex->pd->radius = 0.3f;
496                 tex->pd->falloff_type = TEX_PD_FALLOFF_STD;
497         }
498         
499         if (tex->vd) {
500                 tex->vd->resol[0] = tex->vd->resol[1] = tex->vd->resol[2] = 0;
501                 tex->vd->interp_type = TEX_VD_LINEAR;
502                 tex->vd->file_format = TEX_VD_SMOKE;
503         }
504         
505         if (tex->ot) {
506                 tex->ot->output = TEX_OCN_DISPLACEMENT;
507                 tex->ot->object = NULL;
508         }
509         
510         tex->iuser.fie_ima = 2;
511         tex->iuser.ok = 1;
512         tex->iuser.frames = 100;
513         tex->iuser.sfra = 1;
514         
515         tex->preview = NULL;
516 }
517
518 void tex_set_type(Tex *tex, int type)
519 {
520         switch (type) {
521                         
522                 case TEX_VOXELDATA:
523                         if (tex->vd == NULL)
524                                 tex->vd = BKE_add_voxeldata();
525                         break;
526                 case TEX_POINTDENSITY:
527                         if (tex->pd == NULL)
528                                 tex->pd = BKE_add_pointdensity();
529                         break;
530                 case TEX_ENVMAP:
531                         if (tex->env == NULL)
532                                 tex->env = BKE_add_envmap();
533                         break;
534                 case TEX_OCEAN:
535                         if (tex->ot == NULL)
536                                 tex->ot = BKE_add_oceantex();
537                         break;
538         }
539         
540         tex->type = type;
541 }
542
543 /* ------------------------------------------------------------------------- */
544
545 Tex *add_texture(Main *bmain, const char *name)
546 {
547         Tex *tex;
548
549         tex = BKE_libblock_alloc(&bmain->tex, ID_TE, name);
550         
551         default_tex(tex);
552         
553         return tex;
554 }
555
556 /* ------------------------------------------------------------------------- */
557
558 void default_mtex(MTex *mtex)
559 {
560         mtex->texco = TEXCO_ORCO;
561         mtex->mapto = MAP_COL;
562         mtex->object = NULL;
563         mtex->projx = PROJ_X;
564         mtex->projy = PROJ_Y;
565         mtex->projz = PROJ_Z;
566         mtex->mapping = MTEX_FLAT;
567         mtex->ofs[0] = 0.0;
568         mtex->ofs[1] = 0.0;
569         mtex->ofs[2] = 0.0;
570         mtex->size[0] = 1.0;
571         mtex->size[1] = 1.0;
572         mtex->size[2] = 1.0;
573         mtex->tex = NULL;
574         mtex->texflag = MTEX_3TAP_BUMP | MTEX_BUMP_OBJECTSPACE | MTEX_MAPTO_BOUNDS;
575         mtex->colormodel = 0;
576         mtex->r = 1.0;
577         mtex->g = 0.0;
578         mtex->b = 1.0;
579         mtex->k = 1.0;
580         mtex->def_var = 1.0;
581         mtex->blendtype = MTEX_BLEND;
582         mtex->colfac = 1.0;
583         mtex->norfac = 1.0;
584         mtex->varfac = 1.0;
585         mtex->dispfac = 0.2;
586         mtex->colspecfac = 1.0f;
587         mtex->mirrfac = 1.0f;
588         mtex->alphafac = 1.0f;
589         mtex->difffac = 1.0f;
590         mtex->specfac = 1.0f;
591         mtex->emitfac = 1.0f;
592         mtex->hardfac = 1.0f;
593         mtex->raymirrfac = 1.0f;
594         mtex->translfac = 1.0f;
595         mtex->ambfac = 1.0f;
596         mtex->colemitfac = 1.0f;
597         mtex->colreflfac = 1.0f;
598         mtex->coltransfac = 1.0f;
599         mtex->densfac = 1.0f;
600         mtex->scatterfac = 1.0f;
601         mtex->reflfac = 1.0f;
602         mtex->shadowfac = 1.0f;
603         mtex->zenupfac = 1.0f;
604         mtex->zendownfac = 1.0f;
605         mtex->blendfac = 1.0f;
606         mtex->timefac = 1.0f;
607         mtex->lengthfac = 1.0f;
608         mtex->clumpfac = 1.0f;
609         mtex->kinkfac = 1.0f;
610         mtex->roughfac = 1.0f;
611         mtex->padensfac = 1.0f;
612         mtex->lifefac = 1.0f;
613         mtex->sizefac = 1.0f;
614         mtex->ivelfac = 1.0f;
615         mtex->dampfac = 1.0f;
616         mtex->gravityfac = 1.0f;
617         mtex->fieldfac = 1.0f;
618         mtex->normapspace = MTEX_NSPACE_TANGENT;
619         mtex->brush_map_mode = MTEX_MAP_MODE_TILED;
620 }
621
622
623 /* ------------------------------------------------------------------------- */
624
625 MTex *add_mtex(void)
626 {
627         MTex *mtex;
628         
629         mtex = MEM_callocN(sizeof(MTex), "add_mtex");
630         
631         default_mtex(mtex);
632         
633         return mtex;
634 }
635
636 /* slot -1 for first free ID */
637 MTex *add_mtex_id(ID *id, int slot)
638 {
639         MTex **mtex_ar;
640         short act;
641
642         give_active_mtex(id, &mtex_ar, &act);
643
644         if (mtex_ar == NULL) {
645                 return NULL;
646         }
647         
648         if (slot == -1) {
649                 /* find first free */
650                 int i;
651                 for (i = 0; i < MAX_MTEX; i++) {
652                         if (!mtex_ar[i]) {
653                                 slot = i;
654                                 break;
655                         }
656                 }
657                 if (slot == -1) {
658                         return NULL;
659                 }
660         }
661         else {
662                 /* make sure slot is valid */
663                 if (slot < 0 || slot >= MAX_MTEX) {
664                         return NULL;
665                 }
666         }
667
668         if (mtex_ar[slot]) {
669                 id_us_min((ID *)mtex_ar[slot]->tex);
670                 MEM_freeN(mtex_ar[slot]);
671                 mtex_ar[slot] = NULL;
672         }
673
674         mtex_ar[slot] = add_mtex();
675
676         return mtex_ar[slot];
677 }
678
679 /* ------------------------------------------------------------------------- */
680
681 Tex *BKE_texture_copy(Tex *tex)
682 {
683         Tex *texn;
684         
685         texn = BKE_libblock_copy(&tex->id);
686         if (texn->type == TEX_IMAGE) id_us_plus((ID *)texn->ima);
687         else texn->ima = NULL;
688         
689         if (texn->coba) texn->coba = MEM_dupallocN(texn->coba);
690         if (texn->env) texn->env = BKE_copy_envmap(texn->env);
691         if (texn->pd) texn->pd = BKE_copy_pointdensity(texn->pd);
692         if (texn->vd) texn->vd = MEM_dupallocN(texn->vd);
693         if (texn->ot) texn->ot = BKE_copy_oceantex(texn->ot);
694         if (tex->preview) texn->preview = BKE_previewimg_copy(tex->preview);
695
696         if (tex->nodetree) {
697                 if (tex->nodetree->execdata) {
698                         ntreeTexEndExecTree(tex->nodetree->execdata);
699                 }
700                 texn->nodetree = ntreeCopyTree(tex->nodetree);
701         }
702         
703         return texn;
704 }
705
706 /* texture copy without adding to main dbase */
707 Tex *localize_texture(Tex *tex)
708 {
709         Tex *texn;
710         
711         texn = BKE_libblock_copy(&tex->id);
712         BLI_remlink(&G.main->tex, texn);
713         
714         /* image texture: BKE_texture_free also doesn't decrease */
715         
716         if (texn->coba) texn->coba = MEM_dupallocN(texn->coba);
717         if (texn->env) {
718                 texn->env = BKE_copy_envmap(texn->env);
719                 id_us_min(&texn->env->ima->id);
720         }
721         if (texn->pd) texn->pd = BKE_copy_pointdensity(texn->pd);
722         if (texn->vd) {
723                 texn->vd = MEM_dupallocN(texn->vd);
724                 if (texn->vd->dataset)
725                         texn->vd->dataset = MEM_dupallocN(texn->vd->dataset);
726         }
727         if (texn->ot) {
728                 texn->ot = BKE_copy_oceantex(tex->ot);
729         }
730         
731         texn->preview = NULL;
732         
733         if (tex->nodetree) {
734                 texn->nodetree = ntreeLocalize(tex->nodetree);
735         }
736         
737         return texn;
738 }
739
740
741 /* ------------------------------------------------------------------------- */
742
743 static void extern_local_texture(Tex *tex)
744 {
745         id_lib_extern((ID *)tex->ima);
746 }
747
748 void BKE_texture_make_local(Tex *tex)
749 {
750         Main *bmain = G.main;
751         Material *ma;
752         World *wrld;
753         Lamp *la;
754         Brush *br;
755         ParticleSettings *pa;
756         int a, is_local = FALSE, is_lib = FALSE;
757
758         /* - only lib users: do nothing
759          * - only local users: set flag
760          * - mixed: make copy
761          */
762         
763         if (tex->id.lib == NULL) return;
764
765         if (tex->id.us == 1) {
766                 id_clear_lib_data(bmain, &tex->id);
767                 extern_local_texture(tex);
768                 return;
769         }
770         
771         ma = bmain->mat.first;
772         while (ma) {
773                 for (a = 0; a < MAX_MTEX; a++) {
774                         if (ma->mtex[a] && ma->mtex[a]->tex == tex) {
775                                 if (ma->id.lib) is_lib = TRUE;
776                                 else is_local = TRUE;
777                         }
778                 }
779                 ma = ma->id.next;
780         }
781         la = bmain->lamp.first;
782         while (la) {
783                 for (a = 0; a < MAX_MTEX; a++) {
784                         if (la->mtex[a] && la->mtex[a]->tex == tex) {
785                                 if (la->id.lib) is_lib = TRUE;
786                                 else is_local = TRUE;
787                         }
788                 }
789                 la = la->id.next;
790         }
791         wrld = bmain->world.first;
792         while (wrld) {
793                 for (a = 0; a < MAX_MTEX; a++) {
794                         if (wrld->mtex[a] && wrld->mtex[a]->tex == tex) {
795                                 if (wrld->id.lib) is_lib = TRUE;
796                                 else is_local = TRUE;
797                         }
798                 }
799                 wrld = wrld->id.next;
800         }
801         br = bmain->brush.first;
802         while (br) {
803                 if (br->mtex.tex == tex) {
804                         if (br->id.lib) is_lib = TRUE;
805                         else is_local = TRUE;
806                 }
807                 if (br->mask_mtex.tex == tex) {
808                         if (br->id.lib) is_lib = TRUE;
809                         else is_local = TRUE;
810                 }
811                 br = br->id.next;
812         }
813         pa = bmain->particle.first;
814         while (pa) {
815                 for (a = 0; a < MAX_MTEX; a++) {
816                         if (pa->mtex[a] && pa->mtex[a]->tex == tex) {
817                                 if (pa->id.lib) is_lib = TRUE;
818                                 else is_local = TRUE;
819                         }
820                 }
821                 pa = pa->id.next;
822         }
823         
824         if (is_local && is_lib == FALSE) {
825                 id_clear_lib_data(bmain, &tex->id);
826                 extern_local_texture(tex);
827         }
828         else if (is_local && is_lib) {
829                 Tex *tex_new = BKE_texture_copy(tex);
830
831                 tex_new->id.us = 0;
832
833                 /* Remap paths of new ID using old library as base. */
834                 BKE_id_lib_local_paths(bmain, tex->id.lib, &tex_new->id);
835                 
836                 ma = bmain->mat.first;
837                 while (ma) {
838                         for (a = 0; a < MAX_MTEX; a++) {
839                                 if (ma->mtex[a] && ma->mtex[a]->tex == tex) {
840                                         if (ma->id.lib == NULL) {
841                                                 ma->mtex[a]->tex = tex_new;
842                                                 tex_new->id.us++;
843                                                 tex->id.us--;
844                                         }
845                                 }
846                         }
847                         ma = ma->id.next;
848                 }
849                 la = bmain->lamp.first;
850                 while (la) {
851                         for (a = 0; a < MAX_MTEX; a++) {
852                                 if (la->mtex[a] && la->mtex[a]->tex == tex) {
853                                         if (la->id.lib == NULL) {
854                                                 la->mtex[a]->tex = tex_new;
855                                                 tex_new->id.us++;
856                                                 tex->id.us--;
857                                         }
858                                 }
859                         }
860                         la = la->id.next;
861                 }
862                 wrld = bmain->world.first;
863                 while (wrld) {
864                         for (a = 0; a < MAX_MTEX; a++) {
865                                 if (wrld->mtex[a] && wrld->mtex[a]->tex == tex) {
866                                         if (wrld->id.lib == NULL) {
867                                                 wrld->mtex[a]->tex = tex_new;
868                                                 tex_new->id.us++;
869                                                 tex->id.us--;
870                                         }
871                                 }
872                         }
873                         wrld = wrld->id.next;
874                 }
875                 br = bmain->brush.first;
876                 while (br) {
877                         if (br->mtex.tex == tex) {
878                                 if (br->id.lib == NULL) {
879                                         br->mtex.tex = tex_new;
880                                         tex_new->id.us++;
881                                         tex->id.us--;
882                                 }
883                         }
884                         if (br->mask_mtex.tex == tex) {
885                                 if (br->id.lib == NULL) {
886                                         br->mask_mtex.tex = tex_new;
887                                         tex_new->id.us++;
888                                         tex->id.us--;
889                                 }
890                         }
891                         br = br->id.next;
892                 }
893                 pa = bmain->particle.first;
894                 while (pa) {
895                         for (a = 0; a < MAX_MTEX; a++) {
896                                 if (pa->mtex[a] && pa->mtex[a]->tex == tex) {
897                                         if (pa->id.lib == NULL) {
898                                                 pa->mtex[a]->tex = tex_new;
899                                                 tex_new->id.us++;
900                                                 tex->id.us--;
901                                         }
902                                 }
903                         }
904                         pa = pa->id.next;
905                 }
906         }
907 }
908
909 /* ------------------------------------------------------------------------- */
910 #if 0 /* UNUSED */
911 void autotexname(Tex *tex)
912 {
913         Main *bmain = G.main;
914         char texstr[20][15] = {"None", "Clouds", "Wood", "Marble", "Magic", "Blend",
915                                    "Stucci", "Noise", "Image", "EnvMap", "Musgrave",
916                                    "Voronoi", "DistNoise", "Point Density", "Voxel Data", "Ocean", "", "", ""};
917         Image *ima;
918         char di[FILE_MAXDIR], fi[FILE_MAXFILE];
919         
920         if (tex) {
921                 if (tex->use_nodes) {
922                         new_id(&bmain->tex, (ID *)tex, "Noddy");
923                 }
924                 else if (tex->type == TEX_IMAGE) {
925                         ima = tex->ima;
926                         if (ima) {
927                                 BLI_split_file_part(ima->name, fi, sizeof(fi));
928                                 strcpy(di, "I.");
929                                 strcat(di, fi);
930                                 new_id(&bmain->tex, (ID *)tex, di);
931                         }
932                         else {
933                                 new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
934                         }
935                 }
936                 else {
937                         new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
938                 }
939         }
940 }
941 #endif
942
943 /* ------------------------------------------------------------------------- */
944
945 Tex *give_current_object_texture(Object *ob)
946 {
947         Material *ma, *node_ma;
948         Tex *tex = NULL;
949         
950         if (ob == NULL) return NULL;
951         if (ob->totcol == 0 && !(ob->type == OB_LAMP)) return NULL;
952         
953         if (ob->type == OB_LAMP) {
954                 tex = give_current_lamp_texture(ob->data);
955         }
956         else {
957                 ma = give_current_material(ob, ob->actcol);
958
959                 if ((node_ma = give_node_material(ma)))
960                         ma = node_ma;
961
962                 tex = give_current_material_texture(ma);
963         }
964         
965         return tex;
966 }
967
968 Tex *give_current_lamp_texture(Lamp *la)
969 {
970         MTex *mtex = NULL;
971         Tex *tex = NULL;
972
973         if (la) {
974                 mtex = la->mtex[(int)(la->texact)];
975                 if (mtex) tex = mtex->tex;
976         }
977
978         return tex;
979 }
980
981 void set_current_lamp_texture(Lamp *la, Tex *newtex)
982 {
983         int act = la->texact;
984
985         if (la->mtex[act] && la->mtex[act]->tex)
986                 id_us_min(&la->mtex[act]->tex->id);
987
988         if (newtex) {
989                 if (!la->mtex[act]) {
990                         la->mtex[act] = add_mtex();
991                         la->mtex[act]->texco = TEXCO_GLOB;
992                 }
993                 
994                 la->mtex[act]->tex = newtex;
995                 id_us_plus(&newtex->id);
996         }
997         else if (la->mtex[act]) {
998                 MEM_freeN(la->mtex[act]);
999                 la->mtex[act] = NULL;
1000         }
1001 }
1002
1003 bNode *give_current_material_texture_node(Material *ma)
1004 {
1005         if (ma && ma->use_nodes && ma->nodetree)
1006                 return nodeGetActiveID(ma->nodetree, ID_TE);
1007         
1008         return NULL;
1009 }
1010
1011 Tex *give_current_material_texture(Material *ma)
1012 {
1013         MTex *mtex = NULL;
1014         Tex *tex = NULL;
1015         bNode *node;
1016         
1017         if (ma && ma->use_nodes && ma->nodetree) {
1018                 /* first check texture, then material, this works together
1019                  * with a hack that clears the active ID flag for textures on
1020                  * making a material node active */
1021                 node = nodeGetActiveID(ma->nodetree, ID_TE);
1022
1023                 if (node) {
1024                         tex = (Tex *)node->id;
1025                         ma = NULL;
1026                 }
1027         }
1028
1029         if (ma) {
1030                 mtex = ma->mtex[(int)(ma->texact)];
1031                 if (mtex) tex = mtex->tex;
1032         }
1033         
1034         return tex;
1035 }
1036
1037 int give_active_mtex(ID *id, MTex ***mtex_ar, short *act)
1038 {
1039         switch (GS(id->name)) {
1040                 case ID_MA:
1041                         *mtex_ar =       ((Material *)id)->mtex;
1042                         if (act) *act =  (((Material *)id)->texact);
1043                         break;
1044                 case ID_WO:
1045                         *mtex_ar =       ((World *)id)->mtex;
1046                         if (act) *act =  (((World *)id)->texact);
1047                         break;
1048                 case ID_LA:
1049                         *mtex_ar =       ((Lamp *)id)->mtex;
1050                         if (act) *act =  (((Lamp *)id)->texact);
1051                         break;
1052                 case ID_PA:
1053                         *mtex_ar =       ((ParticleSettings *)id)->mtex;
1054                         if (act) *act =  (((ParticleSettings *)id)->texact);
1055                         break;
1056                 default:
1057                         *mtex_ar = NULL;
1058                         if (act) *act =  0;
1059                         return FALSE;
1060         }
1061
1062         return TRUE;
1063 }
1064
1065 void set_active_mtex(ID *id, short act)
1066 {
1067         if (act < 0) act = 0;
1068         else if (act >= MAX_MTEX) act = MAX_MTEX - 1;
1069
1070         switch (GS(id->name)) {
1071                 case ID_MA:
1072                         ((Material *)id)->texact = act;
1073                         break;
1074                 case ID_WO:
1075                         ((World *)id)->texact = act;
1076                         break;
1077                 case ID_LA:
1078                         ((Lamp *)id)->texact = act;
1079                         break;
1080                 case ID_PA:
1081                         ((ParticleSettings *)id)->texact = act;
1082                         break;
1083         }
1084 }
1085
1086 void set_current_material_texture(Material *ma, Tex *newtex)
1087 {
1088         Tex *tex = NULL;
1089         bNode *node;
1090
1091         if ((ma->use_nodes && ma->nodetree) &&
1092             (node = nodeGetActiveID(ma->nodetree, ID_TE)))
1093         {
1094                 tex = (Tex *)node->id;
1095                 id_us_min(&tex->id);
1096                 if (newtex) {
1097                         node->id = &newtex->id;
1098                         id_us_plus(&newtex->id);
1099                 }
1100                 else {
1101                         node->id = NULL;
1102                 }
1103         }
1104         else {
1105                 int act = (int)ma->texact;
1106
1107                 tex = (ma->mtex[act]) ? ma->mtex[act]->tex : NULL;
1108                 id_us_min(&tex->id);
1109
1110                 if (newtex) {
1111                         if (!ma->mtex[act])
1112                                 ma->mtex[act] = add_mtex();
1113                         
1114                         ma->mtex[act]->tex = newtex;
1115                         id_us_plus(&newtex->id);
1116                 }
1117                 else if (ma->mtex[act]) {
1118                         MEM_freeN(ma->mtex[act]);
1119                         ma->mtex[act] = NULL;
1120                 }
1121         }
1122 }
1123
1124 int has_current_material_texture(Material *ma)
1125 {
1126         bNode *node;
1127
1128         if (ma && ma->use_nodes && ma->nodetree) {
1129                 node = nodeGetActiveID(ma->nodetree, ID_TE);
1130
1131                 if (node)
1132                         return 1;
1133         }
1134
1135         return (ma != NULL);
1136 }
1137
1138 Tex *give_current_world_texture(World *world)
1139 {
1140         MTex *mtex = NULL;
1141         Tex *tex = NULL;
1142         
1143         if (!world) return NULL;
1144         
1145         mtex = world->mtex[(int)(world->texact)];
1146         if (mtex) tex = mtex->tex;
1147         
1148         return tex;
1149 }
1150
1151 void set_current_world_texture(World *wo, Tex *newtex)
1152 {
1153         int act = wo->texact;
1154
1155         if (wo->mtex[act] && wo->mtex[act]->tex)
1156                 id_us_min(&wo->mtex[act]->tex->id);
1157
1158         if (newtex) {
1159                 if (!wo->mtex[act]) {
1160                         wo->mtex[act] = add_mtex();
1161                         wo->mtex[act]->texco = TEXCO_VIEW;
1162                 }
1163                 
1164                 wo->mtex[act]->tex = newtex;
1165                 id_us_plus(&newtex->id);
1166         }
1167         else if (wo->mtex[act]) {
1168                 MEM_freeN(wo->mtex[act]);
1169                 wo->mtex[act] = NULL;
1170         }
1171 }
1172
1173 Tex *give_current_brush_texture(Brush *br)
1174 {
1175         return br->mtex.tex;
1176 }
1177
1178 void set_current_brush_texture(Brush *br, Tex *newtex)
1179 {
1180         if (br->mtex.tex)
1181                 id_us_min(&br->mtex.tex->id);
1182
1183         if (newtex) {
1184                 br->mtex.tex = newtex;
1185                 id_us_plus(&newtex->id);
1186         }
1187 }
1188
1189 Tex *give_current_particle_texture(ParticleSettings *part)
1190 {
1191         MTex *mtex = NULL;
1192         Tex *tex = NULL;
1193         
1194         if (!part) return NULL;
1195         
1196         mtex = part->mtex[(int)(part->texact)];
1197         if (mtex) tex = mtex->tex;
1198         
1199         return tex;
1200 }
1201
1202 void set_current_particle_texture(ParticleSettings *part, Tex *newtex)
1203 {
1204         int act = part->texact;
1205
1206         if (part->mtex[act] && part->mtex[act]->tex)
1207                 id_us_min(&part->mtex[act]->tex->id);
1208
1209         if (newtex) {
1210                 if (!part->mtex[act]) {
1211                         part->mtex[act] = add_mtex();
1212                         part->mtex[act]->texco = TEXCO_ORCO;
1213                         part->mtex[act]->blendtype = MTEX_MUL;
1214                 }
1215                 
1216                 part->mtex[act]->tex = newtex;
1217                 id_us_plus(&newtex->id);
1218         }
1219         else if (part->mtex[act]) {
1220                 MEM_freeN(part->mtex[act]);
1221                 part->mtex[act] = NULL;
1222         }
1223 }
1224
1225 /* ------------------------------------------------------------------------- */
1226
1227 EnvMap *BKE_add_envmap(void)
1228 {
1229         EnvMap *env;
1230         
1231         env = MEM_callocN(sizeof(EnvMap), "envmap");
1232         env->type = ENV_CUBE;
1233         env->stype = ENV_ANIM;
1234         env->clipsta = 0.1;
1235         env->clipend = 100.0;
1236         env->cuberes = 600;
1237         env->viewscale = 0.5;
1238         
1239         return env;
1240
1241
1242 /* ------------------------------------------------------------------------- */
1243
1244 EnvMap *BKE_copy_envmap(EnvMap *env)
1245 {
1246         EnvMap *envn;
1247         int a;
1248         
1249         envn = MEM_dupallocN(env);
1250         envn->ok = 0;
1251         for (a = 0; a < 6; a++) envn->cube[a] = NULL;
1252         if (envn->ima) id_us_plus((ID *)envn->ima);
1253         
1254         return envn;
1255 }
1256
1257 /* ------------------------------------------------------------------------- */
1258
1259 void BKE_free_envmapdata(EnvMap *env)
1260 {
1261         unsigned int part;
1262         
1263         for (part = 0; part < 6; part++) {
1264                 if (env->cube[part])
1265                         IMB_freeImBuf(env->cube[part]);
1266                 env->cube[part] = NULL;
1267         }
1268         env->ok = 0;
1269 }
1270
1271 /* ------------------------------------------------------------------------- */
1272
1273 void BKE_free_envmap(EnvMap *env)
1274 {
1275         
1276         BKE_free_envmapdata(env);
1277         MEM_freeN(env);
1278         
1279 }
1280
1281 /* ------------------------------------------------------------------------- */
1282
1283 PointDensity *BKE_add_pointdensity(void)
1284 {
1285         PointDensity *pd;
1286         
1287         pd = MEM_callocN(sizeof(PointDensity), "pointdensity");
1288         pd->flag = 0;
1289         pd->radius = 0.3f;
1290         pd->falloff_type = TEX_PD_FALLOFF_STD;
1291         pd->falloff_softness = 2.0;
1292         pd->source = TEX_PD_PSYS;
1293         pd->point_tree = NULL;
1294         pd->point_data = NULL;
1295         pd->noise_size = 0.5f;
1296         pd->noise_depth = 1;
1297         pd->noise_fac = 1.0f;
1298         pd->noise_influence = TEX_PD_NOISE_STATIC;
1299         pd->coba = add_colorband(true);
1300         pd->speed_scale = 1.0f;
1301         pd->totpoints = 0;
1302         pd->object = NULL;
1303         pd->psys = 0;
1304         pd->psys_cache_space = TEX_PD_WORLDSPACE;
1305         pd->falloff_curve = curvemapping_add(1, 0, 0, 1, 1);
1306
1307         pd->falloff_curve->preset = CURVE_PRESET_LINE;
1308         pd->falloff_curve->cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
1309         curvemap_reset(pd->falloff_curve->cm, &pd->falloff_curve->clipr, pd->falloff_curve->preset, CURVEMAP_SLOPE_POSITIVE);
1310         curvemapping_changed(pd->falloff_curve, FALSE);
1311
1312         return pd;
1313
1314
1315 PointDensity *BKE_copy_pointdensity(PointDensity *pd)
1316 {
1317         PointDensity *pdn;
1318
1319         pdn = MEM_dupallocN(pd);
1320         pdn->point_tree = NULL;
1321         pdn->point_data = NULL;
1322         if (pdn->coba) pdn->coba = MEM_dupallocN(pdn->coba);
1323         pdn->falloff_curve = curvemapping_copy(pdn->falloff_curve); /* can be NULL */
1324         return pdn;
1325 }
1326
1327 void BKE_free_pointdensitydata(PointDensity *pd)
1328 {
1329         if (pd->point_tree) {
1330                 BLI_bvhtree_free(pd->point_tree);
1331                 pd->point_tree = NULL;
1332         }
1333         if (pd->point_data) {
1334                 MEM_freeN(pd->point_data);
1335                 pd->point_data = NULL;
1336         }
1337         if (pd->coba) {
1338                 MEM_freeN(pd->coba);
1339                 pd->coba = NULL;
1340         }
1341
1342         curvemapping_free(pd->falloff_curve); /* can be NULL */
1343 }
1344
1345 void BKE_free_pointdensity(PointDensity *pd)
1346 {
1347         BKE_free_pointdensitydata(pd);
1348         MEM_freeN(pd);
1349 }
1350
1351 /* ------------------------------------------------------------------------- */
1352
1353 void BKE_free_voxeldatadata(VoxelData *vd)
1354 {
1355         if (vd->dataset) {
1356                 MEM_freeN(vd->dataset);
1357                 vd->dataset = NULL;
1358         }
1359
1360 }
1361  
1362 void BKE_free_voxeldata(VoxelData *vd)
1363 {
1364         BKE_free_voxeldatadata(vd);
1365         MEM_freeN(vd);
1366 }
1367  
1368 VoxelData *BKE_add_voxeldata(void)
1369 {
1370         VoxelData *vd;
1371
1372         vd = MEM_callocN(sizeof(VoxelData), "voxeldata");
1373         vd->dataset = NULL;
1374         vd->resol[0] = vd->resol[1] = vd->resol[2] = 1;
1375         vd->interp_type = TEX_VD_LINEAR;
1376         vd->file_format = TEX_VD_SMOKE;
1377         vd->int_multiplier = 1.0;
1378         vd->extend = TEX_CLIP;
1379         vd->object = NULL;
1380         vd->cachedframe = -1;
1381         vd->ok = 0;
1382         
1383         return vd;
1384 }
1385  
1386 VoxelData *BKE_copy_voxeldata(VoxelData *vd)
1387 {
1388         VoxelData *vdn;
1389
1390         vdn = MEM_dupallocN(vd);
1391         vdn->dataset = NULL;
1392
1393         return vdn;
1394 }
1395
1396 /* ------------------------------------------------------------------------- */
1397
1398 OceanTex *BKE_add_oceantex(void)
1399 {
1400         OceanTex *ot;
1401         
1402         ot = MEM_callocN(sizeof(struct OceanTex), "ocean texture");
1403         ot->output = TEX_OCN_DISPLACEMENT;
1404         ot->object = NULL;
1405         
1406         return ot;
1407 }
1408
1409 OceanTex *BKE_copy_oceantex(struct OceanTex *ot)
1410 {
1411         OceanTex *otn = MEM_dupallocN(ot);
1412         
1413         return otn;
1414 }
1415
1416 void BKE_free_oceantex(struct OceanTex *ot)
1417 {
1418         MEM_freeN(ot);
1419 }
1420
1421
1422 /* ------------------------------------------------------------------------- */
1423 bool BKE_texture_dependsOnTime(const struct Tex *texture)
1424 {
1425         if (texture->ima &&
1426             ELEM(texture->ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE))
1427         {
1428                 return 1;
1429         }
1430         else if (texture->adt) {
1431                 /* assume anything in adt means the texture is animated */
1432                 return 1;
1433         }
1434         else if (texture->type == TEX_NOISE) {
1435                 /* noise always varies with time */
1436                 return 1;
1437         }
1438         return 0;
1439 }
1440
1441 /* ------------------------------------------------------------------------- */