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