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