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