Merge with trunk r41701
[blender.git] / source / blender / blenkernel / intern / texture.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/texture.c
29  *  \ingroup bke
30  */
31
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <math.h>
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_dynlib.h"
42 #include "BLI_math.h"
43 #include "BLI_kdopbvh.h"
44 #include "BLI_utildefines.h"
45 #include "BLI_bpath.h"
46
47 #include "DNA_key_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_lamp_types.h"
50 #include "DNA_material_types.h"
51 #include "DNA_world_types.h"
52 #include "DNA_brush_types.h"
53 #include "DNA_node_types.h"
54 #include "DNA_color_types.h"
55 #include "DNA_particle_types.h"
56
57 #include "IMB_imbuf.h"
58
59 #include "BKE_plugin_types.h"
60 #include "BKE_utildefines.h"
61 #include "BKE_global.h"
62 #include "BKE_main.h"
63
64 #include "BKE_library.h"
65 #include "BKE_image.h"
66 #include "BKE_material.h"
67 #include "BKE_texture.h"
68 #include "BKE_key.h"
69 #include "BKE_icons.h"
70 #include "BKE_node.h"
71 #include "BKE_animsys.h"
72 #include "BKE_colortools.h"
73
74 /* ------------------------------------------------------------------------- */
75
76 /* All support for plugin textures: */
77 int test_dlerr(const char *name, const char *symbol)
78 {
79         char *err;
80         
81         err= BLI_dynlib_get_error_as_string(NULL);
82         if(err) {
83                 printf("var1: %s, var2: %s, var3: %s\n", name, symbol, err);
84                 return 1;
85         }
86         
87         return 0;
88 }
89
90 /* ------------------------------------------------------------------------- */
91
92 void open_plugin_tex(PluginTex *pit)
93 {
94         int (*version)(void);
95         
96         /* init all the happy variables */
97         pit->doit= NULL;
98         pit->pname= NULL;
99         pit->stnames= NULL;
100         pit->varstr= NULL;
101         pit->result= NULL;
102         pit->cfra= NULL;
103         pit->version= 0;
104         pit->instance_init= NULL;
105         
106         /* clear the error list */
107         BLI_dynlib_get_error_as_string(NULL);
108
109         /* no BLI_dynlib_close! multiple opened plugins... */
110         /* if(pit->handle) BLI_dynlib_close(pit->handle); */
111         /* pit->handle= 0; */
112
113         /* open the needed object */
114         pit->handle= BLI_dynlib_open(pit->name);
115         if(test_dlerr(pit->name, pit->name)) return;
116
117         if (pit->handle != NULL) {
118                 /* find the address of the version function */
119                 version= (int (*)(void)) BLI_dynlib_find_symbol(pit->handle, "plugin_tex_getversion");
120                 if (test_dlerr(pit->name, "plugin_tex_getversion")) return;
121                 
122                 if (version != NULL) {
123                         pit->version= version();
124                         if( pit->version >= 2 && pit->version <=6) {
125                                 int (*info_func)(PluginInfo *);
126                                 PluginInfo *info= (PluginInfo*) MEM_mallocN(sizeof(PluginInfo), "plugin_info"); 
127
128                                 info_func= (int (*)(PluginInfo *))BLI_dynlib_find_symbol(pit->handle, "plugin_getinfo");
129                                 if (!test_dlerr(pit->name, "plugin_getinfo")) {
130                                         info->instance_init = NULL;
131
132                                         info_func(info);
133
134                                         pit->doit= (int(*)(void)) info->tex_doit;
135                                         pit->callback= (void(*)(unsigned short)) info->callback;
136                                         pit->stypes= info->stypes;
137                                         pit->vars= info->nvars;
138                                         pit->pname= info->name;
139                                         pit->stnames= info->snames;
140                                         pit->varstr= info->varstr;
141                                         pit->result= info->result;
142                                         pit->cfra= info->cfra;
143                                         pit->instance_init = info->instance_init;
144                                         if (info->init) info->init();
145                                 }
146                                 MEM_freeN(info);
147                         } else {
148                                 printf ("Plugin returned unrecognized version number\n");
149                                 return;
150                         }
151                 }
152         }
153 }
154
155 /* ------------------------------------------------------------------------- */
156
157 /* very badlevel define to bypass linking with BIF_interface.h */
158 #define INT     96
159 #define FLO     128
160
161 PluginTex *add_plugin_tex(char *str)
162 {
163         PluginTex *pit;
164         VarStruct *varstr;
165         int a;
166         
167         pit= MEM_callocN(sizeof(PluginTex), "plugintex");
168         
169         strcpy(pit->name, str);
170         open_plugin_tex(pit);
171         
172         if(pit->doit==NULL) {
173                 if(pit->handle==NULL) {;} //XXX error("no plugin: %s", str);
174                 else {;} //XXX error("in plugin: %s", str);
175                 MEM_freeN(pit);
176                 return NULL;
177         }
178         
179         varstr= pit->varstr;
180         for(a=0; a<pit->vars; a++, varstr++) {
181                 if( (varstr->type & FLO)==FLO)
182                         pit->data[a]= varstr->def;
183                 else if( (varstr->type & INT)==INT)
184                         *((int *)(pit->data+a))= (int) varstr->def;
185         }
186
187         if (pit->instance_init)
188                 pit->instance_init((void *) pit->data);
189
190         return pit;
191 }
192
193 /* ------------------------------------------------------------------------- */
194
195 void free_plugin_tex(PluginTex *pit)
196 {
197         if(pit==NULL) return;
198                 
199         /* no BLI_dynlib_close: same plugin can be opened multiple times, 1 handle */
200         MEM_freeN(pit); 
201 }
202
203 /* ****************** Mapping ******************* */
204
205 TexMapping *add_tex_mapping(void)
206 {
207         TexMapping *texmap= MEM_callocN(sizeof(TexMapping), "TexMapping");
208         
209         default_tex_mapping(texmap);
210         
211         return texmap;
212 }
213
214 void default_tex_mapping(TexMapping *texmap)
215 {
216         memset(texmap, 0, sizeof(TexMapping));
217
218         texmap->size[0]= texmap->size[1]= texmap->size[2]= 1.0f;
219         texmap->max[0]= texmap->max[1]= texmap->max[2]= 1.0f;
220         unit_m4(texmap->mat);
221
222         texmap->projx= PROJ_X;
223         texmap->projy= PROJ_Y;
224         texmap->projz= PROJ_Z;
225         texmap->mapping= MTEX_FLAT;
226 }
227
228 void init_tex_mapping(TexMapping *texmap)
229 {
230         float eul[3], smat[3][3], rmat[3][3], mat[3][3], proj[3][3];
231
232         if(texmap->projx == PROJ_X && texmap->projy == PROJ_Y && texmap->projz == PROJ_Z &&
233            is_zero_v3(texmap->loc) && is_zero_v3(texmap->rot) && is_one_v3(texmap->size)) {
234                 unit_m4(texmap->mat);
235
236                 texmap->flag |= TEXMAP_UNIT_MATRIX;
237         }
238         else {
239                 /* axis projection */
240                 zero_m3(proj);
241
242                 if(texmap->projx != PROJ_N)
243                         proj[texmap->projx-1][0]= 1.0f;
244                 if(texmap->projy != PROJ_N)
245                         proj[texmap->projy-1][1]= 1.0f;
246                 if(texmap->projz != PROJ_N)
247                         proj[texmap->projz-1][2]= 1.0f;
248
249                 /* scale */
250                 size_to_mat3(smat, texmap->size);
251                 
252                 /* rotation */
253                 eul[0]= DEG2RADF(texmap->rot[0]);
254                 eul[1]= DEG2RADF(texmap->rot[1]);
255                 eul[2]= DEG2RADF(texmap->rot[2]);
256                 eul_to_mat3( rmat,eul);
257                 
258                 /* compose it all */
259                 mul_m3_m3m3(mat, rmat, smat);
260                 mul_m3_m3m3(mat, proj, mat);
261                 
262                 /* translation */
263                 copy_m4_m3(texmap->mat, mat);
264                 copy_v3_v3(texmap->mat[3], texmap->loc);
265
266                 texmap->flag &= ~TEXMAP_UNIT_MATRIX;
267         }
268 }
269
270 ColorMapping *add_color_mapping(void)
271 {
272         ColorMapping *colormap= MEM_callocN(sizeof(ColorMapping), "ColorMapping");
273         
274         default_color_mapping(colormap);
275         
276         return colormap;
277 }
278
279 void default_color_mapping(ColorMapping *colormap)
280 {
281         memset(colormap, 0, sizeof(ColorMapping));
282
283         init_colorband(&colormap->coba, 1);
284
285         colormap->bright= 1.0;
286         colormap->contrast= 1.0;
287         colormap->saturation= 1.0;
288
289         colormap->blend_color[0]= 0.8f;
290         colormap->blend_color[1]= 0.8f;
291         colormap->blend_color[2]= 0.8f;
292         colormap->blend_type= MA_RAMP_BLEND;
293         colormap->blend_factor= 0.0f;
294 }
295
296 /* ****************** COLORBAND ******************* */
297
298 void init_colorband(ColorBand *coba, int rangetype)
299 {
300         int a;
301         
302         coba->data[0].pos= 0.0;
303         coba->data[1].pos= 1.0;
304         
305         if(rangetype==0) {
306                 coba->data[0].r= 0.0;
307                 coba->data[0].g= 0.0;
308                 coba->data[0].b= 0.0;
309                 coba->data[0].a= 0.0;
310                 
311                 coba->data[1].r= 1.0;
312                 coba->data[1].g= 1.0;
313                 coba->data[1].b= 1.0;
314                 coba->data[1].a= 1.0;
315         }
316         else {
317                 coba->data[0].r= 0.0;
318                 coba->data[0].g= 0.0;
319                 coba->data[0].b= 0.0;
320                 coba->data[0].a= 1.0;
321                 
322                 coba->data[1].r= 1.0;
323                 coba->data[1].g= 1.0;
324                 coba->data[1].b= 1.0;
325                 coba->data[1].a= 1.0;
326         }
327         
328         for(a=2; a<MAXCOLORBAND; a++) {
329                 coba->data[a].r= 0.5;
330                 coba->data[a].g= 0.5;
331                 coba->data[a].b= 0.5;
332                 coba->data[a].a= 1.0;
333                 coba->data[a].pos= 0.5;
334         }
335         
336         coba->tot= 2;
337         
338 }
339
340 ColorBand *add_colorband(int rangetype)
341 {
342         ColorBand *coba;
343         
344         coba= MEM_callocN( sizeof(ColorBand), "colorband");
345         init_colorband(coba, rangetype);
346         
347         return coba;
348 }
349
350 /* ------------------------------------------------------------------------- */
351
352 int do_colorband(ColorBand *coba, float in, float out[4])
353 {
354         CBData *cbd1, *cbd2, *cbd0, *cbd3;
355         float fac, mfac, t[4];
356         int a;
357         
358         if(coba==NULL || coba->tot==0) return 0;
359         
360         cbd1= coba->data;
361         if(coba->tot==1) {
362                 out[0]= cbd1->r;
363                 out[1]= cbd1->g;
364                 out[2]= cbd1->b;
365                 out[3]= cbd1->a;
366         }
367         else {
368                 if(in <= cbd1->pos && coba->ipotype<2) {
369                         out[0]= cbd1->r;
370                         out[1]= cbd1->g;
371                         out[2]= cbd1->b;
372                         out[3]= cbd1->a;
373                 }
374                 else {
375                         CBData left, right;
376                         
377                         /* we're looking for first pos > in */
378                         for(a=0; a<coba->tot; a++, cbd1++) if(cbd1->pos > in) break;
379                                 
380                         if(a==coba->tot) {
381                                 cbd2= cbd1-1;
382                                 right= *cbd2;
383                                 right.pos= 1.0f;
384                                 cbd1= &right;
385                         }
386                         else if(a==0) {
387                                 left= *cbd1;
388                                 left.pos= 0.0f;
389                                 cbd2= &left;
390                         }
391                         else cbd2= cbd1-1;
392                         
393                         if(in >= cbd1->pos && coba->ipotype<2) {
394                                 out[0]= cbd1->r;
395                                 out[1]= cbd1->g;
396                                 out[2]= cbd1->b;
397                                 out[3]= cbd1->a;
398                         }
399                         else {
400                 
401                                 if(cbd2->pos!=cbd1->pos)
402                                         fac= (in-cbd1->pos)/(cbd2->pos-cbd1->pos);
403                                 else {
404                                         /* was setting to 0.0 in 2.56 & previous, but this
405                                          * is incorrect for the last element, see [#26732] */
406                                         fac= (a != coba->tot) ? 0.0f : 1.0f;
407                                 }
408                                 
409                                 if (coba->ipotype==4) {
410                                         /* constant */
411                                         out[0]= cbd2->r;
412                                         out[1]= cbd2->g;
413                                         out[2]= cbd2->b;
414                                         out[3]= cbd2->a;
415                                         return 1;
416                                 }
417                                 
418                                 if(coba->ipotype>=2) {
419                                         /* ipo from right to left: 3 2 1 0 */
420                                         
421                                         if(a>=coba->tot-1) cbd0= cbd1;
422                                         else cbd0= cbd1+1;
423                                         if(a<2) cbd3= cbd2;
424                                         else cbd3= cbd2-1;
425                                         
426                                         CLAMP(fac, 0.0f, 1.0f);
427                                         
428                                         if(coba->ipotype==3)
429                                                 key_curve_position_weights(fac, t, KEY_CARDINAL);
430                                         else
431                                                 key_curve_position_weights(fac, t, KEY_BSPLINE);
432
433                                         out[0]= t[3]*cbd3->r +t[2]*cbd2->r +t[1]*cbd1->r +t[0]*cbd0->r;
434                                         out[1]= t[3]*cbd3->g +t[2]*cbd2->g +t[1]*cbd1->g +t[0]*cbd0->g;
435                                         out[2]= t[3]*cbd3->b +t[2]*cbd2->b +t[1]*cbd1->b +t[0]*cbd0->b;
436                                         out[3]= t[3]*cbd3->a +t[2]*cbd2->a +t[1]*cbd1->a +t[0]*cbd0->a;
437                                         CLAMP(out[0], 0.0f, 1.0f);
438                                         CLAMP(out[1], 0.0f, 1.0f);
439                                         CLAMP(out[2], 0.0f, 1.0f);
440                                         CLAMP(out[3], 0.0f, 1.0f);
441                                 }
442                                 else {
443                                 
444                                         if(coba->ipotype==1) {  /* EASE */
445                                                 mfac= fac*fac;
446                                                 fac= 3.0f*mfac-2.0f*mfac*fac;
447                                         }
448                                         mfac= 1.0f-fac;
449                                         
450                                         out[0]= mfac*cbd1->r + fac*cbd2->r;
451                                         out[1]= mfac*cbd1->g + fac*cbd2->g;
452                                         out[2]= mfac*cbd1->b + fac*cbd2->b;
453                                         out[3]= mfac*cbd1->a + fac*cbd2->a;
454                                 }
455                         }
456                 }
457         }
458         return 1;       /* OK */
459 }
460
461 void colorband_table_RGBA(ColorBand *coba, float **array, int *size)
462 {
463         int a;
464         
465         *size = CM_TABLE+1;
466         *array = MEM_callocN(sizeof(float)*(*size)*4, "ColorBand");
467
468         for(a=0; a<*size; a++)
469                 do_colorband(coba, (float)a/(float)CM_TABLE, &(*array)[a*4]);
470 }
471
472 int vergcband(const void *a1, const void *a2)
473 {
474         const CBData *x1=a1, *x2=a2;
475
476         if( x1->pos > x2->pos ) return 1;
477         else if( x1->pos < x2->pos) return -1;
478         return 0;
479 }
480
481 CBData *colorband_element_add(struct ColorBand *coba, float position)
482 {
483         int a;
484
485         if(coba->tot==MAXCOLORBAND) {
486                 return NULL;
487         }
488         else if(coba->tot > 0) {
489                 CBData *xnew;
490                 float col[4];
491
492                 do_colorband(coba, position, col);
493
494                 xnew = &coba->data[coba->tot];
495                 xnew->pos = position;
496
497                 xnew->r = col[0];
498                 xnew->g = col[1];
499                 xnew->b = col[2];
500                 xnew->a = col[3];
501         }
502
503         coba->tot++;
504         coba->cur = coba->tot-1;
505
506         for(a = 0; a < coba->tot; a++)
507                 coba->data[a].cur = a;
508
509         qsort(coba->data, coba->tot, sizeof(CBData), vergcband);
510
511         for(a = 0; a < coba->tot; a++) {
512                 if(coba->data[a].cur == coba->cur) {
513                         coba->cur = a;
514                         break;
515                 }
516         }
517
518         return coba->data + coba->cur;
519 }
520
521 int colorband_element_remove(struct ColorBand *coba, int index)
522 {
523         int a;
524
525         if(coba->tot < 2)
526                 return 0;
527
528         if(index < 0 || index >= coba->tot)
529                 return 0;
530
531         for(a = index; a < coba->tot; a++) {
532                 coba->data[a] = coba->data[a + 1];
533         }
534         if(coba->cur) coba->cur--;
535         coba->tot--;
536         return 1;
537 }
538
539 /* ******************* TEX ************************ */
540
541 void free_texture(Tex *tex)
542 {
543         free_plugin_tex(tex->plugin);
544         
545         if(tex->coba) MEM_freeN(tex->coba);
546         if(tex->env) BKE_free_envmap(tex->env);
547         if(tex->pd) BKE_free_pointdensity(tex->pd);
548         if(tex->vd) BKE_free_voxeldata(tex->vd);
549         BKE_free_animdata((struct ID *)tex);
550         
551         BKE_previewimg_free(&tex->preview);
552         BKE_icon_delete((struct ID*)tex);
553         tex->id.icon_id = 0;
554         
555         if(tex->nodetree) {
556                 ntreeFreeTree(tex->nodetree);
557                 MEM_freeN(tex->nodetree);
558         }
559 }
560
561 /* ------------------------------------------------------------------------- */
562
563 void default_tex(Tex *tex)
564 {
565         PluginTex *pit;
566         VarStruct *varstr;
567         int a;
568
569         tex->type= TEX_CLOUDS;
570         tex->stype= 0;
571         tex->flag= TEX_CHECKER_ODD;
572         tex->imaflag= TEX_INTERPOL|TEX_MIPMAP|TEX_USEALPHA;
573         tex->extend= TEX_REPEAT;
574         tex->cropxmin= tex->cropymin= 0.0;
575         tex->cropxmax= tex->cropymax= 1.0;
576         tex->texfilter = TXF_EWA;
577         tex->afmax = 8;
578         tex->xrepeat= tex->yrepeat= 1;
579         tex->fie_ima= 2;
580         tex->sfra= 1;
581         tex->frames= 0;
582         tex->offset= 0;
583         tex->noisesize= 0.25;
584         tex->noisedepth= 2;
585         tex->turbul= 5.0;
586         tex->nabla= 0.025;      // also in do_versions
587         tex->bright= 1.0;
588         tex->contrast= 1.0;
589         tex->saturation= 1.0;
590         tex->filtersize= 1.0;
591         tex->rfac= 1.0;
592         tex->gfac= 1.0;
593         tex->bfac= 1.0;
594         /* newnoise: init. */
595         tex->noisebasis = 0;
596         tex->noisebasis2 = 0;
597         /* musgrave */
598         tex->mg_H = 1.0;
599         tex->mg_lacunarity = 2.0;
600         tex->mg_octaves = 2.0;
601         tex->mg_offset = 1.0;
602         tex->mg_gain = 1.0;
603         tex->ns_outscale = 1.0;
604         /* distnoise */
605         tex->dist_amount = 1.0;
606         /* voronoi */
607         tex->vn_w1 = 1.0;
608         tex->vn_w2 = tex->vn_w3 = tex->vn_w4 = 0.0;
609         tex->vn_mexp = 2.5;
610         tex->vn_distm = 0;
611         tex->vn_coltype = 0;
612
613         if (tex->env) {
614                 tex->env->stype=ENV_ANIM;
615                 tex->env->clipsta=0.1;
616                 tex->env->clipend=100;
617                 tex->env->cuberes=600;
618                 tex->env->depth=0;
619         }
620
621         if (tex->pd) {
622                 tex->pd->radius = 0.3f;
623                 tex->pd->falloff_type = TEX_PD_FALLOFF_STD;
624         }
625         
626         if (tex->vd) {
627                 tex->vd->resol[0] = tex->vd->resol[1] = tex->vd->resol[2] = 0;
628                 tex->vd->interp_type=TEX_VD_LINEAR;
629                 tex->vd->file_format=TEX_VD_SMOKE;
630         }
631         pit = tex->plugin;
632         if (pit) {
633                 varstr= pit->varstr;
634                 if(varstr) {
635                         for(a=0; a<pit->vars; a++, varstr++) {
636                                 pit->data[a] = varstr->def;
637                         }
638                 }
639         }
640         
641         tex->iuser.fie_ima= 2;
642         tex->iuser.ok= 1;
643         tex->iuser.frames= 100;
644         tex->iuser.sfra= 1;
645         
646         tex->preview = NULL;
647 }
648
649 void tex_set_type(Tex *tex, int type)
650 {
651         switch(type) {
652                         
653                 case TEX_VOXELDATA:
654                         if (tex->vd == NULL)
655                                 tex->vd = BKE_add_voxeldata();
656                         break;
657                 case TEX_POINTDENSITY:
658                         if (tex->pd == NULL)
659                                 tex->pd = BKE_add_pointdensity();
660                         break;
661                 case TEX_ENVMAP:
662                         if (tex->env == NULL)
663                                 tex->env = BKE_add_envmap();
664                         break;
665         }
666         
667         tex->type = type;
668 }
669
670 /* ------------------------------------------------------------------------- */
671
672 Tex *add_texture(const char *name)
673 {
674         Main *bmain= G.main;
675         Tex *tex;
676
677         tex= alloc_libblock(&bmain->tex, ID_TE, name);
678         
679         default_tex(tex);
680         
681         return tex;
682 }
683
684 /* ------------------------------------------------------------------------- */
685
686 void default_mtex(MTex *mtex)
687 {
688         mtex->texco= TEXCO_ORCO;
689         mtex->mapto= MAP_COL;
690         mtex->object= NULL;
691         mtex->projx= PROJ_X;
692         mtex->projy= PROJ_Y;
693         mtex->projz= PROJ_Z;
694         mtex->mapping= MTEX_FLAT;
695         mtex->ofs[0]= 0.0;
696         mtex->ofs[1]= 0.0;
697         mtex->ofs[2]= 0.0;
698         mtex->size[0]= 1.0;
699         mtex->size[1]= 1.0;
700         mtex->size[2]= 1.0;
701         mtex->tex= NULL;
702
703         /* MTEX_BUMP_FLIPPED is temporary before 2.61 release to prevent flipping normals
704            when creating file in 2.60, opening it in 2.59, saving and opening in 2.60 again */
705         mtex->texflag= MTEX_3TAP_BUMP | MTEX_BUMP_OBJECTSPACE | MTEX_BUMP_FLIPPED;
706
707         mtex->colormodel= 0;
708         mtex->r= 1.0;
709         mtex->g= 0.0;
710         mtex->b= 1.0;
711         mtex->k= 1.0;
712         mtex->def_var= 1.0;
713         mtex->blendtype= MTEX_BLEND;
714         mtex->colfac= 1.0;
715         mtex->norfac= 1.0;
716         mtex->varfac= 1.0;
717         mtex->dispfac=0.2;
718         mtex->colspecfac= 1.0f;
719         mtex->mirrfac= 1.0f;
720         mtex->alphafac= 1.0f;
721         mtex->difffac= 1.0f;
722         mtex->specfac= 1.0f;
723         mtex->emitfac= 1.0f;
724         mtex->hardfac= 1.0f;
725         mtex->raymirrfac= 1.0f;
726         mtex->translfac= 1.0f;
727         mtex->ambfac= 1.0f;
728         mtex->colemitfac= 1.0f;
729         mtex->colreflfac= 1.0f;
730         mtex->coltransfac= 1.0f;
731         mtex->densfac= 1.0f;
732         mtex->scatterfac= 1.0f;
733         mtex->reflfac= 1.0f;
734         mtex->shadowfac= 1.0f;
735         mtex->zenupfac= 1.0f;
736         mtex->zendownfac= 1.0f;
737         mtex->blendfac= 1.0f;
738         mtex->timefac= 1.0f;
739         mtex->lengthfac= 1.0f;
740         mtex->clumpfac= 1.0f;
741         mtex->kinkfac= 1.0f;
742         mtex->roughfac= 1.0f;
743         mtex->padensfac= 1.0f;
744         mtex->lifefac= 1.0f;
745         mtex->sizefac= 1.0f;
746         mtex->ivelfac= 1.0f;
747         mtex->dampfac= 1.0f;
748         mtex->gravityfac= 1.0f;
749         mtex->fieldfac= 1.0f;
750         mtex->normapspace= MTEX_NSPACE_TANGENT;
751 }
752
753
754 /* ------------------------------------------------------------------------- */
755
756 MTex *add_mtex(void)
757 {
758         MTex *mtex;
759         
760         mtex= MEM_callocN(sizeof(MTex), "add_mtex");
761         
762         default_mtex(mtex);
763         
764         return mtex;
765 }
766
767 /* slot -1 for first free ID */
768 MTex *add_mtex_id(ID *id, int slot)
769 {
770         MTex **mtex_ar;
771         short act;
772
773         give_active_mtex(id, &mtex_ar, &act);
774
775         if(mtex_ar==NULL) {
776                 return NULL;
777         }
778         
779         if(slot==-1) {
780                 /* find first free */
781                 int i;          
782                 for (i= 0; i < MAX_MTEX; i++) {
783                         if (!mtex_ar[i]) {
784                                 slot= i;
785                                 break;
786                         }
787                 }
788                 if(slot == -1) {
789                         return NULL;
790                 }
791         }
792         else {
793                 /* make sure slot is valid */
794                 if(slot < 0 || slot >= MAX_MTEX) {
795                         return NULL;
796                 }
797         }
798
799         if (mtex_ar[slot]) {
800                 id_us_min((ID *)mtex_ar[slot]->tex);
801                 MEM_freeN(mtex_ar[slot]);
802                 mtex_ar[slot]= NULL;
803         }
804
805         mtex_ar[slot]= add_mtex();
806
807         return mtex_ar[slot];
808 }
809
810 /* ------------------------------------------------------------------------- */
811
812 Tex *copy_texture(Tex *tex)
813 {
814         Tex *texn;
815         
816         texn= copy_libblock(&tex->id);
817         if(texn->type==TEX_IMAGE) id_us_plus((ID *)texn->ima);
818         else texn->ima= NULL;
819         
820         if(texn->plugin) {
821                 texn->plugin= MEM_dupallocN(texn->plugin);
822                 open_plugin_tex(texn->plugin);
823         }
824         
825         if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
826         if(texn->env) texn->env= BKE_copy_envmap(texn->env);
827         if(texn->pd) texn->pd= BKE_copy_pointdensity(texn->pd);
828         if(texn->vd) texn->vd= MEM_dupallocN(texn->vd);
829         if(tex->preview) texn->preview = BKE_previewimg_copy(tex->preview);
830
831         if(tex->nodetree) {
832                 if (tex->nodetree->execdata) {
833                         ntreeTexEndExecTree(tex->nodetree->execdata, 1);
834                 }
835                 texn->nodetree= ntreeCopyTree(tex->nodetree); 
836         }
837         
838         return texn;
839 }
840
841 /* texture copy without adding to main dbase */
842 Tex *localize_texture(Tex *tex)
843 {
844         Tex *texn;
845         
846         texn= copy_libblock(&tex->id);
847         BLI_remlink(&G.main->tex, texn);
848         
849         /* image texture: free_texture also doesn't decrease */
850         
851         if(texn->plugin) {
852                 texn->plugin= MEM_dupallocN(texn->plugin);
853                 open_plugin_tex(texn->plugin);
854         }
855         
856         if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
857         if(texn->env) {
858                 texn->env= BKE_copy_envmap(texn->env);
859                 id_us_min(&texn->env->ima->id);
860         }
861         if(texn->pd) texn->pd= BKE_copy_pointdensity(texn->pd);
862         if(texn->vd) {
863                 texn->vd= MEM_dupallocN(texn->vd);
864                 if(texn->vd->dataset)
865                         texn->vd->dataset= MEM_dupallocN(texn->vd->dataset);
866         }
867         
868         texn->preview = NULL;
869         
870         if(tex->nodetree) {
871                 texn->nodetree= ntreeLocalize(tex->nodetree);
872         }
873         
874         return texn;
875 }
876
877
878 /* ------------------------------------------------------------------------- */
879
880 static void extern_local_texture(Tex *tex)
881 {
882         id_lib_extern((ID *)tex->ima);
883 }
884
885 void make_local_texture(Tex *tex)
886 {
887         Main *bmain= G.main;
888         Material *ma;
889         World *wrld;
890         Lamp *la;
891         Brush *br;
892         ParticleSettings *pa;
893         int a, is_local= FALSE, is_lib= FALSE;
894
895         /* - only lib users: do nothing
896                 * - only local users: set flag
897                 * - mixed: make copy
898                 */
899         
900         if(tex->id.lib==NULL) return;
901
902         if(tex->id.us==1) {
903                 id_clear_lib_data(bmain, &tex->id);
904                 extern_local_texture(tex);
905                 return;
906         }
907         
908         ma= bmain->mat.first;
909         while(ma) {
910                 for(a=0; a<MAX_MTEX; a++) {
911                         if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
912                                 if(ma->id.lib) is_lib= TRUE;
913                                 else is_local= TRUE;
914                         }
915                 }
916                 ma= ma->id.next;
917         }
918         la= bmain->lamp.first;
919         while(la) {
920                 for(a=0; a<MAX_MTEX; a++) {
921                         if(la->mtex[a] && la->mtex[a]->tex==tex) {
922                                 if(la->id.lib) is_lib= TRUE;
923                                 else is_local= TRUE;
924                         }
925                 }
926                 la= la->id.next;
927         }
928         wrld= bmain->world.first;
929         while(wrld) {
930                 for(a=0; a<MAX_MTEX; a++) {
931                         if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
932                                 if(wrld->id.lib) is_lib= TRUE;
933                                 else is_local= TRUE;
934                         }
935                 }
936                 wrld= wrld->id.next;
937         }
938         br= bmain->brush.first;
939         while(br) {
940                 if(br->mtex.tex==tex) {
941                         if(br->id.lib) is_lib= TRUE;
942                         else is_local= TRUE;
943                 }
944                 br= br->id.next;
945         }
946         pa= bmain->particle.first;
947         while(pa) {
948                 for(a=0; a<MAX_MTEX; a++) {
949                         if(pa->mtex[a] && pa->mtex[a]->tex==tex) {
950                                 if(pa->id.lib) is_lib= TRUE;
951                                 else is_local= TRUE;
952                         }
953                 }
954                 pa= pa->id.next;
955         }
956         
957         if(is_local && is_lib == FALSE) {
958                 id_clear_lib_data(bmain, &tex->id);
959                 extern_local_texture(tex);
960         }
961         else if(is_local && is_lib) {
962                 Tex *texn= copy_texture(tex);
963
964                 texn->id.us= 0;
965
966                 /* Remap paths of new ID using old library as base. */
967                 BKE_id_lib_local_paths(bmain, &texn->id);
968                 
969                 ma= bmain->mat.first;
970                 while(ma) {
971                         for(a=0; a<MAX_MTEX; a++) {
972                                 if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
973                                         if(ma->id.lib==NULL) {
974                                                 ma->mtex[a]->tex= texn;
975                                                 texn->id.us++;
976                                                 tex->id.us--;
977                                         }
978                                 }
979                         }
980                         ma= ma->id.next;
981                 }
982                 la= bmain->lamp.first;
983                 while(la) {
984                         for(a=0; a<MAX_MTEX; a++) {
985                                 if(la->mtex[a] && la->mtex[a]->tex==tex) {
986                                         if(la->id.lib==NULL) {
987                                                 la->mtex[a]->tex= texn;
988                                                 texn->id.us++;
989                                                 tex->id.us--;
990                                         }
991                                 }
992                         }
993                         la= la->id.next;
994                 }
995                 wrld= bmain->world.first;
996                 while(wrld) {
997                         for(a=0; a<MAX_MTEX; a++) {
998                                 if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
999                                         if(wrld->id.lib==NULL) {
1000                                                 wrld->mtex[a]->tex= texn;
1001                                                 texn->id.us++;
1002                                                 tex->id.us--;
1003                                         }
1004                                 }
1005                         }
1006                         wrld= wrld->id.next;
1007                 }
1008                 br= bmain->brush.first;
1009                 while(br) {
1010                         if(br->mtex.tex==tex) {
1011                                 if(br->id.lib==NULL) {
1012                                         br->mtex.tex= texn;
1013                                         texn->id.us++;
1014                                         tex->id.us--;
1015                                 }
1016                         }
1017                         br= br->id.next;
1018                 }
1019                 pa= bmain->particle.first;
1020                 while(pa) {
1021                         for(a=0; a<MAX_MTEX; a++) {
1022                                 if(pa->mtex[a] && pa->mtex[a]->tex==tex) {
1023                                         if(pa->id.lib==NULL) {
1024                                                 pa->mtex[a]->tex= texn;
1025                                                 texn->id.us++;
1026                                                 tex->id.us--;
1027                                         }
1028                                 }
1029                         }
1030                         pa= pa->id.next;
1031                 }
1032         }
1033 }
1034
1035 /* ------------------------------------------------------------------------- */
1036
1037 void autotexname(Tex *tex)
1038 {
1039         Main *bmain= G.main;
1040         char texstr[20][15]= {"None"  , "Clouds" , "Wood", "Marble", "Magic"  , "Blend",
1041                 "Stucci", "Noise"  , "Image", "Plugin", "EnvMap" , "Musgrave",
1042                 "Voronoi", "DistNoise", "Point Density", "Voxel Data", "", "", "", ""};
1043         Image *ima;
1044         char di[FILE_MAXDIR], fi[FILE_MAXFILE];
1045         
1046         if(tex) {
1047                 if(tex->use_nodes) {
1048                         new_id(&bmain->tex, (ID *)tex, "Noddy");
1049                 }
1050                 else
1051                 if(tex->type==TEX_IMAGE) {
1052                         ima= tex->ima;
1053                         if(ima) {
1054                                 BLI_strncpy(di, ima->name, sizeof(di));
1055                                 BLI_splitdirstring(di, fi);
1056                                 strcpy(di, "I.");
1057                                 strcat(di, fi);
1058                                 new_id(&bmain->tex, (ID *)tex, di);
1059                         }
1060                         else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
1061                 }
1062                 else if(tex->type==TEX_PLUGIN && tex->plugin) new_id(&bmain->tex, (ID *)tex, tex->plugin->pname);
1063                 else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
1064         }
1065 }
1066
1067 /* ------------------------------------------------------------------------- */
1068
1069 Tex *give_current_object_texture(Object *ob)
1070 {
1071         Material *ma, *node_ma;
1072         Tex *tex= NULL;
1073         
1074         if(ob==NULL) return NULL;
1075         if(ob->totcol==0 && !(ob->type==OB_LAMP)) return NULL;
1076         
1077         if(ob->type==OB_LAMP) {
1078                 tex= give_current_lamp_texture(ob->data);
1079         } else {
1080                 ma= give_current_material(ob, ob->actcol);
1081
1082                 if((node_ma=give_node_material(ma)))
1083                         ma= node_ma;
1084
1085                 tex= give_current_material_texture(ma);
1086         }
1087         
1088         return tex;
1089 }
1090
1091 Tex *give_current_lamp_texture(Lamp *la)
1092 {
1093         MTex *mtex= NULL;
1094         Tex *tex= NULL;
1095
1096         if(la) {
1097                 mtex= la->mtex[(int)(la->texact)];
1098                 if(mtex) tex= mtex->tex;
1099         }
1100
1101         return tex;
1102 }
1103
1104 void set_current_lamp_texture(Lamp *la, Tex *newtex)
1105 {
1106         int act= la->texact;
1107
1108         if(la->mtex[act] && la->mtex[act]->tex)
1109                 id_us_min(&la->mtex[act]->tex->id);
1110
1111         if(newtex) {
1112                 if(!la->mtex[act]) {
1113                         la->mtex[act]= add_mtex();
1114                         la->mtex[act]->texco= TEXCO_GLOB;
1115                 }
1116                 
1117                 la->mtex[act]->tex= newtex;
1118                 id_us_plus(&newtex->id);
1119         }
1120         else if(la->mtex[act]) {
1121                 MEM_freeN(la->mtex[act]);
1122                 la->mtex[act]= NULL;
1123         }
1124 }
1125
1126 bNode *give_current_material_texture_node(Material *ma)
1127 {
1128         if(ma && ma->use_nodes && ma->nodetree)
1129                 return nodeGetActiveID(ma->nodetree, ID_TE);
1130         
1131         return NULL;
1132 }
1133
1134 Tex *give_current_material_texture(Material *ma)
1135 {
1136         MTex *mtex= NULL;
1137         Tex *tex= NULL;
1138         bNode *node;
1139         
1140         if(ma && ma->use_nodes && ma->nodetree) {
1141                 /* first check texture, then material, this works together
1142                    with a hack that clears the active ID flag for textures on
1143                    making a material node active */
1144                 node= nodeGetActiveID(ma->nodetree, ID_TE);
1145
1146                 if(node) {
1147                         tex= (Tex *)node->id;
1148                         ma= NULL;
1149                 }
1150         }
1151
1152         if(ma) {
1153                 mtex= ma->mtex[(int)(ma->texact)];
1154                 if(mtex) tex= mtex->tex;
1155         }
1156         
1157         return tex;
1158 }
1159
1160 int give_active_mtex(ID *id, MTex ***mtex_ar, short *act)
1161 {
1162         switch(GS(id->name)) {
1163         case ID_MA:
1164                 *mtex_ar=               ((Material *)id)->mtex;
1165                 if(act) *act=   (((Material *)id)->texact);
1166                 break;
1167         case ID_WO:
1168                 *mtex_ar=               ((World *)id)->mtex;
1169                 if(act) *act=   (((World *)id)->texact);
1170                 break;
1171         case ID_LA:
1172                 *mtex_ar=               ((Lamp *)id)->mtex;
1173                 if(act) *act=   (((Lamp *)id)->texact);
1174                 break;
1175         case ID_PA:
1176                 *mtex_ar=               ((ParticleSettings *)id)->mtex;
1177                 if(act) *act=   (((ParticleSettings *)id)->texact);
1178                 break;
1179         default:
1180                 *mtex_ar = NULL;
1181                 if(act) *act=   0;
1182                 return FALSE;
1183         }
1184
1185         return TRUE;
1186 }
1187
1188 void set_active_mtex(ID *id, short act)
1189 {
1190         if(act<0)                               act= 0;
1191         else if(act>=MAX_MTEX)  act= MAX_MTEX-1;
1192
1193         switch(GS(id->name)) {
1194         case ID_MA:
1195                 ((Material *)id)->texact= act;
1196                 break;
1197         case ID_WO:
1198                 ((World *)id)->texact= act;
1199                 break;
1200         case ID_LA:
1201                 ((Lamp *)id)->texact= act;
1202                 break;
1203         case ID_PA:
1204                 ((ParticleSettings *)id)->texact= act;
1205                 break;
1206         }
1207 }
1208
1209 void set_current_material_texture(Material *ma, Tex *newtex)
1210 {
1211         Tex *tex= NULL;
1212         bNode *node;
1213         
1214         if(ma && ma->use_nodes && ma->nodetree) {
1215                 node= nodeGetActiveID(ma->nodetree, ID_TE);
1216
1217                 if(node) {
1218                         tex= (Tex *)node->id;
1219                         id_us_min(&tex->id);
1220                         node->id= &newtex->id;
1221                         id_us_plus(&newtex->id);
1222                         ma= NULL;
1223                 }
1224         }
1225         if(ma) {
1226                 int act= (int)ma->texact;
1227
1228                 tex= (ma->mtex[act])? ma->mtex[act]->tex: NULL;
1229                 id_us_min(&tex->id);
1230
1231                 if(newtex) {
1232                         if(!ma->mtex[act])
1233                                 ma->mtex[act]= add_mtex();
1234                         
1235                         ma->mtex[act]->tex= newtex;
1236                         id_us_plus(&newtex->id);
1237                 }
1238                 else if(ma->mtex[act]) {
1239                         MEM_freeN(ma->mtex[act]);
1240                         ma->mtex[act]= NULL;
1241                 }
1242         }
1243 }
1244
1245 int has_current_material_texture(Material *ma)
1246 {
1247         bNode *node;
1248
1249         if(ma && ma->use_nodes && ma->nodetree) {
1250                 node= nodeGetActiveID(ma->nodetree, ID_TE);
1251
1252                 if(node)
1253                         return 1;
1254         }
1255
1256         return (ma != NULL);
1257 }
1258
1259 Tex *give_current_world_texture(World *world)
1260 {
1261         MTex *mtex= NULL;
1262         Tex *tex= NULL;
1263         
1264         if(!world) return NULL;
1265         
1266         mtex= world->mtex[(int)(world->texact)];
1267         if(mtex) tex= mtex->tex;
1268         
1269         return tex;
1270 }
1271
1272 void set_current_world_texture(World *wo, Tex *newtex)
1273 {
1274         int act= wo->texact;
1275
1276         if(wo->mtex[act] && wo->mtex[act]->tex)
1277                 id_us_min(&wo->mtex[act]->tex->id);
1278
1279         if(newtex) {
1280                 if(!wo->mtex[act]) {
1281                         wo->mtex[act]= add_mtex();
1282                         wo->mtex[act]->texco= TEXCO_VIEW;
1283                 }
1284                 
1285                 wo->mtex[act]->tex= newtex;
1286                 id_us_plus(&newtex->id);
1287         }
1288         else if(wo->mtex[act]) {
1289                 MEM_freeN(wo->mtex[act]);
1290                 wo->mtex[act]= NULL;
1291         }
1292 }
1293
1294 Tex *give_current_brush_texture(Brush *br)
1295 {
1296         return br->mtex.tex;
1297 }
1298
1299 void set_current_brush_texture(Brush *br, Tex *newtex)
1300 {
1301         if(br->mtex.tex)
1302                 id_us_min(&br->mtex.tex->id);
1303
1304         if(newtex) {
1305                 br->mtex.tex= newtex;
1306                 id_us_plus(&newtex->id);
1307         }
1308 }
1309
1310 Tex *give_current_particle_texture(ParticleSettings *part)
1311 {
1312         MTex *mtex= NULL;
1313         Tex *tex= NULL;
1314         
1315         if(!part) return NULL;
1316         
1317         mtex= part->mtex[(int)(part->texact)];
1318         if(mtex) tex= mtex->tex;
1319         
1320         return tex;
1321 }
1322
1323 void set_current_particle_texture(ParticleSettings *part, Tex *newtex)
1324 {
1325         int act= part->texact;
1326
1327         if(part->mtex[act] && part->mtex[act]->tex)
1328                 id_us_min(&part->mtex[act]->tex->id);
1329
1330         if(newtex) {
1331                 if(!part->mtex[act]) {
1332                         part->mtex[act]= add_mtex();
1333                         part->mtex[act]->texco= TEXCO_ORCO;
1334                         part->mtex[act]->blendtype= MTEX_MUL;
1335                 }
1336                 
1337                 part->mtex[act]->tex= newtex;
1338                 id_us_plus(&newtex->id);
1339         }
1340         else if(part->mtex[act]) {
1341                 MEM_freeN(part->mtex[act]);
1342                 part->mtex[act]= NULL;
1343         }
1344 }
1345
1346 /* ------------------------------------------------------------------------- */
1347
1348 EnvMap *BKE_add_envmap(void)
1349 {
1350         EnvMap *env;
1351         
1352         env= MEM_callocN(sizeof(EnvMap), "envmap");
1353         env->type= ENV_CUBE;
1354         env->stype= ENV_ANIM;
1355         env->clipsta= 0.1;
1356         env->clipend= 100.0;
1357         env->cuberes= 600;
1358         env->viewscale = 0.5;
1359         
1360         return env;
1361
1362
1363 /* ------------------------------------------------------------------------- */
1364
1365 EnvMap *BKE_copy_envmap(EnvMap *env)
1366 {
1367         EnvMap *envn;
1368         int a;
1369         
1370         envn= MEM_dupallocN(env);
1371         envn->ok= 0;
1372         for(a=0; a<6; a++) envn->cube[a]= NULL;
1373         if(envn->ima) id_us_plus((ID *)envn->ima);
1374         
1375         return envn;
1376 }
1377
1378 /* ------------------------------------------------------------------------- */
1379
1380 void BKE_free_envmapdata(EnvMap *env)
1381 {
1382         unsigned int part;
1383         
1384         for(part=0; part<6; part++) {
1385                 if(env->cube[part])
1386                         IMB_freeImBuf(env->cube[part]);
1387                 env->cube[part]= NULL;
1388         }
1389         env->ok= 0;
1390 }
1391
1392 /* ------------------------------------------------------------------------- */
1393
1394 void BKE_free_envmap(EnvMap *env)
1395 {
1396         
1397         BKE_free_envmapdata(env);
1398         MEM_freeN(env);
1399         
1400 }
1401
1402 /* ------------------------------------------------------------------------- */
1403
1404 PointDensity *BKE_add_pointdensity(void)
1405 {
1406         PointDensity *pd;
1407         
1408         pd= MEM_callocN(sizeof(PointDensity), "pointdensity");
1409         pd->flag = 0;
1410         pd->radius = 0.3f;
1411         pd->falloff_type = TEX_PD_FALLOFF_STD;
1412         pd->falloff_softness = 2.0;
1413         pd->source = TEX_PD_PSYS;
1414         pd->point_tree = NULL;
1415         pd->point_data = NULL;
1416         pd->noise_size = 0.5f;
1417         pd->noise_depth = 1;
1418         pd->noise_fac = 1.0f;
1419         pd->noise_influence = TEX_PD_NOISE_STATIC;
1420         pd->coba = add_colorband(1);
1421         pd->speed_scale = 1.0f;
1422         pd->totpoints = 0;
1423         pd->object = NULL;
1424         pd->psys = 0;
1425         pd->psys_cache_space= TEX_PD_WORLDSPACE;
1426         pd->falloff_curve = curvemapping_add(1, 0, 0, 1, 1);
1427
1428         pd->falloff_curve->preset = CURVE_PRESET_LINE;
1429         pd->falloff_curve->cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
1430         curvemap_reset(pd->falloff_curve->cm, &pd->falloff_curve->clipr, pd->falloff_curve->preset, CURVEMAP_SLOPE_POSITIVE);
1431         curvemapping_changed(pd->falloff_curve, 0);
1432
1433         return pd;
1434
1435
1436 PointDensity *BKE_copy_pointdensity(PointDensity *pd)
1437 {
1438         PointDensity *pdn;
1439
1440         pdn= MEM_dupallocN(pd);
1441         pdn->point_tree = NULL;
1442         pdn->point_data = NULL;
1443         if(pdn->coba) pdn->coba= MEM_dupallocN(pdn->coba);
1444         pdn->falloff_curve = curvemapping_copy(pdn->falloff_curve); /* can be NULL */
1445         return pdn;
1446 }
1447
1448 void BKE_free_pointdensitydata(PointDensity *pd)
1449 {
1450         if (pd->point_tree) {
1451                 BLI_bvhtree_free(pd->point_tree);
1452                 pd->point_tree = NULL;
1453         }
1454         if (pd->point_data) {
1455                 MEM_freeN(pd->point_data);
1456                 pd->point_data = NULL;
1457         }
1458         if(pd->coba) {
1459                 MEM_freeN(pd->coba);
1460                 pd->coba = NULL;
1461         }
1462
1463         curvemapping_free(pd->falloff_curve); /* can be NULL */
1464 }
1465
1466 void BKE_free_pointdensity(PointDensity *pd)
1467 {
1468         BKE_free_pointdensitydata(pd);
1469         MEM_freeN(pd);
1470 }
1471
1472
1473 void BKE_free_voxeldatadata(struct VoxelData *vd)
1474 {
1475         if (vd->dataset) {
1476                 MEM_freeN(vd->dataset);
1477                 vd->dataset = NULL;
1478         }
1479
1480 }
1481  
1482 void BKE_free_voxeldata(struct VoxelData *vd)
1483 {
1484         BKE_free_voxeldatadata(vd);
1485         MEM_freeN(vd);
1486 }
1487  
1488 struct VoxelData *BKE_add_voxeldata(void)
1489 {
1490         VoxelData *vd;
1491
1492         vd= MEM_callocN(sizeof(struct VoxelData), "voxeldata");
1493         vd->dataset = NULL;
1494         vd->resol[0] = vd->resol[1] = vd->resol[2] = 1;
1495         vd->interp_type= TEX_VD_LINEAR;
1496         vd->file_format= TEX_VD_SMOKE;
1497         vd->int_multiplier = 1.0;
1498         vd->extend = TEX_CLIP;
1499         vd->object = NULL;
1500         vd->cachedframe = -1;
1501         vd->ok = 0;
1502         
1503         return vd;
1504  }
1505  
1506 struct VoxelData *BKE_copy_voxeldata(struct VoxelData *vd)
1507 {
1508         VoxelData *vdn;
1509
1510         vdn= MEM_dupallocN(vd); 
1511         vdn->dataset = NULL;
1512
1513         return vdn;
1514 }
1515
1516
1517 /* ------------------------------------------------------------------------- */
1518 int BKE_texture_dependsOnTime(const struct Tex *texture)
1519 {
1520         if(texture->plugin) {
1521                 // assume all plugins depend on time
1522                 return 1;
1523         } 
1524         else if(        texture->ima && 
1525                         ELEM(texture->ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
1526                 return 1;
1527         }
1528         else if(texture->adt) {
1529                 // assume anything in adt means the texture is animated
1530                 return 1;
1531         }
1532         else if(texture->type == TEX_NOISE) {
1533                 // noise always varies with time
1534                 return 1;
1535         }
1536         return 0;
1537 }
1538
1539 /* ------------------------------------------------------------------------- */