svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r21899:21908
[blender-staging.git] / source / blender / blenkernel / intern / texture.c
1 /* texture.c
2  *
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 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->type= TEX_CLOUDS;
439         tex->stype= 0;
440         tex->flag= TEX_CHECKER_ODD;
441         tex->imaflag= TEX_INTERPOL|TEX_MIPMAP|TEX_USEALPHA;
442         tex->extend= TEX_REPEAT;
443         tex->cropxmin= tex->cropymin= 0.0;
444         tex->cropxmax= tex->cropymax= 1.0;
445         tex->texfilter = TXF_EWA;
446         tex->afmax = 8;
447         tex->xrepeat= tex->yrepeat= 1;
448         tex->fie_ima= 2;
449         tex->sfra= 1;
450         tex->frames= 0;
451         tex->offset= 0;
452         tex->noisesize= 0.25;
453         tex->noisedepth= 2;
454         tex->turbul= 5.0;
455         tex->nabla= 0.025;      // also in do_versions
456         tex->bright= 1.0;
457         tex->contrast= 1.0;
458         tex->filtersize= 1.0;
459         tex->rfac= 1.0;
460         tex->gfac= 1.0;
461         tex->bfac= 1.0;
462         /* newnoise: init. */
463         tex->noisebasis = 0;
464         tex->noisebasis2 = 0;
465         /* musgrave */
466         tex->mg_H = 1.0;
467         tex->mg_lacunarity = 2.0;
468         tex->mg_octaves = 2.0;
469         tex->mg_offset = 1.0;
470         tex->mg_gain = 1.0;
471         tex->ns_outscale = 1.0;
472         /* distnoise */
473         tex->dist_amount = 1.0;
474         /* voronoi */
475         tex->vn_w1 = 1.0;
476         tex->vn_w2 = tex->vn_w3 = tex->vn_w4 = 0.0;
477         tex->vn_mexp = 2.5;
478         tex->vn_distm = 0;
479         tex->vn_coltype = 0;
480
481         if (tex->env) {
482                 tex->env->stype=ENV_STATIC;
483                 tex->env->clipsta=0.1;
484                 tex->env->clipend=100;
485                 tex->env->cuberes=100;
486                 tex->env->depth=0;
487         }
488
489         pit = tex->plugin;
490         if (pit) {
491                 varstr= pit->varstr;
492                 if(varstr) {
493                         for(a=0; a<pit->vars; a++, varstr++) {
494                                 pit->data[a] = varstr->def;
495                         }
496                 }
497         }
498         
499         tex->iuser.fie_ima= 2;
500         tex->iuser.ok= 1;
501         tex->iuser.frames= 100;
502         
503         tex->preview = NULL;
504 }
505
506 /* ------------------------------------------------------------------------- */
507
508 Tex *add_texture(char *name)
509 {
510         Tex *tex;
511
512         tex= alloc_libblock(&G.main->tex, ID_TE, name);
513         
514         default_tex(tex);
515         
516         return tex;
517 }
518
519 /* ------------------------------------------------------------------------- */
520
521 void default_mtex(MTex *mtex)
522 {
523         mtex->texco= TEXCO_ORCO;
524         mtex->mapto= MAP_COL;
525         mtex->object= 0;
526         mtex->projx= PROJ_X;
527         mtex->projy= PROJ_Y;
528         mtex->projz= PROJ_Z;
529         mtex->mapping= MTEX_FLAT;
530         mtex->ofs[0]= 0.0;
531         mtex->ofs[1]= 0.0;
532         mtex->ofs[2]= 0.0;
533         mtex->size[0]= 1.0;
534         mtex->size[1]= 1.0;
535         mtex->size[2]= 1.0;
536         mtex->tex= 0;
537         mtex->texflag= MTEX_NEW_BUMP;
538         mtex->colormodel= 0;
539         mtex->r= 1.0;
540         mtex->g= 0.0;
541         mtex->b= 1.0;
542         mtex->k= 1.0;
543         mtex->def_var= 1.0;
544         mtex->blendtype= MTEX_BLEND;
545         mtex->colfac= 1.0;
546         mtex->norfac= 1.0;
547         mtex->varfac= 1.0;
548         mtex->dispfac=0.2;
549         mtex->normapspace= MTEX_NSPACE_TANGENT;
550 }
551
552
553 /* ------------------------------------------------------------------------- */
554
555 MTex *add_mtex()
556 {
557         MTex *mtex;
558         
559         mtex= MEM_callocN(sizeof(MTex), "add_mtex");
560         
561         default_mtex(mtex);
562         
563         return mtex;
564 }
565
566 /* ------------------------------------------------------------------------- */
567
568 Tex *copy_texture(Tex *tex)
569 {
570         Tex *texn;
571         
572         texn= copy_libblock(tex);
573         if(texn->type==TEX_IMAGE) id_us_plus((ID *)texn->ima);
574         else texn->ima= 0;
575         
576 #if 0 // XXX old animation system
577         id_us_plus((ID *)texn->ipo);
578 #endif // XXX old animation system
579         
580         if(texn->plugin) {
581                 texn->plugin= MEM_dupallocN(texn->plugin);
582                 open_plugin_tex(texn->plugin);
583         }
584         
585         if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
586         if(texn->env) texn->env= BKE_copy_envmap(texn->env);
587         
588         if(tex->preview) texn->preview = BKE_previewimg_copy(tex->preview);
589
590         if(tex->nodetree) {
591                 ntreeEndExecTree(tex->nodetree);
592                 texn->nodetree= ntreeCopyTree(tex->nodetree, 0); /* 0 == full new tree */
593         }
594         
595         return texn;
596 }
597
598 /* ------------------------------------------------------------------------- */
599
600 void make_local_texture(Tex *tex)
601 {
602         Tex *texn;
603         Material *ma;
604         World *wrld;
605         Lamp *la;
606         Brush *br;
607         int a, local=0, lib=0;
608
609         /* - only lib users: do nothing
610             * - only local users: set flag
611             * - mixed: make copy
612             */
613         
614         if(tex->id.lib==0) return;
615
616         /* special case: ima always local immediately */
617         if(tex->ima) {
618                 tex->ima->id.lib= 0;
619                 tex->ima->id.flag= LIB_LOCAL;
620                 new_id(0, (ID *)tex->ima, 0);
621         }
622
623         if(tex->id.us==1) {
624                 tex->id.lib= 0;
625                 tex->id.flag= LIB_LOCAL;
626                 new_id(0, (ID *)tex, 0);
627
628                 return;
629         }
630         
631         ma= G.main->mat.first;
632         while(ma) {
633                 for(a=0; a<MAX_MTEX; a++) {
634                         if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
635                                 if(ma->id.lib) lib= 1;
636                                 else local= 1;
637                         }
638                 }
639                 ma= ma->id.next;
640         }
641         la= G.main->lamp.first;
642         while(la) {
643                 for(a=0; a<MAX_MTEX; a++) {
644                         if(la->mtex[a] && la->mtex[a]->tex==tex) {
645                                 if(la->id.lib) lib= 1;
646                                 else local= 1;
647                         }
648                 }
649                 la= la->id.next;
650         }
651         wrld= G.main->world.first;
652         while(wrld) {
653                 for(a=0; a<MAX_MTEX; a++) {
654                         if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
655                                 if(wrld->id.lib) lib= 1;
656                                 else local= 1;
657                         }
658                 }
659                 wrld= wrld->id.next;
660         }
661         br= G.main->brush.first;
662         while(br) {
663                 for(a=0; a<MAX_MTEX; a++) {
664                         if(br->mtex[a] && br->mtex[a]->tex==tex) {
665                                 if(br->id.lib) lib= 1;
666                                 else local= 1;
667                         }
668                 }
669                 br= br->id.next;
670         }
671         
672         if(local && lib==0) {
673                 tex->id.lib= 0;
674                 tex->id.flag= LIB_LOCAL;
675                 new_id(0, (ID *)tex, 0);
676         }
677         else if(local && lib) {
678                 texn= copy_texture(tex);
679                 texn->id.us= 0;
680                 
681                 ma= G.main->mat.first;
682                 while(ma) {
683                         for(a=0; a<MAX_MTEX; a++) {
684                                 if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
685                                         if(ma->id.lib==0) {
686                                                 ma->mtex[a]->tex= texn;
687                                                 texn->id.us++;
688                                                 tex->id.us--;
689                                         }
690                                 }
691                         }
692                         ma= ma->id.next;
693                 }
694                 la= G.main->lamp.first;
695                 while(la) {
696                         for(a=0; a<MAX_MTEX; a++) {
697                                 if(la->mtex[a] && la->mtex[a]->tex==tex) {
698                                         if(la->id.lib==0) {
699                                                 la->mtex[a]->tex= texn;
700                                                 texn->id.us++;
701                                                 tex->id.us--;
702                                         }
703                                 }
704                         }
705                         la= la->id.next;
706                 }
707                 wrld= G.main->world.first;
708                 while(wrld) {
709                         for(a=0; a<MAX_MTEX; a++) {
710                                 if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
711                                         if(wrld->id.lib==0) {
712                                                 wrld->mtex[a]->tex= texn;
713                                                 texn->id.us++;
714                                                 tex->id.us--;
715                                         }
716                                 }
717                         }
718                         wrld= wrld->id.next;
719                 }
720                 br= G.main->brush.first;
721                 while(br) {
722                         for(a=0; a<MAX_MTEX; a++) {
723                                 if(br->mtex[a] && br->mtex[a]->tex==tex) {
724                                         if(br->id.lib==0) {
725                                                 br->mtex[a]->tex= texn;
726                                                 texn->id.us++;
727                                                 tex->id.us--;
728                                         }
729                                 }
730                         }
731                         br= br->id.next;
732                 }
733         }
734 }
735
736 /* ------------------------------------------------------------------------- */
737
738 void autotexname(Tex *tex)
739 {
740         char texstr[20][12]= {"None"  , "Clouds" , "Wood", "Marble", "Magic"  , "Blend",
741                 "Stucci", "Noise"  , "Image", "Plugin", "EnvMap" , "Musgrave",
742                 "Voronoi", "DistNoise", "", "", "", "", "", ""};
743         Image *ima;
744         char di[FILE_MAXDIR], fi[FILE_MAXFILE];
745         
746         if(tex) {
747                 if(tex->use_nodes) {
748                         new_id(&G.main->tex, (ID *)tex, "Noddy");
749                 }
750                 else
751                 if(tex->type==TEX_IMAGE) {
752                         ima= tex->ima;
753                         if(ima) {
754                                 strcpy(di, ima->name);
755                                 BLI_splitdirstring(di, fi);
756                                 strcpy(di, "I.");
757                                 strcat(di, fi);
758                                 new_id(&G.main->tex, (ID *)tex, di);
759                         }
760                         else new_id(&G.main->tex, (ID *)tex, texstr[tex->type]);
761                 }
762                 else if(tex->type==TEX_PLUGIN && tex->plugin) new_id(&G.main->tex, (ID *)tex, tex->plugin->pname);
763                 else new_id(&G.main->tex, (ID *)tex, texstr[tex->type]);
764         }
765 }
766
767 /* ------------------------------------------------------------------------- */
768
769 Tex *give_current_texture(Object *ob, int act)
770 {
771         Material ***matarar, *ma;
772         Lamp *la = 0;
773         MTex *mtex = 0;
774         Tex *tex = 0;
775         bNode *node;
776         
777         if(ob==0) return 0;
778         if(ob->totcol==0 && !(ob->type==OB_LAMP)) return 0;
779         
780         if(ob->type==OB_LAMP) {
781                 la=(Lamp *)ob->data;
782                 if(la) {
783                         mtex= la->mtex[(int)(la->texact)];
784                         if(mtex) tex= mtex->tex;
785                 }
786         } else {
787                 if(act>ob->totcol) act= ob->totcol;
788                 else if(act==0) act= 1;
789                 
790                 if(ob->matbits[act-1]) {        /* in object */
791                         ma= ob->mat[act-1];
792                 }
793                 else {                                                          /* in data */
794                         matarar= give_matarar(ob);
795                         
796                         if(matarar && *matarar) ma= (*matarar)[act-1];
797                         else ma= 0;
798                 }
799
800                 if(ma && ma->use_nodes && ma->nodetree) {
801                         node= nodeGetActiveID(ma->nodetree, ID_TE);
802
803                         if(node) {
804                                 tex= (Tex *)node->id;
805                                 ma= NULL;
806                         }
807                         else {
808                                 node= nodeGetActiveID(ma->nodetree, ID_MA);
809                                 if(node)
810                                         ma= (Material*)node->id;
811                         }
812                 }
813                 if(ma) {
814                         mtex= ma->mtex[(int)(ma->texact)];
815                         if(mtex) tex= mtex->tex;
816                 }
817         }
818         
819         return tex;
820 }
821
822 Tex *give_current_world_texture(Scene *scene)
823 {
824         MTex *mtex = 0;
825         Tex *tex = 0;
826         
827         if(!(scene->world)) return 0;
828         
829         mtex= scene->world->mtex[(int)(scene->world->texact)];
830         if(mtex) tex= mtex->tex;
831         
832         return tex;
833 }
834
835 /* ------------------------------------------------------------------------- */
836
837 EnvMap *BKE_add_envmap(void)
838 {
839         EnvMap *env;
840         
841         env= MEM_callocN(sizeof(EnvMap), "envmap");
842         env->type= ENV_CUBE;
843         env->stype= ENV_STATIC;
844         env->clipsta= 0.1;
845         env->clipend= 100.0;
846         env->cuberes= 100;
847         
848         return env;
849
850
851 /* ------------------------------------------------------------------------- */
852
853 EnvMap *BKE_copy_envmap(EnvMap *env)
854 {
855         EnvMap *envn;
856         int a;
857         
858         envn= MEM_dupallocN(env);
859         envn->ok= 0;
860         for(a=0; a<6; a++) envn->cube[a]= NULL;
861         if(envn->ima) id_us_plus((ID *)envn->ima);
862         
863         return envn;
864 }
865
866 /* ------------------------------------------------------------------------- */
867
868 void BKE_free_envmapdata(EnvMap *env)
869 {
870         unsigned int part;
871         
872         for(part=0; part<6; part++) {
873                 if(env->cube[part])
874                         IMB_freeImBuf(env->cube[part]);
875                 env->cube[part]= NULL;
876         }
877         env->ok= 0;
878 }
879
880 /* ------------------------------------------------------------------------- */
881
882 void BKE_free_envmap(EnvMap *env)
883 {
884         
885         BKE_free_envmapdata(env);
886         MEM_freeN(env);
887         
888 }
889
890 /* ------------------------------------------------------------------------- */
891 int BKE_texture_dependsOnTime(const struct Tex *texture)
892 {
893         if(texture->plugin) {
894                 // assume all plugins depend on time
895                 return 1;
896         } 
897         else if(        texture->ima && 
898                         ELEM(texture->ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
899                 return 1;
900         } 
901 #if 0 // XXX old animation system
902         else if(texture->ipo) {
903                 // assume any ipo means the texture is animated
904                 return 1;
905         }
906 #endif // XXX old animation system
907         return 0;
908 }
909
910 /* ------------------------------------------------------------------------- */