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