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