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