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