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