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