Animation Editors - Texture Animation:
[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., 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_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_free_animdata((struct ID *)tex);
424         BKE_previewimg_free(&tex->preview);
425         BKE_icon_delete((struct ID*)tex);
426         tex->id.icon_id = 0;
427         
428         if(tex->nodetree) {
429                 ntreeFreeTree(tex->nodetree);
430                 MEM_freeN(tex->nodetree);
431         }
432 }
433
434 /* ------------------------------------------------------------------------- */
435
436 void default_tex(Tex *tex)
437 {
438         PluginTex *pit;
439         VarStruct *varstr;
440         int a;
441
442         tex->type= TEX_CLOUDS;
443         tex->stype= 0;
444         tex->flag= TEX_CHECKER_ODD;
445         tex->imaflag= TEX_INTERPOL|TEX_MIPMAP|TEX_USEALPHA;
446         tex->extend= TEX_REPEAT;
447         tex->cropxmin= tex->cropymin= 0.0;
448         tex->cropxmax= tex->cropymax= 1.0;
449         tex->texfilter = TXF_EWA;
450         tex->afmax = 8;
451         tex->xrepeat= tex->yrepeat= 1;
452         tex->fie_ima= 2;
453         tex->sfra= 1;
454         tex->frames= 0;
455         tex->offset= 0;
456         tex->noisesize= 0.25;
457         tex->noisedepth= 2;
458         tex->turbul= 5.0;
459         tex->nabla= 0.025;      // also in do_versions
460         tex->bright= 1.0;
461         tex->contrast= 1.0;
462         tex->filtersize= 1.0;
463         tex->rfac= 1.0;
464         tex->gfac= 1.0;
465         tex->bfac= 1.0;
466         /* newnoise: init. */
467         tex->noisebasis = 0;
468         tex->noisebasis2 = 0;
469         /* musgrave */
470         tex->mg_H = 1.0;
471         tex->mg_lacunarity = 2.0;
472         tex->mg_octaves = 2.0;
473         tex->mg_offset = 1.0;
474         tex->mg_gain = 1.0;
475         tex->ns_outscale = 1.0;
476         /* distnoise */
477         tex->dist_amount = 1.0;
478         /* voronoi */
479         tex->vn_w1 = 1.0;
480         tex->vn_w2 = tex->vn_w3 = tex->vn_w4 = 0.0;
481         tex->vn_mexp = 2.5;
482         tex->vn_distm = 0;
483         tex->vn_coltype = 0;
484
485         if (tex->env) {
486                 tex->env->stype=ENV_STATIC;
487                 tex->env->clipsta=0.1;
488                 tex->env->clipend=100;
489                 tex->env->cuberes=100;
490                 tex->env->depth=0;
491         }
492
493         if (tex->pd) {
494                 tex->pd->radius = 0.3f;
495                 tex->pd->falloff_type = TEX_PD_FALLOFF_STD;
496         }
497         
498         if (tex->vd) {
499                 tex->vd->resol[0] = tex->vd->resol[1] = tex->vd->resol[2] = 0;
500                 tex->vd->interp_type=TEX_VD_LINEAR;
501                 tex->vd->file_format=TEX_VD_SMOKE;
502         }
503         pit = tex->plugin;
504         if (pit) {
505                 varstr= pit->varstr;
506                 if(varstr) {
507                         for(a=0; a<pit->vars; a++, varstr++) {
508                                 pit->data[a] = varstr->def;
509                         }
510                 }
511         }
512         
513         tex->iuser.fie_ima= 2;
514         tex->iuser.ok= 1;
515         tex->iuser.frames= 100;
516         
517         tex->preview = NULL;
518 }
519
520 /* ------------------------------------------------------------------------- */
521
522 Tex *add_texture(const char *name)
523 {
524         Tex *tex;
525
526         tex= alloc_libblock(&G.main->tex, ID_TE, name);
527         
528         default_tex(tex);
529         
530         return tex;
531 }
532
533 /* ------------------------------------------------------------------------- */
534
535 void default_mtex(MTex *mtex)
536 {
537         mtex->texco= TEXCO_ORCO;
538         mtex->mapto= MAP_COL;
539         mtex->object= 0;
540         mtex->projx= PROJ_X;
541         mtex->projy= PROJ_Y;
542         mtex->projz= PROJ_Z;
543         mtex->mapping= MTEX_FLAT;
544         mtex->ofs[0]= 0.0;
545         mtex->ofs[1]= 0.0;
546         mtex->ofs[2]= 0.0;
547         mtex->size[0]= 1.0;
548         mtex->size[1]= 1.0;
549         mtex->size[2]= 1.0;
550         mtex->tex= 0;
551         mtex->texflag= MTEX_NEW_BUMP;
552         mtex->colormodel= 0;
553         mtex->r= 1.0;
554         mtex->g= 0.0;
555         mtex->b= 1.0;
556         mtex->k= 1.0;
557         mtex->def_var= 1.0;
558         mtex->blendtype= MTEX_BLEND;
559         mtex->colfac= 1.0;
560         mtex->norfac= 1.0;
561         mtex->varfac= 1.0;
562         mtex->dispfac=0.2;
563         mtex->colspecfac= 1.0f;
564         mtex->mirrfac= 1.0f;
565         mtex->alphafac= 1.0f;
566         mtex->difffac= 1.0f;
567         mtex->specfac= 1.0f;
568         mtex->emitfac= 1.0f;
569         mtex->hardfac= 1.0f;
570         mtex->raymirrfac= 1.0f;
571         mtex->translfac= 1.0f;
572         mtex->ambfac= 1.0f;
573         mtex->colemitfac= 1.0f;
574         mtex->colreflfac= 1.0f;
575         mtex->coltransfac= 1.0f;
576         mtex->densfac= 1.0f;
577         mtex->scatterfac= 1.0f;
578         mtex->reflfac= 1.0f;
579         mtex->shadowfac= 1.0f;
580         mtex->zenupfac= 1.0f;
581         mtex->zendownfac= 1.0f;
582         mtex->blendfac= 1.0f;
583         mtex->timefac= 1.0f;
584         mtex->lengthfac= 1.0f;
585         mtex->clumpfac= 1.0f;
586         mtex->kinkfac= 1.0f;
587         mtex->roughfac= 1.0f;
588         mtex->padensfac= 1.0f;
589         mtex->lifefac= 1.0f;
590         mtex->sizefac= 1.0f;
591         mtex->ivelfac= 1.0f;
592         mtex->pvelfac= 1.0f;
593         mtex->normapspace= MTEX_NSPACE_TANGENT;
594 }
595
596
597 /* ------------------------------------------------------------------------- */
598
599 MTex *add_mtex()
600 {
601         MTex *mtex;
602         
603         mtex= MEM_callocN(sizeof(MTex), "add_mtex");
604         
605         default_mtex(mtex);
606         
607         return mtex;
608 }
609
610 /* ------------------------------------------------------------------------- */
611
612 Tex *copy_texture(Tex *tex)
613 {
614         Tex *texn;
615         
616         texn= copy_libblock(tex);
617         if(texn->type==TEX_IMAGE) id_us_plus((ID *)texn->ima);
618         else texn->ima= 0;
619         
620 #if 0 // XXX old animation system
621         id_us_plus((ID *)texn->ipo);
622 #endif // XXX old animation system
623         
624         if(texn->plugin) {
625                 texn->plugin= MEM_dupallocN(texn->plugin);
626                 open_plugin_tex(texn->plugin);
627         }
628         
629         if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
630         if(texn->env) texn->env= BKE_copy_envmap(texn->env);
631         
632         if(tex->preview) texn->preview = BKE_previewimg_copy(tex->preview);
633
634         if(tex->nodetree) {
635                 ntreeEndExecTree(tex->nodetree);
636                 texn->nodetree= ntreeCopyTree(tex->nodetree, 0); /* 0 == full new tree */
637         }
638         
639         return texn;
640 }
641
642 /* ------------------------------------------------------------------------- */
643
644 void make_local_texture(Tex *tex)
645 {
646         Tex *texn;
647         Material *ma;
648         World *wrld;
649         Lamp *la;
650         Brush *br;
651         int a, local=0, lib=0;
652
653         /* - only lib users: do nothing
654             * - only local users: set flag
655             * - mixed: make copy
656             */
657         
658         if(tex->id.lib==0) return;
659
660         /* special case: ima always local immediately */
661         if(tex->ima) {
662                 tex->ima->id.lib= 0;
663                 tex->ima->id.flag= LIB_LOCAL;
664                 new_id(0, (ID *)tex->ima, 0);
665         }
666
667         if(tex->id.us==1) {
668                 tex->id.lib= 0;
669                 tex->id.flag= LIB_LOCAL;
670                 new_id(0, (ID *)tex, 0);
671
672                 return;
673         }
674         
675         ma= G.main->mat.first;
676         while(ma) {
677                 for(a=0; a<MAX_MTEX; a++) {
678                         if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
679                                 if(ma->id.lib) lib= 1;
680                                 else local= 1;
681                         }
682                 }
683                 ma= ma->id.next;
684         }
685         la= G.main->lamp.first;
686         while(la) {
687                 for(a=0; a<MAX_MTEX; a++) {
688                         if(la->mtex[a] && la->mtex[a]->tex==tex) {
689                                 if(la->id.lib) lib= 1;
690                                 else local= 1;
691                         }
692                 }
693                 la= la->id.next;
694         }
695         wrld= G.main->world.first;
696         while(wrld) {
697                 for(a=0; a<MAX_MTEX; a++) {
698                         if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
699                                 if(wrld->id.lib) lib= 1;
700                                 else local= 1;
701                         }
702                 }
703                 wrld= wrld->id.next;
704         }
705         br= G.main->brush.first;
706         while(br) {
707                 if(br->mtex.tex==tex) {
708                         if(br->id.lib) lib= 1;
709                         else local= 1;
710                 }
711                 br= br->id.next;
712         }
713         
714         if(local && lib==0) {
715                 tex->id.lib= 0;
716                 tex->id.flag= LIB_LOCAL;
717                 new_id(0, (ID *)tex, 0);
718         }
719         else if(local && lib) {
720                 texn= copy_texture(tex);
721                 texn->id.us= 0;
722                 
723                 ma= G.main->mat.first;
724                 while(ma) {
725                         for(a=0; a<MAX_MTEX; a++) {
726                                 if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
727                                         if(ma->id.lib==0) {
728                                                 ma->mtex[a]->tex= texn;
729                                                 texn->id.us++;
730                                                 tex->id.us--;
731                                         }
732                                 }
733                         }
734                         ma= ma->id.next;
735                 }
736                 la= G.main->lamp.first;
737                 while(la) {
738                         for(a=0; a<MAX_MTEX; a++) {
739                                 if(la->mtex[a] && la->mtex[a]->tex==tex) {
740                                         if(la->id.lib==0) {
741                                                 la->mtex[a]->tex= texn;
742                                                 texn->id.us++;
743                                                 tex->id.us--;
744                                         }
745                                 }
746                         }
747                         la= la->id.next;
748                 }
749                 wrld= G.main->world.first;
750                 while(wrld) {
751                         for(a=0; a<MAX_MTEX; a++) {
752                                 if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
753                                         if(wrld->id.lib==0) {
754                                                 wrld->mtex[a]->tex= texn;
755                                                 texn->id.us++;
756                                                 tex->id.us--;
757                                         }
758                                 }
759                         }
760                         wrld= wrld->id.next;
761                 }
762                 br= G.main->brush.first;
763                 while(br) {
764                         if(br->mtex.tex==tex) {
765                                 if(br->id.lib==0) {
766                                         br->mtex.tex= texn;
767                                         texn->id.us++;
768                                         tex->id.us--;
769                                 }
770                         }
771                         br= br->id.next;
772                 }
773         }
774 }
775
776 /* ------------------------------------------------------------------------- */
777
778 void autotexname(Tex *tex)
779 {
780         char texstr[20][15]= {"None"  , "Clouds" , "Wood", "Marble", "Magic"  , "Blend",
781                 "Stucci", "Noise"  , "Image", "Plugin", "EnvMap" , "Musgrave",
782                 "Voronoi", "DistNoise", "Point Density", "Voxel Data", "", "", "", ""};
783         Image *ima;
784         char di[FILE_MAXDIR], fi[FILE_MAXFILE];
785         
786         if(tex) {
787                 if(tex->use_nodes) {
788                         new_id(&G.main->tex, (ID *)tex, "Noddy");
789                 }
790                 else
791                 if(tex->type==TEX_IMAGE) {
792                         ima= tex->ima;
793                         if(ima) {
794                                 strcpy(di, ima->name);
795                                 BLI_splitdirstring(di, fi);
796                                 strcpy(di, "I.");
797                                 strcat(di, fi);
798                                 new_id(&G.main->tex, (ID *)tex, di);
799                         }
800                         else new_id(&G.main->tex, (ID *)tex, texstr[tex->type]);
801                 }
802                 else if(tex->type==TEX_PLUGIN && tex->plugin) new_id(&G.main->tex, (ID *)tex, tex->plugin->pname);
803                 else new_id(&G.main->tex, (ID *)tex, texstr[tex->type]);
804         }
805 }
806
807 /* ------------------------------------------------------------------------- */
808
809 Tex *give_current_object_texture(Object *ob)
810 {
811         Material *ma;
812         Tex *tex= NULL;
813         
814         if(ob==0) return 0;
815         if(ob->totcol==0 && !(ob->type==OB_LAMP)) return 0;
816         
817         if(ob->type==OB_LAMP) {
818                 tex= give_current_lamp_texture(ob->data);
819         } else {
820                 ma= give_current_material(ob, ob->actcol);
821                 tex= give_current_material_texture(ma);
822         }
823         
824         return tex;
825 }
826
827 Tex *give_current_lamp_texture(Lamp *la)
828 {
829         MTex *mtex= NULL;
830         Tex *tex= NULL;
831
832         if(la) {
833                 mtex= la->mtex[(int)(la->texact)];
834                 if(mtex) tex= mtex->tex;
835         }
836
837         return tex;
838 }
839
840 void set_current_lamp_texture(Lamp *la, Tex *newtex)
841 {
842         int act= la->texact;
843
844         if(la->mtex[act] && la->mtex[act]->tex)
845                 id_us_min(&la->mtex[act]->tex->id);
846
847         if(newtex) {
848                 if(!la->mtex[act]) {
849                         la->mtex[act]= add_mtex();
850                         la->mtex[act]->texco= TEXCO_GLOB;
851                 }
852                 
853                 la->mtex[act]->tex= newtex;
854                 id_us_plus(&newtex->id);
855         }
856         else if(la->mtex[act]) {
857                 MEM_freeN(la->mtex[act]);
858                 la->mtex[act]= NULL;
859         }
860 }
861
862 bNode *give_current_material_texture_node(Material *ma)
863 {
864         if(ma && ma->use_nodes && ma->nodetree)
865                 return nodeGetActiveID(ma->nodetree, ID_TE);
866         
867         return NULL;
868 }
869
870 Tex *give_current_material_texture(Material *ma)
871 {
872         MTex *mtex= NULL;
873         Tex *tex= NULL;
874         bNode *node;
875         
876         if(ma && ma->use_nodes && ma->nodetree) {
877                 /* first check texture, then material, this works together
878                    with a hack that clears the active ID flag for textures on
879                    making a material node active */
880                 node= nodeGetActiveID(ma->nodetree, ID_TE);
881
882                 if(node) {
883                         tex= (Tex *)node->id;
884                         ma= NULL;
885                 }
886                 else {
887                         node= nodeGetActiveID(ma->nodetree, ID_MA);
888                         if(node)
889                                 ma= (Material*)node->id;
890                 }
891         }
892
893         if(ma) {
894                 mtex= ma->mtex[(int)(ma->texact)];
895                 if(mtex) tex= mtex->tex;
896         }
897         
898         return tex;
899 }
900
901 int give_active_mtex(ID *id, MTex ***mtex_ar, short *act)
902 {
903         switch(GS(id->name)) {
904         case ID_MA:
905                 *mtex_ar=               ((Material *)id)->mtex;
906                 if(act) *act=   (((Material *)id)->texact);
907                 break;
908         case ID_WO:
909                 *mtex_ar=               ((World *)id)->mtex;
910                 if(act) *act=   (((World *)id)->texact);
911                 break;
912         case ID_LA:
913                 *mtex_ar=               ((Lamp *)id)->mtex;
914                 if(act) *act=   (((Lamp *)id)->texact);
915                 break;
916         default:
917                 *mtex_ar = NULL;
918                 if(act) *act=   0;
919                 return FALSE;
920         }
921
922         return TRUE;
923 }
924
925 void set_active_mtex(ID *id, short act)
926 {
927         if(act<0)                               act= 0;
928         else if(act>=MAX_MTEX)  act= MAX_MTEX-1;
929
930         switch(GS(id->name)) {
931         case ID_MA:
932                 ((Material *)id)->texact= act;
933                 break;
934         case ID_WO:
935                 ((World *)id)->texact= act;
936                 break;
937         case ID_LA:
938                 ((Lamp *)id)->texact= act;
939                 break;
940         }
941 }
942
943 void set_current_material_texture(Material *ma, Tex *newtex)
944 {
945         Tex *tex= NULL;
946         bNode *node;
947         
948         if(ma && ma->use_nodes && ma->nodetree) {
949                 node= nodeGetActiveID(ma->nodetree, ID_TE);
950
951                 if(node) {
952                         tex= (Tex *)node->id;
953                         id_us_min(&tex->id);
954                         node->id= &newtex->id;
955                         id_us_plus(&newtex->id);
956                         ma= NULL;
957                 }
958                 else {
959                         node= nodeGetActiveID(ma->nodetree, ID_MA);
960                         if(node)
961                                 ma= (Material*)node->id;
962                 }
963         }
964         if(ma) {
965                 int act= (int)ma->texact;
966
967                 tex= (ma->mtex[act])? ma->mtex[act]->tex: NULL;
968                 id_us_min(&tex->id);
969
970                 if(newtex) {
971                         if(!ma->mtex[act])
972                                 ma->mtex[act]= add_mtex();
973                         
974                         ma->mtex[act]->tex= newtex;
975                         id_us_plus(&newtex->id);
976                 }
977                 else if(ma->mtex[act]) {
978                         MEM_freeN(ma->mtex[act]);
979                         ma->mtex[act]= NULL;
980                 }
981         }
982 }
983
984 Tex *give_current_world_texture(World *world)
985 {
986         MTex *mtex= NULL;
987         Tex *tex= NULL;
988         
989         if(!world) return 0;
990         
991         mtex= world->mtex[(int)(world->texact)];
992         if(mtex) tex= mtex->tex;
993         
994         return tex;
995 }
996
997 void set_current_world_texture(World *wo, Tex *newtex)
998 {
999         int act= wo->texact;
1000
1001         if(wo->mtex[act] && wo->mtex[act]->tex)
1002                 id_us_min(&wo->mtex[act]->tex->id);
1003
1004         if(newtex) {
1005                 if(!wo->mtex[act]) {
1006                         wo->mtex[act]= add_mtex();
1007                         wo->mtex[act]->texco= TEXCO_VIEW;
1008                 }
1009                 
1010                 wo->mtex[act]->tex= newtex;
1011                 id_us_plus(&newtex->id);
1012         }
1013         else if(wo->mtex[act]) {
1014                 MEM_freeN(wo->mtex[act]);
1015                 wo->mtex[act]= NULL;
1016         }
1017 }
1018
1019 Tex *give_current_brush_texture(Brush *br)
1020 {
1021         return br->mtex.tex;
1022 }
1023
1024 void set_current_brush_texture(Brush *br, Tex *newtex)
1025 {
1026         if(br->mtex.tex)
1027                 id_us_min(&br->mtex.tex->id);
1028
1029         if(newtex) {
1030                 br->mtex.tex= newtex;
1031                 id_us_plus(&newtex->id);
1032         }
1033 }
1034
1035 /* ------------------------------------------------------------------------- */
1036
1037 EnvMap *BKE_add_envmap(void)
1038 {
1039         EnvMap *env;
1040         
1041         env= MEM_callocN(sizeof(EnvMap), "envmap");
1042         env->type= ENV_CUBE;
1043         env->stype= ENV_STATIC;
1044         env->clipsta= 0.1;
1045         env->clipend= 100.0;
1046         env->cuberes= 100;
1047         
1048         return env;
1049
1050
1051 /* ------------------------------------------------------------------------- */
1052
1053 EnvMap *BKE_copy_envmap(EnvMap *env)
1054 {
1055         EnvMap *envn;
1056         int a;
1057         
1058         envn= MEM_dupallocN(env);
1059         envn->ok= 0;
1060         for(a=0; a<6; a++) envn->cube[a]= NULL;
1061         if(envn->ima) id_us_plus((ID *)envn->ima);
1062         
1063         return envn;
1064 }
1065
1066 /* ------------------------------------------------------------------------- */
1067
1068 void BKE_free_envmapdata(EnvMap *env)
1069 {
1070         unsigned int part;
1071         
1072         for(part=0; part<6; part++) {
1073                 if(env->cube[part])
1074                         IMB_freeImBuf(env->cube[part]);
1075                 env->cube[part]= NULL;
1076         }
1077         env->ok= 0;
1078 }
1079
1080 /* ------------------------------------------------------------------------- */
1081
1082 void BKE_free_envmap(EnvMap *env)
1083 {
1084         
1085         BKE_free_envmapdata(env);
1086         MEM_freeN(env);
1087         
1088 }
1089
1090 /* ------------------------------------------------------------------------- */
1091
1092 PointDensity *BKE_add_pointdensity(void)
1093 {
1094         PointDensity *pd;
1095         
1096         pd= MEM_callocN(sizeof(PointDensity), "pointdensity");
1097         pd->flag = 0;
1098         pd->radius = 0.3f;
1099         pd->falloff_type = TEX_PD_FALLOFF_STD;
1100         pd->falloff_softness = 2.0;
1101         pd->source = TEX_PD_PSYS;
1102         pd->point_tree = NULL;
1103         pd->point_data = NULL;
1104         pd->noise_size = 0.5f;
1105         pd->noise_depth = 1;
1106         pd->noise_fac = 1.0f;
1107         pd->noise_influence = TEX_PD_NOISE_STATIC;
1108         pd->coba = add_colorband(1);
1109         pd->speed_scale = 1.0f;
1110         pd->totpoints = 0;
1111         pd->object = NULL;
1112         pd->psys = 0;
1113         return pd;
1114
1115
1116 PointDensity *BKE_copy_pointdensity(PointDensity *pd)
1117 {
1118         PointDensity *pdn;
1119
1120         pdn= MEM_dupallocN(pd);
1121         pdn->point_tree = NULL;
1122         pdn->point_data = NULL;
1123         if(pdn->coba) pdn->coba= MEM_dupallocN(pdn->coba);
1124         
1125         return pdn;
1126 }
1127
1128 void BKE_free_pointdensitydata(PointDensity *pd)
1129 {
1130         if (pd->point_tree) {
1131                 BLI_bvhtree_free(pd->point_tree);
1132                 pd->point_tree = NULL;
1133         }
1134         if (pd->point_data) {
1135                 MEM_freeN(pd->point_data);
1136                 pd->point_data = NULL;
1137         }
1138         if(pd->coba) MEM_freeN(pd->coba);
1139 }
1140
1141 void BKE_free_pointdensity(PointDensity *pd)
1142 {
1143         BKE_free_pointdensitydata(pd);
1144         MEM_freeN(pd);
1145 }
1146
1147
1148 void BKE_free_voxeldatadata(struct VoxelData *vd)
1149 {
1150         if (vd->dataset) {
1151                 MEM_freeN(vd->dataset);
1152                 vd->dataset = NULL;
1153         }
1154
1155 }
1156  
1157 void BKE_free_voxeldata(struct VoxelData *vd)
1158 {
1159         BKE_free_voxeldatadata(vd);
1160         MEM_freeN(vd);
1161 }
1162  
1163 struct VoxelData *BKE_add_voxeldata(void)
1164 {
1165         VoxelData *vd;
1166
1167         vd= MEM_callocN(sizeof(struct VoxelData), "voxeldata");
1168         vd->dataset = NULL;
1169         vd->resol[0] = vd->resol[1] = vd->resol[2] = 1;
1170         vd->interp_type= TEX_VD_LINEAR;
1171         vd->file_format= TEX_VD_SMOKE;
1172         vd->int_multiplier = 1.0;
1173         vd->extend = TEX_CLIP;
1174         vd->object = NULL;
1175         
1176         return vd;
1177  }
1178  
1179 struct VoxelData *BKE_copy_voxeldata(struct VoxelData *vd)
1180 {
1181         VoxelData *vdn;
1182
1183         vdn= MEM_dupallocN(vd); 
1184         vdn->dataset = NULL;
1185
1186         return vdn;
1187 }
1188
1189
1190 /* ------------------------------------------------------------------------- */
1191 int BKE_texture_dependsOnTime(const struct Tex *texture)
1192 {
1193         if(texture->plugin) {
1194                 // assume all plugins depend on time
1195                 return 1;
1196         } 
1197         else if(        texture->ima && 
1198                         ELEM(texture->ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
1199                 return 1;
1200         } 
1201 #if 0 // XXX old animation system
1202         else if(texture->ipo) {
1203                 // assume any ipo means the texture is animated
1204                 return 1;
1205         }
1206 #endif // XXX old animation system
1207         return 0;
1208 }
1209
1210 /* ------------------------------------------------------------------------- */