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