634d9e6ce0d923d372204c303e1585ac75a620ab
[blender-staging.git] / source / blender / blenkernel / intern / texture.c
1 /* texture.c
2  *
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <math.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "PIL_dynlib.h"
40
41
42
43 #include "BLI_blenlib.h"
44 #include "BLI_math.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         unit_m4(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         size_to_mat3( smat,texmap->size);
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         eul_to_mat3( rmat,eul);
233         
234         mul_m3_m3m3(mat, rmat, smat);
235         
236         copy_m4_m3(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(const 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->colspecfac= 1.0f;
563         mtex->mirrfac= 1.0f;
564         mtex->alphafac= 1.0f;
565         mtex->difffac= 1.0f;
566         mtex->specfac= 1.0f;
567         mtex->emitfac= 1.0f;
568         mtex->hardfac= 1.0f;
569         mtex->raymirrfac= 1.0f;
570         mtex->translfac= 1.0f;
571         mtex->ambfac= 1.0f;
572         mtex->colemitfac= 1.0f;
573         mtex->colreflfac= 1.0f;
574         mtex->coltransfac= 1.0f;
575         mtex->densfac= 1.0f;
576         mtex->scatterfac= 1.0f;
577         mtex->reflfac= 1.0f;
578         mtex->shadowfac= 1.0f;
579         mtex->zenupfac= 1.0f;
580         mtex->zendownfac= 1.0f;
581         mtex->blendfac= 1.0f;
582         mtex->timefac= 1.0f;
583         mtex->lengthfac= 1.0f;
584         mtex->clumpfac= 1.0f;
585         mtex->kinkfac= 1.0f;
586         mtex->roughfac= 1.0f;
587         mtex->padensfac= 1.0f;
588         mtex->lifefac= 1.0f;
589         mtex->sizefac= 1.0f;
590         mtex->ivelfac= 1.0f;
591         mtex->pvelfac= 1.0f;
592         mtex->normapspace= MTEX_NSPACE_TANGENT;
593 }
594
595
596 /* ------------------------------------------------------------------------- */
597
598 MTex *add_mtex()
599 {
600         MTex *mtex;
601         
602         mtex= MEM_callocN(sizeof(MTex), "add_mtex");
603         
604         default_mtex(mtex);
605         
606         return mtex;
607 }
608
609 /* ------------------------------------------------------------------------- */
610
611 Tex *copy_texture(Tex *tex)
612 {
613         Tex *texn;
614         
615         texn= copy_libblock(tex);
616         if(texn->type==TEX_IMAGE) id_us_plus((ID *)texn->ima);
617         else texn->ima= 0;
618         
619 #if 0 // XXX old animation system
620         id_us_plus((ID *)texn->ipo);
621 #endif // XXX old animation system
622         
623         if(texn->plugin) {
624                 texn->plugin= MEM_dupallocN(texn->plugin);
625                 open_plugin_tex(texn->plugin);
626         }
627         
628         if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
629         if(texn->env) texn->env= BKE_copy_envmap(texn->env);
630         
631         if(tex->preview) texn->preview = BKE_previewimg_copy(tex->preview);
632
633         if(tex->nodetree) {
634                 ntreeEndExecTree(tex->nodetree);
635                 texn->nodetree= ntreeCopyTree(tex->nodetree, 0); /* 0 == full new tree */
636         }
637         
638         return texn;
639 }
640
641 /* ------------------------------------------------------------------------- */
642
643 void make_local_texture(Tex *tex)
644 {
645         Tex *texn;
646         Material *ma;
647         World *wrld;
648         Lamp *la;
649         Brush *br;
650         int a, local=0, lib=0;
651
652         /* - only lib users: do nothing
653             * - only local users: set flag
654             * - mixed: make copy
655             */
656         
657         if(tex->id.lib==0) return;
658
659         /* special case: ima always local immediately */
660         if(tex->ima) {
661                 tex->ima->id.lib= 0;
662                 tex->ima->id.flag= LIB_LOCAL;
663                 new_id(0, (ID *)tex->ima, 0);
664         }
665
666         if(tex->id.us==1) {
667                 tex->id.lib= 0;
668                 tex->id.flag= LIB_LOCAL;
669                 new_id(0, (ID *)tex, 0);
670
671                 return;
672         }
673         
674         ma= G.main->mat.first;
675         while(ma) {
676                 for(a=0; a<MAX_MTEX; a++) {
677                         if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
678                                 if(ma->id.lib) lib= 1;
679                                 else local= 1;
680                         }
681                 }
682                 ma= ma->id.next;
683         }
684         la= G.main->lamp.first;
685         while(la) {
686                 for(a=0; a<MAX_MTEX; a++) {
687                         if(la->mtex[a] && la->mtex[a]->tex==tex) {
688                                 if(la->id.lib) lib= 1;
689                                 else local= 1;
690                         }
691                 }
692                 la= la->id.next;
693         }
694         wrld= G.main->world.first;
695         while(wrld) {
696                 for(a=0; a<MAX_MTEX; a++) {
697                         if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
698                                 if(wrld->id.lib) lib= 1;
699                                 else local= 1;
700                         }
701                 }
702                 wrld= wrld->id.next;
703         }
704         br= G.main->brush.first;
705         while(br) {
706                 if(br->mtex.tex==tex) {
707                         if(br->id.lib) lib= 1;
708                         else local= 1;
709                 }
710                 br= br->id.next;
711         }
712         
713         if(local && lib==0) {
714                 tex->id.lib= 0;
715                 tex->id.flag= LIB_LOCAL;
716                 new_id(0, (ID *)tex, 0);
717         }
718         else if(local && lib) {
719                 texn= copy_texture(tex);
720                 texn->id.us= 0;
721                 
722                 ma= G.main->mat.first;
723                 while(ma) {
724                         for(a=0; a<MAX_MTEX; a++) {
725                                 if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
726                                         if(ma->id.lib==0) {
727                                                 ma->mtex[a]->tex= texn;
728                                                 texn->id.us++;
729                                                 tex->id.us--;
730                                         }
731                                 }
732                         }
733                         ma= ma->id.next;
734                 }
735                 la= G.main->lamp.first;
736                 while(la) {
737                         for(a=0; a<MAX_MTEX; a++) {
738                                 if(la->mtex[a] && la->mtex[a]->tex==tex) {
739                                         if(la->id.lib==0) {
740                                                 la->mtex[a]->tex= texn;
741                                                 texn->id.us++;
742                                                 tex->id.us--;
743                                         }
744                                 }
745                         }
746                         la= la->id.next;
747                 }
748                 wrld= G.main->world.first;
749                 while(wrld) {
750                         for(a=0; a<MAX_MTEX; a++) {
751                                 if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
752                                         if(wrld->id.lib==0) {
753                                                 wrld->mtex[a]->tex= texn;
754                                                 texn->id.us++;
755                                                 tex->id.us--;
756                                         }
757                                 }
758                         }
759                         wrld= wrld->id.next;
760                 }
761                 br= G.main->brush.first;
762                 while(br) {
763                         if(br->mtex.tex==tex) {
764                                 if(br->id.lib==0) {
765                                         br->mtex.tex= texn;
766                                         texn->id.us++;
767                                         tex->id.us--;
768                                 }
769                         }
770                         br= br->id.next;
771                 }
772         }
773 }
774
775 /* ------------------------------------------------------------------------- */
776
777 void autotexname(Tex *tex)
778 {
779         char texstr[20][15]= {"None"  , "Clouds" , "Wood", "Marble", "Magic"  , "Blend",
780                 "Stucci", "Noise"  , "Image", "Plugin", "EnvMap" , "Musgrave",
781                 "Voronoi", "DistNoise", "Point Density", "Voxel Data", "", "", "", ""};
782         Image *ima;
783         char di[FILE_MAXDIR], fi[FILE_MAXFILE];
784         
785         if(tex) {
786                 if(tex->use_nodes) {
787                         new_id(&G.main->tex, (ID *)tex, "Noddy");
788                 }
789                 else
790                 if(tex->type==TEX_IMAGE) {
791                         ima= tex->ima;
792                         if(ima) {
793                                 strcpy(di, ima->name);
794                                 BLI_splitdirstring(di, fi);
795                                 strcpy(di, "I.");
796                                 strcat(di, fi);
797                                 new_id(&G.main->tex, (ID *)tex, di);
798                         }
799                         else new_id(&G.main->tex, (ID *)tex, texstr[tex->type]);
800                 }
801                 else if(tex->type==TEX_PLUGIN && tex->plugin) new_id(&G.main->tex, (ID *)tex, tex->plugin->pname);
802                 else new_id(&G.main->tex, (ID *)tex, texstr[tex->type]);
803         }
804 }
805
806 /* ------------------------------------------------------------------------- */
807
808 Tex *give_current_object_texture(Object *ob)
809 {
810         Material *ma;
811         Tex *tex= NULL;
812         
813         if(ob==0) return 0;
814         if(ob->totcol==0 && !(ob->type==OB_LAMP)) return 0;
815         
816         if(ob->type==OB_LAMP) {
817                 tex= give_current_lamp_texture(ob->data);
818         } else {
819                 ma= give_current_material(ob, ob->actcol);
820                 tex= give_current_material_texture(ma);
821         }
822         
823         return tex;
824 }
825
826 Tex *give_current_lamp_texture(Lamp *la)
827 {
828         MTex *mtex= NULL;
829         Tex *tex= NULL;
830
831         if(la) {
832                 mtex= la->mtex[(int)(la->texact)];
833                 if(mtex) tex= mtex->tex;
834         }
835
836         return tex;
837 }
838
839 void set_current_lamp_texture(Lamp *la, Tex *newtex)
840 {
841         int act= la->texact;
842
843         if(la->mtex[act] && la->mtex[act]->tex)
844                 id_us_min(&la->mtex[act]->tex->id);
845
846         if(newtex) {
847                 if(!la->mtex[act]) {
848                         la->mtex[act]= add_mtex();
849                         la->mtex[act]->texco= TEXCO_GLOB;
850                 }
851                 
852                 la->mtex[act]->tex= newtex;
853                 id_us_plus(&newtex->id);
854         }
855         else if(la->mtex[act]) {
856                 MEM_freeN(la->mtex[act]);
857                 la->mtex[act]= NULL;
858         }
859 }
860
861 bNode *give_current_material_texture_node(Material *ma)
862 {
863         bNode *node;
864         
865         if(ma && ma->use_nodes && ma->nodetree)
866                 return nodeGetActiveID(ma->nodetree, ID_TE);
867         
868         return NULL;
869 }
870
871 Tex *give_current_material_texture(Material *ma)
872 {
873         MTex *mtex= NULL;
874         Tex *tex= NULL;
875         bNode *node;
876         
877         if(ma && ma->use_nodes && ma->nodetree) {
878                 /* first check texture, then material, this works together
879                    with a hack that clears the active ID flag for textures on
880                    making a material node active */
881                 node= nodeGetActiveID(ma->nodetree, ID_TE);
882
883                 if(node) {
884                         tex= (Tex *)node->id;
885                         ma= NULL;
886                 }
887                 else {
888                         node= nodeGetActiveID(ma->nodetree, ID_MA);
889                         if(node)
890                                 ma= (Material*)node->id;
891                 }
892         }
893
894         if(ma) {
895                 mtex= ma->mtex[(int)(ma->texact)];
896                 if(mtex) tex= mtex->tex;
897         }
898         
899         return tex;
900 }
901
902 int give_active_mtex(ID *id, MTex ***mtex_ar, short *act)
903 {
904         switch(GS(id->name)) {
905         case ID_MA:
906                 *mtex_ar=               ((Material *)id)->mtex;
907                 if(act) *act=   (((Material *)id)->texact);
908                 break;
909         case ID_WO:
910                 *mtex_ar=               ((World *)id)->mtex;
911                 if(act) *act=   (((World *)id)->texact);
912                 break;
913         case ID_LA:
914                 *mtex_ar=               ((Lamp *)id)->mtex;
915                 if(act) *act=   (((Lamp *)id)->texact);
916                 break;
917         default:
918                 *mtex_ar = NULL;
919                 if(act) *act=   0;
920                 return FALSE;
921         }
922
923         return TRUE;
924 }
925
926 void set_active_mtex(ID *id, short act)
927 {
928         if(act<0)                               act= 0;
929         else if(act>=MAX_MTEX)  act= MAX_MTEX-1;
930
931         switch(GS(id->name)) {
932         case ID_MA:
933                 ((Material *)id)->texact= act;
934                 break;
935         case ID_WO:
936                 ((World *)id)->texact= act;
937                 break;
938         case ID_LA:
939                 ((Lamp *)id)->texact= act;
940                 break;
941         }
942 }
943
944 void set_current_material_texture(Material *ma, Tex *newtex)
945 {
946         Tex *tex= NULL;
947         bNode *node;
948         
949         if(ma && ma->use_nodes && ma->nodetree) {
950                 node= nodeGetActiveID(ma->nodetree, ID_TE);
951
952                 if(node) {
953                         tex= (Tex *)node->id;
954                         id_us_min(&tex->id);
955                         node->id= &newtex->id;
956                         id_us_plus(&newtex->id);
957                         ma= NULL;
958                 }
959                 else {
960                         node= nodeGetActiveID(ma->nodetree, ID_MA);
961                         if(node)
962                                 ma= (Material*)node->id;
963                 }
964         }
965         if(ma) {
966                 int act= (int)ma->texact;
967
968                 tex= (ma->mtex[act])? ma->mtex[act]->tex: NULL;
969                 id_us_min(&tex->id);
970
971                 if(newtex) {
972                         if(!ma->mtex[act])
973                                 ma->mtex[act]= add_mtex();
974                         
975                         ma->mtex[act]->tex= newtex;
976                         id_us_plus(&newtex->id);
977                 }
978                 else if(ma->mtex[act]) {
979                         MEM_freeN(ma->mtex[act]);
980                         ma->mtex[act]= NULL;
981                 }
982         }
983 }
984
985 Tex *give_current_world_texture(World *world)
986 {
987         MTex *mtex= NULL;
988         Tex *tex= NULL;
989         
990         if(!world) return 0;
991         
992         mtex= world->mtex[(int)(world->texact)];
993         if(mtex) tex= mtex->tex;
994         
995         return tex;
996 }
997
998 void set_current_world_texture(World *wo, Tex *newtex)
999 {
1000         int act= wo->texact;
1001
1002         if(wo->mtex[act] && wo->mtex[act]->tex)
1003                 id_us_min(&wo->mtex[act]->tex->id);
1004
1005         if(newtex) {
1006                 if(!wo->mtex[act]) {
1007                         wo->mtex[act]= add_mtex();
1008                         wo->mtex[act]->texco= TEXCO_VIEW;
1009                 }
1010                 
1011                 wo->mtex[act]->tex= newtex;
1012                 id_us_plus(&newtex->id);
1013         }
1014         else if(wo->mtex[act]) {
1015                 MEM_freeN(wo->mtex[act]);
1016                 wo->mtex[act]= NULL;
1017         }
1018 }
1019
1020 Tex *give_current_brush_texture(Brush *br)
1021 {
1022         return br->mtex.tex;
1023 }
1024
1025 void set_current_brush_texture(Brush *br, Tex *newtex)
1026 {
1027         if(br->mtex.tex)
1028                 id_us_min(&br->mtex.tex->id);
1029
1030         if(newtex) {
1031                 br->mtex.tex= newtex;
1032                 id_us_plus(&newtex->id);
1033         }
1034 }
1035
1036 /* ------------------------------------------------------------------------- */
1037
1038 EnvMap *BKE_add_envmap(void)
1039 {
1040         EnvMap *env;
1041         
1042         env= MEM_callocN(sizeof(EnvMap), "envmap");
1043         env->type= ENV_CUBE;
1044         env->stype= ENV_STATIC;
1045         env->clipsta= 0.1;
1046         env->clipend= 100.0;
1047         env->cuberes= 100;
1048         
1049         return env;
1050
1051
1052 /* ------------------------------------------------------------------------- */
1053
1054 EnvMap *BKE_copy_envmap(EnvMap *env)
1055 {
1056         EnvMap *envn;
1057         int a;
1058         
1059         envn= MEM_dupallocN(env);
1060         envn->ok= 0;
1061         for(a=0; a<6; a++) envn->cube[a]= NULL;
1062         if(envn->ima) id_us_plus((ID *)envn->ima);
1063         
1064         return envn;
1065 }
1066
1067 /* ------------------------------------------------------------------------- */
1068
1069 void BKE_free_envmapdata(EnvMap *env)
1070 {
1071         unsigned int part;
1072         
1073         for(part=0; part<6; part++) {
1074                 if(env->cube[part])
1075                         IMB_freeImBuf(env->cube[part]);
1076                 env->cube[part]= NULL;
1077         }
1078         env->ok= 0;
1079 }
1080
1081 /* ------------------------------------------------------------------------- */
1082
1083 void BKE_free_envmap(EnvMap *env)
1084 {
1085         
1086         BKE_free_envmapdata(env);
1087         MEM_freeN(env);
1088         
1089 }
1090
1091 /* ------------------------------------------------------------------------- */
1092
1093 PointDensity *BKE_add_pointdensity(void)
1094 {
1095         PointDensity *pd;
1096         
1097         pd= MEM_callocN(sizeof(PointDensity), "pointdensity");
1098         pd->flag = 0;
1099         pd->radius = 0.3f;
1100         pd->falloff_type = TEX_PD_FALLOFF_STD;
1101         pd->falloff_softness = 2.0;
1102         pd->source = TEX_PD_PSYS;
1103         pd->point_tree = NULL;
1104         pd->point_data = NULL;
1105         pd->noise_size = 0.5f;
1106         pd->noise_depth = 1;
1107         pd->noise_fac = 1.0f;
1108         pd->noise_influence = TEX_PD_NOISE_STATIC;
1109         pd->coba = add_colorband(1);
1110         pd->speed_scale = 1.0f;
1111         pd->totpoints = 0;
1112         pd->coba = add_colorband(1);
1113         pd->object = NULL;
1114         pd->psys = 0;
1115         return pd;
1116
1117
1118 PointDensity *BKE_copy_pointdensity(PointDensity *pd)
1119 {
1120         PointDensity *pdn;
1121
1122         pdn= MEM_dupallocN(pd);
1123         pdn->point_tree = NULL;
1124         pdn->point_data = NULL;
1125         if(pdn->coba) pdn->coba= MEM_dupallocN(pdn->coba);
1126         
1127         return pdn;
1128 }
1129
1130 void BKE_free_pointdensitydata(PointDensity *pd)
1131 {
1132         if (pd->point_tree) {
1133                 BLI_bvhtree_free(pd->point_tree);
1134                 pd->point_tree = NULL;
1135         }
1136         if (pd->point_data) {
1137                 MEM_freeN(pd->point_data);
1138                 pd->point_data = NULL;
1139         }
1140         if(pd->coba) MEM_freeN(pd->coba);
1141 }
1142
1143 void BKE_free_pointdensity(PointDensity *pd)
1144 {
1145         BKE_free_pointdensitydata(pd);
1146         MEM_freeN(pd);
1147 }
1148
1149
1150 void BKE_free_voxeldatadata(struct VoxelData *vd)
1151 {
1152         if (vd->dataset) {
1153                 MEM_freeN(vd->dataset);
1154                 vd->dataset = NULL;
1155         }
1156
1157 }
1158  
1159 void BKE_free_voxeldata(struct VoxelData *vd)
1160 {
1161         BKE_free_voxeldatadata(vd);
1162         MEM_freeN(vd);
1163 }
1164  
1165 struct VoxelData *BKE_add_voxeldata(void)
1166 {
1167         VoxelData *vd;
1168
1169         vd= MEM_callocN(sizeof(struct VoxelData), "voxeldata");
1170         vd->dataset = NULL;
1171         vd->resol[0] = vd->resol[1] = vd->resol[2] = 1;
1172         vd->interp_type= TEX_VD_LINEAR;
1173         vd->file_format= TEX_VD_SMOKE;
1174         vd->int_multiplier = 1.0;
1175         vd->extend = TEX_CLIP;
1176         vd->object = NULL;
1177         
1178         return vd;
1179  }
1180  
1181 struct VoxelData *BKE_copy_voxeldata(struct VoxelData *vd)
1182 {
1183         VoxelData *vdn;
1184
1185         vdn= MEM_dupallocN(vd); 
1186         vdn->dataset = NULL;
1187
1188         return vdn;
1189 }
1190
1191
1192 /* ------------------------------------------------------------------------- */
1193 int BKE_texture_dependsOnTime(const struct Tex *texture)
1194 {
1195         if(texture->plugin) {
1196                 // assume all plugins depend on time
1197                 return 1;
1198         } 
1199         else if(        texture->ima && 
1200                         ELEM(texture->ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
1201                 return 1;
1202         } 
1203 #if 0 // XXX old animation system
1204         else if(texture->ipo) {
1205                 // assume any ipo means the texture is animated
1206                 return 1;
1207         }
1208 #endif // XXX old animation system
1209         return 0;
1210 }
1211
1212 /* ------------------------------------------------------------------------- */