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