svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r20855:20928
[blender.git] / source / blender / blenkernel / intern / texture.c
1 /* texture.c
2  *
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <math.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "PIL_dynlib.h"
40
41 #include "MTC_matrixops.h"
42
43 #include "BLI_blenlib.h"
44 #include "BLI_arithb.h"
45 #include "BLI_rand.h"
46
47 #include "DNA_texture_types.h"
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_image_types.h"
53 #include "DNA_world_types.h"
54 #include "DNA_brush_types.h"
55 #include "DNA_node_types.h"
56 #include "DNA_color_types.h"
57 #include "DNA_scene_types.h"
58
59 #include "IMB_imbuf_types.h"
60 #include "IMB_imbuf.h"
61
62 #include "BKE_plugin_types.h"
63
64 #include "BKE_utildefines.h"
65
66 #include "BKE_global.h"
67 #include "BKE_main.h"
68
69 #include "BKE_library.h"
70 #include "BKE_image.h"
71 #include "BKE_material.h"
72 #include "BKE_texture.h"
73 #include "BKE_key.h"
74 #include "BKE_icons.h"
75 #include "BKE_brush.h"
76 #include "BKE_node.h"
77 #include "BKE_animsys.h"
78
79
80 /* ------------------------------------------------------------------------- */
81
82 /* All support for plugin textures: */
83 int test_dlerr(const char *name, const char *symbol)
84 {
85         char *err;
86         
87         err= PIL_dynlib_get_error_as_string(NULL);
88         if(err) {
89                 printf("var1: %s, var2: %s, var3: %s\n", name, symbol, err);
90                 return 1;
91         }
92         
93         return 0;
94 }
95
96 /* ------------------------------------------------------------------------- */
97
98 void open_plugin_tex(PluginTex *pit)
99 {
100         int (*version)(void);
101         
102         /* init all the happy variables */
103         pit->doit= 0;
104         pit->pname= 0;
105         pit->stnames= 0;
106         pit->varstr= 0;
107         pit->result= 0;
108         pit->cfra= 0;
109         pit->version= 0;
110         pit->instance_init= 0;
111         
112         /* clear the error list */
113         PIL_dynlib_get_error_as_string(NULL);
114
115         /* no PIL_dynlib_close! multiple opened plugins... */
116         /* if(pit->handle) PIL_dynlib_close(pit->handle); */
117         /* pit->handle= 0; */
118
119         /* open the needed object */
120         pit->handle= PIL_dynlib_open(pit->name);
121         if(test_dlerr(pit->name, pit->name)) return;
122
123         if (pit->handle != 0) {
124                 /* find the address of the version function */
125                 version= (int (*)(void)) PIL_dynlib_find_symbol(pit->handle, "plugin_tex_getversion");
126                 if (test_dlerr(pit->name, "plugin_tex_getversion")) return;
127                 
128                 if (version != 0) {
129                         pit->version= version();
130                         if( pit->version >= 2 && pit->version <=6) {
131                                 int (*info_func)(PluginInfo *);
132                                 PluginInfo *info= (PluginInfo*) MEM_mallocN(sizeof(PluginInfo), "plugin_info"); 
133
134                                 info_func= (int (*)(PluginInfo *))PIL_dynlib_find_symbol(pit->handle, "plugin_getinfo");
135                                 if (!test_dlerr(pit->name, "plugin_getinfo")) {
136                                         info->instance_init = NULL;
137
138                                         info_func(info);
139
140                                         pit->doit= (int(*)(void)) info->tex_doit;
141                                         pit->callback= (void(*)(unsigned short)) info->callback;
142                                         pit->stypes= info->stypes;
143                                         pit->vars= info->nvars;
144                                         pit->pname= info->name;
145                                         pit->stnames= info->snames;
146                                         pit->varstr= info->varstr;
147                                         pit->result= info->result;
148                                         pit->cfra= info->cfra;
149                                         pit->instance_init = info->instance_init;
150                                         if (info->init) info->init();
151                                 }
152                                 MEM_freeN(info);
153                         } else {
154                                 printf ("Plugin returned unrecognized version number\n");
155                                 return;
156                         }
157                 }
158         }
159 }
160
161 /* ------------------------------------------------------------------------- */
162
163 /* very badlevel define to bypass linking with BIF_interface.h */
164 #define INT     96
165 #define FLO     128
166
167 PluginTex *add_plugin_tex(char *str)
168 {
169         PluginTex *pit;
170         VarStruct *varstr;
171         int a;
172         
173         pit= MEM_callocN(sizeof(PluginTex), "plugintex");
174         
175         strcpy(pit->name, str);
176         open_plugin_tex(pit);
177         
178         if(pit->doit==0) {
179                 if(pit->handle==0); //XXX error("no plugin: %s", str);
180                 else ; //XXX error("in plugin: %s", str);
181                 MEM_freeN(pit);
182                 return NULL;
183         }
184         
185         varstr= pit->varstr;
186         for(a=0; a<pit->vars; a++, varstr++) {
187                 if( (varstr->type & FLO)==FLO)
188                         pit->data[a]= varstr->def;
189                 else if( (varstr->type & INT)==INT)
190                         *((int *)(pit->data+a))= (int) varstr->def;
191         }
192
193         if (pit->instance_init)
194                 pit->instance_init((void *) pit->data);
195
196         return pit;
197 }
198
199 /* ------------------------------------------------------------------------- */
200
201 void free_plugin_tex(PluginTex *pit)
202 {
203         if(pit==0) return;
204                 
205         /* no PIL_dynlib_close: same plugin can be opened multiple times, 1 handle */
206         MEM_freeN(pit); 
207 }
208
209 /* ****************** Mapping ******************* */
210
211 TexMapping *add_mapping(void)
212 {
213         TexMapping *texmap= MEM_callocN(sizeof(TexMapping), "Tex map");
214         
215         texmap->size[0]= texmap->size[1]= texmap->size[2]= 1.0f;
216         texmap->max[0]= texmap->max[1]= texmap->max[2]= 1.0f;
217         Mat4One(texmap->mat);
218         
219         return texmap;
220 }
221
222 void init_mapping(TexMapping *texmap)
223 {
224         float eul[3], smat[3][3], rmat[3][3], mat[3][3];
225         
226         SizeToMat3(texmap->size, smat);
227         
228         eul[0]= (M_PI/180.0f)*texmap->rot[0];
229         eul[1]= (M_PI/180.0f)*texmap->rot[1];
230         eul[2]= (M_PI/180.0f)*texmap->rot[2];
231         EulToMat3(eul, rmat);
232         
233         Mat3MulMat3(mat, rmat, smat);
234         
235         Mat4CpyMat3(texmap->mat, mat);
236         VECCOPY(texmap->mat[3], texmap->loc);
237
238 }
239
240 /* ****************** COLORBAND ******************* */
241
242 void init_colorband(ColorBand *coba, int rangetype)
243 {
244         int a;
245         
246         coba->data[0].pos= 0.0;
247         coba->data[1].pos= 1.0;
248         
249         if(rangetype==0) {
250                 coba->data[0].r= 0.0;
251                 coba->data[0].g= 0.0;
252                 coba->data[0].b= 0.0;
253                 coba->data[0].a= 0.0;
254                 
255                 coba->data[1].r= 0.0;
256                 coba->data[1].g= 1.0;
257                 coba->data[1].b= 1.0;
258                 coba->data[1].a= 1.0;
259         }
260         else {
261                 coba->data[0].r= 0.0;
262                 coba->data[0].g= 0.0;
263                 coba->data[0].b= 0.0;
264                 coba->data[0].a= 1.0;
265                 
266                 coba->data[1].r= 1.0;
267                 coba->data[1].g= 1.0;
268                 coba->data[1].b= 1.0;
269                 coba->data[1].a= 1.0;
270         }
271         
272         for(a=2; a<MAXCOLORBAND; a++) {
273                 coba->data[a].r= 0.5;
274                 coba->data[a].g= 0.5;
275                 coba->data[a].b= 0.5;
276                 coba->data[a].a= 1.0;
277                 coba->data[a].pos= 0.5;
278         }
279         
280         coba->tot= 2;
281         
282 }
283
284 ColorBand *add_colorband(int rangetype)
285 {
286         ColorBand *coba;
287         
288         coba= MEM_callocN( sizeof(ColorBand), "colorband");
289         init_colorband(coba, rangetype);
290         
291         return coba;
292 }
293
294 /* ------------------------------------------------------------------------- */
295
296 int do_colorband(ColorBand *coba, float in, float out[4])
297 {
298         CBData *cbd1, *cbd2, *cbd0, *cbd3;
299         float fac, mfac, t[4];
300         int a;
301         
302         if(coba==NULL || coba->tot==0) return 0;
303         
304         cbd1= coba->data;
305         if(coba->tot==1) {
306                 out[0]= cbd1->r;
307                 out[1]= cbd1->g;
308                 out[2]= cbd1->b;
309                 out[3]= cbd1->a;
310         }
311         else {
312                 if(in <= cbd1->pos && coba->ipotype<2) {
313                         out[0]= cbd1->r;
314                         out[1]= cbd1->g;
315                         out[2]= cbd1->b;
316                         out[3]= cbd1->a;
317                 }
318                 else {
319                         CBData left, right;
320                         
321                         /* we're looking for first pos > in */
322                         for(a=0; a<coba->tot; a++, cbd1++) if(cbd1->pos > in) break;
323                                 
324                         if(a==coba->tot) {
325                                 cbd2= cbd1-1;
326                                 right= *cbd2;
327                                 right.pos= 1.0f;
328                                 cbd1= &right;
329                         }
330                         else if(a==0) {
331                                 left= *cbd1;
332                                 left.pos= 0.0f;
333                                 cbd2= &left;
334                         }
335                         else cbd2= cbd1-1;
336                         
337                         if(in >= cbd1->pos && coba->ipotype<2) {
338                                 out[0]= cbd1->r;
339                                 out[1]= cbd1->g;
340                                 out[2]= cbd1->b;
341                                 out[3]= cbd1->a;
342                         }
343                         else {
344                 
345                                 if(cbd2->pos!=cbd1->pos)
346                                         fac= (in-cbd1->pos)/(cbd2->pos-cbd1->pos);
347                                 else
348                                         fac= 0.0f;
349                                 
350                                 if (coba->ipotype==4) {
351                                         /* constant */
352                                         out[0]= cbd2->r;
353                                         out[1]= cbd2->g;
354                                         out[2]= cbd2->b;
355                                         out[3]= cbd2->a;
356                                         return 1;
357                                 }
358                                 
359                                 if(coba->ipotype>=2) {
360                                         /* ipo from right to left: 3 2 1 0 */
361                                         
362                                         if(a>=coba->tot-1) cbd0= cbd1;
363                                         else cbd0= cbd1+1;
364                                         if(a<2) cbd3= cbd2;
365                                         else cbd3= cbd2-1;
366                                         
367                                         CLAMP(fac, 0.0f, 1.0f);
368                                         
369                                         if(coba->ipotype==3)
370                                                 set_four_ipo(fac, t, KEY_CARDINAL);
371                                         else
372                                                 set_four_ipo(fac, t, KEY_BSPLINE);
373
374                                         out[0]= t[3]*cbd3->r +t[2]*cbd2->r +t[1]*cbd1->r +t[0]*cbd0->r;
375                                         out[1]= t[3]*cbd3->g +t[2]*cbd2->g +t[1]*cbd1->g +t[0]*cbd0->g;
376                                         out[2]= t[3]*cbd3->b +t[2]*cbd2->b +t[1]*cbd1->b +t[0]*cbd0->b;
377                                         out[3]= t[3]*cbd3->a +t[2]*cbd2->a +t[1]*cbd1->a +t[0]*cbd0->a;
378                                         CLAMP(out[0], 0.0, 1.0);
379                                         CLAMP(out[1], 0.0, 1.0);
380                                         CLAMP(out[2], 0.0, 1.0);
381                                         CLAMP(out[3], 0.0, 1.0);
382                                 }
383                                 else {
384                                 
385                                         if(coba->ipotype==1) {  /* EASE */
386                                                 mfac= fac*fac;
387                                                 fac= 3.0f*mfac-2.0f*mfac*fac;
388                                         }
389                                         mfac= 1.0f-fac;
390                                         
391                                         out[0]= mfac*cbd1->r + fac*cbd2->r;
392                                         out[1]= mfac*cbd1->g + fac*cbd2->g;
393                                         out[2]= mfac*cbd1->b + fac*cbd2->b;
394                                         out[3]= mfac*cbd1->a + fac*cbd2->a;
395                                 }
396                         }
397                 }
398         }
399         return 1;       /* OK */
400 }
401
402 void colorband_table_RGBA(ColorBand *coba, float **array, int *size)
403 {
404         int a;
405         
406         *size = CM_TABLE+1;
407         *array = MEM_callocN(sizeof(float)*(*size)*4, "ColorBand");
408
409         for(a=0; a<*size; a++)
410                 do_colorband(coba, (float)a/(float)CM_TABLE, &(*array)[a*4]);
411 }
412
413 /* ******************* TEX ************************ */
414
415 void free_texture(Tex *tex)
416 {
417         free_plugin_tex(tex->plugin);
418         if(tex->coba) MEM_freeN(tex->coba);
419         if(tex->env) BKE_free_envmap(tex->env);
420         BKE_previewimg_free(&tex->preview);
421         BKE_icon_delete((struct ID*)tex);
422         tex->id.icon_id = 0;
423         
424         if(tex->nodetree) {
425                 ntreeFreeTree(tex->nodetree);
426                 MEM_freeN(tex->nodetree);
427         }
428 }
429
430 /* ------------------------------------------------------------------------- */
431
432 void default_tex(Tex *tex)
433 {
434         PluginTex *pit;
435         VarStruct *varstr;
436         int a;
437
438         tex->stype= 0;
439         tex->flag= TEX_CHECKER_ODD;
440         tex->imaflag= TEX_INTERPOL+TEX_MIPMAP+TEX_USEALPHA;
441         tex->extend= TEX_REPEAT;
442         tex->cropxmin= tex->cropymin= 0.0;
443         tex->cropxmax= tex->cropymax= 1.0;
444         tex->xrepeat= tex->yrepeat= 1;
445         tex->fie_ima= 2;
446         tex->sfra= 1;
447         tex->frames= 0;
448         tex->offset= 0;
449         tex->noisesize= 0.25;
450         tex->noisedepth= 2;
451         tex->turbul= 5.0;
452         tex->nabla= 0.025;      // also in do_versions
453         tex->bright= 1.0;
454         tex->contrast= 1.0;
455         tex->filtersize= 1.0;
456         tex->rfac= 1.0;
457         tex->gfac= 1.0;
458         tex->bfac= 1.0;
459         /* newnoise: init. */
460         tex->noisebasis = 0;
461         tex->noisebasis2 = 0;
462         /* musgrave */
463         tex->mg_H = 1.0;
464         tex->mg_lacunarity = 2.0;
465         tex->mg_octaves = 2.0;
466         tex->mg_offset = 1.0;
467         tex->mg_gain = 1.0;
468         tex->ns_outscale = 1.0;
469         /* distnoise */
470         tex->dist_amount = 1.0;
471         /* voronoi */
472         tex->vn_w1 = 1.0;
473         tex->vn_w2 = tex->vn_w3 = tex->vn_w4 = 0.0;
474         tex->vn_mexp = 2.5;
475         tex->vn_distm = 0;
476         tex->vn_coltype = 0;
477
478         if (tex->env) {
479                 tex->env->stype=ENV_STATIC;
480                 tex->env->clipsta=0.1;
481                 tex->env->clipend=100;
482                 tex->env->cuberes=100;
483                 tex->env->depth=0;
484         }
485
486         pit = tex->plugin;
487         if (pit) {
488                 varstr= pit->varstr;
489                 if(varstr) {
490                         for(a=0; a<pit->vars; a++, varstr++) {
491                                 pit->data[a] = varstr->def;
492                         }
493                 }
494         }
495         
496         tex->iuser.fie_ima= 2;
497         tex->iuser.ok= 1;
498         tex->iuser.frames= 100;
499         
500         tex->preview = NULL;
501 }
502
503 /* ------------------------------------------------------------------------- */
504
505 Tex *add_texture(char *name)
506 {
507         Tex *tex;
508
509         tex= alloc_libblock(&G.main->tex, ID_TE, name);
510         
511         default_tex(tex);
512         
513         return tex;
514 }
515
516 /* ------------------------------------------------------------------------- */
517
518 void default_mtex(MTex *mtex)
519 {
520         mtex->texco= TEXCO_ORCO;
521         mtex->mapto= MAP_COL;
522         mtex->object= 0;
523         mtex->projx= PROJ_X;
524         mtex->projy= PROJ_Y;
525         mtex->projz= PROJ_Z;
526         mtex->mapping= MTEX_FLAT;
527         mtex->ofs[0]= 0.0;
528         mtex->ofs[1]= 0.0;
529         mtex->ofs[2]= 0.0;
530         mtex->size[0]= 1.0;
531         mtex->size[1]= 1.0;
532         mtex->size[2]= 1.0;
533         mtex->tex= 0;
534         mtex->texflag= 0;
535         mtex->colormodel= 0;
536         mtex->r= 1.0;
537         mtex->g= 0.0;
538         mtex->b= 1.0;
539         mtex->k= 1.0;
540         mtex->def_var= 1.0;
541         mtex->blendtype= MTEX_BLEND;
542         mtex->colfac= 1.0;
543         mtex->norfac= 0.5;
544         mtex->varfac= 1.0;
545         mtex->dispfac=0.2;
546         mtex->normapspace= MTEX_NSPACE_TANGENT;
547 }
548
549
550 /* ------------------------------------------------------------------------- */
551
552 MTex *add_mtex()
553 {
554         MTex *mtex;
555         
556         mtex= MEM_callocN(sizeof(MTex), "add_mtex");
557         
558         default_mtex(mtex);
559         
560         return mtex;
561 }
562
563 /* ------------------------------------------------------------------------- */
564
565 Tex *copy_texture(Tex *tex)
566 {
567         Tex *texn;
568         
569         texn= copy_libblock(tex);
570         if(texn->type==TEX_IMAGE) id_us_plus((ID *)texn->ima);
571         else texn->ima= 0;
572         
573 #if 0 // XXX old animation system
574         id_us_plus((ID *)texn->ipo);
575 #endif // XXX old animation system
576         
577         if(texn->plugin) {
578                 texn->plugin= MEM_dupallocN(texn->plugin);
579                 open_plugin_tex(texn->plugin);
580         }
581         
582         if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
583         if(texn->env) texn->env= BKE_copy_envmap(texn->env);
584         
585         if(tex->preview) texn->preview = BKE_previewimg_copy(tex->preview);
586
587         if(tex->nodetree) {
588                 ntreeEndExecTree(tex->nodetree);
589                 texn->nodetree= ntreeCopyTree(tex->nodetree, 0); /* 0 == full new tree */
590         }
591         
592         return texn;
593 }
594
595 /* ------------------------------------------------------------------------- */
596
597 void make_local_texture(Tex *tex)
598 {
599         Tex *texn;
600         Material *ma;
601         World *wrld;
602         Lamp *la;
603         Brush *br;
604         int a, local=0, lib=0;
605
606         /* - only lib users: do nothing
607             * - only local users: set flag
608             * - mixed: make copy
609             */
610         
611         if(tex->id.lib==0) return;
612
613         /* special case: ima always local immediately */
614         if(tex->ima) {
615                 tex->ima->id.lib= 0;
616                 tex->ima->id.flag= LIB_LOCAL;
617                 new_id(0, (ID *)tex->ima, 0);
618         }
619
620         if(tex->id.us==1) {
621                 tex->id.lib= 0;
622                 tex->id.flag= LIB_LOCAL;
623                 new_id(0, (ID *)tex, 0);
624
625                 return;
626         }
627         
628         ma= G.main->mat.first;
629         while(ma) {
630                 for(a=0; a<MAX_MTEX; a++) {
631                         if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
632                                 if(ma->id.lib) lib= 1;
633                                 else local= 1;
634                         }
635                 }
636                 ma= ma->id.next;
637         }
638         la= G.main->lamp.first;
639         while(la) {
640                 for(a=0; a<MAX_MTEX; a++) {
641                         if(la->mtex[a] && la->mtex[a]->tex==tex) {
642                                 if(la->id.lib) lib= 1;
643                                 else local= 1;
644                         }
645                 }
646                 la= la->id.next;
647         }
648         wrld= G.main->world.first;
649         while(wrld) {
650                 for(a=0; a<MAX_MTEX; a++) {
651                         if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
652                                 if(wrld->id.lib) lib= 1;
653                                 else local= 1;
654                         }
655                 }
656                 wrld= wrld->id.next;
657         }
658         br= G.main->brush.first;
659         while(br) {
660                 for(a=0; a<MAX_MTEX; a++) {
661                         if(br->mtex[a] && br->mtex[a]->tex==tex) {
662                                 if(br->id.lib) lib= 1;
663                                 else local= 1;
664                         }
665                 }
666                 br= br->id.next;
667         }
668         
669         if(local && lib==0) {
670                 tex->id.lib= 0;
671                 tex->id.flag= LIB_LOCAL;
672                 new_id(0, (ID *)tex, 0);
673         }
674         else if(local && lib) {
675                 texn= copy_texture(tex);
676                 texn->id.us= 0;
677                 
678                 ma= G.main->mat.first;
679                 while(ma) {
680                         for(a=0; a<MAX_MTEX; a++) {
681                                 if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
682                                         if(ma->id.lib==0) {
683                                                 ma->mtex[a]->tex= texn;
684                                                 texn->id.us++;
685                                                 tex->id.us--;
686                                         }
687                                 }
688                         }
689                         ma= ma->id.next;
690                 }
691                 la= G.main->lamp.first;
692                 while(la) {
693                         for(a=0; a<MAX_MTEX; a++) {
694                                 if(la->mtex[a] && la->mtex[a]->tex==tex) {
695                                         if(la->id.lib==0) {
696                                                 la->mtex[a]->tex= texn;
697                                                 texn->id.us++;
698                                                 tex->id.us--;
699                                         }
700                                 }
701                         }
702                         la= la->id.next;
703                 }
704                 wrld= G.main->world.first;
705                 while(wrld) {
706                         for(a=0; a<MAX_MTEX; a++) {
707                                 if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
708                                         if(wrld->id.lib==0) {
709                                                 wrld->mtex[a]->tex= texn;
710                                                 texn->id.us++;
711                                                 tex->id.us--;
712                                         }
713                                 }
714                         }
715                         wrld= wrld->id.next;
716                 }
717                 br= G.main->brush.first;
718                 while(br) {
719                         for(a=0; a<MAX_MTEX; a++) {
720                                 if(br->mtex[a] && br->mtex[a]->tex==tex) {
721                                         if(br->id.lib==0) {
722                                                 br->mtex[a]->tex= texn;
723                                                 texn->id.us++;
724                                                 tex->id.us--;
725                                         }
726                                 }
727                         }
728                         br= br->id.next;
729                 }
730         }
731 }
732
733 /* ------------------------------------------------------------------------- */
734
735 void autotexname(Tex *tex)
736 {
737         char texstr[20][12]= {"None"  , "Clouds" , "Wood", "Marble", "Magic"  , "Blend",
738                 "Stucci", "Noise"  , "Image", "Plugin", "EnvMap" , "Musgrave",
739                 "Voronoi", "DistNoise", "", "", "", "", "", ""};
740         Image *ima;
741         char di[FILE_MAXDIR], fi[FILE_MAXFILE];
742         
743         if(tex) {
744                 if(tex->use_nodes) {
745                         new_id(&G.main->tex, (ID *)tex, "Noddy");
746                 }
747                 else
748                 if(tex->type==TEX_IMAGE) {
749                         ima= tex->ima;
750                         if(ima) {
751                                 strcpy(di, ima->name);
752                                 BLI_splitdirstring(di, fi);
753                                 strcpy(di, "I.");
754                                 strcat(di, fi);
755                                 new_id(&G.main->tex, (ID *)tex, di);
756                         }
757                         else new_id(&G.main->tex, (ID *)tex, texstr[tex->type]);
758                 }
759                 else if(tex->type==TEX_PLUGIN && tex->plugin) new_id(&G.main->tex, (ID *)tex, tex->plugin->pname);
760                 else new_id(&G.main->tex, (ID *)tex, texstr[tex->type]);
761         }
762 }
763
764 /* ------------------------------------------------------------------------- */
765
766 Tex *give_current_texture(Object *ob, int act)
767 {
768         Material ***matarar, *ma;
769         Lamp *la = 0;
770         MTex *mtex = 0;
771         Tex *tex = 0;
772         bNode *node;
773         
774         if(ob==0) return 0;
775         if(ob->totcol==0 && !(ob->type==OB_LAMP)) return 0;
776         
777         if(ob->type==OB_LAMP) {
778                 la=(Lamp *)ob->data;
779                 if(la) {
780                         mtex= la->mtex[(int)(la->texact)];
781                         if(mtex) tex= mtex->tex;
782                 }
783         } else {
784                 if(act>ob->totcol) act= ob->totcol;
785                 else if(act==0) act= 1;
786                 
787                 if( BTST(ob->colbits, act-1) ) {        /* in object */
788                         ma= ob->mat[act-1];
789                 }
790                 else {                                                          /* in data */
791                         matarar= give_matarar(ob);
792                         
793                         if(matarar && *matarar) ma= (*matarar)[act-1];
794                         else ma= 0;
795                 }
796
797                 if(ma && ma->use_nodes && ma->nodetree) {
798                         node= nodeGetActiveID(ma->nodetree, ID_TE);
799
800                         if(node) {
801                                 tex= (Tex *)node->id;
802                                 ma= NULL;
803                         }
804                         else {
805                                 node= nodeGetActiveID(ma->nodetree, ID_MA);
806                                 if(node)
807                                         ma= (Material*)node->id;
808                         }
809                 }
810                 if(ma) {
811                         mtex= ma->mtex[(int)(ma->texact)];
812                         if(mtex) tex= mtex->tex;
813                 }
814         }
815         
816         return tex;
817 }
818
819 Tex *give_current_world_texture(Scene *scene)
820 {
821         MTex *mtex = 0;
822         Tex *tex = 0;
823         
824         if(!(scene->world)) return 0;
825         
826         mtex= scene->world->mtex[(int)(scene->world->texact)];
827         if(mtex) tex= mtex->tex;
828         
829         return tex;
830 }
831
832 /* ------------------------------------------------------------------------- */
833
834 EnvMap *BKE_add_envmap(void)
835 {
836         EnvMap *env;
837         
838         env= MEM_callocN(sizeof(EnvMap), "envmap");
839         env->type= ENV_CUBE;
840         env->stype= ENV_STATIC;
841         env->clipsta= 0.1;
842         env->clipend= 100.0;
843         env->cuberes= 100;
844         
845         return env;
846
847
848 /* ------------------------------------------------------------------------- */
849
850 EnvMap *BKE_copy_envmap(EnvMap *env)
851 {
852         EnvMap *envn;
853         int a;
854         
855         envn= MEM_dupallocN(env);
856         envn->ok= 0;
857         for(a=0; a<6; a++) envn->cube[a]= NULL;
858         if(envn->ima) id_us_plus((ID *)envn->ima);
859         
860         return envn;
861 }
862
863 /* ------------------------------------------------------------------------- */
864
865 void BKE_free_envmapdata(EnvMap *env)
866 {
867         unsigned int part;
868         
869         for(part=0; part<6; part++) {
870                 if(env->cube[part])
871                         IMB_freeImBuf(env->cube[part]);
872                 env->cube[part]= NULL;
873         }
874         env->ok= 0;
875 }
876
877 /* ------------------------------------------------------------------------- */
878
879 void BKE_free_envmap(EnvMap *env)
880 {
881         
882         BKE_free_envmapdata(env);
883         MEM_freeN(env);
884         
885 }
886
887 /* ------------------------------------------------------------------------- */
888 int BKE_texture_dependsOnTime(const struct Tex *texture)
889 {
890         if(texture->plugin) {
891                 // assume all plugins depend on time
892                 return 1;
893         } 
894         else if(        texture->ima && 
895                         ELEM(texture->ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
896                 return 1;
897         } 
898 #if 0 // XXX old animation system
899         else if(texture->ipo) {
900                 // assume any ipo means the texture is animated
901                 return 1;
902         }
903 #endif // XXX old animation system
904         return 0;
905 }
906
907 /* ------------------------------------------------------------------------- */