Fix bug #19699: point density texture doesn't save particle system.
[blender.git] / source / blender / blenkernel / intern / texture.c
1 /* texture.c
2  *
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <math.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "PIL_dynlib.h"
40
41
42
43 #include "BLI_blenlib.h"
44 #include "BLI_arithb.h"
45 #include "BLI_rand.h"
46 #include "BLI_kdopbvh.h"
47
48 #include "DNA_texture_types.h"
49 #include "DNA_key_types.h"
50 #include "DNA_object_types.h"
51 #include "DNA_lamp_types.h"
52 #include "DNA_material_types.h"
53 #include "DNA_image_types.h"
54 #include "DNA_world_types.h"
55 #include "DNA_brush_types.h"
56 #include "DNA_node_types.h"
57 #include "DNA_color_types.h"
58 #include "DNA_scene_types.h"
59
60 #include "IMB_imbuf_types.h"
61 #include "IMB_imbuf.h"
62
63 #include "BKE_plugin_types.h"
64
65 #include "BKE_utildefines.h"
66
67 #include "BKE_global.h"
68 #include "BKE_main.h"
69
70 #include "BKE_library.h"
71 #include "BKE_image.h"
72 #include "BKE_material.h"
73 #include "BKE_texture.h"
74 #include "BKE_key.h"
75 #include "BKE_icons.h"
76 #include "BKE_brush.h"
77 #include "BKE_node.h"
78 #include "BKE_animsys.h"
79
80
81 /* ------------------------------------------------------------------------- */
82
83 /* All support for plugin textures: */
84 int test_dlerr(const char *name, const char *symbol)
85 {
86         char *err;
87         
88         err= PIL_dynlib_get_error_as_string(NULL);
89         if(err) {
90                 printf("var1: %s, var2: %s, var3: %s\n", name, symbol, err);
91                 return 1;
92         }
93         
94         return 0;
95 }
96
97 /* ------------------------------------------------------------------------- */
98
99 void open_plugin_tex(PluginTex *pit)
100 {
101         int (*version)(void);
102         
103         /* init all the happy variables */
104         pit->doit= 0;
105         pit->pname= 0;
106         pit->stnames= 0;
107         pit->varstr= 0;
108         pit->result= 0;
109         pit->cfra= 0;
110         pit->version= 0;
111         pit->instance_init= 0;
112         
113         /* clear the error list */
114         PIL_dynlib_get_error_as_string(NULL);
115
116         /* no PIL_dynlib_close! multiple opened plugins... */
117         /* if(pit->handle) PIL_dynlib_close(pit->handle); */
118         /* pit->handle= 0; */
119
120         /* open the needed object */
121         pit->handle= PIL_dynlib_open(pit->name);
122         if(test_dlerr(pit->name, pit->name)) return;
123
124         if (pit->handle != 0) {
125                 /* find the address of the version function */
126                 version= (int (*)(void)) PIL_dynlib_find_symbol(pit->handle, "plugin_tex_getversion");
127                 if (test_dlerr(pit->name, "plugin_tex_getversion")) return;
128                 
129                 if (version != 0) {
130                         pit->version= version();
131                         if( pit->version >= 2 && pit->version <=6) {
132                                 int (*info_func)(PluginInfo *);
133                                 PluginInfo *info= (PluginInfo*) MEM_mallocN(sizeof(PluginInfo), "plugin_info"); 
134
135                                 info_func= (int (*)(PluginInfo *))PIL_dynlib_find_symbol(pit->handle, "plugin_getinfo");
136                                 if (!test_dlerr(pit->name, "plugin_getinfo")) {
137                                         info->instance_init = NULL;
138
139                                         info_func(info);
140
141                                         pit->doit= (int(*)(void)) info->tex_doit;
142                                         pit->callback= (void(*)(unsigned short)) info->callback;
143                                         pit->stypes= info->stypes;
144                                         pit->vars= info->nvars;
145                                         pit->pname= info->name;
146                                         pit->stnames= info->snames;
147                                         pit->varstr= info->varstr;
148                                         pit->result= info->result;
149                                         pit->cfra= info->cfra;
150                                         pit->instance_init = info->instance_init;
151                                         if (info->init) info->init();
152                                 }
153                                 MEM_freeN(info);
154                         } else {
155                                 printf ("Plugin returned unrecognized version number\n");
156                                 return;
157                         }
158                 }
159         }
160 }
161
162 /* ------------------------------------------------------------------------- */
163
164 /* very badlevel define to bypass linking with BIF_interface.h */
165 #define INT     96
166 #define FLO     128
167
168 PluginTex *add_plugin_tex(char *str)
169 {
170         PluginTex *pit;
171         VarStruct *varstr;
172         int a;
173         
174         pit= MEM_callocN(sizeof(PluginTex), "plugintex");
175         
176         strcpy(pit->name, str);
177         open_plugin_tex(pit);
178         
179         if(pit->doit==0) {
180                 if(pit->handle==0); //XXX error("no plugin: %s", str);
181                 else ; //XXX error("in plugin: %s", str);
182                 MEM_freeN(pit);
183                 return NULL;
184         }
185         
186         varstr= pit->varstr;
187         for(a=0; a<pit->vars; a++, varstr++) {
188                 if( (varstr->type & FLO)==FLO)
189                         pit->data[a]= varstr->def;
190                 else if( (varstr->type & INT)==INT)
191                         *((int *)(pit->data+a))= (int) varstr->def;
192         }
193
194         if (pit->instance_init)
195                 pit->instance_init((void *) pit->data);
196
197         return pit;
198 }
199
200 /* ------------------------------------------------------------------------- */
201
202 void free_plugin_tex(PluginTex *pit)
203 {
204         if(pit==0) return;
205                 
206         /* no PIL_dynlib_close: same plugin can be opened multiple times, 1 handle */
207         MEM_freeN(pit); 
208 }
209
210 /* ****************** Mapping ******************* */
211
212 TexMapping *add_mapping(void)
213 {
214         TexMapping *texmap= MEM_callocN(sizeof(TexMapping), "Tex map");
215         
216         texmap->size[0]= texmap->size[1]= texmap->size[2]= 1.0f;
217         texmap->max[0]= texmap->max[1]= texmap->max[2]= 1.0f;
218         Mat4One(texmap->mat);
219         
220         return texmap;
221 }
222
223 void init_mapping(TexMapping *texmap)
224 {
225         float eul[3], smat[3][3], rmat[3][3], mat[3][3];
226         
227         SizeToMat3(texmap->size, smat);
228         
229         eul[0]= (M_PI/180.0f)*texmap->rot[0];
230         eul[1]= (M_PI/180.0f)*texmap->rot[1];
231         eul[2]= (M_PI/180.0f)*texmap->rot[2];
232         EulToMat3(eul, rmat);
233         
234         Mat3MulMat3(mat, rmat, smat);
235         
236         Mat4CpyMat3(texmap->mat, mat);
237         VECCOPY(texmap->mat[3], texmap->loc);
238
239 }
240
241 /* ****************** COLORBAND ******************* */
242
243 void init_colorband(ColorBand *coba, int rangetype)
244 {
245         int a;
246         
247         coba->data[0].pos= 0.0;
248         coba->data[1].pos= 1.0;
249         
250         if(rangetype==0) {
251                 coba->data[0].r= 0.0;
252                 coba->data[0].g= 0.0;
253                 coba->data[0].b= 0.0;
254                 coba->data[0].a= 0.0;
255                 
256                 coba->data[1].r= 0.0;
257                 coba->data[1].g= 1.0;
258                 coba->data[1].b= 1.0;
259                 coba->data[1].a= 1.0;
260         }
261         else {
262                 coba->data[0].r= 0.0;
263                 coba->data[0].g= 0.0;
264                 coba->data[0].b= 0.0;
265                 coba->data[0].a= 1.0;
266                 
267                 coba->data[1].r= 1.0;
268                 coba->data[1].g= 1.0;
269                 coba->data[1].b= 1.0;
270                 coba->data[1].a= 1.0;
271         }
272         
273         for(a=2; a<MAXCOLORBAND; a++) {
274                 coba->data[a].r= 0.5;
275                 coba->data[a].g= 0.5;
276                 coba->data[a].b= 0.5;
277                 coba->data[a].a= 1.0;
278                 coba->data[a].pos= 0.5;
279         }
280         
281         coba->tot= 2;
282         
283 }
284
285 ColorBand *add_colorband(int rangetype)
286 {
287         ColorBand *coba;
288         
289         coba= MEM_callocN( sizeof(ColorBand), "colorband");
290         init_colorband(coba, rangetype);
291         
292         return coba;
293 }
294
295 /* ------------------------------------------------------------------------- */
296
297 int do_colorband(ColorBand *coba, float in, float out[4])
298 {
299         CBData *cbd1, *cbd2, *cbd0, *cbd3;
300         float fac, mfac, t[4];
301         int a;
302         
303         if(coba==NULL || coba->tot==0) return 0;
304         
305         cbd1= coba->data;
306         if(coba->tot==1) {
307                 out[0]= cbd1->r;
308                 out[1]= cbd1->g;
309                 out[2]= cbd1->b;
310                 out[3]= cbd1->a;
311         }
312         else {
313                 if(in <= cbd1->pos && coba->ipotype<2) {
314                         out[0]= cbd1->r;
315                         out[1]= cbd1->g;
316                         out[2]= cbd1->b;
317                         out[3]= cbd1->a;
318                 }
319                 else {
320                         CBData left, right;
321                         
322                         /* we're looking for first pos > in */
323                         for(a=0; a<coba->tot; a++, cbd1++) if(cbd1->pos > in) break;
324                                 
325                         if(a==coba->tot) {
326                                 cbd2= cbd1-1;
327                                 right= *cbd2;
328                                 right.pos= 1.0f;
329                                 cbd1= &right;
330                         }
331                         else if(a==0) {
332                                 left= *cbd1;
333                                 left.pos= 0.0f;
334                                 cbd2= &left;
335                         }
336                         else cbd2= cbd1-1;
337                         
338                         if(in >= cbd1->pos && coba->ipotype<2) {
339                                 out[0]= cbd1->r;
340                                 out[1]= cbd1->g;
341                                 out[2]= cbd1->b;
342                                 out[3]= cbd1->a;
343                         }
344                         else {
345                 
346                                 if(cbd2->pos!=cbd1->pos)
347                                         fac= (in-cbd1->pos)/(cbd2->pos-cbd1->pos);
348                                 else
349                                         fac= 0.0f;
350                                 
351                                 if (coba->ipotype==4) {
352                                         /* constant */
353                                         out[0]= cbd2->r;
354                                         out[1]= cbd2->g;
355                                         out[2]= cbd2->b;
356                                         out[3]= cbd2->a;
357                                         return 1;
358                                 }
359                                 
360                                 if(coba->ipotype>=2) {
361                                         /* ipo from right to left: 3 2 1 0 */
362                                         
363                                         if(a>=coba->tot-1) cbd0= cbd1;
364                                         else cbd0= cbd1+1;
365                                         if(a<2) cbd3= cbd2;
366                                         else cbd3= cbd2-1;
367                                         
368                                         CLAMP(fac, 0.0f, 1.0f);
369                                         
370                                         if(coba->ipotype==3)
371                                                 key_curve_position_weights(fac, t, KEY_CARDINAL);
372                                         else
373                                                 key_curve_position_weights(fac, t, KEY_BSPLINE);
374
375                                         out[0]= t[3]*cbd3->r +t[2]*cbd2->r +t[1]*cbd1->r +t[0]*cbd0->r;
376                                         out[1]= t[3]*cbd3->g +t[2]*cbd2->g +t[1]*cbd1->g +t[0]*cbd0->g;
377                                         out[2]= t[3]*cbd3->b +t[2]*cbd2->b +t[1]*cbd1->b +t[0]*cbd0->b;
378                                         out[3]= t[3]*cbd3->a +t[2]*cbd2->a +t[1]*cbd1->a +t[0]*cbd0->a;
379                                         CLAMP(out[0], 0.0, 1.0);
380                                         CLAMP(out[1], 0.0, 1.0);
381                                         CLAMP(out[2], 0.0, 1.0);
382                                         CLAMP(out[3], 0.0, 1.0);
383                                 }
384                                 else {
385                                 
386                                         if(coba->ipotype==1) {  /* EASE */
387                                                 mfac= fac*fac;
388                                                 fac= 3.0f*mfac-2.0f*mfac*fac;
389                                         }
390                                         mfac= 1.0f-fac;
391                                         
392                                         out[0]= mfac*cbd1->r + fac*cbd2->r;
393                                         out[1]= mfac*cbd1->g + fac*cbd2->g;
394                                         out[2]= mfac*cbd1->b + fac*cbd2->b;
395                                         out[3]= mfac*cbd1->a + fac*cbd2->a;
396                                 }
397                         }
398                 }
399         }
400         return 1;       /* OK */
401 }
402
403 void colorband_table_RGBA(ColorBand *coba, float **array, int *size)
404 {
405         int a;
406         
407         *size = CM_TABLE+1;
408         *array = MEM_callocN(sizeof(float)*(*size)*4, "ColorBand");
409
410         for(a=0; a<*size; a++)
411                 do_colorband(coba, (float)a/(float)CM_TABLE, &(*array)[a*4]);
412 }
413
414 /* ******************* TEX ************************ */
415
416 void free_texture(Tex *tex)
417 {
418         free_plugin_tex(tex->plugin);
419         if(tex->coba) MEM_freeN(tex->coba);
420         if(tex->env) BKE_free_envmap(tex->env);
421         if(tex->pd) BKE_free_pointdensity(tex->pd);
422         if(tex->vd) BKE_free_voxeldata(tex->vd);
423         BKE_previewimg_free(&tex->preview);
424         BKE_icon_delete((struct ID*)tex);
425         tex->id.icon_id = 0;
426         
427         if(tex->nodetree) {
428                 ntreeFreeTree(tex->nodetree);
429                 MEM_freeN(tex->nodetree);
430         }
431 }
432
433 /* ------------------------------------------------------------------------- */
434
435 void default_tex(Tex *tex)
436 {
437         PluginTex *pit;
438         VarStruct *varstr;
439         int a;
440
441         tex->type= TEX_CLOUDS;
442         tex->stype= 0;
443         tex->flag= TEX_CHECKER_ODD;
444         tex->imaflag= TEX_INTERPOL|TEX_MIPMAP|TEX_USEALPHA;
445         tex->extend= TEX_REPEAT;
446         tex->cropxmin= tex->cropymin= 0.0;
447         tex->cropxmax= tex->cropymax= 1.0;
448         tex->texfilter = TXF_EWA;
449         tex->afmax = 8;
450         tex->xrepeat= tex->yrepeat= 1;
451         tex->fie_ima= 2;
452         tex->sfra= 1;
453         tex->frames= 0;
454         tex->offset= 0;
455         tex->noisesize= 0.25;
456         tex->noisedepth= 2;
457         tex->turbul= 5.0;
458         tex->nabla= 0.025;      // also in do_versions
459         tex->bright= 1.0;
460         tex->contrast= 1.0;
461         tex->filtersize= 1.0;
462         tex->rfac= 1.0;
463         tex->gfac= 1.0;
464         tex->bfac= 1.0;
465         /* newnoise: init. */
466         tex->noisebasis = 0;
467         tex->noisebasis2 = 0;
468         /* musgrave */
469         tex->mg_H = 1.0;
470         tex->mg_lacunarity = 2.0;
471         tex->mg_octaves = 2.0;
472         tex->mg_offset = 1.0;
473         tex->mg_gain = 1.0;
474         tex->ns_outscale = 1.0;
475         /* distnoise */
476         tex->dist_amount = 1.0;
477         /* voronoi */
478         tex->vn_w1 = 1.0;
479         tex->vn_w2 = tex->vn_w3 = tex->vn_w4 = 0.0;
480         tex->vn_mexp = 2.5;
481         tex->vn_distm = 0;
482         tex->vn_coltype = 0;
483
484         if (tex->env) {
485                 tex->env->stype=ENV_STATIC;
486                 tex->env->clipsta=0.1;
487                 tex->env->clipend=100;
488                 tex->env->cuberes=100;
489                 tex->env->depth=0;
490         }
491
492         if (tex->pd) {
493                 tex->pd->radius = 0.3f;
494                 tex->pd->falloff_type = TEX_PD_FALLOFF_STD;
495         }
496         
497         if (tex->vd) {
498                 tex->vd->resol[0] = tex->vd->resol[1] = tex->vd->resol[2] = 0;
499                 tex->vd->interp_type=TEX_VD_LINEAR;
500                 tex->vd->file_format=TEX_VD_SMOKE;
501         }
502         pit = tex->plugin;
503         if (pit) {
504                 varstr= pit->varstr;
505                 if(varstr) {
506                         for(a=0; a<pit->vars; a++, varstr++) {
507                                 pit->data[a] = varstr->def;
508                         }
509                 }
510         }
511         
512         tex->iuser.fie_ima= 2;
513         tex->iuser.ok= 1;
514         tex->iuser.frames= 100;
515         
516         tex->preview = NULL;
517 }
518
519 /* ------------------------------------------------------------------------- */
520
521 Tex *add_texture(char *name)
522 {
523         Tex *tex;
524
525         tex= alloc_libblock(&G.main->tex, ID_TE, name);
526         
527         default_tex(tex);
528         
529         return tex;
530 }
531
532 /* ------------------------------------------------------------------------- */
533
534 void default_mtex(MTex *mtex)
535 {
536         mtex->texco= TEXCO_ORCO;
537         mtex->mapto= MAP_COL;
538         mtex->object= 0;
539         mtex->projx= PROJ_X;
540         mtex->projy= PROJ_Y;
541         mtex->projz= PROJ_Z;
542         mtex->mapping= MTEX_FLAT;
543         mtex->ofs[0]= 0.0;
544         mtex->ofs[1]= 0.0;
545         mtex->ofs[2]= 0.0;
546         mtex->size[0]= 1.0;
547         mtex->size[1]= 1.0;
548         mtex->size[2]= 1.0;
549         mtex->tex= 0;
550         mtex->texflag= MTEX_NEW_BUMP;
551         mtex->colormodel= 0;
552         mtex->r= 1.0;
553         mtex->g= 0.0;
554         mtex->b= 1.0;
555         mtex->k= 1.0;
556         mtex->def_var= 1.0;
557         mtex->blendtype= MTEX_BLEND;
558         mtex->colfac= 1.0;
559         mtex->norfac= 1.0;
560         mtex->varfac= 1.0;
561         mtex->dispfac=0.2;
562         mtex->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                 for(a=0; a<MAX_MTEX; a++) {
707                         if(br->mtex[a] && br->mtex[a]->tex==tex) {
708                                 if(br->id.lib) lib= 1;
709                                 else local= 1;
710                         }
711                 }
712                 br= br->id.next;
713         }
714         
715         if(local && lib==0) {
716                 tex->id.lib= 0;
717                 tex->id.flag= LIB_LOCAL;
718                 new_id(0, (ID *)tex, 0);
719         }
720         else if(local && lib) {
721                 texn= copy_texture(tex);
722                 texn->id.us= 0;
723                 
724                 ma= G.main->mat.first;
725                 while(ma) {
726                         for(a=0; a<MAX_MTEX; a++) {
727                                 if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
728                                         if(ma->id.lib==0) {
729                                                 ma->mtex[a]->tex= texn;
730                                                 texn->id.us++;
731                                                 tex->id.us--;
732                                         }
733                                 }
734                         }
735                         ma= ma->id.next;
736                 }
737                 la= G.main->lamp.first;
738                 while(la) {
739                         for(a=0; a<MAX_MTEX; a++) {
740                                 if(la->mtex[a] && la->mtex[a]->tex==tex) {
741                                         if(la->id.lib==0) {
742                                                 la->mtex[a]->tex= texn;
743                                                 texn->id.us++;
744                                                 tex->id.us--;
745                                         }
746                                 }
747                         }
748                         la= la->id.next;
749                 }
750                 wrld= G.main->world.first;
751                 while(wrld) {
752                         for(a=0; a<MAX_MTEX; a++) {
753                                 if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
754                                         if(wrld->id.lib==0) {
755                                                 wrld->mtex[a]->tex= texn;
756                                                 texn->id.us++;
757                                                 tex->id.us--;
758                                         }
759                                 }
760                         }
761                         wrld= wrld->id.next;
762                 }
763                 br= G.main->brush.first;
764                 while(br) {
765                         for(a=0; a<MAX_MTEX; a++) {
766                                 if(br->mtex[a] && br->mtex[a]->tex==tex) {
767                                         if(br->id.lib==0) {
768                                                 br->mtex[a]->tex= texn;
769                                                 texn->id.us++;
770                                                 tex->id.us--;
771                                         }
772                                 }
773                         }
774                         br= br->id.next;
775                 }
776         }
777 }
778
779 /* ------------------------------------------------------------------------- */
780
781 void autotexname(Tex *tex)
782 {
783         char texstr[20][15]= {"None"  , "Clouds" , "Wood", "Marble", "Magic"  , "Blend",
784                 "Stucci", "Noise"  , "Image", "Plugin", "EnvMap" , "Musgrave",
785                 "Voronoi", "DistNoise", "Point Density", "Voxel Data", "", "", "", ""};
786         Image *ima;
787         char di[FILE_MAXDIR], fi[FILE_MAXFILE];
788         
789         if(tex) {
790                 if(tex->use_nodes) {
791                         new_id(&G.main->tex, (ID *)tex, "Noddy");
792                 }
793                 else
794                 if(tex->type==TEX_IMAGE) {
795                         ima= tex->ima;
796                         if(ima) {
797                                 strcpy(di, ima->name);
798                                 BLI_splitdirstring(di, fi);
799                                 strcpy(di, "I.");
800                                 strcat(di, fi);
801                                 new_id(&G.main->tex, (ID *)tex, di);
802                         }
803                         else new_id(&G.main->tex, (ID *)tex, texstr[tex->type]);
804                 }
805                 else if(tex->type==TEX_PLUGIN && tex->plugin) new_id(&G.main->tex, (ID *)tex, tex->plugin->pname);
806                 else new_id(&G.main->tex, (ID *)tex, texstr[tex->type]);
807         }
808 }
809
810 /* ------------------------------------------------------------------------- */
811
812 Tex *give_current_object_texture(Object *ob)
813 {
814         Material *ma;
815         Tex *tex= NULL;
816         
817         if(ob==0) return 0;
818         if(ob->totcol==0 && !(ob->type==OB_LAMP)) return 0;
819         
820         if(ob->type==OB_LAMP) {
821                 tex= give_current_lamp_texture(ob->data);
822         } else {
823                 ma= give_current_material(ob, ob->actcol);
824                 tex= give_current_material_texture(ma);
825         }
826         
827         return tex;
828 }
829
830 Tex *give_current_lamp_texture(Lamp *la)
831 {
832         MTex *mtex= NULL;
833         Tex *tex= NULL;
834
835         if(la) {
836                 mtex= la->mtex[(int)(la->texact)];
837                 if(mtex) tex= mtex->tex;
838         }
839
840         return tex;
841 }
842
843 void set_current_lamp_texture(Lamp *la, Tex *newtex)
844 {
845         int act= la->texact;
846
847         if(la->mtex[act] && la->mtex[act]->tex)
848                 id_us_min(&la->mtex[act]->tex->id);
849
850         if(newtex) {
851                 if(!la->mtex[act]) {
852                         la->mtex[act]= add_mtex();
853                         la->mtex[act]->texco= TEXCO_GLOB;
854                 }
855                 
856                 la->mtex[act]->tex= newtex;
857                 id_us_plus(&newtex->id);
858         }
859         else if(la->mtex[act]) {
860                 MEM_freeN(la->mtex[act]);
861                 la->mtex[act]= NULL;
862         }
863 }
864
865 Tex *give_current_material_texture(Material *ma)
866 {
867         MTex *mtex= NULL;
868         Tex *tex= NULL;
869         bNode *node;
870         
871         if(ma && ma->use_nodes && ma->nodetree) {
872                 node= nodeGetActiveID(ma->nodetree, ID_TE);
873
874                 if(node) {
875                         tex= (Tex *)node->id;
876                         ma= NULL;
877                 }
878                 else {
879                         node= nodeGetActiveID(ma->nodetree, ID_MA);
880                         if(node)
881                                 ma= (Material*)node->id;
882                 }
883         }
884         if(ma) {
885                 mtex= ma->mtex[(int)(ma->texact)];
886                 if(mtex) tex= mtex->tex;
887         }
888         
889         return tex;
890 }
891
892 int give_active_mtex(ID *id, MTex ***mtex_ar, short *act)
893 {
894         switch(GS(id->name)) {
895         case ID_MA:
896                 *mtex_ar=               ((Material *)id)->mtex;
897                 if(act) *act=   (((Material *)id)->texact);
898                 break;
899         case ID_WO:
900                 *mtex_ar=               ((World *)id)->mtex;
901                 if(act) *act=   (((World *)id)->texact);
902                 break;
903         case ID_LA:
904                 *mtex_ar=               ((Lamp *)id)->mtex;
905                 if(act) *act=   (((Lamp *)id)->texact);
906                 break;
907         case ID_BR:
908                 *mtex_ar=               ((Brush *)id)->mtex;
909                 if(act) *act=   (((Brush *)id)->texact);
910                 break;
911         default:
912                 *mtex_ar = NULL;
913                 if(act) *act=   0;
914                 return FALSE;
915         }
916
917         return TRUE;
918 }
919
920 void set_active_mtex(ID *id, short act)
921 {
922         if(act<0)                               act= 0;
923         else if(act>=MAX_MTEX)  act= MAX_MTEX-1;
924
925         switch(GS(id->name)) {
926         case ID_MA:
927                 ((Material *)id)->texact= act;
928                 break;
929         case ID_WO:
930                 ((World *)id)->texact= act;
931                 break;
932         case ID_LA:
933                 ((Lamp *)id)->texact= act;
934                 break;
935         case ID_BR:
936                 ((Brush *)id)->texact= act;
937                 break;
938         }
939 }
940
941 void set_current_material_texture(Material *ma, Tex *newtex)
942 {
943         Tex *tex= NULL;
944         bNode *node;
945         
946         if(ma && ma->use_nodes && ma->nodetree) {
947                 node= nodeGetActiveID(ma->nodetree, ID_TE);
948
949                 if(node) {
950                         tex= (Tex *)node->id;
951                         id_us_min(&tex->id);
952                         node->id= &newtex->id;
953                         id_us_plus(&newtex->id);
954                         ma= NULL;
955                 }
956                 else {
957                         node= nodeGetActiveID(ma->nodetree, ID_MA);
958                         if(node)
959                                 ma= (Material*)node->id;
960                 }
961         }
962         if(ma) {
963                 int act= (int)ma->texact;
964
965                 tex= (ma->mtex[act])? ma->mtex[act]->tex: NULL;
966                 id_us_min(&tex->id);
967
968                 if(newtex) {
969                         if(!ma->mtex[act])
970                                 ma->mtex[act]= add_mtex();
971                         
972                         ma->mtex[act]->tex= newtex;
973                         id_us_plus(&newtex->id);
974                 }
975                 else if(ma->mtex[act]) {
976                         MEM_freeN(ma->mtex[act]);
977                         ma->mtex[act]= NULL;
978                 }
979         }
980 }
981
982 Tex *give_current_world_texture(World *world)
983 {
984         MTex *mtex= NULL;
985         Tex *tex= NULL;
986         
987         if(!world) return 0;
988         
989         mtex= world->mtex[(int)(world->texact)];
990         if(mtex) tex= mtex->tex;
991         
992         return tex;
993 }
994
995 void set_current_world_texture(World *wo, Tex *newtex)
996 {
997         int act= wo->texact;
998
999         if(wo->mtex[act] && wo->mtex[act]->tex)
1000                 id_us_min(&wo->mtex[act]->tex->id);
1001
1002         if(newtex) {
1003                 if(!wo->mtex[act]) {
1004                         wo->mtex[act]= add_mtex();
1005                         wo->mtex[act]->texco= TEXCO_VIEW;
1006                 }
1007                 
1008                 wo->mtex[act]->tex= newtex;
1009                 id_us_plus(&newtex->id);
1010         }
1011         else if(wo->mtex[act]) {
1012                 MEM_freeN(wo->mtex[act]);
1013                 wo->mtex[act]= NULL;
1014         }
1015 }
1016
1017 Tex *give_current_brush_texture(Brush *br)
1018 {
1019         MTex *mtex= NULL;
1020         Tex *tex= NULL;
1021
1022         if(br) {
1023                 mtex= br->mtex[(int)(br->texact)];
1024                 if(mtex) tex= mtex->tex;
1025         }
1026
1027         return tex;
1028 }
1029
1030 void set_current_brush_texture(Brush *br, Tex *newtex)
1031 {
1032         int act= br->texact;
1033
1034         if(br->mtex[act] && br->mtex[act]->tex)
1035                 id_us_min(&br->mtex[act]->tex->id);
1036
1037         if(newtex) {
1038                 if(!br->mtex[act])
1039                         br->mtex[act]= add_mtex();
1040                 
1041                 br->mtex[act]->tex= newtex;
1042                 id_us_plus(&newtex->id);
1043         }
1044         else if(br->mtex[act]) {
1045                 MEM_freeN(br->mtex[act]);
1046                 br->mtex[act]= NULL;
1047         }
1048 }
1049
1050 /* ------------------------------------------------------------------------- */
1051
1052 EnvMap *BKE_add_envmap(void)
1053 {
1054         EnvMap *env;
1055         
1056         env= MEM_callocN(sizeof(EnvMap), "envmap");
1057         env->type= ENV_CUBE;
1058         env->stype= ENV_STATIC;
1059         env->clipsta= 0.1;
1060         env->clipend= 100.0;
1061         env->cuberes= 100;
1062         
1063         return env;
1064
1065
1066 /* ------------------------------------------------------------------------- */
1067
1068 EnvMap *BKE_copy_envmap(EnvMap *env)
1069 {
1070         EnvMap *envn;
1071         int a;
1072         
1073         envn= MEM_dupallocN(env);
1074         envn->ok= 0;
1075         for(a=0; a<6; a++) envn->cube[a]= NULL;
1076         if(envn->ima) id_us_plus((ID *)envn->ima);
1077         
1078         return envn;
1079 }
1080
1081 /* ------------------------------------------------------------------------- */
1082
1083 void BKE_free_envmapdata(EnvMap *env)
1084 {
1085         unsigned int part;
1086         
1087         for(part=0; part<6; part++) {
1088                 if(env->cube[part])
1089                         IMB_freeImBuf(env->cube[part]);
1090                 env->cube[part]= NULL;
1091         }
1092         env->ok= 0;
1093 }
1094
1095 /* ------------------------------------------------------------------------- */
1096
1097 void BKE_free_envmap(EnvMap *env)
1098 {
1099         
1100         BKE_free_envmapdata(env);
1101         MEM_freeN(env);
1102         
1103 }
1104
1105 /* ------------------------------------------------------------------------- */
1106
1107 PointDensity *BKE_add_pointdensity(void)
1108 {
1109         PointDensity *pd;
1110         
1111         pd= MEM_callocN(sizeof(PointDensity), "pointdensity");
1112         pd->flag = 0;
1113         pd->radius = 0.3f;
1114         pd->falloff_type = TEX_PD_FALLOFF_STD;
1115         pd->falloff_softness = 2.0;
1116         pd->source = TEX_PD_PSYS;
1117         pd->point_tree = NULL;
1118         pd->point_data = NULL;
1119         pd->noise_size = 0.5f;
1120         pd->noise_depth = 1;
1121         pd->noise_fac = 1.0f;
1122         pd->noise_influence = TEX_PD_NOISE_STATIC;
1123         pd->coba = add_colorband(1);
1124         pd->speed_scale = 1.0f;
1125         pd->totpoints = 0;
1126         pd->coba = add_colorband(1);
1127         pd->object = NULL;
1128         pd->psys = 0;
1129         return pd;
1130
1131
1132 PointDensity *BKE_copy_pointdensity(PointDensity *pd)
1133 {
1134         PointDensity *pdn;
1135
1136         pdn= MEM_dupallocN(pd);
1137         pdn->point_tree = NULL;
1138         pdn->point_data = NULL;
1139         if(pdn->coba) pdn->coba= MEM_dupallocN(pdn->coba);
1140         
1141         return pdn;
1142 }
1143
1144 void BKE_free_pointdensitydata(PointDensity *pd)
1145 {
1146         if (pd->point_tree) {
1147                 BLI_bvhtree_free(pd->point_tree);
1148                 pd->point_tree = NULL;
1149         }
1150         if (pd->point_data) {
1151                 MEM_freeN(pd->point_data);
1152                 pd->point_data = NULL;
1153         }
1154         if(pd->coba) MEM_freeN(pd->coba);
1155 }
1156
1157 void BKE_free_pointdensity(PointDensity *pd)
1158 {
1159         BKE_free_pointdensitydata(pd);
1160         MEM_freeN(pd);
1161 }
1162
1163
1164 void BKE_free_voxeldatadata(struct VoxelData *vd)
1165 {
1166         if (vd->dataset) {
1167                 MEM_freeN(vd->dataset);
1168                 vd->dataset = NULL;
1169         }
1170
1171 }
1172  
1173 void BKE_free_voxeldata(struct VoxelData *vd)
1174 {
1175         BKE_free_voxeldatadata(vd);
1176         MEM_freeN(vd);
1177 }
1178  
1179 struct VoxelData *BKE_add_voxeldata(void)
1180 {
1181         VoxelData *vd;
1182
1183         vd= MEM_callocN(sizeof(struct VoxelData), "voxeldata");
1184         vd->dataset = NULL;
1185         vd->resol[0] = vd->resol[1] = vd->resol[2] = 1;
1186         vd->interp_type= TEX_VD_LINEAR;
1187         vd->file_format= TEX_VD_SMOKE;
1188         vd->int_multiplier = 1.0;
1189         vd->extend = TEX_CLIP;
1190         vd->object = NULL;
1191         
1192         return vd;
1193  }
1194  
1195 struct VoxelData *BKE_copy_voxeldata(struct VoxelData *vd)
1196 {
1197         VoxelData *vdn;
1198
1199         vdn= MEM_dupallocN(vd); 
1200         vdn->dataset = NULL;
1201
1202         return vdn;
1203 }
1204
1205
1206 /* ------------------------------------------------------------------------- */
1207 int BKE_texture_dependsOnTime(const struct Tex *texture)
1208 {
1209         if(texture->plugin) {
1210                 // assume all plugins depend on time
1211                 return 1;
1212         } 
1213         else if(        texture->ima && 
1214                         ELEM(texture->ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
1215                 return 1;
1216         } 
1217 #if 0 // XXX old animation system
1218         else if(texture->ipo) {
1219                 // assume any ipo means the texture is animated
1220                 return 1;
1221         }
1222 #endif // XXX old animation system
1223         return 0;
1224 }
1225
1226 /* ------------------------------------------------------------------------- */