svn merge -r41779:41847 ^/trunk/blender
[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 #include "BKE_ocean.h"
64
65 #include "BKE_library.h"
66 #include "BKE_image.h"
67 #include "BKE_material.h"
68 #include "BKE_texture.h"
69 #include "BKE_key.h"
70 #include "BKE_icons.h"
71 #include "BKE_node.h"
72 #include "BKE_animsys.h"
73 #include "BKE_colortools.h"
74
75
76 /* ------------------------------------------------------------------------- */
77
78 /* All support for plugin textures: */
79 int test_dlerr(const char *name, const char *symbol)
80 {
81         char *err;
82         
83         err= BLI_dynlib_get_error_as_string(NULL);
84         if(err) {
85                 printf("var1: %s, var2: %s, var3: %s\n", name, symbol, err);
86                 return 1;
87         }
88         
89         return 0;
90 }
91
92 /* ------------------------------------------------------------------------- */
93
94 void open_plugin_tex(PluginTex *pit)
95 {
96         int (*version)(void);
97         
98         /* init all the happy variables */
99         pit->doit= NULL;
100         pit->pname= NULL;
101         pit->stnames= NULL;
102         pit->varstr= NULL;
103         pit->result= NULL;
104         pit->cfra= NULL;
105         pit->version= 0;
106         pit->instance_init= NULL;
107         
108         /* clear the error list */
109         BLI_dynlib_get_error_as_string(NULL);
110
111         /* no BLI_dynlib_close! multiple opened plugins... */
112         /* if(pit->handle) BLI_dynlib_close(pit->handle); */
113         /* pit->handle= 0; */
114
115         /* open the needed object */
116         pit->handle= BLI_dynlib_open(pit->name);
117         if(test_dlerr(pit->name, pit->name)) return;
118
119         if (pit->handle != NULL) {
120                 /* find the address of the version function */
121                 version= (int (*)(void)) BLI_dynlib_find_symbol(pit->handle, "plugin_tex_getversion");
122                 if (test_dlerr(pit->name, "plugin_tex_getversion")) return;
123                 
124                 if (version != NULL) {
125                         pit->version= version();
126                         if( pit->version >= 2 && pit->version <=6) {
127                                 int (*info_func)(PluginInfo *);
128                                 PluginInfo *info= (PluginInfo*) MEM_mallocN(sizeof(PluginInfo), "plugin_info"); 
129
130                                 info_func= (int (*)(PluginInfo *))BLI_dynlib_find_symbol(pit->handle, "plugin_getinfo");
131                                 if (!test_dlerr(pit->name, "plugin_getinfo")) {
132                                         info->instance_init = NULL;
133
134                                         info_func(info);
135
136                                         pit->doit= (int(*)(void)) info->tex_doit;
137                                         pit->callback= (void(*)(unsigned short)) info->callback;
138                                         pit->stypes= info->stypes;
139                                         pit->vars= info->nvars;
140                                         pit->pname= info->name;
141                                         pit->stnames= info->snames;
142                                         pit->varstr= info->varstr;
143                                         pit->result= info->result;
144                                         pit->cfra= info->cfra;
145                                         pit->instance_init = info->instance_init;
146                                         if (info->init) info->init();
147                                 }
148                                 MEM_freeN(info);
149                         } else {
150                                 printf ("Plugin returned unrecognized version number\n");
151                                 return;
152                         }
153                 }
154         }
155 }
156
157 /* ------------------------------------------------------------------------- */
158
159 /* very badlevel define to bypass linking with BIF_interface.h */
160 #define INT     96
161 #define FLO     128
162
163 PluginTex *add_plugin_tex(char *str)
164 {
165         PluginTex *pit;
166         VarStruct *varstr;
167         int a;
168         
169         pit= MEM_callocN(sizeof(PluginTex), "plugintex");
170         
171         strcpy(pit->name, str);
172         open_plugin_tex(pit);
173         
174         if(pit->doit==NULL) {
175                 if(pit->handle==NULL) {;} //XXX error("no plugin: %s", str);
176                 else {;} //XXX error("in plugin: %s", str);
177                 MEM_freeN(pit);
178                 return NULL;
179         }
180         
181         varstr= pit->varstr;
182         for(a=0; a<pit->vars; a++, varstr++) {
183                 if( (varstr->type & FLO)==FLO)
184                         pit->data[a]= varstr->def;
185                 else if( (varstr->type & INT)==INT)
186                         *((int *)(pit->data+a))= (int) varstr->def;
187         }
188
189         if (pit->instance_init)
190                 pit->instance_init((void *) pit->data);
191
192         return pit;
193 }
194
195 /* ------------------------------------------------------------------------- */
196
197 void free_plugin_tex(PluginTex *pit)
198 {
199         if(pit==NULL) return;
200                 
201         /* no BLI_dynlib_close: same plugin can be opened multiple times, 1 handle */
202         MEM_freeN(pit); 
203 }
204
205 /* ****************** Mapping ******************* */
206
207 TexMapping *add_tex_mapping(void)
208 {
209         TexMapping *texmap= MEM_callocN(sizeof(TexMapping), "TexMapping");
210         
211         default_tex_mapping(texmap);
212         
213         return texmap;
214 }
215
216 void default_tex_mapping(TexMapping *texmap)
217 {
218         memset(texmap, 0, sizeof(TexMapping));
219
220         texmap->size[0]= texmap->size[1]= texmap->size[2]= 1.0f;
221         texmap->max[0]= texmap->max[1]= texmap->max[2]= 1.0f;
222         unit_m4(texmap->mat);
223
224         texmap->projx= PROJ_X;
225         texmap->projy= PROJ_Y;
226         texmap->projz= PROJ_Z;
227         texmap->mapping= MTEX_FLAT;
228 }
229
230 void init_tex_mapping(TexMapping *texmap)
231 {
232         float smat[3][3], rmat[3][3], mat[3][3], proj[3][3];
233
234         if(texmap->projx == PROJ_X && texmap->projy == PROJ_Y && texmap->projz == PROJ_Z &&
235            is_zero_v3(texmap->loc) && is_zero_v3(texmap->rot) && is_one_v3(texmap->size)) {
236                 unit_m4(texmap->mat);
237
238                 texmap->flag |= TEXMAP_UNIT_MATRIX;
239         }
240         else {
241                 /* axis projection */
242                 zero_m3(proj);
243
244                 if(texmap->projx != PROJ_N)
245                         proj[texmap->projx-1][0]= 1.0f;
246                 if(texmap->projy != PROJ_N)
247                         proj[texmap->projy-1][1]= 1.0f;
248                 if(texmap->projz != PROJ_N)
249                         proj[texmap->projz-1][2]= 1.0f;
250
251                 /* scale */
252                 size_to_mat3(smat, texmap->size);
253                 
254                 /* rotation */
255                 /* XXX TexMapping rotation are now in radians. */
256                 eul_to_mat3(rmat, texmap->rot);
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         if(tex->ot) BKE_free_oceantex(tex->ot);
550         BKE_free_animdata((struct ID *)tex);
551         
552         BKE_previewimg_free(&tex->preview);
553         BKE_icon_delete((struct ID*)tex);
554         tex->id.icon_id = 0;
555         
556         if(tex->nodetree) {
557                 ntreeFreeTree(tex->nodetree);
558                 MEM_freeN(tex->nodetree);
559         }
560 }
561
562 /* ------------------------------------------------------------------------- */
563
564 void default_tex(Tex *tex)
565 {
566         PluginTex *pit;
567         VarStruct *varstr;
568         int a;
569
570         tex->type= TEX_CLOUDS;
571         tex->stype= 0;
572         tex->flag= TEX_CHECKER_ODD;
573         tex->imaflag= TEX_INTERPOL|TEX_MIPMAP|TEX_USEALPHA;
574         tex->extend= TEX_REPEAT;
575         tex->cropxmin= tex->cropymin= 0.0;
576         tex->cropxmax= tex->cropymax= 1.0;
577         tex->texfilter = TXF_EWA;
578         tex->afmax = 8;
579         tex->xrepeat= tex->yrepeat= 1;
580         tex->fie_ima= 2;
581         tex->sfra= 1;
582         tex->frames= 0;
583         tex->offset= 0;
584         tex->noisesize= 0.25;
585         tex->noisedepth= 2;
586         tex->turbul= 5.0;
587         tex->nabla= 0.025;      // also in do_versions
588         tex->bright= 1.0;
589         tex->contrast= 1.0;
590         tex->saturation= 1.0;
591         tex->filtersize= 1.0;
592         tex->rfac= 1.0;
593         tex->gfac= 1.0;
594         tex->bfac= 1.0;
595         /* newnoise: init. */
596         tex->noisebasis = 0;
597         tex->noisebasis2 = 0;
598         /* musgrave */
599         tex->mg_H = 1.0;
600         tex->mg_lacunarity = 2.0;
601         tex->mg_octaves = 2.0;
602         tex->mg_offset = 1.0;
603         tex->mg_gain = 1.0;
604         tex->ns_outscale = 1.0;
605         /* distnoise */
606         tex->dist_amount = 1.0;
607         /* voronoi */
608         tex->vn_w1 = 1.0;
609         tex->vn_w2 = tex->vn_w3 = tex->vn_w4 = 0.0;
610         tex->vn_mexp = 2.5;
611         tex->vn_distm = 0;
612         tex->vn_coltype = 0;
613
614         if (tex->env) {
615                 tex->env->stype=ENV_ANIM;
616                 tex->env->clipsta=0.1;
617                 tex->env->clipend=100;
618                 tex->env->cuberes=600;
619                 tex->env->depth=0;
620         }
621
622         if (tex->pd) {
623                 tex->pd->radius = 0.3f;
624                 tex->pd->falloff_type = TEX_PD_FALLOFF_STD;
625         }
626         
627         if (tex->vd) {
628                 tex->vd->resol[0] = tex->vd->resol[1] = tex->vd->resol[2] = 0;
629                 tex->vd->interp_type=TEX_VD_LINEAR;
630                 tex->vd->file_format=TEX_VD_SMOKE;
631         }
632         
633         if (tex->ot) {
634                 tex->ot->output = TEX_OCN_DISPLACEMENT;
635                 tex->ot->object = NULL;
636         }
637         pit = tex->plugin;
638         if (pit) {
639                 varstr= pit->varstr;
640                 if(varstr) {
641                         for(a=0; a<pit->vars; a++, varstr++) {
642                                 pit->data[a] = varstr->def;
643                         }
644                 }
645         }
646         
647         tex->iuser.fie_ima= 2;
648         tex->iuser.ok= 1;
649         tex->iuser.frames= 100;
650         tex->iuser.sfra= 1;
651         
652         tex->preview = NULL;
653 }
654
655 void tex_set_type(Tex *tex, int type)
656 {
657         switch(type) {
658                         
659                 case TEX_VOXELDATA:
660                         if (tex->vd == NULL)
661                                 tex->vd = BKE_add_voxeldata();
662                         break;
663                 case TEX_POINTDENSITY:
664                         if (tex->pd == NULL)
665                                 tex->pd = BKE_add_pointdensity();
666                         break;
667                 case TEX_ENVMAP:
668                         if (tex->env == NULL)
669                                 tex->env = BKE_add_envmap();
670                         break;
671                 case TEX_OCEAN:
672                         if (tex->ot == NULL)
673                                 tex->ot = BKE_add_oceantex();
674                         break;
675         }
676         
677         tex->type = type;
678 }
679
680 /* ------------------------------------------------------------------------- */
681
682 Tex *add_texture(const char *name)
683 {
684         Main *bmain= G.main;
685         Tex *tex;
686
687         tex= alloc_libblock(&bmain->tex, ID_TE, name);
688         
689         default_tex(tex);
690         
691         return tex;
692 }
693
694 /* ------------------------------------------------------------------------- */
695
696 void default_mtex(MTex *mtex)
697 {
698         mtex->texco= TEXCO_ORCO;
699         mtex->mapto= MAP_COL;
700         mtex->object= NULL;
701         mtex->projx= PROJ_X;
702         mtex->projy= PROJ_Y;
703         mtex->projz= PROJ_Z;
704         mtex->mapping= MTEX_FLAT;
705         mtex->ofs[0]= 0.0;
706         mtex->ofs[1]= 0.0;
707         mtex->ofs[2]= 0.0;
708         mtex->size[0]= 1.0;
709         mtex->size[1]= 1.0;
710         mtex->size[2]= 1.0;
711         mtex->tex= NULL;
712
713         /* MTEX_BUMP_FLIPPED is temporary before 2.61 release to prevent flipping normals
714            when creating file in 2.60, opening it in 2.59, saving and opening in 2.60 again */
715         mtex->texflag= MTEX_3TAP_BUMP | MTEX_BUMP_OBJECTSPACE | MTEX_BUMP_FLIPPED;
716
717         mtex->colormodel= 0;
718         mtex->r= 1.0;
719         mtex->g= 0.0;
720         mtex->b= 1.0;
721         mtex->k= 1.0;
722         mtex->def_var= 1.0;
723         mtex->blendtype= MTEX_BLEND;
724         mtex->colfac= 1.0;
725         mtex->norfac= 1.0;
726         mtex->varfac= 1.0;
727         mtex->dispfac=0.2;
728         mtex->colspecfac= 1.0f;
729         mtex->mirrfac= 1.0f;
730         mtex->alphafac= 1.0f;
731         mtex->difffac= 1.0f;
732         mtex->specfac= 1.0f;
733         mtex->emitfac= 1.0f;
734         mtex->hardfac= 1.0f;
735         mtex->raymirrfac= 1.0f;
736         mtex->translfac= 1.0f;
737         mtex->ambfac= 1.0f;
738         mtex->colemitfac= 1.0f;
739         mtex->colreflfac= 1.0f;
740         mtex->coltransfac= 1.0f;
741         mtex->densfac= 1.0f;
742         mtex->scatterfac= 1.0f;
743         mtex->reflfac= 1.0f;
744         mtex->shadowfac= 1.0f;
745         mtex->zenupfac= 1.0f;
746         mtex->zendownfac= 1.0f;
747         mtex->blendfac= 1.0f;
748         mtex->timefac= 1.0f;
749         mtex->lengthfac= 1.0f;
750         mtex->clumpfac= 1.0f;
751         mtex->kinkfac= 1.0f;
752         mtex->roughfac= 1.0f;
753         mtex->padensfac= 1.0f;
754         mtex->lifefac= 1.0f;
755         mtex->sizefac= 1.0f;
756         mtex->ivelfac= 1.0f;
757         mtex->dampfac= 1.0f;
758         mtex->gravityfac= 1.0f;
759         mtex->fieldfac= 1.0f;
760         mtex->normapspace= MTEX_NSPACE_TANGENT;
761 }
762
763
764 /* ------------------------------------------------------------------------- */
765
766 MTex *add_mtex(void)
767 {
768         MTex *mtex;
769         
770         mtex= MEM_callocN(sizeof(MTex), "add_mtex");
771         
772         default_mtex(mtex);
773         
774         return mtex;
775 }
776
777 /* slot -1 for first free ID */
778 MTex *add_mtex_id(ID *id, int slot)
779 {
780         MTex **mtex_ar;
781         short act;
782
783         give_active_mtex(id, &mtex_ar, &act);
784
785         if(mtex_ar==NULL) {
786                 return NULL;
787         }
788         
789         if(slot==-1) {
790                 /* find first free */
791                 int i;          
792                 for (i= 0; i < MAX_MTEX; i++) {
793                         if (!mtex_ar[i]) {
794                                 slot= i;
795                                 break;
796                         }
797                 }
798                 if(slot == -1) {
799                         return NULL;
800                 }
801         }
802         else {
803                 /* make sure slot is valid */
804                 if(slot < 0 || slot >= MAX_MTEX) {
805                         return NULL;
806                 }
807         }
808
809         if (mtex_ar[slot]) {
810                 id_us_min((ID *)mtex_ar[slot]->tex);
811                 MEM_freeN(mtex_ar[slot]);
812                 mtex_ar[slot]= NULL;
813         }
814
815         mtex_ar[slot]= add_mtex();
816
817         return mtex_ar[slot];
818 }
819
820 /* ------------------------------------------------------------------------- */
821
822 Tex *copy_texture(Tex *tex)
823 {
824         Tex *texn;
825         
826         texn= copy_libblock(&tex->id);
827         if(texn->type==TEX_IMAGE) id_us_plus((ID *)texn->ima);
828         else texn->ima= NULL;
829         
830         if(texn->plugin) {
831                 texn->plugin= MEM_dupallocN(texn->plugin);
832                 open_plugin_tex(texn->plugin);
833         }
834         
835         if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
836         if(texn->env) texn->env= BKE_copy_envmap(texn->env);
837         if(texn->pd) texn->pd= BKE_copy_pointdensity(texn->pd);
838         if(texn->vd) texn->vd= MEM_dupallocN(texn->vd);
839         if(texn->ot) texn->ot= BKE_copy_oceantex(texn->ot);
840         if(tex->preview) texn->preview = BKE_previewimg_copy(tex->preview);
841
842         if(tex->nodetree) {
843                 if (tex->nodetree->execdata) {
844                         ntreeTexEndExecTree(tex->nodetree->execdata, 1);
845                 }
846                 texn->nodetree= ntreeCopyTree(tex->nodetree); 
847         }
848         
849         return texn;
850 }
851
852 /* texture copy without adding to main dbase */
853 Tex *localize_texture(Tex *tex)
854 {
855         Tex *texn;
856         
857         texn= copy_libblock(&tex->id);
858         BLI_remlink(&G.main->tex, texn);
859         
860         /* image texture: free_texture also doesn't decrease */
861         
862         if(texn->plugin) {
863                 texn->plugin= MEM_dupallocN(texn->plugin);
864                 open_plugin_tex(texn->plugin);
865         }
866         
867         if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
868         if(texn->env) {
869                 texn->env= BKE_copy_envmap(texn->env);
870                 id_us_min(&texn->env->ima->id);
871         }
872         if(texn->pd) texn->pd= BKE_copy_pointdensity(texn->pd);
873         if(texn->vd) {
874                 texn->vd= MEM_dupallocN(texn->vd);
875                 if(texn->vd->dataset)
876                         texn->vd->dataset= MEM_dupallocN(texn->vd->dataset);
877         }
878         if(texn->ot) {
879                 texn->ot= BKE_copy_oceantex(tex->ot);
880         }
881         
882         texn->preview = NULL;
883         
884         if(tex->nodetree) {
885                 texn->nodetree= ntreeLocalize(tex->nodetree);
886         }
887         
888         return texn;
889 }
890
891
892 /* ------------------------------------------------------------------------- */
893
894 static void extern_local_texture(Tex *tex)
895 {
896         id_lib_extern((ID *)tex->ima);
897 }
898
899 void make_local_texture(Tex *tex)
900 {
901         Main *bmain= G.main;
902         Material *ma;
903         World *wrld;
904         Lamp *la;
905         Brush *br;
906         ParticleSettings *pa;
907         int a, is_local= FALSE, is_lib= FALSE;
908
909         /* - only lib users: do nothing
910                 * - only local users: set flag
911                 * - mixed: make copy
912                 */
913         
914         if(tex->id.lib==NULL) return;
915
916         if(tex->id.us==1) {
917                 id_clear_lib_data(bmain, &tex->id);
918                 extern_local_texture(tex);
919                 return;
920         }
921         
922         ma= bmain->mat.first;
923         while(ma) {
924                 for(a=0; a<MAX_MTEX; a++) {
925                         if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
926                                 if(ma->id.lib) is_lib= TRUE;
927                                 else is_local= TRUE;
928                         }
929                 }
930                 ma= ma->id.next;
931         }
932         la= bmain->lamp.first;
933         while(la) {
934                 for(a=0; a<MAX_MTEX; a++) {
935                         if(la->mtex[a] && la->mtex[a]->tex==tex) {
936                                 if(la->id.lib) is_lib= TRUE;
937                                 else is_local= TRUE;
938                         }
939                 }
940                 la= la->id.next;
941         }
942         wrld= bmain->world.first;
943         while(wrld) {
944                 for(a=0; a<MAX_MTEX; a++) {
945                         if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
946                                 if(wrld->id.lib) is_lib= TRUE;
947                                 else is_local= TRUE;
948                         }
949                 }
950                 wrld= wrld->id.next;
951         }
952         br= bmain->brush.first;
953         while(br) {
954                 if(br->mtex.tex==tex) {
955                         if(br->id.lib) is_lib= TRUE;
956                         else is_local= TRUE;
957                 }
958                 br= br->id.next;
959         }
960         pa= bmain->particle.first;
961         while(pa) {
962                 for(a=0; a<MAX_MTEX; a++) {
963                         if(pa->mtex[a] && pa->mtex[a]->tex==tex) {
964                                 if(pa->id.lib) is_lib= TRUE;
965                                 else is_local= TRUE;
966                         }
967                 }
968                 pa= pa->id.next;
969         }
970         
971         if(is_local && is_lib == FALSE) {
972                 id_clear_lib_data(bmain, &tex->id);
973                 extern_local_texture(tex);
974         }
975         else if(is_local && is_lib) {
976                 Tex *texn= copy_texture(tex);
977
978                 texn->id.us= 0;
979
980                 /* Remap paths of new ID using old library as base. */
981                 BKE_id_lib_local_paths(bmain, &texn->id);
982                 
983                 ma= bmain->mat.first;
984                 while(ma) {
985                         for(a=0; a<MAX_MTEX; a++) {
986                                 if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
987                                         if(ma->id.lib==NULL) {
988                                                 ma->mtex[a]->tex= texn;
989                                                 texn->id.us++;
990                                                 tex->id.us--;
991                                         }
992                                 }
993                         }
994                         ma= ma->id.next;
995                 }
996                 la= bmain->lamp.first;
997                 while(la) {
998                         for(a=0; a<MAX_MTEX; a++) {
999                                 if(la->mtex[a] && la->mtex[a]->tex==tex) {
1000                                         if(la->id.lib==NULL) {
1001                                                 la->mtex[a]->tex= texn;
1002                                                 texn->id.us++;
1003                                                 tex->id.us--;
1004                                         }
1005                                 }
1006                         }
1007                         la= la->id.next;
1008                 }
1009                 wrld= bmain->world.first;
1010                 while(wrld) {
1011                         for(a=0; a<MAX_MTEX; a++) {
1012                                 if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
1013                                         if(wrld->id.lib==NULL) {
1014                                                 wrld->mtex[a]->tex= texn;
1015                                                 texn->id.us++;
1016                                                 tex->id.us--;
1017                                         }
1018                                 }
1019                         }
1020                         wrld= wrld->id.next;
1021                 }
1022                 br= bmain->brush.first;
1023                 while(br) {
1024                         if(br->mtex.tex==tex) {
1025                                 if(br->id.lib==NULL) {
1026                                         br->mtex.tex= texn;
1027                                         texn->id.us++;
1028                                         tex->id.us--;
1029                                 }
1030                         }
1031                         br= br->id.next;
1032                 }
1033                 pa= bmain->particle.first;
1034                 while(pa) {
1035                         for(a=0; a<MAX_MTEX; a++) {
1036                                 if(pa->mtex[a] && pa->mtex[a]->tex==tex) {
1037                                         if(pa->id.lib==NULL) {
1038                                                 pa->mtex[a]->tex= texn;
1039                                                 texn->id.us++;
1040                                                 tex->id.us--;
1041                                         }
1042                                 }
1043                         }
1044                         pa= pa->id.next;
1045                 }
1046         }
1047 }
1048
1049 /* ------------------------------------------------------------------------- */
1050
1051 void autotexname(Tex *tex)
1052 {
1053         Main *bmain= G.main;
1054         char texstr[20][15]= {"None"  , "Clouds" , "Wood", "Marble", "Magic"  , "Blend",
1055                 "Stucci", "Noise"  , "Image", "Plugin", "EnvMap" , "Musgrave",
1056                 "Voronoi", "DistNoise", "Point Density", "Voxel Data", "Ocean", "", "", ""};
1057         Image *ima;
1058         char di[FILE_MAXDIR], fi[FILE_MAXFILE];
1059         
1060         if(tex) {
1061                 if(tex->use_nodes) {
1062                         new_id(&bmain->tex, (ID *)tex, "Noddy");
1063                 }
1064                 else
1065                 if(tex->type==TEX_IMAGE) {
1066                         ima= tex->ima;
1067                         if(ima) {
1068                                 BLI_strncpy(di, ima->name, sizeof(di));
1069                                 BLI_splitdirstring(di, fi);
1070                                 strcpy(di, "I.");
1071                                 strcat(di, fi);
1072                                 new_id(&bmain->tex, (ID *)tex, di);
1073                         }
1074                         else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
1075                 }
1076                 else if(tex->type==TEX_PLUGIN && tex->plugin) new_id(&bmain->tex, (ID *)tex, tex->plugin->pname);
1077                 else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
1078         }
1079 }
1080
1081 /* ------------------------------------------------------------------------- */
1082
1083 Tex *give_current_object_texture(Object *ob)
1084 {
1085         Material *ma, *node_ma;
1086         Tex *tex= NULL;
1087         
1088         if(ob==NULL) return NULL;
1089         if(ob->totcol==0 && !(ob->type==OB_LAMP)) return NULL;
1090         
1091         if(ob->type==OB_LAMP) {
1092                 tex= give_current_lamp_texture(ob->data);
1093         } else {
1094                 ma= give_current_material(ob, ob->actcol);
1095
1096                 if((node_ma=give_node_material(ma)))
1097                         ma= node_ma;
1098
1099                 tex= give_current_material_texture(ma);
1100         }
1101         
1102         return tex;
1103 }
1104
1105 Tex *give_current_lamp_texture(Lamp *la)
1106 {
1107         MTex *mtex= NULL;
1108         Tex *tex= NULL;
1109
1110         if(la) {
1111                 mtex= la->mtex[(int)(la->texact)];
1112                 if(mtex) tex= mtex->tex;
1113         }
1114
1115         return tex;
1116 }
1117
1118 void set_current_lamp_texture(Lamp *la, Tex *newtex)
1119 {
1120         int act= la->texact;
1121
1122         if(la->mtex[act] && la->mtex[act]->tex)
1123                 id_us_min(&la->mtex[act]->tex->id);
1124
1125         if(newtex) {
1126                 if(!la->mtex[act]) {
1127                         la->mtex[act]= add_mtex();
1128                         la->mtex[act]->texco= TEXCO_GLOB;
1129                 }
1130                 
1131                 la->mtex[act]->tex= newtex;
1132                 id_us_plus(&newtex->id);
1133         }
1134         else if(la->mtex[act]) {
1135                 MEM_freeN(la->mtex[act]);
1136                 la->mtex[act]= NULL;
1137         }
1138 }
1139
1140 bNode *give_current_material_texture_node(Material *ma)
1141 {
1142         if(ma && ma->use_nodes && ma->nodetree)
1143                 return nodeGetActiveID(ma->nodetree, ID_TE);
1144         
1145         return NULL;
1146 }
1147
1148 Tex *give_current_material_texture(Material *ma)
1149 {
1150         MTex *mtex= NULL;
1151         Tex *tex= NULL;
1152         bNode *node;
1153         
1154         if(ma && ma->use_nodes && ma->nodetree) {
1155                 /* first check texture, then material, this works together
1156                    with a hack that clears the active ID flag for textures on
1157                    making a material node active */
1158                 node= nodeGetActiveID(ma->nodetree, ID_TE);
1159
1160                 if(node) {
1161                         tex= (Tex *)node->id;
1162                         ma= NULL;
1163                 }
1164         }
1165
1166         if(ma) {
1167                 mtex= ma->mtex[(int)(ma->texact)];
1168                 if(mtex) tex= mtex->tex;
1169         }
1170         
1171         return tex;
1172 }
1173
1174 int give_active_mtex(ID *id, MTex ***mtex_ar, short *act)
1175 {
1176         switch(GS(id->name)) {
1177         case ID_MA:
1178                 *mtex_ar=               ((Material *)id)->mtex;
1179                 if(act) *act=   (((Material *)id)->texact);
1180                 break;
1181         case ID_WO:
1182                 *mtex_ar=               ((World *)id)->mtex;
1183                 if(act) *act=   (((World *)id)->texact);
1184                 break;
1185         case ID_LA:
1186                 *mtex_ar=               ((Lamp *)id)->mtex;
1187                 if(act) *act=   (((Lamp *)id)->texact);
1188                 break;
1189         case ID_PA:
1190                 *mtex_ar=               ((ParticleSettings *)id)->mtex;
1191                 if(act) *act=   (((ParticleSettings *)id)->texact);
1192                 break;
1193         default:
1194                 *mtex_ar = NULL;
1195                 if(act) *act=   0;
1196                 return FALSE;
1197         }
1198
1199         return TRUE;
1200 }
1201
1202 void set_active_mtex(ID *id, short act)
1203 {
1204         if(act<0)                               act= 0;
1205         else if(act>=MAX_MTEX)  act= MAX_MTEX-1;
1206
1207         switch(GS(id->name)) {
1208         case ID_MA:
1209                 ((Material *)id)->texact= act;
1210                 break;
1211         case ID_WO:
1212                 ((World *)id)->texact= act;
1213                 break;
1214         case ID_LA:
1215                 ((Lamp *)id)->texact= act;
1216                 break;
1217         case ID_PA:
1218                 ((ParticleSettings *)id)->texact= act;
1219                 break;
1220         }
1221 }
1222
1223 void set_current_material_texture(Material *ma, Tex *newtex)
1224 {
1225         Tex *tex= NULL;
1226         bNode *node;
1227         
1228         if(ma && ma->use_nodes && ma->nodetree) {
1229                 node= nodeGetActiveID(ma->nodetree, ID_TE);
1230
1231                 if(node) {
1232                         tex= (Tex *)node->id;
1233                         id_us_min(&tex->id);
1234                         node->id= &newtex->id;
1235                         id_us_plus(&newtex->id);
1236                         ma= NULL;
1237                 }
1238         }
1239         if(ma) {
1240                 int act= (int)ma->texact;
1241
1242                 tex= (ma->mtex[act])? ma->mtex[act]->tex: NULL;
1243                 id_us_min(&tex->id);
1244
1245                 if(newtex) {
1246                         if(!ma->mtex[act])
1247                                 ma->mtex[act]= add_mtex();
1248                         
1249                         ma->mtex[act]->tex= newtex;
1250                         id_us_plus(&newtex->id);
1251                 }
1252                 else if(ma->mtex[act]) {
1253                         MEM_freeN(ma->mtex[act]);
1254                         ma->mtex[act]= NULL;
1255                 }
1256         }
1257 }
1258
1259 int has_current_material_texture(Material *ma)
1260 {
1261         bNode *node;
1262
1263         if(ma && ma->use_nodes && ma->nodetree) {
1264                 node= nodeGetActiveID(ma->nodetree, ID_TE);
1265
1266                 if(node)
1267                         return 1;
1268         }
1269
1270         return (ma != NULL);
1271 }
1272
1273 Tex *give_current_world_texture(World *world)
1274 {
1275         MTex *mtex= NULL;
1276         Tex *tex= NULL;
1277         
1278         if(!world) return NULL;
1279         
1280         mtex= world->mtex[(int)(world->texact)];
1281         if(mtex) tex= mtex->tex;
1282         
1283         return tex;
1284 }
1285
1286 void set_current_world_texture(World *wo, Tex *newtex)
1287 {
1288         int act= wo->texact;
1289
1290         if(wo->mtex[act] && wo->mtex[act]->tex)
1291                 id_us_min(&wo->mtex[act]->tex->id);
1292
1293         if(newtex) {
1294                 if(!wo->mtex[act]) {
1295                         wo->mtex[act]= add_mtex();
1296                         wo->mtex[act]->texco= TEXCO_VIEW;
1297                 }
1298                 
1299                 wo->mtex[act]->tex= newtex;
1300                 id_us_plus(&newtex->id);
1301         }
1302         else if(wo->mtex[act]) {
1303                 MEM_freeN(wo->mtex[act]);
1304                 wo->mtex[act]= NULL;
1305         }
1306 }
1307
1308 Tex *give_current_brush_texture(Brush *br)
1309 {
1310         return br->mtex.tex;
1311 }
1312
1313 void set_current_brush_texture(Brush *br, Tex *newtex)
1314 {
1315         if(br->mtex.tex)
1316                 id_us_min(&br->mtex.tex->id);
1317
1318         if(newtex) {
1319                 br->mtex.tex= newtex;
1320                 id_us_plus(&newtex->id);
1321         }
1322 }
1323
1324 Tex *give_current_particle_texture(ParticleSettings *part)
1325 {
1326         MTex *mtex= NULL;
1327         Tex *tex= NULL;
1328         
1329         if(!part) return NULL;
1330         
1331         mtex= part->mtex[(int)(part->texact)];
1332         if(mtex) tex= mtex->tex;
1333         
1334         return tex;
1335 }
1336
1337 void set_current_particle_texture(ParticleSettings *part, Tex *newtex)
1338 {
1339         int act= part->texact;
1340
1341         if(part->mtex[act] && part->mtex[act]->tex)
1342                 id_us_min(&part->mtex[act]->tex->id);
1343
1344         if(newtex) {
1345                 if(!part->mtex[act]) {
1346                         part->mtex[act]= add_mtex();
1347                         part->mtex[act]->texco= TEXCO_ORCO;
1348                         part->mtex[act]->blendtype= MTEX_MUL;
1349                 }
1350                 
1351                 part->mtex[act]->tex= newtex;
1352                 id_us_plus(&newtex->id);
1353         }
1354         else if(part->mtex[act]) {
1355                 MEM_freeN(part->mtex[act]);
1356                 part->mtex[act]= NULL;
1357         }
1358 }
1359
1360 /* ------------------------------------------------------------------------- */
1361
1362 EnvMap *BKE_add_envmap(void)
1363 {
1364         EnvMap *env;
1365         
1366         env= MEM_callocN(sizeof(EnvMap), "envmap");
1367         env->type= ENV_CUBE;
1368         env->stype= ENV_ANIM;
1369         env->clipsta= 0.1;
1370         env->clipend= 100.0;
1371         env->cuberes= 600;
1372         env->viewscale = 0.5;
1373         
1374         return env;
1375
1376
1377 /* ------------------------------------------------------------------------- */
1378
1379 EnvMap *BKE_copy_envmap(EnvMap *env)
1380 {
1381         EnvMap *envn;
1382         int a;
1383         
1384         envn= MEM_dupallocN(env);
1385         envn->ok= 0;
1386         for(a=0; a<6; a++) envn->cube[a]= NULL;
1387         if(envn->ima) id_us_plus((ID *)envn->ima);
1388         
1389         return envn;
1390 }
1391
1392 /* ------------------------------------------------------------------------- */
1393
1394 void BKE_free_envmapdata(EnvMap *env)
1395 {
1396         unsigned int part;
1397         
1398         for(part=0; part<6; part++) {
1399                 if(env->cube[part])
1400                         IMB_freeImBuf(env->cube[part]);
1401                 env->cube[part]= NULL;
1402         }
1403         env->ok= 0;
1404 }
1405
1406 /* ------------------------------------------------------------------------- */
1407
1408 void BKE_free_envmap(EnvMap *env)
1409 {
1410         
1411         BKE_free_envmapdata(env);
1412         MEM_freeN(env);
1413         
1414 }
1415
1416 /* ------------------------------------------------------------------------- */
1417
1418 PointDensity *BKE_add_pointdensity(void)
1419 {
1420         PointDensity *pd;
1421         
1422         pd= MEM_callocN(sizeof(PointDensity), "pointdensity");
1423         pd->flag = 0;
1424         pd->radius = 0.3f;
1425         pd->falloff_type = TEX_PD_FALLOFF_STD;
1426         pd->falloff_softness = 2.0;
1427         pd->source = TEX_PD_PSYS;
1428         pd->point_tree = NULL;
1429         pd->point_data = NULL;
1430         pd->noise_size = 0.5f;
1431         pd->noise_depth = 1;
1432         pd->noise_fac = 1.0f;
1433         pd->noise_influence = TEX_PD_NOISE_STATIC;
1434         pd->coba = add_colorband(1);
1435         pd->speed_scale = 1.0f;
1436         pd->totpoints = 0;
1437         pd->object = NULL;
1438         pd->psys = 0;
1439         pd->psys_cache_space= TEX_PD_WORLDSPACE;
1440         pd->falloff_curve = curvemapping_add(1, 0, 0, 1, 1);
1441
1442         pd->falloff_curve->preset = CURVE_PRESET_LINE;
1443         pd->falloff_curve->cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
1444         curvemap_reset(pd->falloff_curve->cm, &pd->falloff_curve->clipr, pd->falloff_curve->preset, CURVEMAP_SLOPE_POSITIVE);
1445         curvemapping_changed(pd->falloff_curve, 0);
1446
1447         return pd;
1448
1449
1450 PointDensity *BKE_copy_pointdensity(PointDensity *pd)
1451 {
1452         PointDensity *pdn;
1453
1454         pdn= MEM_dupallocN(pd);
1455         pdn->point_tree = NULL;
1456         pdn->point_data = NULL;
1457         if(pdn->coba) pdn->coba= MEM_dupallocN(pdn->coba);
1458         pdn->falloff_curve = curvemapping_copy(pdn->falloff_curve); /* can be NULL */
1459         return pdn;
1460 }
1461
1462 void BKE_free_pointdensitydata(PointDensity *pd)
1463 {
1464         if (pd->point_tree) {
1465                 BLI_bvhtree_free(pd->point_tree);
1466                 pd->point_tree = NULL;
1467         }
1468         if (pd->point_data) {
1469                 MEM_freeN(pd->point_data);
1470                 pd->point_data = NULL;
1471         }
1472         if(pd->coba) {
1473                 MEM_freeN(pd->coba);
1474                 pd->coba = NULL;
1475         }
1476
1477         curvemapping_free(pd->falloff_curve); /* can be NULL */
1478 }
1479
1480 void BKE_free_pointdensity(PointDensity *pd)
1481 {
1482         BKE_free_pointdensitydata(pd);
1483         MEM_freeN(pd);
1484 }
1485
1486 /* ------------------------------------------------------------------------- */
1487
1488 void BKE_free_voxeldatadata(struct VoxelData *vd)
1489 {
1490         if (vd->dataset) {
1491                 MEM_freeN(vd->dataset);
1492                 vd->dataset = NULL;
1493         }
1494
1495 }
1496  
1497 void BKE_free_voxeldata(struct VoxelData *vd)
1498 {
1499         BKE_free_voxeldatadata(vd);
1500         MEM_freeN(vd);
1501 }
1502  
1503 struct VoxelData *BKE_add_voxeldata(void)
1504 {
1505         VoxelData *vd;
1506
1507         vd= MEM_callocN(sizeof(struct VoxelData), "voxeldata");
1508         vd->dataset = NULL;
1509         vd->resol[0] = vd->resol[1] = vd->resol[2] = 1;
1510         vd->interp_type= TEX_VD_LINEAR;
1511         vd->file_format= TEX_VD_SMOKE;
1512         vd->int_multiplier = 1.0;
1513         vd->extend = TEX_CLIP;
1514         vd->object = NULL;
1515         vd->cachedframe = -1;
1516         vd->ok = 0;
1517         
1518         return vd;
1519  }
1520  
1521 struct VoxelData *BKE_copy_voxeldata(struct VoxelData *vd)
1522 {
1523         VoxelData *vdn;
1524
1525         vdn= MEM_dupallocN(vd); 
1526         vdn->dataset = NULL;
1527
1528         return vdn;
1529 }
1530
1531 /* ------------------------------------------------------------------------- */
1532
1533 struct OceanTex *BKE_add_oceantex(void)
1534 {
1535         OceanTex *ot;
1536         
1537         ot= MEM_callocN(sizeof(struct OceanTex), "ocean texture");
1538         ot->output = TEX_OCN_DISPLACEMENT;
1539         ot->object = NULL;
1540         
1541         return ot;
1542 }
1543
1544 struct OceanTex *BKE_copy_oceantex(struct OceanTex *ot)
1545 {
1546         OceanTex *otn= MEM_dupallocN(ot);
1547         
1548         return otn;
1549 }
1550
1551 void BKE_free_oceantex(struct OceanTex *ot)
1552 {
1553         MEM_freeN(ot);
1554 }
1555
1556
1557 /* ------------------------------------------------------------------------- */
1558 int BKE_texture_dependsOnTime(const struct Tex *texture)
1559 {
1560         if(texture->plugin) {
1561                 // assume all plugins depend on time
1562                 return 1;
1563         } 
1564         else if(        texture->ima && 
1565                         ELEM(texture->ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
1566                 return 1;
1567         }
1568         else if(texture->adt) {
1569                 // assume anything in adt means the texture is animated
1570                 return 1;
1571         }
1572         else if(texture->type == TEX_NOISE) {
1573                 // noise always varies with time
1574                 return 1;
1575         }
1576         return 0;
1577 }
1578
1579 /* ------------------------------------------------------------------------- */