2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenkernel/intern/texture.c
38 #include "MEM_guardedalloc.h"
40 #include "BLI_blenlib.h"
42 #include "BLI_kdopbvh.h"
43 #include "BLI_utildefines.h"
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"
56 #include "IMB_imbuf.h"
58 #include "BKE_global.h"
61 #include "BKE_library.h"
62 #include "BKE_image.h"
63 #include "BKE_material.h"
64 #include "BKE_texture.h"
66 #include "BKE_icons.h"
68 #include "BKE_animsys.h"
69 #include "BKE_colortools.h"
70 #include "BKE_scene.h"
72 #include "RE_shader_ext.h"
74 /* ****************** Mapping ******************* */
76 TexMapping *add_tex_mapping(int type)
78 TexMapping *texmap = MEM_callocN(sizeof(TexMapping), "TexMapping");
80 default_tex_mapping(texmap, type);
85 void default_tex_mapping(TexMapping *texmap, int type)
87 memset(texmap, 0, sizeof(TexMapping));
89 texmap->size[0] = texmap->size[1] = texmap->size[2] = 1.0f;
90 texmap->max[0] = texmap->max[1] = texmap->max[2] = 1.0f;
93 texmap->projx = PROJ_X;
94 texmap->projy = PROJ_Y;
95 texmap->projz = PROJ_Z;
96 texmap->mapping = MTEX_FLAT;
100 void init_tex_mapping(TexMapping *texmap)
102 float smat[4][4], rmat[4][4], tmat[4][4], proj[4][4], size[3];
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))
107 unit_m4(texmap->mat);
109 texmap->flag |= TEXMAP_UNIT_MATRIX;
112 /* axis projection */
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;
124 copy_v3_v3(size, texmap->size);
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;
136 size_to_mat4(smat, texmap->size);
139 eul_to_mat4(rmat, texmap->rot);
143 copy_v3_v3(tmat[3], texmap->loc);
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);
151 else if (texmap->type == TEXMAP_TYPE_POINT) {
152 /* forward transform */
153 mul_m4_series(texmap->mat, tmat, rmat, smat);
155 else if (texmap->type == TEXMAP_TYPE_VECTOR) {
156 /* no translation for vectors */
157 mul_m4_m4m4(texmap->mat, rmat, smat);
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);
166 /* projection last */
167 mul_m4_m4m4(texmap->mat, texmap->mat, proj);
169 texmap->flag &= ~TEXMAP_UNIT_MATRIX;
173 ColorMapping *add_color_mapping(void)
175 ColorMapping *colormap = MEM_callocN(sizeof(ColorMapping), "ColorMapping");
177 default_color_mapping(colormap);
182 void default_color_mapping(ColorMapping *colormap)
184 memset(colormap, 0, sizeof(ColorMapping));
186 init_colorband(&colormap->coba, true);
188 colormap->bright = 1.0;
189 colormap->contrast = 1.0;
190 colormap->saturation = 1.0;
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;
199 /* ****************** COLORBAND ******************* */
201 void init_colorband(ColorBand *coba, bool rangetype)
205 coba->data[0].pos = 0.0;
206 coba->data[1].pos = 1.0;
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;
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;
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;
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;
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;
243 ColorBand *add_colorband(bool rangetype)
247 coba = MEM_callocN(sizeof(ColorBand), "colorband");
248 init_colorband(coba, rangetype);
253 /* ------------------------------------------------------------------------- */
255 bool do_colorband(const ColorBand *coba, float in, float out[4])
257 const CBData *cbd1, *cbd2, *cbd0, *cbd3;
258 float fac, mfac, t[4];
261 if (coba == NULL || coba->tot == 0) return 0;
264 if (coba->tot == 1) {
271 if (in <= cbd1->pos && coba->ipotype < 2) {
280 /* we're looking for first pos > in */
281 for (a = 0; a < coba->tot; a++, cbd1++) if (cbd1->pos > in) break;
283 if (a == coba->tot) {
298 if (in >= cbd1->pos && coba->ipotype < 2) {
306 if (cbd2->pos != cbd1->pos)
307 fac = (in - cbd1->pos) / (cbd2->pos - cbd1->pos);
309 /* was setting to 0.0 in 2.56 & previous, but this
310 * is incorrect for the last element, see [#26732] */
311 fac = (a != coba->tot) ? 0.0f : 1.0f;
314 if (coba->ipotype == 4) {
323 if (coba->ipotype >= 2) {
324 /* ipo from right to left: 3 2 1 0 */
326 if (a >= coba->tot - 1) cbd0 = cbd1;
327 else cbd0 = cbd1 + 1;
328 if (a < 2) cbd3 = cbd2;
329 else cbd3 = cbd2 - 1;
331 CLAMP(fac, 0.0f, 1.0f);
333 if (coba->ipotype == 3)
334 key_curve_position_weights(fac, t, KEY_CARDINAL);
336 key_curve_position_weights(fac, t, KEY_BSPLINE);
338 out[0] = t[3] * cbd3->r + t[2] * cbd2->r + t[1] * cbd1->r + t[0] * cbd0->r;
339 out[1] = t[3] * cbd3->g + t[2] * cbd2->g + t[1] * cbd1->g + t[0] * cbd0->g;
340 out[2] = t[3] * cbd3->b + t[2] * cbd2->b + t[1] * cbd1->b + t[0] * cbd0->b;
341 out[3] = t[3] * cbd3->a + t[2] * cbd2->a + t[1] * cbd1->a + t[0] * cbd0->a;
342 CLAMP(out[0], 0.0f, 1.0f);
343 CLAMP(out[1], 0.0f, 1.0f);
344 CLAMP(out[2], 0.0f, 1.0f);
345 CLAMP(out[3], 0.0f, 1.0f);
349 if (coba->ipotype == 1) { /* EASE */
351 fac = 3.0f * mfac - 2.0f * mfac * fac;
355 out[0] = mfac * cbd1->r + fac * cbd2->r;
356 out[1] = mfac * cbd1->g + fac * cbd2->g;
357 out[2] = mfac * cbd1->b + fac * cbd2->b;
358 out[3] = mfac * cbd1->a + fac * cbd2->a;
366 void colorband_table_RGBA(ColorBand *coba, float **array, int *size)
370 *size = CM_TABLE + 1;
371 *array = MEM_callocN(sizeof(float) * (*size) * 4, "ColorBand");
373 for (a = 0; a < *size; a++)
374 do_colorband(coba, (float)a / (float)CM_TABLE, &(*array)[a * 4]);
377 static int vergcband(const void *a1, const void *a2)
379 const CBData *x1 = a1, *x2 = a2;
381 if (x1->pos > x2->pos) return 1;
382 else if (x1->pos < x2->pos) return -1;
386 void colorband_update_sort(ColorBand *coba)
393 for (a = 0; a < coba->tot; a++)
394 coba->data[a].cur = a;
396 qsort(coba->data, coba->tot, sizeof(CBData), vergcband);
398 for (a = 0; a < coba->tot; a++) {
399 if (coba->data[a].cur == coba->cur) {
406 CBData *colorband_element_add(struct ColorBand *coba, float position)
408 if (coba->tot == MAXCOLORBAND) {
411 else if (coba->tot > 0) {
415 do_colorband(coba, position, col);
417 xnew = &coba->data[coba->tot];
418 xnew->pos = position;
427 coba->cur = coba->tot - 1;
429 colorband_update_sort(coba);
431 return coba->data + coba->cur;
434 int colorband_element_remove(struct ColorBand *coba, int index)
441 if (index < 0 || index >= coba->tot)
444 for (a = index; a < coba->tot; a++) {
445 coba->data[a] = coba->data[a + 1];
447 if (coba->cur) coba->cur--;
452 /* ******************* TEX ************************ */
454 void BKE_texture_free(Tex *tex)
456 if (tex->coba) MEM_freeN(tex->coba);
457 if (tex->env) BKE_free_envmap(tex->env);
458 if (tex->pd) BKE_free_pointdensity(tex->pd);
459 if (tex->vd) BKE_free_voxeldata(tex->vd);
460 if (tex->ot) BKE_free_oceantex(tex->ot);
461 BKE_free_animdata((struct ID *)tex);
463 BKE_previewimg_free(&tex->preview);
464 BKE_icon_delete((struct ID *)tex);
468 ntreeFreeTree(tex->nodetree);
469 MEM_freeN(tex->nodetree);
473 /* ------------------------------------------------------------------------- */
475 void default_tex(Tex *tex)
477 tex->type = TEX_CLOUDS;
479 tex->flag = TEX_CHECKER_ODD;
480 tex->imaflag = TEX_INTERPOL | TEX_MIPMAP | TEX_USEALPHA;
481 tex->extend = TEX_REPEAT;
482 tex->cropxmin = tex->cropymin = 0.0;
483 tex->cropxmax = tex->cropymax = 1.0;
484 tex->texfilter = TXF_EWA;
486 tex->xrepeat = tex->yrepeat = 1;
491 tex->noisesize = 0.25;
494 tex->nabla = 0.025; // also in do_versions
497 tex->saturation = 1.0;
498 tex->filtersize = 1.0;
502 /* newnoise: init. */
504 tex->noisebasis2 = 0;
507 tex->mg_lacunarity = 2.0;
508 tex->mg_octaves = 2.0;
509 tex->mg_offset = 1.0;
511 tex->ns_outscale = 1.0;
513 tex->dist_amount = 1.0;
516 tex->vn_w2 = tex->vn_w3 = tex->vn_w4 = 0.0;
522 tex->env->stype = ENV_ANIM;
523 tex->env->clipsta = 0.1;
524 tex->env->clipend = 100;
525 tex->env->cuberes = 600;
530 tex->pd->radius = 0.3f;
531 tex->pd->falloff_type = TEX_PD_FALLOFF_STD;
535 tex->vd->resol[0] = tex->vd->resol[1] = tex->vd->resol[2] = 0;
536 tex->vd->interp_type = TEX_VD_LINEAR;
537 tex->vd->file_format = TEX_VD_SMOKE;
541 tex->ot->output = TEX_OCN_DISPLACEMENT;
542 tex->ot->object = NULL;
545 tex->iuser.fie_ima = 2;
547 tex->iuser.frames = 100;
553 void tex_set_type(Tex *tex, int type)
559 tex->vd = BKE_add_voxeldata();
561 case TEX_POINTDENSITY:
563 tex->pd = BKE_add_pointdensity();
566 if (tex->env == NULL)
567 tex->env = BKE_add_envmap();
571 tex->ot = BKE_add_oceantex();
578 /* ------------------------------------------------------------------------- */
580 Tex *add_texture(Main *bmain, const char *name)
584 tex = BKE_libblock_alloc(bmain, ID_TE, name);
591 /* ------------------------------------------------------------------------- */
593 void default_mtex(MTex *mtex)
595 mtex->texco = TEXCO_ORCO;
596 mtex->mapto = MAP_COL;
598 mtex->projx = PROJ_X;
599 mtex->projy = PROJ_Y;
600 mtex->projz = PROJ_Z;
601 mtex->mapping = MTEX_FLAT;
609 mtex->texflag = MTEX_3TAP_BUMP | MTEX_BUMP_OBJECTSPACE | MTEX_MAPTO_BOUNDS;
610 mtex->colormodel = 0;
616 mtex->blendtype = MTEX_BLEND;
621 mtex->colspecfac = 1.0f;
622 mtex->mirrfac = 1.0f;
623 mtex->alphafac = 1.0f;
624 mtex->difffac = 1.0f;
625 mtex->specfac = 1.0f;
626 mtex->emitfac = 1.0f;
627 mtex->hardfac = 1.0f;
628 mtex->raymirrfac = 1.0f;
629 mtex->translfac = 1.0f;
631 mtex->colemitfac = 1.0f;
632 mtex->colreflfac = 1.0f;
633 mtex->coltransfac = 1.0f;
634 mtex->densfac = 1.0f;
635 mtex->scatterfac = 1.0f;
636 mtex->reflfac = 1.0f;
637 mtex->shadowfac = 1.0f;
638 mtex->zenupfac = 1.0f;
639 mtex->zendownfac = 1.0f;
640 mtex->blendfac = 1.0f;
641 mtex->timefac = 1.0f;
642 mtex->lengthfac = 1.0f;
643 mtex->clumpfac = 1.0f;
644 mtex->kinkfac = 1.0f;
645 mtex->roughfac = 1.0f;
646 mtex->padensfac = 1.0f;
647 mtex->lifefac = 1.0f;
648 mtex->sizefac = 1.0f;
649 mtex->ivelfac = 1.0f;
650 mtex->dampfac = 1.0f;
651 mtex->gravityfac = 1.0f;
652 mtex->fieldfac = 1.0f;
653 mtex->normapspace = MTEX_NSPACE_TANGENT;
654 mtex->brush_map_mode = MTEX_MAP_MODE_TILED;
658 /* ------------------------------------------------------------------------- */
664 mtex = MEM_callocN(sizeof(MTex), "add_mtex");
671 /* slot -1 for first free ID */
672 MTex *add_mtex_id(ID *id, int slot)
677 give_active_mtex(id, &mtex_ar, &act);
679 if (mtex_ar == NULL) {
684 /* find first free */
686 for (i = 0; i < MAX_MTEX; i++) {
697 /* make sure slot is valid */
698 if (slot < 0 || slot >= MAX_MTEX) {
704 id_us_min((ID *)mtex_ar[slot]->tex);
705 MEM_freeN(mtex_ar[slot]);
706 mtex_ar[slot] = NULL;
708 else if (GS(id->name) == ID_MA) {
709 /* Reset this slot's ON/OFF toggle, for materials, when slot was empty. */
710 ((Material *)id)->septex &= ~(1 << slot);
713 mtex_ar[slot] = add_mtex();
715 return mtex_ar[slot];
718 /* ------------------------------------------------------------------------- */
720 Tex *BKE_texture_copy(Tex *tex)
724 texn = BKE_libblock_copy(&tex->id);
725 if (texn->type == TEX_IMAGE) id_us_plus((ID *)texn->ima);
726 else texn->ima = NULL;
728 if (texn->coba) texn->coba = MEM_dupallocN(texn->coba);
729 if (texn->env) texn->env = BKE_copy_envmap(texn->env);
730 if (texn->pd) texn->pd = BKE_copy_pointdensity(texn->pd);
731 if (texn->vd) texn->vd = MEM_dupallocN(texn->vd);
732 if (texn->ot) texn->ot = BKE_copy_oceantex(texn->ot);
733 if (tex->preview) texn->preview = BKE_previewimg_copy(tex->preview);
736 if (tex->nodetree->execdata) {
737 ntreeTexEndExecTree(tex->nodetree->execdata);
739 texn->nodetree = ntreeCopyTree(tex->nodetree);
745 /* texture copy without adding to main dbase */
746 Tex *localize_texture(Tex *tex)
750 texn = BKE_libblock_copy(&tex->id);
751 BLI_remlink(&G.main->tex, texn);
753 /* image texture: BKE_texture_free also doesn't decrease */
755 if (texn->coba) texn->coba = MEM_dupallocN(texn->coba);
757 texn->env = BKE_copy_envmap(texn->env);
758 id_us_min(&texn->env->ima->id);
760 if (texn->pd) texn->pd = BKE_copy_pointdensity(texn->pd);
762 texn->vd = MEM_dupallocN(texn->vd);
763 if (texn->vd->dataset)
764 texn->vd->dataset = MEM_dupallocN(texn->vd->dataset);
767 texn->ot = BKE_copy_oceantex(tex->ot);
770 texn->preview = NULL;
773 texn->nodetree = ntreeLocalize(tex->nodetree);
780 /* ------------------------------------------------------------------------- */
782 static void extern_local_texture(Tex *tex)
784 id_lib_extern((ID *)tex->ima);
787 void BKE_texture_make_local(Tex *tex)
789 Main *bmain = G.main;
794 ParticleSettings *pa;
795 FreestyleLineStyle *ls;
797 bool is_local = false, is_lib = false;
799 /* - only lib users: do nothing
800 * - only local users: set flag
804 if (tex->id.lib == NULL) return;
806 if (tex->id.us == 1) {
807 id_clear_lib_data(bmain, &tex->id);
808 extern_local_texture(tex);
812 ma = bmain->mat.first;
814 for (a = 0; a < MAX_MTEX; a++) {
815 if (ma->mtex[a] && ma->mtex[a]->tex == tex) {
816 if (ma->id.lib) is_lib = true;
817 else is_local = true;
822 la = bmain->lamp.first;
824 for (a = 0; a < MAX_MTEX; a++) {
825 if (la->mtex[a] && la->mtex[a]->tex == tex) {
826 if (la->id.lib) is_lib = true;
827 else is_local = true;
832 wrld = bmain->world.first;
834 for (a = 0; a < MAX_MTEX; a++) {
835 if (wrld->mtex[a] && wrld->mtex[a]->tex == tex) {
836 if (wrld->id.lib) is_lib = true;
837 else is_local = true;
840 wrld = wrld->id.next;
842 br = bmain->brush.first;
844 if (br->mtex.tex == tex) {
845 if (br->id.lib) is_lib = true;
846 else is_local = true;
848 if (br->mask_mtex.tex == tex) {
849 if (br->id.lib) is_lib = true;
850 else is_local = true;
854 pa = bmain->particle.first;
856 for (a = 0; a < MAX_MTEX; a++) {
857 if (pa->mtex[a] && pa->mtex[a]->tex == tex) {
858 if (pa->id.lib) is_lib = true;
859 else is_local = true;
864 ls = bmain->linestyle.first;
866 for (a = 0; a < MAX_MTEX; a++) {
867 if (ls->mtex[a] && ls->mtex[a]->tex == tex) {
868 if (ls->id.lib) is_lib = true;
869 else is_local = true;
875 if (is_local && is_lib == false) {
876 id_clear_lib_data(bmain, &tex->id);
877 extern_local_texture(tex);
879 else if (is_local && is_lib) {
880 Tex *tex_new = BKE_texture_copy(tex);
884 /* Remap paths of new ID using old library as base. */
885 BKE_id_lib_local_paths(bmain, tex->id.lib, &tex_new->id);
887 ma = bmain->mat.first;
889 for (a = 0; a < MAX_MTEX; a++) {
890 if (ma->mtex[a] && ma->mtex[a]->tex == tex) {
891 if (ma->id.lib == NULL) {
892 ma->mtex[a]->tex = tex_new;
900 la = bmain->lamp.first;
902 for (a = 0; a < MAX_MTEX; a++) {
903 if (la->mtex[a] && la->mtex[a]->tex == tex) {
904 if (la->id.lib == NULL) {
905 la->mtex[a]->tex = tex_new;
913 wrld = bmain->world.first;
915 for (a = 0; a < MAX_MTEX; a++) {
916 if (wrld->mtex[a] && wrld->mtex[a]->tex == tex) {
917 if (wrld->id.lib == NULL) {
918 wrld->mtex[a]->tex = tex_new;
924 wrld = wrld->id.next;
926 br = bmain->brush.first;
928 if (br->mtex.tex == tex) {
929 if (br->id.lib == NULL) {
930 br->mtex.tex = tex_new;
935 if (br->mask_mtex.tex == tex) {
936 if (br->id.lib == NULL) {
937 br->mask_mtex.tex = tex_new;
944 pa = bmain->particle.first;
946 for (a = 0; a < MAX_MTEX; a++) {
947 if (pa->mtex[a] && pa->mtex[a]->tex == tex) {
948 if (pa->id.lib == NULL) {
949 pa->mtex[a]->tex = tex_new;
957 ls = bmain->linestyle.first;
959 for (a = 0; a < MAX_MTEX; a++) {
960 if (ls->mtex[a] && ls->mtex[a]->tex == tex) {
961 if (ls->id.lib == NULL) {
962 ls->mtex[a]->tex = tex_new;
973 Tex *give_current_object_texture(Object *ob)
975 Material *ma, *node_ma;
978 if (ob == NULL) return NULL;
979 if (ob->totcol == 0 && !(ob->type == OB_LAMP)) return NULL;
981 if (ob->type == OB_LAMP) {
982 tex = give_current_lamp_texture(ob->data);
985 ma = give_current_material(ob, ob->actcol);
987 if ((node_ma = give_node_material(ma)))
990 tex = give_current_material_texture(ma);
996 Tex *give_current_lamp_texture(Lamp *la)
1002 mtex = la->mtex[(int)(la->texact)];
1003 if (mtex) tex = mtex->tex;
1009 void set_current_lamp_texture(Lamp *la, Tex *newtex)
1011 int act = la->texact;
1013 if (la->mtex[act] && la->mtex[act]->tex)
1014 id_us_min(&la->mtex[act]->tex->id);
1017 if (!la->mtex[act]) {
1018 la->mtex[act] = add_mtex();
1019 la->mtex[act]->texco = TEXCO_GLOB;
1022 la->mtex[act]->tex = newtex;
1023 id_us_plus(&newtex->id);
1025 else if (la->mtex[act]) {
1026 MEM_freeN(la->mtex[act]);
1027 la->mtex[act] = NULL;
1031 Tex *give_current_linestyle_texture(FreestyleLineStyle *linestyle)
1037 mtex = linestyle->mtex[(int)(linestyle->texact)];
1038 if (mtex) tex = mtex->tex;
1044 void set_current_linestyle_texture(FreestyleLineStyle *linestyle, Tex *newtex)
1046 int act = linestyle->texact;
1048 if (linestyle->mtex[act] && linestyle->mtex[act]->tex)
1049 id_us_min(&linestyle->mtex[act]->tex->id);
1052 if (!linestyle->mtex[act]) {
1053 linestyle->mtex[act] = add_mtex();
1054 linestyle->mtex[act]->texco = TEXCO_STROKE;
1057 linestyle->mtex[act]->tex = newtex;
1058 id_us_plus(&newtex->id);
1060 else if (linestyle->mtex[act]) {
1061 MEM_freeN(linestyle->mtex[act]);
1062 linestyle->mtex[act] = NULL;
1066 bNode *give_current_material_texture_node(Material *ma)
1068 if (ma && ma->use_nodes && ma->nodetree)
1069 return nodeGetActiveID(ma->nodetree, ID_TE);
1074 Tex *give_current_material_texture(Material *ma)
1080 if (ma && ma->use_nodes && ma->nodetree) {
1081 /* first check texture, then material, this works together
1082 * with a hack that clears the active ID flag for textures on
1083 * making a material node active */
1084 node = nodeGetActiveID(ma->nodetree, ID_TE);
1087 tex = (Tex *)node->id;
1093 mtex = ma->mtex[(int)(ma->texact)];
1094 if (mtex) tex = mtex->tex;
1100 bool give_active_mtex(ID *id, MTex ***mtex_ar, short *act)
1102 switch (GS(id->name)) {
1104 *mtex_ar = ((Material *)id)->mtex;
1105 if (act) *act = (((Material *)id)->texact);
1108 *mtex_ar = ((World *)id)->mtex;
1109 if (act) *act = (((World *)id)->texact);
1112 *mtex_ar = ((Lamp *)id)->mtex;
1113 if (act) *act = (((Lamp *)id)->texact);
1116 *mtex_ar = ((FreestyleLineStyle *)id)->mtex;
1117 if (act) *act = (((FreestyleLineStyle *)id)->texact);
1120 *mtex_ar = ((ParticleSettings *)id)->mtex;
1121 if (act) *act = (((ParticleSettings *)id)->texact);
1132 void set_active_mtex(ID *id, short act)
1134 if (act < 0) act = 0;
1135 else if (act >= MAX_MTEX) act = MAX_MTEX - 1;
1137 switch (GS(id->name)) {
1139 ((Material *)id)->texact = act;
1142 ((World *)id)->texact = act;
1145 ((Lamp *)id)->texact = act;
1148 ((FreestyleLineStyle *)id)->texact = act;
1151 ((ParticleSettings *)id)->texact = act;
1156 void set_current_material_texture(Material *ma, Tex *newtex)
1161 if ((ma->use_nodes && ma->nodetree) &&
1162 (node = nodeGetActiveID(ma->nodetree, ID_TE)))
1164 tex = (Tex *)node->id;
1165 id_us_min(&tex->id);
1167 node->id = &newtex->id;
1168 id_us_plus(&newtex->id);
1175 int act = (int)ma->texact;
1177 tex = (ma->mtex[act]) ? ma->mtex[act]->tex : NULL;
1178 id_us_min(&tex->id);
1181 if (!ma->mtex[act]) {
1182 ma->mtex[act] = add_mtex();
1183 /* Reset this slot's ON/OFF toggle, for materials, when slot was empty. */
1184 ma->septex &= ~(1 << act);
1187 ma->mtex[act]->tex = newtex;
1188 id_us_plus(&newtex->id);
1190 else if (ma->mtex[act]) {
1191 MEM_freeN(ma->mtex[act]);
1192 ma->mtex[act] = NULL;
1197 bool has_current_material_texture(Material *ma)
1201 if (ma && ma->use_nodes && ma->nodetree) {
1202 node = nodeGetActiveID(ma->nodetree, ID_TE);
1208 return (ma != NULL);
1211 Tex *give_current_world_texture(World *world)
1216 if (!world) return NULL;
1218 mtex = world->mtex[(int)(world->texact)];
1219 if (mtex) tex = mtex->tex;
1224 void set_current_world_texture(World *wo, Tex *newtex)
1226 int act = wo->texact;
1228 if (wo->mtex[act] && wo->mtex[act]->tex)
1229 id_us_min(&wo->mtex[act]->tex->id);
1232 if (!wo->mtex[act]) {
1233 wo->mtex[act] = add_mtex();
1234 wo->mtex[act]->texco = TEXCO_VIEW;
1237 wo->mtex[act]->tex = newtex;
1238 id_us_plus(&newtex->id);
1240 else if (wo->mtex[act]) {
1241 MEM_freeN(wo->mtex[act]);
1242 wo->mtex[act] = NULL;
1246 Tex *give_current_brush_texture(Brush *br)
1248 return br->mtex.tex;
1251 void set_current_brush_texture(Brush *br, Tex *newtex)
1254 id_us_min(&br->mtex.tex->id);
1257 br->mtex.tex = newtex;
1258 id_us_plus(&newtex->id);
1262 Tex *give_current_particle_texture(ParticleSettings *part)
1267 if (!part) return NULL;
1269 mtex = part->mtex[(int)(part->texact)];
1270 if (mtex) tex = mtex->tex;
1275 void set_current_particle_texture(ParticleSettings *part, Tex *newtex)
1277 int act = part->texact;
1279 if (part->mtex[act] && part->mtex[act]->tex)
1280 id_us_min(&part->mtex[act]->tex->id);
1283 if (!part->mtex[act]) {
1284 part->mtex[act] = add_mtex();
1285 part->mtex[act]->texco = TEXCO_ORCO;
1286 part->mtex[act]->blendtype = MTEX_MUL;
1289 part->mtex[act]->tex = newtex;
1290 id_us_plus(&newtex->id);
1292 else if (part->mtex[act]) {
1293 MEM_freeN(part->mtex[act]);
1294 part->mtex[act] = NULL;
1298 /* ------------------------------------------------------------------------- */
1300 EnvMap *BKE_add_envmap(void)
1304 env = MEM_callocN(sizeof(EnvMap), "envmap");
1305 env->type = ENV_CUBE;
1306 env->stype = ENV_ANIM;
1308 env->clipend = 100.0;
1310 env->viewscale = 0.5;
1315 /* ------------------------------------------------------------------------- */
1317 EnvMap *BKE_copy_envmap(EnvMap *env)
1322 envn = MEM_dupallocN(env);
1324 for (a = 0; a < 6; a++) envn->cube[a] = NULL;
1325 if (envn->ima) id_us_plus((ID *)envn->ima);
1330 /* ------------------------------------------------------------------------- */
1332 void BKE_free_envmapdata(EnvMap *env)
1336 for (part = 0; part < 6; part++) {
1337 if (env->cube[part])
1338 IMB_freeImBuf(env->cube[part]);
1339 env->cube[part] = NULL;
1344 /* ------------------------------------------------------------------------- */
1346 void BKE_free_envmap(EnvMap *env)
1349 BKE_free_envmapdata(env);
1354 /* ------------------------------------------------------------------------- */
1356 PointDensity *BKE_add_pointdensity(void)
1360 pd = MEM_callocN(sizeof(PointDensity), "pointdensity");
1363 pd->falloff_type = TEX_PD_FALLOFF_STD;
1364 pd->falloff_softness = 2.0;
1365 pd->source = TEX_PD_PSYS;
1366 pd->point_tree = NULL;
1367 pd->point_data = NULL;
1368 pd->noise_size = 0.5f;
1369 pd->noise_depth = 1;
1370 pd->noise_fac = 1.0f;
1371 pd->noise_influence = TEX_PD_NOISE_STATIC;
1372 pd->coba = add_colorband(true);
1373 pd->speed_scale = 1.0f;
1377 pd->psys_cache_space = TEX_PD_WORLDSPACE;
1378 pd->falloff_curve = curvemapping_add(1, 0, 0, 1, 1);
1380 pd->falloff_curve->preset = CURVE_PRESET_LINE;
1381 pd->falloff_curve->cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
1382 curvemap_reset(pd->falloff_curve->cm, &pd->falloff_curve->clipr, pd->falloff_curve->preset, CURVEMAP_SLOPE_POSITIVE);
1383 curvemapping_changed(pd->falloff_curve, false);
1388 PointDensity *BKE_copy_pointdensity(PointDensity *pd)
1392 pdn = MEM_dupallocN(pd);
1393 pdn->point_tree = NULL;
1394 pdn->point_data = NULL;
1395 if (pdn->coba) pdn->coba = MEM_dupallocN(pdn->coba);
1396 pdn->falloff_curve = curvemapping_copy(pdn->falloff_curve); /* can be NULL */
1400 void BKE_free_pointdensitydata(PointDensity *pd)
1402 if (pd->point_tree) {
1403 BLI_bvhtree_free(pd->point_tree);
1404 pd->point_tree = NULL;
1406 if (pd->point_data) {
1407 MEM_freeN(pd->point_data);
1408 pd->point_data = NULL;
1411 MEM_freeN(pd->coba);
1415 curvemapping_free(pd->falloff_curve); /* can be NULL */
1418 void BKE_free_pointdensity(PointDensity *pd)
1420 BKE_free_pointdensitydata(pd);
1424 /* ------------------------------------------------------------------------- */
1426 void BKE_free_voxeldatadata(VoxelData *vd)
1429 MEM_freeN(vd->dataset);
1435 void BKE_free_voxeldata(VoxelData *vd)
1437 BKE_free_voxeldatadata(vd);
1441 VoxelData *BKE_add_voxeldata(void)
1445 vd = MEM_callocN(sizeof(VoxelData), "voxeldata");
1447 vd->resol[0] = vd->resol[1] = vd->resol[2] = 1;
1448 vd->interp_type = TEX_VD_LINEAR;
1449 vd->file_format = TEX_VD_SMOKE;
1450 vd->int_multiplier = 1.0;
1451 vd->extend = TEX_CLIP;
1453 vd->cachedframe = -1;
1459 VoxelData *BKE_copy_voxeldata(VoxelData *vd)
1463 vdn = MEM_dupallocN(vd);
1464 vdn->dataset = NULL;
1469 /* ------------------------------------------------------------------------- */
1471 OceanTex *BKE_add_oceantex(void)
1475 ot = MEM_callocN(sizeof(struct OceanTex), "ocean texture");
1476 ot->output = TEX_OCN_DISPLACEMENT;
1482 OceanTex *BKE_copy_oceantex(struct OceanTex *ot)
1484 OceanTex *otn = MEM_dupallocN(ot);
1489 void BKE_free_oceantex(struct OceanTex *ot)
1495 /* ------------------------------------------------------------------------- */
1496 bool BKE_texture_dependsOnTime(const struct Tex *texture)
1498 if (texture->ima && BKE_image_is_animated(texture->ima)) {
1501 else if (texture->adt) {
1502 /* assume anything in adt means the texture is animated */
1505 else if (texture->type == TEX_NOISE) {
1506 /* noise always varies with time */
1512 /* ------------------------------------------------------------------------- */
1514 void BKE_texture_get_value(Scene *scene, Tex *texture, float *tex_co, TexResult *texres, bool use_color_management)
1517 bool do_color_manage = false;
1519 if (scene && use_color_management) {
1520 do_color_manage = BKE_scene_check_color_management_enabled(scene);
1523 /* no node textures for now */
1524 result_type = multitex_ext_safe(texture, tex_co, texres, NULL, do_color_manage);
1526 /* if the texture gave an RGB value, we assume it didn't give a valid
1527 * intensity, since this is in the context of modifiers don't use perceptual color conversion.
1528 * if the texture didn't give an RGB value, copy the intensity across
1530 if (result_type & TEX_RGB) {
1531 texres->tin = (1.0f / 3.0f) * (texres->tr + texres->tg + texres->tb);
1534 copy_v3_fl(&texres->tr, texres->tin);