svn merge -r 22423:22450 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[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 #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                                                 set_four_ipo(fac, t, KEY_CARDINAL);
372                                         else
373                                                 set_four_ipo(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=0;
500         }
501         pit = tex->plugin;
502         if (pit) {
503                 varstr= pit->varstr;
504                 if(varstr) {
505                         for(a=0; a<pit->vars; a++, varstr++) {
506                                 pit->data[a] = varstr->def;
507                         }
508                 }
509         }
510         
511         tex->iuser.fie_ima= 2;
512         tex->iuser.ok= 1;
513         tex->iuser.frames= 100;
514         
515         tex->preview = NULL;
516 }
517
518 /* ------------------------------------------------------------------------- */
519
520 Tex *add_texture(char *name)
521 {
522         Tex *tex;
523
524         tex= alloc_libblock(&G.main->tex, ID_TE, name);
525         
526         default_tex(tex);
527         
528         return tex;
529 }
530
531 /* ------------------------------------------------------------------------- */
532
533 void default_mtex(MTex *mtex)
534 {
535         mtex->texco= TEXCO_ORCO;
536         mtex->mapto= MAP_COL;
537         mtex->object= 0;
538         mtex->projx= PROJ_X;
539         mtex->projy= PROJ_Y;
540         mtex->projz= PROJ_Z;
541         mtex->mapping= MTEX_FLAT;
542         mtex->ofs[0]= 0.0;
543         mtex->ofs[1]= 0.0;
544         mtex->ofs[2]= 0.0;
545         mtex->size[0]= 1.0;
546         mtex->size[1]= 1.0;
547         mtex->size[2]= 1.0;
548         mtex->tex= 0;
549         mtex->texflag= MTEX_NEW_BUMP;
550         mtex->colormodel= 0;
551         mtex->r= 1.0;
552         mtex->g= 0.0;
553         mtex->b= 1.0;
554         mtex->k= 1.0;
555         mtex->def_var= 1.0;
556         mtex->blendtype= MTEX_BLEND;
557         mtex->colfac= 1.0;
558         mtex->norfac= 1.0;
559         mtex->varfac= 1.0;
560         mtex->dispfac=0.2;
561         mtex->normapspace= MTEX_NSPACE_TANGENT;
562 }
563
564
565 /* ------------------------------------------------------------------------- */
566
567 MTex *add_mtex()
568 {
569         MTex *mtex;
570         
571         mtex= MEM_callocN(sizeof(MTex), "add_mtex");
572         
573         default_mtex(mtex);
574         
575         return mtex;
576 }
577
578 /* ------------------------------------------------------------------------- */
579
580 Tex *copy_texture(Tex *tex)
581 {
582         Tex *texn;
583         
584         texn= copy_libblock(tex);
585         if(texn->type==TEX_IMAGE) id_us_plus((ID *)texn->ima);
586         else texn->ima= 0;
587         
588 #if 0 // XXX old animation system
589         id_us_plus((ID *)texn->ipo);
590 #endif // XXX old animation system
591         
592         if(texn->plugin) {
593                 texn->plugin= MEM_dupallocN(texn->plugin);
594                 open_plugin_tex(texn->plugin);
595         }
596         
597         if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
598         if(texn->env) texn->env= BKE_copy_envmap(texn->env);
599         
600         if(tex->preview) texn->preview = BKE_previewimg_copy(tex->preview);
601
602         if(tex->nodetree) {
603                 ntreeEndExecTree(tex->nodetree);
604                 texn->nodetree= ntreeCopyTree(tex->nodetree, 0); /* 0 == full new tree */
605         }
606         
607         return texn;
608 }
609
610 /* ------------------------------------------------------------------------- */
611
612 void make_local_texture(Tex *tex)
613 {
614         Tex *texn;
615         Material *ma;
616         World *wrld;
617         Lamp *la;
618         Brush *br;
619         int a, local=0, lib=0;
620
621         /* - only lib users: do nothing
622             * - only local users: set flag
623             * - mixed: make copy
624             */
625         
626         if(tex->id.lib==0) return;
627
628         /* special case: ima always local immediately */
629         if(tex->ima) {
630                 tex->ima->id.lib= 0;
631                 tex->ima->id.flag= LIB_LOCAL;
632                 new_id(0, (ID *)tex->ima, 0);
633         }
634
635         if(tex->id.us==1) {
636                 tex->id.lib= 0;
637                 tex->id.flag= LIB_LOCAL;
638                 new_id(0, (ID *)tex, 0);
639
640                 return;
641         }
642         
643         ma= G.main->mat.first;
644         while(ma) {
645                 for(a=0; a<MAX_MTEX; a++) {
646                         if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
647                                 if(ma->id.lib) lib= 1;
648                                 else local= 1;
649                         }
650                 }
651                 ma= ma->id.next;
652         }
653         la= G.main->lamp.first;
654         while(la) {
655                 for(a=0; a<MAX_MTEX; a++) {
656                         if(la->mtex[a] && la->mtex[a]->tex==tex) {
657                                 if(la->id.lib) lib= 1;
658                                 else local= 1;
659                         }
660                 }
661                 la= la->id.next;
662         }
663         wrld= G.main->world.first;
664         while(wrld) {
665                 for(a=0; a<MAX_MTEX; a++) {
666                         if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
667                                 if(wrld->id.lib) lib= 1;
668                                 else local= 1;
669                         }
670                 }
671                 wrld= wrld->id.next;
672         }
673         br= G.main->brush.first;
674         while(br) {
675                 for(a=0; a<MAX_MTEX; a++) {
676                         if(br->mtex[a] && br->mtex[a]->tex==tex) {
677                                 if(br->id.lib) lib= 1;
678                                 else local= 1;
679                         }
680                 }
681                 br= br->id.next;
682         }
683         
684         if(local && lib==0) {
685                 tex->id.lib= 0;
686                 tex->id.flag= LIB_LOCAL;
687                 new_id(0, (ID *)tex, 0);
688         }
689         else if(local && lib) {
690                 texn= copy_texture(tex);
691                 texn->id.us= 0;
692                 
693                 ma= G.main->mat.first;
694                 while(ma) {
695                         for(a=0; a<MAX_MTEX; a++) {
696                                 if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
697                                         if(ma->id.lib==0) {
698                                                 ma->mtex[a]->tex= texn;
699                                                 texn->id.us++;
700                                                 tex->id.us--;
701                                         }
702                                 }
703                         }
704                         ma= ma->id.next;
705                 }
706                 la= G.main->lamp.first;
707                 while(la) {
708                         for(a=0; a<MAX_MTEX; a++) {
709                                 if(la->mtex[a] && la->mtex[a]->tex==tex) {
710                                         if(la->id.lib==0) {
711                                                 la->mtex[a]->tex= texn;
712                                                 texn->id.us++;
713                                                 tex->id.us--;
714                                         }
715                                 }
716                         }
717                         la= la->id.next;
718                 }
719                 wrld= G.main->world.first;
720                 while(wrld) {
721                         for(a=0; a<MAX_MTEX; a++) {
722                                 if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
723                                         if(wrld->id.lib==0) {
724                                                 wrld->mtex[a]->tex= texn;
725                                                 texn->id.us++;
726                                                 tex->id.us--;
727                                         }
728                                 }
729                         }
730                         wrld= wrld->id.next;
731                 }
732                 br= G.main->brush.first;
733                 while(br) {
734                         for(a=0; a<MAX_MTEX; a++) {
735                                 if(br->mtex[a] && br->mtex[a]->tex==tex) {
736                                         if(br->id.lib==0) {
737                                                 br->mtex[a]->tex= texn;
738                                                 texn->id.us++;
739                                                 tex->id.us--;
740                                         }
741                                 }
742                         }
743                         br= br->id.next;
744                 }
745         }
746 }
747
748 /* ------------------------------------------------------------------------- */
749
750 void autotexname(Tex *tex)
751 {
752         char texstr[20][12]= {"None"  , "Clouds" , "Wood", "Marble", "Magic"  , "Blend",
753                 "Stucci", "Noise"  , "Image", "Plugin", "EnvMap" , "Musgrave",
754                 "Voronoi", "DistNoise", "Point Density", "Voxel Data", "", "", "", ""};
755         Image *ima;
756         char di[FILE_MAXDIR], fi[FILE_MAXFILE];
757         
758         if(tex) {
759                 if(tex->use_nodes) {
760                         new_id(&G.main->tex, (ID *)tex, "Noddy");
761                 }
762                 else
763                 if(tex->type==TEX_IMAGE) {
764                         ima= tex->ima;
765                         if(ima) {
766                                 strcpy(di, ima->name);
767                                 BLI_splitdirstring(di, fi);
768                                 strcpy(di, "I.");
769                                 strcat(di, fi);
770                                 new_id(&G.main->tex, (ID *)tex, di);
771                         }
772                         else new_id(&G.main->tex, (ID *)tex, texstr[tex->type]);
773                 }
774                 else if(tex->type==TEX_PLUGIN && tex->plugin) new_id(&G.main->tex, (ID *)tex, tex->plugin->pname);
775                 else new_id(&G.main->tex, (ID *)tex, texstr[tex->type]);
776         }
777 }
778
779 /* ------------------------------------------------------------------------- */
780
781 Tex *give_current_texture(Object *ob, int act)
782 {
783         Material ***matarar, *ma;
784         Lamp *la = 0;
785         MTex *mtex = 0;
786         Tex *tex = 0;
787         bNode *node;
788         
789         if(ob==0) return 0;
790         if(ob->totcol==0 && !(ob->type==OB_LAMP)) return 0;
791         
792         if(ob->type==OB_LAMP) {
793                 la=(Lamp *)ob->data;
794                 if(la) {
795                         mtex= la->mtex[(int)(la->texact)];
796                         if(mtex) tex= mtex->tex;
797                 }
798         } else {
799                 if(act>ob->totcol) act= ob->totcol;
800                 else if(act==0) act= 1;
801                 
802                 if(ob->matbits[act-1]) {        /* in object */
803                         ma= ob->mat[act-1];
804                 }
805                 else {                                                          /* in data */
806                         matarar= give_matarar(ob);
807                         
808                         if(matarar && *matarar) ma= (*matarar)[act-1];
809                         else ma= 0;
810                 }
811
812                 if(ma && ma->use_nodes && ma->nodetree) {
813                         node= nodeGetActiveID(ma->nodetree, ID_TE);
814
815                         if(node) {
816                                 tex= (Tex *)node->id;
817                                 ma= NULL;
818                         }
819                         else {
820                                 node= nodeGetActiveID(ma->nodetree, ID_MA);
821                                 if(node)
822                                         ma= (Material*)node->id;
823                         }
824                 }
825                 if(ma) {
826                         mtex= ma->mtex[(int)(ma->texact)];
827                         if(mtex) tex= mtex->tex;
828                 }
829         }
830         
831         return tex;
832 }
833
834 Tex *give_current_world_texture(Scene *scene)
835 {
836         MTex *mtex = 0;
837         Tex *tex = 0;
838         
839         if(!(scene->world)) return 0;
840         
841         mtex= scene->world->mtex[(int)(scene->world->texact)];
842         if(mtex) tex= mtex->tex;
843         
844         return tex;
845 }
846
847 /* ------------------------------------------------------------------------- */
848
849 EnvMap *BKE_add_envmap(void)
850 {
851         EnvMap *env;
852         
853         env= MEM_callocN(sizeof(EnvMap), "envmap");
854         env->type= ENV_CUBE;
855         env->stype= ENV_STATIC;
856         env->clipsta= 0.1;
857         env->clipend= 100.0;
858         env->cuberes= 100;
859         
860         return env;
861
862
863 /* ------------------------------------------------------------------------- */
864
865 EnvMap *BKE_copy_envmap(EnvMap *env)
866 {
867         EnvMap *envn;
868         int a;
869         
870         envn= MEM_dupallocN(env);
871         envn->ok= 0;
872         for(a=0; a<6; a++) envn->cube[a]= NULL;
873         if(envn->ima) id_us_plus((ID *)envn->ima);
874         
875         return envn;
876 }
877
878 /* ------------------------------------------------------------------------- */
879
880 void BKE_free_envmapdata(EnvMap *env)
881 {
882         unsigned int part;
883         
884         for(part=0; part<6; part++) {
885                 if(env->cube[part])
886                         IMB_freeImBuf(env->cube[part]);
887                 env->cube[part]= NULL;
888         }
889         env->ok= 0;
890 }
891
892 /* ------------------------------------------------------------------------- */
893
894 void BKE_free_envmap(EnvMap *env)
895 {
896         
897         BKE_free_envmapdata(env);
898         MEM_freeN(env);
899         
900 }
901
902 /* ------------------------------------------------------------------------- */
903
904 PointDensity *BKE_add_pointdensity(void)
905 {
906         PointDensity *pd;
907         
908         pd= MEM_callocN(sizeof(PointDensity), "pointdensity");
909         pd->flag = 0;
910         pd->radius = 0.3f;
911         pd->falloff_type = TEX_PD_FALLOFF_STD;
912         pd->falloff_softness = 2.0;
913         pd->source = TEX_PD_PSYS;
914         pd->point_tree = NULL;
915         pd->point_data = NULL;
916         pd->noise_size = 0.5f;
917         pd->noise_depth = 1;
918         pd->noise_fac = 1.0f;
919         pd->noise_influence = TEX_PD_NOISE_STATIC;
920         pd->coba = add_colorband(1);
921         pd->speed_scale = 1.0f;
922         pd->totpoints = 0;
923         pd->coba = add_colorband(1);
924         return pd;
925
926
927 PointDensity *BKE_copy_pointdensity(PointDensity *pd)
928 {
929         PointDensity *pdn;
930
931         pdn= MEM_dupallocN(pd);
932         pdn->point_tree = NULL;
933         pdn->point_data = NULL;
934         if(pdn->coba) pdn->coba= MEM_dupallocN(pdn->coba);
935         
936         return pdn;
937 }
938
939 void BKE_free_pointdensitydata(PointDensity *pd)
940 {
941         if (pd->point_tree) {
942                 BLI_bvhtree_free(pd->point_tree);
943                 pd->point_tree = NULL;
944         }
945         if (pd->point_data) {
946                 MEM_freeN(pd->point_data);
947                 pd->point_data = NULL;
948         }
949         if(pd->coba) MEM_freeN(pd->coba);
950 }
951
952 void BKE_free_pointdensity(PointDensity *pd)
953 {
954         BKE_free_pointdensitydata(pd);
955         MEM_freeN(pd);
956 }
957
958
959 void BKE_free_voxeldatadata(struct VoxelData *vd)
960 {
961         if (vd->dataset) {
962                 MEM_freeN(vd->dataset);
963                 vd->dataset = NULL;
964         }
965
966 }
967  
968 void BKE_free_voxeldata(struct VoxelData *vd)
969 {
970         BKE_free_voxeldatadata(vd);
971         MEM_freeN(vd);
972 }
973  
974 struct VoxelData *BKE_add_voxeldata(void)
975 {
976         VoxelData *vd;
977
978         vd= MEM_callocN(sizeof(struct VoxelData), "voxeldata");
979         vd->dataset = NULL;
980         vd->resol[0] = vd->resol[1] = vd->resol[2] = 1;
981         vd->interp_type= TEX_VD_NEARESTNEIGHBOR;
982         vd->int_multiplier = 1.0;
983         
984         return vd;
985  }
986  
987 struct VoxelData *BKE_copy_voxeldata(struct VoxelData *vd)
988 {
989         VoxelData *vdn;
990
991         vdn= MEM_dupallocN(vd); 
992         vdn->dataset = NULL;
993
994         return vdn;
995 }
996
997
998 /* ------------------------------------------------------------------------- */
999 int BKE_texture_dependsOnTime(const struct Tex *texture)
1000 {
1001         if(texture->plugin) {
1002                 // assume all plugins depend on time
1003                 return 1;
1004         } 
1005         else if(        texture->ima && 
1006                         ELEM(texture->ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
1007                 return 1;
1008         } 
1009 #if 0 // XXX old animation system
1010         else if(texture->ipo) {
1011                 // assume any ipo means the texture is animated
1012                 return 1;
1013         }
1014 #endif // XXX old animation system
1015         return 0;
1016 }
1017
1018 /* ------------------------------------------------------------------------- */