Camera tracking: improvements of track preview widget
[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                 /* 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(const ColorBand *coba, float in, float out[4])
353 {
354         const 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         mtex->texflag= MTEX_3TAP_BUMP | MTEX_BUMP_OBJECTSPACE;
713         mtex->colormodel= 0;
714         mtex->r= 1.0;
715         mtex->g= 0.0;
716         mtex->b= 1.0;
717         mtex->k= 1.0;
718         mtex->def_var= 1.0;
719         mtex->blendtype= MTEX_BLEND;
720         mtex->colfac= 1.0;
721         mtex->norfac= 1.0;
722         mtex->varfac= 1.0;
723         mtex->dispfac=0.2;
724         mtex->colspecfac= 1.0f;
725         mtex->mirrfac= 1.0f;
726         mtex->alphafac= 1.0f;
727         mtex->difffac= 1.0f;
728         mtex->specfac= 1.0f;
729         mtex->emitfac= 1.0f;
730         mtex->hardfac= 1.0f;
731         mtex->raymirrfac= 1.0f;
732         mtex->translfac= 1.0f;
733         mtex->ambfac= 1.0f;
734         mtex->colemitfac= 1.0f;
735         mtex->colreflfac= 1.0f;
736         mtex->coltransfac= 1.0f;
737         mtex->densfac= 1.0f;
738         mtex->scatterfac= 1.0f;
739         mtex->reflfac= 1.0f;
740         mtex->shadowfac= 1.0f;
741         mtex->zenupfac= 1.0f;
742         mtex->zendownfac= 1.0f;
743         mtex->blendfac= 1.0f;
744         mtex->timefac= 1.0f;
745         mtex->lengthfac= 1.0f;
746         mtex->clumpfac= 1.0f;
747         mtex->kinkfac= 1.0f;
748         mtex->roughfac= 1.0f;
749         mtex->padensfac= 1.0f;
750         mtex->lifefac= 1.0f;
751         mtex->sizefac= 1.0f;
752         mtex->ivelfac= 1.0f;
753         mtex->dampfac= 1.0f;
754         mtex->gravityfac= 1.0f;
755         mtex->fieldfac= 1.0f;
756         mtex->normapspace= MTEX_NSPACE_TANGENT;
757 }
758
759
760 /* ------------------------------------------------------------------------- */
761
762 MTex *add_mtex(void)
763 {
764         MTex *mtex;
765         
766         mtex= MEM_callocN(sizeof(MTex), "add_mtex");
767         
768         default_mtex(mtex);
769         
770         return mtex;
771 }
772
773 /* slot -1 for first free ID */
774 MTex *add_mtex_id(ID *id, int slot)
775 {
776         MTex **mtex_ar;
777         short act;
778
779         give_active_mtex(id, &mtex_ar, &act);
780
781         if(mtex_ar==NULL) {
782                 return NULL;
783         }
784         
785         if(slot==-1) {
786                 /* find first free */
787                 int i;          
788                 for (i= 0; i < MAX_MTEX; i++) {
789                         if (!mtex_ar[i]) {
790                                 slot= i;
791                                 break;
792                         }
793                 }
794                 if(slot == -1) {
795                         return NULL;
796                 }
797         }
798         else {
799                 /* make sure slot is valid */
800                 if(slot < 0 || slot >= MAX_MTEX) {
801                         return NULL;
802                 }
803         }
804
805         if (mtex_ar[slot]) {
806                 id_us_min((ID *)mtex_ar[slot]->tex);
807                 MEM_freeN(mtex_ar[slot]);
808                 mtex_ar[slot]= NULL;
809         }
810
811         mtex_ar[slot]= add_mtex();
812
813         return mtex_ar[slot];
814 }
815
816 /* ------------------------------------------------------------------------- */
817
818 Tex *copy_texture(Tex *tex)
819 {
820         Tex *texn;
821         
822         texn= copy_libblock(&tex->id);
823         if(texn->type==TEX_IMAGE) id_us_plus((ID *)texn->ima);
824         else texn->ima= NULL;
825         
826         if(texn->plugin) {
827                 texn->plugin= MEM_dupallocN(texn->plugin);
828                 open_plugin_tex(texn->plugin);
829         }
830         
831         if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
832         if(texn->env) texn->env= BKE_copy_envmap(texn->env);
833         if(texn->pd) texn->pd= BKE_copy_pointdensity(texn->pd);
834         if(texn->vd) texn->vd= MEM_dupallocN(texn->vd);
835         if(texn->ot) texn->ot= BKE_copy_oceantex(texn->ot);
836         if(tex->preview) texn->preview = BKE_previewimg_copy(tex->preview);
837
838         if(tex->nodetree) {
839                 if (tex->nodetree->execdata) {
840                         ntreeTexEndExecTree(tex->nodetree->execdata, 1);
841                 }
842                 texn->nodetree= ntreeCopyTree(tex->nodetree); 
843         }
844         
845         return texn;
846 }
847
848 /* texture copy without adding to main dbase */
849 Tex *localize_texture(Tex *tex)
850 {
851         Tex *texn;
852         
853         texn= copy_libblock(&tex->id);
854         BLI_remlink(&G.main->tex, texn);
855         
856         /* image texture: free_texture also doesn't decrease */
857         
858         if(texn->plugin) {
859                 texn->plugin= MEM_dupallocN(texn->plugin);
860                 open_plugin_tex(texn->plugin);
861         }
862         
863         if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
864         if(texn->env) {
865                 texn->env= BKE_copy_envmap(texn->env);
866                 id_us_min(&texn->env->ima->id);
867         }
868         if(texn->pd) texn->pd= BKE_copy_pointdensity(texn->pd);
869         if(texn->vd) {
870                 texn->vd= MEM_dupallocN(texn->vd);
871                 if(texn->vd->dataset)
872                         texn->vd->dataset= MEM_dupallocN(texn->vd->dataset);
873         }
874         if(texn->ot) {
875                 texn->ot= BKE_copy_oceantex(tex->ot);
876         }
877         
878         texn->preview = NULL;
879         
880         if(tex->nodetree) {
881                 texn->nodetree= ntreeLocalize(tex->nodetree);
882         }
883         
884         return texn;
885 }
886
887
888 /* ------------------------------------------------------------------------- */
889
890 static void extern_local_texture(Tex *tex)
891 {
892         id_lib_extern((ID *)tex->ima);
893 }
894
895 void make_local_texture(Tex *tex)
896 {
897         Main *bmain= G.main;
898         Material *ma;
899         World *wrld;
900         Lamp *la;
901         Brush *br;
902         ParticleSettings *pa;
903         int a, is_local= FALSE, is_lib= FALSE;
904
905         /* - only lib users: do nothing
906                 * - only local users: set flag
907                 * - mixed: make copy
908                 */
909         
910         if(tex->id.lib==NULL) return;
911
912         if(tex->id.us==1) {
913                 id_clear_lib_data(bmain, &tex->id);
914                 extern_local_texture(tex);
915                 return;
916         }
917         
918         ma= bmain->mat.first;
919         while(ma) {
920                 for(a=0; a<MAX_MTEX; a++) {
921                         if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
922                                 if(ma->id.lib) is_lib= TRUE;
923                                 else is_local= TRUE;
924                         }
925                 }
926                 ma= ma->id.next;
927         }
928         la= bmain->lamp.first;
929         while(la) {
930                 for(a=0; a<MAX_MTEX; a++) {
931                         if(la->mtex[a] && la->mtex[a]->tex==tex) {
932                                 if(la->id.lib) is_lib= TRUE;
933                                 else is_local= TRUE;
934                         }
935                 }
936                 la= la->id.next;
937         }
938         wrld= bmain->world.first;
939         while(wrld) {
940                 for(a=0; a<MAX_MTEX; a++) {
941                         if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
942                                 if(wrld->id.lib) is_lib= TRUE;
943                                 else is_local= TRUE;
944                         }
945                 }
946                 wrld= wrld->id.next;
947         }
948         br= bmain->brush.first;
949         while(br) {
950                 if(br->mtex.tex==tex) {
951                         if(br->id.lib) is_lib= TRUE;
952                         else is_local= TRUE;
953                 }
954                 br= br->id.next;
955         }
956         pa= bmain->particle.first;
957         while(pa) {
958                 for(a=0; a<MAX_MTEX; a++) {
959                         if(pa->mtex[a] && pa->mtex[a]->tex==tex) {
960                                 if(pa->id.lib) is_lib= TRUE;
961                                 else is_local= TRUE;
962                         }
963                 }
964                 pa= pa->id.next;
965         }
966         
967         if(is_local && is_lib == FALSE) {
968                 id_clear_lib_data(bmain, &tex->id);
969                 extern_local_texture(tex);
970         }
971         else if(is_local && is_lib) {
972                 Tex *tex_new= copy_texture(tex);
973
974                 tex_new->id.us= 0;
975
976                 /* Remap paths of new ID using old library as base. */
977                 BKE_id_lib_local_paths(bmain, tex->id.lib, &tex_new->id);
978                 
979                 ma= bmain->mat.first;
980                 while(ma) {
981                         for(a=0; a<MAX_MTEX; a++) {
982                                 if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
983                                         if(ma->id.lib==NULL) {
984                                                 ma->mtex[a]->tex= tex_new;
985                                                 tex_new->id.us++;
986                                                 tex->id.us--;
987                                         }
988                                 }
989                         }
990                         ma= ma->id.next;
991                 }
992                 la= bmain->lamp.first;
993                 while(la) {
994                         for(a=0; a<MAX_MTEX; a++) {
995                                 if(la->mtex[a] && la->mtex[a]->tex==tex) {
996                                         if(la->id.lib==NULL) {
997                                                 la->mtex[a]->tex= tex_new;
998                                                 tex_new->id.us++;
999                                                 tex->id.us--;
1000                                         }
1001                                 }
1002                         }
1003                         la= la->id.next;
1004                 }
1005                 wrld= bmain->world.first;
1006                 while(wrld) {
1007                         for(a=0; a<MAX_MTEX; a++) {
1008                                 if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
1009                                         if(wrld->id.lib==NULL) {
1010                                                 wrld->mtex[a]->tex= tex_new;
1011                                                 tex_new->id.us++;
1012                                                 tex->id.us--;
1013                                         }
1014                                 }
1015                         }
1016                         wrld= wrld->id.next;
1017                 }
1018                 br= bmain->brush.first;
1019                 while(br) {
1020                         if(br->mtex.tex==tex) {
1021                                 if(br->id.lib==NULL) {
1022                                         br->mtex.tex= tex_new;
1023                                         tex_new->id.us++;
1024                                         tex->id.us--;
1025                                 }
1026                         }
1027                         br= br->id.next;
1028                 }
1029                 pa= bmain->particle.first;
1030                 while(pa) {
1031                         for(a=0; a<MAX_MTEX; a++) {
1032                                 if(pa->mtex[a] && pa->mtex[a]->tex==tex) {
1033                                         if(pa->id.lib==NULL) {
1034                                                 pa->mtex[a]->tex= tex_new;
1035                                                 tex_new->id.us++;
1036                                                 tex->id.us--;
1037                                         }
1038                                 }
1039                         }
1040                         pa= pa->id.next;
1041                 }
1042         }
1043 }
1044
1045 /* ------------------------------------------------------------------------- */
1046
1047 void autotexname(Tex *tex)
1048 {
1049         Main *bmain= G.main;
1050         char texstr[20][15]= {"None"  , "Clouds" , "Wood", "Marble", "Magic"  , "Blend",
1051                 "Stucci", "Noise"  , "Image", "Plugin", "EnvMap" , "Musgrave",
1052                 "Voronoi", "DistNoise", "Point Density", "Voxel Data", "Ocean", "", "", ""};
1053         Image *ima;
1054         char di[FILE_MAXDIR], fi[FILE_MAXFILE];
1055         
1056         if(tex) {
1057                 if(tex->use_nodes) {
1058                         new_id(&bmain->tex, (ID *)tex, "Noddy");
1059                 }
1060                 else
1061                 if(tex->type==TEX_IMAGE) {
1062                         ima= tex->ima;
1063                         if(ima) {
1064                                 BLI_strncpy(di, ima->name, sizeof(di));
1065                                 BLI_splitdirstring(di, fi);
1066                                 strcpy(di, "I.");
1067                                 strcat(di, fi);
1068                                 new_id(&bmain->tex, (ID *)tex, di);
1069                         }
1070                         else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
1071                 }
1072                 else if(tex->type==TEX_PLUGIN && tex->plugin) new_id(&bmain->tex, (ID *)tex, tex->plugin->pname);
1073                 else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
1074         }
1075 }
1076
1077 /* ------------------------------------------------------------------------- */
1078
1079 Tex *give_current_object_texture(Object *ob)
1080 {
1081         Material *ma, *node_ma;
1082         Tex *tex= NULL;
1083         
1084         if(ob==NULL) return NULL;
1085         if(ob->totcol==0 && !(ob->type==OB_LAMP)) return NULL;
1086         
1087         if(ob->type==OB_LAMP) {
1088                 tex= give_current_lamp_texture(ob->data);
1089         } else {
1090                 ma= give_current_material(ob, ob->actcol);
1091
1092                 if((node_ma=give_node_material(ma)))
1093                         ma= node_ma;
1094
1095                 tex= give_current_material_texture(ma);
1096         }
1097         
1098         return tex;
1099 }
1100
1101 Tex *give_current_lamp_texture(Lamp *la)
1102 {
1103         MTex *mtex= NULL;
1104         Tex *tex= NULL;
1105
1106         if(la) {
1107                 mtex= la->mtex[(int)(la->texact)];
1108                 if(mtex) tex= mtex->tex;
1109         }
1110
1111         return tex;
1112 }
1113
1114 void set_current_lamp_texture(Lamp *la, Tex *newtex)
1115 {
1116         int act= la->texact;
1117
1118         if(la->mtex[act] && la->mtex[act]->tex)
1119                 id_us_min(&la->mtex[act]->tex->id);
1120
1121         if(newtex) {
1122                 if(!la->mtex[act]) {
1123                         la->mtex[act]= add_mtex();
1124                         la->mtex[act]->texco= TEXCO_GLOB;
1125                 }
1126                 
1127                 la->mtex[act]->tex= newtex;
1128                 id_us_plus(&newtex->id);
1129         }
1130         else if(la->mtex[act]) {
1131                 MEM_freeN(la->mtex[act]);
1132                 la->mtex[act]= NULL;
1133         }
1134 }
1135
1136 bNode *give_current_material_texture_node(Material *ma)
1137 {
1138         if(ma && ma->use_nodes && ma->nodetree)
1139                 return nodeGetActiveID(ma->nodetree, ID_TE);
1140         
1141         return NULL;
1142 }
1143
1144 Tex *give_current_material_texture(Material *ma)
1145 {
1146         MTex *mtex= NULL;
1147         Tex *tex= NULL;
1148         bNode *node;
1149         
1150         if(ma && ma->use_nodes && ma->nodetree) {
1151                 /* first check texture, then material, this works together
1152                    with a hack that clears the active ID flag for textures on
1153                    making a material node active */
1154                 node= nodeGetActiveID(ma->nodetree, ID_TE);
1155
1156                 if(node) {
1157                         tex= (Tex *)node->id;
1158                         ma= NULL;
1159                 }
1160         }
1161
1162         if(ma) {
1163                 mtex= ma->mtex[(int)(ma->texact)];
1164                 if(mtex) tex= mtex->tex;
1165         }
1166         
1167         return tex;
1168 }
1169
1170 int give_active_mtex(ID *id, MTex ***mtex_ar, short *act)
1171 {
1172         switch(GS(id->name)) {
1173         case ID_MA:
1174                 *mtex_ar=               ((Material *)id)->mtex;
1175                 if(act) *act=   (((Material *)id)->texact);
1176                 break;
1177         case ID_WO:
1178                 *mtex_ar=               ((World *)id)->mtex;
1179                 if(act) *act=   (((World *)id)->texact);
1180                 break;
1181         case ID_LA:
1182                 *mtex_ar=               ((Lamp *)id)->mtex;
1183                 if(act) *act=   (((Lamp *)id)->texact);
1184                 break;
1185         case ID_PA:
1186                 *mtex_ar=               ((ParticleSettings *)id)->mtex;
1187                 if(act) *act=   (((ParticleSettings *)id)->texact);
1188                 break;
1189         default:
1190                 *mtex_ar = NULL;
1191                 if(act) *act=   0;
1192                 return FALSE;
1193         }
1194
1195         return TRUE;
1196 }
1197
1198 void set_active_mtex(ID *id, short act)
1199 {
1200         if(act<0)                               act= 0;
1201         else if(act>=MAX_MTEX)  act= MAX_MTEX-1;
1202
1203         switch(GS(id->name)) {
1204         case ID_MA:
1205                 ((Material *)id)->texact= act;
1206                 break;
1207         case ID_WO:
1208                 ((World *)id)->texact= act;
1209                 break;
1210         case ID_LA:
1211                 ((Lamp *)id)->texact= act;
1212                 break;
1213         case ID_PA:
1214                 ((ParticleSettings *)id)->texact= act;
1215                 break;
1216         }
1217 }
1218
1219 void set_current_material_texture(Material *ma, Tex *newtex)
1220 {
1221         Tex *tex= NULL;
1222         bNode *node;
1223         
1224         if(ma && ma->use_nodes && ma->nodetree) {
1225                 node= nodeGetActiveID(ma->nodetree, ID_TE);
1226
1227                 if(node) {
1228                         tex= (Tex *)node->id;
1229                         id_us_min(&tex->id);
1230                         node->id= &newtex->id;
1231                         id_us_plus(&newtex->id);
1232                         ma= NULL;
1233                 }
1234         }
1235         if(ma) {
1236                 int act= (int)ma->texact;
1237
1238                 tex= (ma->mtex[act])? ma->mtex[act]->tex: NULL;
1239                 id_us_min(&tex->id);
1240
1241                 if(newtex) {
1242                         if(!ma->mtex[act])
1243                                 ma->mtex[act]= add_mtex();
1244                         
1245                         ma->mtex[act]->tex= newtex;
1246                         id_us_plus(&newtex->id);
1247                 }
1248                 else if(ma->mtex[act]) {
1249                         MEM_freeN(ma->mtex[act]);
1250                         ma->mtex[act]= NULL;
1251                 }
1252         }
1253 }
1254
1255 int has_current_material_texture(Material *ma)
1256 {
1257         bNode *node;
1258
1259         if(ma && ma->use_nodes && ma->nodetree) {
1260                 node= nodeGetActiveID(ma->nodetree, ID_TE);
1261
1262                 if(node)
1263                         return 1;
1264         }
1265
1266         return (ma != NULL);
1267 }
1268
1269 Tex *give_current_world_texture(World *world)
1270 {
1271         MTex *mtex= NULL;
1272         Tex *tex= NULL;
1273         
1274         if(!world) return NULL;
1275         
1276         mtex= world->mtex[(int)(world->texact)];
1277         if(mtex) tex= mtex->tex;
1278         
1279         return tex;
1280 }
1281
1282 void set_current_world_texture(World *wo, Tex *newtex)
1283 {
1284         int act= wo->texact;
1285
1286         if(wo->mtex[act] && wo->mtex[act]->tex)
1287                 id_us_min(&wo->mtex[act]->tex->id);
1288
1289         if(newtex) {
1290                 if(!wo->mtex[act]) {
1291                         wo->mtex[act]= add_mtex();
1292                         wo->mtex[act]->texco= TEXCO_VIEW;
1293                 }
1294                 
1295                 wo->mtex[act]->tex= newtex;
1296                 id_us_plus(&newtex->id);
1297         }
1298         else if(wo->mtex[act]) {
1299                 MEM_freeN(wo->mtex[act]);
1300                 wo->mtex[act]= NULL;
1301         }
1302 }
1303
1304 Tex *give_current_brush_texture(Brush *br)
1305 {
1306         return br->mtex.tex;
1307 }
1308
1309 void set_current_brush_texture(Brush *br, Tex *newtex)
1310 {
1311         if(br->mtex.tex)
1312                 id_us_min(&br->mtex.tex->id);
1313
1314         if(newtex) {
1315                 br->mtex.tex= newtex;
1316                 id_us_plus(&newtex->id);
1317         }
1318 }
1319
1320 Tex *give_current_particle_texture(ParticleSettings *part)
1321 {
1322         MTex *mtex= NULL;
1323         Tex *tex= NULL;
1324         
1325         if(!part) return NULL;
1326         
1327         mtex= part->mtex[(int)(part->texact)];
1328         if(mtex) tex= mtex->tex;
1329         
1330         return tex;
1331 }
1332
1333 void set_current_particle_texture(ParticleSettings *part, Tex *newtex)
1334 {
1335         int act= part->texact;
1336
1337         if(part->mtex[act] && part->mtex[act]->tex)
1338                 id_us_min(&part->mtex[act]->tex->id);
1339
1340         if(newtex) {
1341                 if(!part->mtex[act]) {
1342                         part->mtex[act]= add_mtex();
1343                         part->mtex[act]->texco= TEXCO_ORCO;
1344                         part->mtex[act]->blendtype= MTEX_MUL;
1345                 }
1346                 
1347                 part->mtex[act]->tex= newtex;
1348                 id_us_plus(&newtex->id);
1349         }
1350         else if(part->mtex[act]) {
1351                 MEM_freeN(part->mtex[act]);
1352                 part->mtex[act]= NULL;
1353         }
1354 }
1355
1356 /* ------------------------------------------------------------------------- */
1357
1358 EnvMap *BKE_add_envmap(void)
1359 {
1360         EnvMap *env;
1361         
1362         env= MEM_callocN(sizeof(EnvMap), "envmap");
1363         env->type= ENV_CUBE;
1364         env->stype= ENV_ANIM;
1365         env->clipsta= 0.1;
1366         env->clipend= 100.0;
1367         env->cuberes= 600;
1368         env->viewscale = 0.5;
1369         
1370         return env;
1371
1372
1373 /* ------------------------------------------------------------------------- */
1374
1375 EnvMap *BKE_copy_envmap(EnvMap *env)
1376 {
1377         EnvMap *envn;
1378         int a;
1379         
1380         envn= MEM_dupallocN(env);
1381         envn->ok= 0;
1382         for(a=0; a<6; a++) envn->cube[a]= NULL;
1383         if(envn->ima) id_us_plus((ID *)envn->ima);
1384         
1385         return envn;
1386 }
1387
1388 /* ------------------------------------------------------------------------- */
1389
1390 void BKE_free_envmapdata(EnvMap *env)
1391 {
1392         unsigned int part;
1393         
1394         for(part=0; part<6; part++) {
1395                 if(env->cube[part])
1396                         IMB_freeImBuf(env->cube[part]);
1397                 env->cube[part]= NULL;
1398         }
1399         env->ok= 0;
1400 }
1401
1402 /* ------------------------------------------------------------------------- */
1403
1404 void BKE_free_envmap(EnvMap *env)
1405 {
1406         
1407         BKE_free_envmapdata(env);
1408         MEM_freeN(env);
1409         
1410 }
1411
1412 /* ------------------------------------------------------------------------- */
1413
1414 PointDensity *BKE_add_pointdensity(void)
1415 {
1416         PointDensity *pd;
1417         
1418         pd= MEM_callocN(sizeof(PointDensity), "pointdensity");
1419         pd->flag = 0;
1420         pd->radius = 0.3f;
1421         pd->falloff_type = TEX_PD_FALLOFF_STD;
1422         pd->falloff_softness = 2.0;
1423         pd->source = TEX_PD_PSYS;
1424         pd->point_tree = NULL;
1425         pd->point_data = NULL;
1426         pd->noise_size = 0.5f;
1427         pd->noise_depth = 1;
1428         pd->noise_fac = 1.0f;
1429         pd->noise_influence = TEX_PD_NOISE_STATIC;
1430         pd->coba = add_colorband(1);
1431         pd->speed_scale = 1.0f;
1432         pd->totpoints = 0;
1433         pd->object = NULL;
1434         pd->psys = 0;
1435         pd->psys_cache_space= TEX_PD_WORLDSPACE;
1436         pd->falloff_curve = curvemapping_add(1, 0, 0, 1, 1);
1437
1438         pd->falloff_curve->preset = CURVE_PRESET_LINE;
1439         pd->falloff_curve->cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
1440         curvemap_reset(pd->falloff_curve->cm, &pd->falloff_curve->clipr, pd->falloff_curve->preset, CURVEMAP_SLOPE_POSITIVE);
1441         curvemapping_changed(pd->falloff_curve, 0);
1442
1443         return pd;
1444
1445
1446 PointDensity *BKE_copy_pointdensity(PointDensity *pd)
1447 {
1448         PointDensity *pdn;
1449
1450         pdn= MEM_dupallocN(pd);
1451         pdn->point_tree = NULL;
1452         pdn->point_data = NULL;
1453         if(pdn->coba) pdn->coba= MEM_dupallocN(pdn->coba);
1454         pdn->falloff_curve = curvemapping_copy(pdn->falloff_curve); /* can be NULL */
1455         return pdn;
1456 }
1457
1458 void BKE_free_pointdensitydata(PointDensity *pd)
1459 {
1460         if (pd->point_tree) {
1461                 BLI_bvhtree_free(pd->point_tree);
1462                 pd->point_tree = NULL;
1463         }
1464         if (pd->point_data) {
1465                 MEM_freeN(pd->point_data);
1466                 pd->point_data = NULL;
1467         }
1468         if(pd->coba) {
1469                 MEM_freeN(pd->coba);
1470                 pd->coba = NULL;
1471         }
1472
1473         curvemapping_free(pd->falloff_curve); /* can be NULL */
1474 }
1475
1476 void BKE_free_pointdensity(PointDensity *pd)
1477 {
1478         BKE_free_pointdensitydata(pd);
1479         MEM_freeN(pd);
1480 }
1481
1482 /* ------------------------------------------------------------------------- */
1483
1484 void BKE_free_voxeldatadata(struct VoxelData *vd)
1485 {
1486         if (vd->dataset) {
1487                 MEM_freeN(vd->dataset);
1488                 vd->dataset = NULL;
1489         }
1490
1491 }
1492  
1493 void BKE_free_voxeldata(struct VoxelData *vd)
1494 {
1495         BKE_free_voxeldatadata(vd);
1496         MEM_freeN(vd);
1497 }
1498  
1499 struct VoxelData *BKE_add_voxeldata(void)
1500 {
1501         VoxelData *vd;
1502
1503         vd= MEM_callocN(sizeof(struct VoxelData), "voxeldata");
1504         vd->dataset = NULL;
1505         vd->resol[0] = vd->resol[1] = vd->resol[2] = 1;
1506         vd->interp_type= TEX_VD_LINEAR;
1507         vd->file_format= TEX_VD_SMOKE;
1508         vd->int_multiplier = 1.0;
1509         vd->extend = TEX_CLIP;
1510         vd->object = NULL;
1511         vd->cachedframe = -1;
1512         vd->ok = 0;
1513         
1514         return vd;
1515  }
1516  
1517 struct VoxelData *BKE_copy_voxeldata(struct VoxelData *vd)
1518 {
1519         VoxelData *vdn;
1520
1521         vdn= MEM_dupallocN(vd); 
1522         vdn->dataset = NULL;
1523
1524         return vdn;
1525 }
1526
1527 /* ------------------------------------------------------------------------- */
1528
1529 struct OceanTex *BKE_add_oceantex(void)
1530 {
1531         OceanTex *ot;
1532         
1533         ot= MEM_callocN(sizeof(struct OceanTex), "ocean texture");
1534         ot->output = TEX_OCN_DISPLACEMENT;
1535         ot->object = NULL;
1536         
1537         return ot;
1538 }
1539
1540 struct OceanTex *BKE_copy_oceantex(struct OceanTex *ot)
1541 {
1542         OceanTex *otn= MEM_dupallocN(ot);
1543         
1544         return otn;
1545 }
1546
1547 void BKE_free_oceantex(struct OceanTex *ot)
1548 {
1549         MEM_freeN(ot);
1550 }
1551
1552
1553 /* ------------------------------------------------------------------------- */
1554 int BKE_texture_dependsOnTime(const struct Tex *texture)
1555 {
1556         if(texture->plugin) {
1557                 // assume all plugins depend on time
1558                 return 1;
1559         } 
1560         else if(        texture->ima && 
1561                         ELEM(texture->ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
1562                 return 1;
1563         }
1564         else if(texture->adt) {
1565                 // assume anything in adt means the texture is animated
1566                 return 1;
1567         }
1568         else if(texture->type == TEX_NOISE) {
1569                 // noise always varies with time
1570                 return 1;
1571         }
1572         return 0;
1573 }
1574
1575 /* ------------------------------------------------------------------------- */