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