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