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