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