bugfix #26267
[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) texn->vd= MEM_dupallocN(texn->vd);
794         
795         texn->preview = NULL;
796         
797         if(tex->nodetree) {
798                 texn->nodetree= ntreeLocalize(tex->nodetree);
799         }
800         
801         return texn;
802 }
803
804
805 /* ------------------------------------------------------------------------- */
806
807 void make_local_texture(Tex *tex)
808 {
809         Main *bmain= G.main;
810         Tex *texn;
811         Material *ma;
812         World *wrld;
813         Lamp *la;
814         Brush *br;
815         ParticleSettings *pa;
816         int a, local=0, lib=0;
817
818         /* - only lib users: do nothing
819                 * - only local users: set flag
820                 * - mixed: make copy
821                 */
822         
823         if(tex->id.lib==NULL) return;
824
825         /* special case: ima always local immediately */
826         if(tex->ima) {
827                 tex->ima->id.lib= NULL;
828                 tex->ima->id.flag= LIB_LOCAL;
829                 new_id(NULL, (ID *)tex->ima, NULL);
830         }
831
832         if(tex->id.us==1) {
833                 tex->id.lib= NULL;
834                 tex->id.flag= LIB_LOCAL;
835                 new_id(NULL, (ID *)tex, NULL);
836
837                 return;
838         }
839         
840         ma= bmain->mat.first;
841         while(ma) {
842                 for(a=0; a<MAX_MTEX; a++) {
843                         if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
844                                 if(ma->id.lib) lib= 1;
845                                 else local= 1;
846                         }
847                 }
848                 ma= ma->id.next;
849         }
850         la= bmain->lamp.first;
851         while(la) {
852                 for(a=0; a<MAX_MTEX; a++) {
853                         if(la->mtex[a] && la->mtex[a]->tex==tex) {
854                                 if(la->id.lib) lib= 1;
855                                 else local= 1;
856                         }
857                 }
858                 la= la->id.next;
859         }
860         wrld= bmain->world.first;
861         while(wrld) {
862                 for(a=0; a<MAX_MTEX; a++) {
863                         if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
864                                 if(wrld->id.lib) lib= 1;
865                                 else local= 1;
866                         }
867                 }
868                 wrld= wrld->id.next;
869         }
870         br= bmain->brush.first;
871         while(br) {
872                 if(br->mtex.tex==tex) {
873                         if(br->id.lib) lib= 1;
874                         else local= 1;
875                 }
876                 br= br->id.next;
877         }
878         pa= bmain->particle.first;
879         while(pa) {
880                 for(a=0; a<MAX_MTEX; a++) {
881                         if(pa->mtex[a] && pa->mtex[a]->tex==tex) {
882                                 if(pa->id.lib) lib= 1;
883                                 else local= 1;
884                         }
885                 }
886                 pa= pa->id.next;
887         }
888         
889         if(local && lib==0) {
890                 tex->id.lib= NULL;
891                 tex->id.flag= LIB_LOCAL;
892                 new_id(NULL, (ID *)tex, NULL);
893         }
894         else if(local && lib) {
895                 texn= copy_texture(tex);
896                 texn->id.us= 0;
897                 
898                 ma= bmain->mat.first;
899                 while(ma) {
900                         for(a=0; a<MAX_MTEX; a++) {
901                                 if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
902                                         if(ma->id.lib==NULL) {
903                                                 ma->mtex[a]->tex= texn;
904                                                 texn->id.us++;
905                                                 tex->id.us--;
906                                         }
907                                 }
908                         }
909                         ma= ma->id.next;
910                 }
911                 la= bmain->lamp.first;
912                 while(la) {
913                         for(a=0; a<MAX_MTEX; a++) {
914                                 if(la->mtex[a] && la->mtex[a]->tex==tex) {
915                                         if(la->id.lib==NULL) {
916                                                 la->mtex[a]->tex= texn;
917                                                 texn->id.us++;
918                                                 tex->id.us--;
919                                         }
920                                 }
921                         }
922                         la= la->id.next;
923                 }
924                 wrld= bmain->world.first;
925                 while(wrld) {
926                         for(a=0; a<MAX_MTEX; a++) {
927                                 if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
928                                         if(wrld->id.lib==NULL) {
929                                                 wrld->mtex[a]->tex= texn;
930                                                 texn->id.us++;
931                                                 tex->id.us--;
932                                         }
933                                 }
934                         }
935                         wrld= wrld->id.next;
936                 }
937                 br= bmain->brush.first;
938                 while(br) {
939                         if(br->mtex.tex==tex) {
940                                 if(br->id.lib==NULL) {
941                                         br->mtex.tex= texn;
942                                         texn->id.us++;
943                                         tex->id.us--;
944                                 }
945                         }
946                         br= br->id.next;
947                 }
948                 pa= bmain->particle.first;
949                 while(pa) {
950                         for(a=0; a<MAX_MTEX; a++) {
951                                 if(pa->mtex[a] && pa->mtex[a]->tex==tex) {
952                                         if(pa->id.lib==NULL) {
953                                                 pa->mtex[a]->tex= texn;
954                                                 texn->id.us++;
955                                                 tex->id.us--;
956                                         }
957                                 }
958                         }
959                         pa= pa->id.next;
960                 }
961         }
962 }
963
964 /* ------------------------------------------------------------------------- */
965
966 void autotexname(Tex *tex)
967 {
968         Main *bmain= G.main;
969         char texstr[20][15]= {"None"  , "Clouds" , "Wood", "Marble", "Magic"  , "Blend",
970                 "Stucci", "Noise"  , "Image", "Plugin", "EnvMap" , "Musgrave",
971                 "Voronoi", "DistNoise", "Point Density", "Voxel Data", "", "", "", ""};
972         Image *ima;
973         char di[FILE_MAXDIR], fi[FILE_MAXFILE];
974         
975         if(tex) {
976                 if(tex->use_nodes) {
977                         new_id(&bmain->tex, (ID *)tex, "Noddy");
978                 }
979                 else
980                 if(tex->type==TEX_IMAGE) {
981                         ima= tex->ima;
982                         if(ima) {
983                                 strcpy(di, ima->name);
984                                 BLI_splitdirstring(di, fi);
985                                 strcpy(di, "I.");
986                                 strcat(di, fi);
987                                 new_id(&bmain->tex, (ID *)tex, di);
988                         }
989                         else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
990                 }
991                 else if(tex->type==TEX_PLUGIN && tex->plugin) new_id(&bmain->tex, (ID *)tex, tex->plugin->pname);
992                 else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
993         }
994 }
995
996 /* ------------------------------------------------------------------------- */
997
998 Tex *give_current_object_texture(Object *ob)
999 {
1000         Material *ma;
1001         Tex *tex= NULL;
1002         
1003         if(ob==NULL) return NULL;
1004         if(ob->totcol==0 && !(ob->type==OB_LAMP)) return NULL;
1005         
1006         if(ob->type==OB_LAMP) {
1007                 tex= give_current_lamp_texture(ob->data);
1008         } else {
1009                 ma= give_current_material(ob, ob->actcol);
1010                 tex= give_current_material_texture(ma);
1011         }
1012         
1013         return tex;
1014 }
1015
1016 Tex *give_current_lamp_texture(Lamp *la)
1017 {
1018         MTex *mtex= NULL;
1019         Tex *tex= NULL;
1020
1021         if(la) {
1022                 mtex= la->mtex[(int)(la->texact)];
1023                 if(mtex) tex= mtex->tex;
1024         }
1025
1026         return tex;
1027 }
1028
1029 void set_current_lamp_texture(Lamp *la, Tex *newtex)
1030 {
1031         int act= la->texact;
1032
1033         if(la->mtex[act] && la->mtex[act]->tex)
1034                 id_us_min(&la->mtex[act]->tex->id);
1035
1036         if(newtex) {
1037                 if(!la->mtex[act]) {
1038                         la->mtex[act]= add_mtex();
1039                         la->mtex[act]->texco= TEXCO_GLOB;
1040                 }
1041                 
1042                 la->mtex[act]->tex= newtex;
1043                 id_us_plus(&newtex->id);
1044         }
1045         else if(la->mtex[act]) {
1046                 MEM_freeN(la->mtex[act]);
1047                 la->mtex[act]= NULL;
1048         }
1049 }
1050
1051 bNode *give_current_material_texture_node(Material *ma)
1052 {
1053         if(ma && ma->use_nodes && ma->nodetree)
1054                 return nodeGetActiveID(ma->nodetree, ID_TE);
1055         
1056         return NULL;
1057 }
1058
1059 Tex *give_current_material_texture(Material *ma)
1060 {
1061         MTex *mtex= NULL;
1062         Tex *tex= NULL;
1063         bNode *node;
1064         
1065         if(ma && ma->use_nodes && ma->nodetree) {
1066                 /* first check texture, then material, this works together
1067                    with a hack that clears the active ID flag for textures on
1068                    making a material node active */
1069                 node= nodeGetActiveID(ma->nodetree, ID_TE);
1070
1071                 if(node) {
1072                         tex= (Tex *)node->id;
1073                         ma= NULL;
1074                 }
1075                 else {
1076                         node= nodeGetActiveID(ma->nodetree, ID_MA);
1077                         if(node) {
1078                                 ma= (Material*)node->id;
1079                                 if(ma) {
1080                                         mtex= ma->mtex[(int)(ma->texact)];
1081                                         if(mtex) tex= mtex->tex;
1082                                 }
1083                         }
1084                 }
1085                 return tex;
1086         }
1087
1088         if(ma) {
1089                 mtex= ma->mtex[(int)(ma->texact)];
1090                 if(mtex) tex= mtex->tex;
1091         }
1092         
1093         return tex;
1094 }
1095
1096 int give_active_mtex(ID *id, MTex ***mtex_ar, short *act)
1097 {
1098         switch(GS(id->name)) {
1099         case ID_MA:
1100                 *mtex_ar=               ((Material *)id)->mtex;
1101                 if(act) *act=   (((Material *)id)->texact);
1102                 break;
1103         case ID_WO:
1104                 *mtex_ar=               ((World *)id)->mtex;
1105                 if(act) *act=   (((World *)id)->texact);
1106                 break;
1107         case ID_LA:
1108                 *mtex_ar=               ((Lamp *)id)->mtex;
1109                 if(act) *act=   (((Lamp *)id)->texact);
1110                 break;
1111         case ID_PA:
1112                 *mtex_ar=               ((ParticleSettings *)id)->mtex;
1113                 if(act) *act=   (((ParticleSettings *)id)->texact);
1114                 break;
1115         default:
1116                 *mtex_ar = NULL;
1117                 if(act) *act=   0;
1118                 return FALSE;
1119         }
1120
1121         return TRUE;
1122 }
1123
1124 void set_active_mtex(ID *id, short act)
1125 {
1126         if(act<0)                               act= 0;
1127         else if(act>=MAX_MTEX)  act= MAX_MTEX-1;
1128
1129         switch(GS(id->name)) {
1130         case ID_MA:
1131                 ((Material *)id)->texact= act;
1132                 break;
1133         case ID_WO:
1134                 ((World *)id)->texact= act;
1135                 break;
1136         case ID_LA:
1137                 ((Lamp *)id)->texact= act;
1138                 break;
1139         case ID_PA:
1140                 ((ParticleSettings *)id)->texact= act;
1141                 break;
1142         }
1143 }
1144
1145 void set_current_material_texture(Material *ma, Tex *newtex)
1146 {
1147         Tex *tex= NULL;
1148         bNode *node;
1149         
1150         if(ma && ma->use_nodes && ma->nodetree) {
1151                 node= nodeGetActiveID(ma->nodetree, ID_TE);
1152
1153                 if(node) {
1154                         tex= (Tex *)node->id;
1155                         id_us_min(&tex->id);
1156                         node->id= &newtex->id;
1157                         id_us_plus(&newtex->id);
1158                         ma= NULL;
1159                 }
1160                 else {
1161                         node= nodeGetActiveID(ma->nodetree, ID_MA);
1162                         if(node)
1163                                 ma= (Material*)node->id;
1164                 }
1165         }
1166         if(ma) {
1167                 int act= (int)ma->texact;
1168
1169                 tex= (ma->mtex[act])? ma->mtex[act]->tex: NULL;
1170                 id_us_min(&tex->id);
1171
1172                 if(newtex) {
1173                         if(!ma->mtex[act])
1174                                 ma->mtex[act]= add_mtex();
1175                         
1176                         ma->mtex[act]->tex= newtex;
1177                         id_us_plus(&newtex->id);
1178                 }
1179                 else if(ma->mtex[act]) {
1180                         MEM_freeN(ma->mtex[act]);
1181                         ma->mtex[act]= NULL;
1182                 }
1183         }
1184 }
1185
1186 Tex *give_current_world_texture(World *world)
1187 {
1188         MTex *mtex= NULL;
1189         Tex *tex= NULL;
1190         
1191         if(!world) return NULL;
1192         
1193         mtex= world->mtex[(int)(world->texact)];
1194         if(mtex) tex= mtex->tex;
1195         
1196         return tex;
1197 }
1198
1199 void set_current_world_texture(World *wo, Tex *newtex)
1200 {
1201         int act= wo->texact;
1202
1203         if(wo->mtex[act] && wo->mtex[act]->tex)
1204                 id_us_min(&wo->mtex[act]->tex->id);
1205
1206         if(newtex) {
1207                 if(!wo->mtex[act]) {
1208                         wo->mtex[act]= add_mtex();
1209                         wo->mtex[act]->texco= TEXCO_VIEW;
1210                 }
1211                 
1212                 wo->mtex[act]->tex= newtex;
1213                 id_us_plus(&newtex->id);
1214         }
1215         else if(wo->mtex[act]) {
1216                 MEM_freeN(wo->mtex[act]);
1217                 wo->mtex[act]= NULL;
1218         }
1219 }
1220
1221 Tex *give_current_brush_texture(Brush *br)
1222 {
1223         return br->mtex.tex;
1224 }
1225
1226 void set_current_brush_texture(Brush *br, Tex *newtex)
1227 {
1228         if(br->mtex.tex)
1229                 id_us_min(&br->mtex.tex->id);
1230
1231         if(newtex) {
1232                 br->mtex.tex= newtex;
1233                 id_us_plus(&newtex->id);
1234         }
1235 }
1236
1237 Tex *give_current_particle_texture(ParticleSettings *part)
1238 {
1239         MTex *mtex= NULL;
1240         Tex *tex= NULL;
1241         
1242         if(!part) return NULL;
1243         
1244         mtex= part->mtex[(int)(part->texact)];
1245         if(mtex) tex= mtex->tex;
1246         
1247         return tex;
1248 }
1249
1250 void set_current_particle_texture(ParticleSettings *part, Tex *newtex)
1251 {
1252         int act= part->texact;
1253
1254         if(part->mtex[act] && part->mtex[act]->tex)
1255                 id_us_min(&part->mtex[act]->tex->id);
1256
1257         if(newtex) {
1258                 if(!part->mtex[act]) {
1259                         part->mtex[act]= add_mtex();
1260                         part->mtex[act]->texco= TEXCO_ORCO;
1261                         part->mtex[act]->blendtype= MTEX_MUL;
1262                 }
1263                 
1264                 part->mtex[act]->tex= newtex;
1265                 id_us_plus(&newtex->id);
1266         }
1267         else if(part->mtex[act]) {
1268                 MEM_freeN(part->mtex[act]);
1269                 part->mtex[act]= NULL;
1270         }
1271 }
1272
1273 /* ------------------------------------------------------------------------- */
1274
1275 EnvMap *BKE_add_envmap(void)
1276 {
1277         EnvMap *env;
1278         
1279         env= MEM_callocN(sizeof(EnvMap), "envmap");
1280         env->type= ENV_CUBE;
1281         env->stype= ENV_ANIM;
1282         env->clipsta= 0.1;
1283         env->clipend= 100.0;
1284         env->cuberes= 600;
1285         env->viewscale = 0.5;
1286         
1287         return env;
1288
1289
1290 /* ------------------------------------------------------------------------- */
1291
1292 EnvMap *BKE_copy_envmap(EnvMap *env)
1293 {
1294         EnvMap *envn;
1295         int a;
1296         
1297         envn= MEM_dupallocN(env);
1298         envn->ok= 0;
1299         for(a=0; a<6; a++) envn->cube[a]= NULL;
1300         if(envn->ima) id_us_plus((ID *)envn->ima);
1301         
1302         return envn;
1303 }
1304
1305 /* ------------------------------------------------------------------------- */
1306
1307 void BKE_free_envmapdata(EnvMap *env)
1308 {
1309         unsigned int part;
1310         
1311         for(part=0; part<6; part++) {
1312                 if(env->cube[part])
1313                         IMB_freeImBuf(env->cube[part]);
1314                 env->cube[part]= NULL;
1315         }
1316         env->ok= 0;
1317 }
1318
1319 /* ------------------------------------------------------------------------- */
1320
1321 void BKE_free_envmap(EnvMap *env)
1322 {
1323         
1324         BKE_free_envmapdata(env);
1325         MEM_freeN(env);
1326         
1327 }
1328
1329 /* ------------------------------------------------------------------------- */
1330
1331 PointDensity *BKE_add_pointdensity(void)
1332 {
1333         PointDensity *pd;
1334         
1335         pd= MEM_callocN(sizeof(PointDensity), "pointdensity");
1336         pd->flag = 0;
1337         pd->radius = 0.3f;
1338         pd->falloff_type = TEX_PD_FALLOFF_STD;
1339         pd->falloff_softness = 2.0;
1340         pd->source = TEX_PD_PSYS;
1341         pd->point_tree = NULL;
1342         pd->point_data = NULL;
1343         pd->noise_size = 0.5f;
1344         pd->noise_depth = 1;
1345         pd->noise_fac = 1.0f;
1346         pd->noise_influence = TEX_PD_NOISE_STATIC;
1347         pd->coba = add_colorband(1);
1348         pd->speed_scale = 1.0f;
1349         pd->totpoints = 0;
1350         pd->object = NULL;
1351         pd->psys = 0;
1352         pd->psys_cache_space= TEX_PD_WORLDSPACE;
1353         return pd;
1354
1355
1356 PointDensity *BKE_copy_pointdensity(PointDensity *pd)
1357 {
1358         PointDensity *pdn;
1359
1360         pdn= MEM_dupallocN(pd);
1361         pdn->point_tree = NULL;
1362         pdn->point_data = NULL;
1363         if(pdn->coba) pdn->coba= MEM_dupallocN(pdn->coba);
1364         
1365         return pdn;
1366 }
1367
1368 void BKE_free_pointdensitydata(PointDensity *pd)
1369 {
1370         if (pd->point_tree) {
1371                 BLI_bvhtree_free(pd->point_tree);
1372                 pd->point_tree = NULL;
1373         }
1374         if (pd->point_data) {
1375                 MEM_freeN(pd->point_data);
1376                 pd->point_data = NULL;
1377         }
1378         if(pd->coba) MEM_freeN(pd->coba);
1379 }
1380
1381 void BKE_free_pointdensity(PointDensity *pd)
1382 {
1383         BKE_free_pointdensitydata(pd);
1384         MEM_freeN(pd);
1385 }
1386
1387
1388 void BKE_free_voxeldatadata(struct VoxelData *vd)
1389 {
1390         if (vd->dataset) {
1391                 MEM_freeN(vd->dataset);
1392                 vd->dataset = NULL;
1393         }
1394
1395 }
1396  
1397 void BKE_free_voxeldata(struct VoxelData *vd)
1398 {
1399         BKE_free_voxeldatadata(vd);
1400         MEM_freeN(vd);
1401 }
1402  
1403 struct VoxelData *BKE_add_voxeldata(void)
1404 {
1405         VoxelData *vd;
1406
1407         vd= MEM_callocN(sizeof(struct VoxelData), "voxeldata");
1408         vd->dataset = NULL;
1409         vd->resol[0] = vd->resol[1] = vd->resol[2] = 1;
1410         vd->interp_type= TEX_VD_LINEAR;
1411         vd->file_format= TEX_VD_SMOKE;
1412         vd->int_multiplier = 1.0;
1413         vd->extend = TEX_CLIP;
1414         vd->object = NULL;
1415         vd->cachedframe = -1;
1416         vd->ok = 0;
1417         
1418         return vd;
1419  }
1420  
1421 struct VoxelData *BKE_copy_voxeldata(struct VoxelData *vd)
1422 {
1423         VoxelData *vdn;
1424
1425         vdn= MEM_dupallocN(vd); 
1426         vdn->dataset = NULL;
1427
1428         return vdn;
1429 }
1430
1431
1432 /* ------------------------------------------------------------------------- */
1433 int BKE_texture_dependsOnTime(const struct Tex *texture)
1434 {
1435         if(texture->plugin) {
1436                 // assume all plugins depend on time
1437                 return 1;
1438         } 
1439         else if(        texture->ima && 
1440                         ELEM(texture->ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
1441                 return 1;
1442         }
1443         else if(texture->adt) {
1444                 // assume anything in adt means the texture is animated
1445                 return 1;
1446         }
1447         return 0;
1448 }
1449
1450 /* ------------------------------------------------------------------------- */