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"
41 #include "BLI_dynlib.h"
43 #include "BLI_kdopbvh.h"
44 #include "BLI_utildefines.h"
45 #include "BLI_bpath.h"
47 #include "DNA_key_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_lamp_types.h"
50 #include "DNA_material_types.h"
51 #include "DNA_world_types.h"
52 #include "DNA_brush_types.h"
53 #include "DNA_node_types.h"
54 #include "DNA_color_types.h"
55 #include "DNA_particle_types.h"
57 #include "IMB_imbuf.h"
59 #include "BKE_plugin_types.h"
60 #include "BKE_utildefines.h"
61 #include "BKE_global.h"
63 #include "BKE_ocean.h"
65 #include "BKE_library.h"
66 #include "BKE_image.h"
67 #include "BKE_material.h"
68 #include "BKE_texture.h"
70 #include "BKE_icons.h"
72 #include "BKE_animsys.h"
73 #include "BKE_colortools.h"
76 /* ------------------------------------------------------------------------- */
78 /* All support for plugin textures: */
79 int test_dlerr(const char *name, const char *symbol)
83 err = BLI_dynlib_get_error_as_string(NULL);
85 printf("var1: %s, var2: %s, var3: %s\n", name, symbol, err);
92 /* ------------------------------------------------------------------------- */
94 void open_plugin_tex(PluginTex *pit)
98 /* init all the happy variables */
106 pit->instance_init = NULL;
108 /* clear the error list */
109 BLI_dynlib_get_error_as_string(NULL);
111 /* no BLI_dynlib_close! multiple opened plugins... */
112 /* if (pit->handle) BLI_dynlib_close(pit->handle); */
113 /* pit->handle= 0; */
115 /* open the needed object */
116 pit->handle = BLI_dynlib_open(pit->name);
117 if (test_dlerr(pit->name, pit->name)) return;
119 if (pit->handle != NULL) {
120 /* find the address of the version function */
121 version = (int (*)(void))BLI_dynlib_find_symbol(pit->handle, "plugin_tex_getversion");
122 if (test_dlerr(pit->name, "plugin_tex_getversion")) return;
124 if (version != NULL) {
125 pit->version = version();
126 if (pit->version >= 2 && pit->version <= 6) {
127 int (*info_func)(PluginInfo *);
128 PluginInfo *info = (PluginInfo *) MEM_mallocN(sizeof(PluginInfo), "plugin_info");
130 info_func = (int (*)(PluginInfo *))BLI_dynlib_find_symbol(pit->handle, "plugin_getinfo");
131 if (!test_dlerr(pit->name, "plugin_getinfo")) {
132 info->instance_init = NULL;
136 pit->doit = (int (*)(void))info->tex_doit;
137 pit->callback = (void (*)(unsigned short))info->callback;
138 pit->stypes = info->stypes;
139 pit->vars = info->nvars;
140 pit->pname = info->name;
141 pit->stnames = info->snames;
142 pit->varstr = info->varstr;
143 pit->result = info->result;
144 pit->cfra = info->cfra;
145 pit->instance_init = info->instance_init;
146 if (info->init) info->init();
151 printf("Plugin returned unrecognized version number\n");
158 /* ------------------------------------------------------------------------- */
160 /* very badlevel define to bypass linking with BIF_interface.h */
164 PluginTex *add_plugin_tex(char *str)
170 pit = MEM_callocN(sizeof(PluginTex), "plugintex");
172 BLI_strncpy(pit->name, str, sizeof(pit->name));
173 open_plugin_tex(pit);
175 if (pit->doit == NULL) {
176 if (pit->handle == NULL) {; } //XXX error("no plugin: %s", str);
177 else {; } //XXX error("in plugin: %s", str);
182 varstr = pit->varstr;
183 for (a = 0; a < pit->vars; a++, varstr++) {
184 if ( (varstr->type & FLO) == FLO)
185 pit->data[a] = varstr->def;
186 else if ( (varstr->type & INT) == INT)
187 *((int *)(pit->data + a)) = (int) varstr->def;
190 if (pit->instance_init)
191 pit->instance_init((void *) pit->data);
196 /* ------------------------------------------------------------------------- */
198 void free_plugin_tex(PluginTex *pit)
200 if (pit == NULL) return;
202 /* no BLI_dynlib_close: same plugin can be opened multiple times, 1 handle */
206 /* ****************** Mapping ******************* */
208 TexMapping *add_tex_mapping(void)
210 TexMapping *texmap = MEM_callocN(sizeof(TexMapping), "TexMapping");
212 default_tex_mapping(texmap);
217 void default_tex_mapping(TexMapping *texmap)
219 memset(texmap, 0, sizeof(TexMapping));
221 texmap->size[0] = texmap->size[1] = texmap->size[2] = 1.0f;
222 texmap->max[0] = texmap->max[1] = texmap->max[2] = 1.0f;
223 unit_m4(texmap->mat);
225 texmap->projx = PROJ_X;
226 texmap->projy = PROJ_Y;
227 texmap->projz = PROJ_Z;
228 texmap->mapping = MTEX_FLAT;
231 void init_tex_mapping(TexMapping *texmap)
233 float smat[3][3], rmat[3][3], mat[3][3], proj[3][3];
235 if (texmap->projx == PROJ_X && texmap->projy == PROJ_Y && texmap->projz == PROJ_Z &&
236 is_zero_v3(texmap->loc) && is_zero_v3(texmap->rot) && is_one_v3(texmap->size))
238 unit_m4(texmap->mat);
240 texmap->flag |= TEXMAP_UNIT_MATRIX;
243 /* axis projection */
246 if (texmap->projx != PROJ_N)
247 proj[texmap->projx - 1][0] = 1.0f;
248 if (texmap->projy != PROJ_N)
249 proj[texmap->projy - 1][1] = 1.0f;
250 if (texmap->projz != PROJ_N)
251 proj[texmap->projz - 1][2] = 1.0f;
254 size_to_mat3(smat, texmap->size);
257 /* TexMapping rotation are now in radians. */
258 eul_to_mat3(rmat, texmap->rot);
261 mul_m3_m3m3(mat, rmat, smat);
262 mul_m3_m3m3(mat, proj, mat);
265 copy_m4_m3(texmap->mat, mat);
266 copy_v3_v3(texmap->mat[3], texmap->loc);
268 texmap->flag &= ~TEXMAP_UNIT_MATRIX;
272 ColorMapping *add_color_mapping(void)
274 ColorMapping *colormap = MEM_callocN(sizeof(ColorMapping), "ColorMapping");
276 default_color_mapping(colormap);
281 void default_color_mapping(ColorMapping *colormap)
283 memset(colormap, 0, sizeof(ColorMapping));
285 init_colorband(&colormap->coba, 1);
287 colormap->bright = 1.0;
288 colormap->contrast = 1.0;
289 colormap->saturation = 1.0;
291 colormap->blend_color[0] = 0.8f;
292 colormap->blend_color[1] = 0.8f;
293 colormap->blend_color[2] = 0.8f;
294 colormap->blend_type = MA_RAMP_BLEND;
295 colormap->blend_factor = 0.0f;
298 /* ****************** COLORBAND ******************* */
300 void init_colorband(ColorBand *coba, int rangetype)
304 coba->data[0].pos = 0.0;
305 coba->data[1].pos = 1.0;
307 if (rangetype == 0) {
308 coba->data[0].r = 0.0;
309 coba->data[0].g = 0.0;
310 coba->data[0].b = 0.0;
311 coba->data[0].a = 0.0;
313 coba->data[1].r = 1.0;
314 coba->data[1].g = 1.0;
315 coba->data[1].b = 1.0;
316 coba->data[1].a = 1.0;
319 coba->data[0].r = 0.0;
320 coba->data[0].g = 0.0;
321 coba->data[0].b = 0.0;
322 coba->data[0].a = 1.0;
324 coba->data[1].r = 1.0;
325 coba->data[1].g = 1.0;
326 coba->data[1].b = 1.0;
327 coba->data[1].a = 1.0;
330 for (a = 2; a < MAXCOLORBAND; a++) {
331 coba->data[a].r = 0.5;
332 coba->data[a].g = 0.5;
333 coba->data[a].b = 0.5;
334 coba->data[a].a = 1.0;
335 coba->data[a].pos = 0.5;
342 ColorBand *add_colorband(int rangetype)
346 coba = MEM_callocN(sizeof(ColorBand), "colorband");
347 init_colorband(coba, rangetype);
352 /* ------------------------------------------------------------------------- */
354 int do_colorband(const ColorBand *coba, float in, float out[4])
356 const CBData *cbd1, *cbd2, *cbd0, *cbd3;
357 float fac, mfac, t[4];
360 if (coba == NULL || coba->tot == 0) return 0;
363 if (coba->tot == 1) {
370 if (in <= cbd1->pos && coba->ipotype < 2) {
379 /* we're looking for first pos > in */
380 for (a = 0; a < coba->tot; a++, cbd1++) if (cbd1->pos > in) break;
382 if (a == coba->tot) {
393 else cbd2 = cbd1 - 1;
395 if (in >= cbd1->pos && coba->ipotype < 2) {
403 if (cbd2->pos != cbd1->pos)
404 fac = (in - cbd1->pos) / (cbd2->pos - cbd1->pos);
406 /* was setting to 0.0 in 2.56 & previous, but this
407 * is incorrect for the last element, see [#26732] */
408 fac = (a != coba->tot) ? 0.0f : 1.0f;
411 if (coba->ipotype == 4) {
420 if (coba->ipotype >= 2) {
421 /* ipo from right to left: 3 2 1 0 */
423 if (a >= coba->tot - 1) cbd0 = cbd1;
424 else cbd0 = cbd1 + 1;
425 if (a < 2) cbd3 = cbd2;
426 else cbd3 = cbd2 - 1;
428 CLAMP(fac, 0.0f, 1.0f);
430 if (coba->ipotype == 3)
431 key_curve_position_weights(fac, t, KEY_CARDINAL);
433 key_curve_position_weights(fac, t, KEY_BSPLINE);
435 out[0] = t[3] * cbd3->r + t[2] * cbd2->r + t[1] * cbd1->r + t[0] * cbd0->r;
436 out[1] = t[3] * cbd3->g + t[2] * cbd2->g + t[1] * cbd1->g + t[0] * cbd0->g;
437 out[2] = t[3] * cbd3->b + t[2] * cbd2->b + t[1] * cbd1->b + t[0] * cbd0->b;
438 out[3] = t[3] * cbd3->a + t[2] * cbd2->a + t[1] * cbd1->a + t[0] * cbd0->a;
439 CLAMP(out[0], 0.0f, 1.0f);
440 CLAMP(out[1], 0.0f, 1.0f);
441 CLAMP(out[2], 0.0f, 1.0f);
442 CLAMP(out[3], 0.0f, 1.0f);
446 if (coba->ipotype == 1) { /* EASE */
448 fac = 3.0f * mfac - 2.0f * mfac * fac;
452 out[0] = mfac * cbd1->r + fac * cbd2->r;
453 out[1] = mfac * cbd1->g + fac * cbd2->g;
454 out[2] = mfac * cbd1->b + fac * cbd2->b;
455 out[3] = mfac * cbd1->a + fac * cbd2->a;
463 void colorband_table_RGBA(ColorBand *coba, float **array, int *size)
467 *size = CM_TABLE + 1;
468 *array = MEM_callocN(sizeof(float) * (*size) * 4, "ColorBand");
470 for (a = 0; a < *size; a++)
471 do_colorband(coba, (float)a / (float)CM_TABLE, &(*array)[a * 4]);
474 int vergcband(const void *a1, const void *a2)
476 const CBData *x1 = a1, *x2 = a2;
478 if (x1->pos > x2->pos) return 1;
479 else if (x1->pos < x2->pos) return -1;
483 void colorband_update_sort(ColorBand *coba)
490 for (a = 0; a < coba->tot; a++)
491 coba->data[a].cur = a;
493 qsort(coba->data, coba->tot, sizeof(CBData), vergcband);
495 for (a = 0; a < coba->tot; a++) {
496 if (coba->data[a].cur == coba->cur) {
503 CBData *colorband_element_add(struct ColorBand *coba, float position)
505 if (coba->tot == MAXCOLORBAND) {
508 else if (coba->tot > 0) {
512 do_colorband(coba, position, col);
514 xnew = &coba->data[coba->tot];
515 xnew->pos = position;
524 coba->cur = coba->tot - 1;
526 colorband_update_sort(coba);
528 return coba->data + coba->cur;
531 int colorband_element_remove(struct ColorBand *coba, int index)
538 if (index < 0 || index >= coba->tot)
541 for (a = index; a < coba->tot; a++) {
542 coba->data[a] = coba->data[a + 1];
544 if (coba->cur) coba->cur--;
549 /* ******************* TEX ************************ */
551 void BKE_texture_free(Tex *tex)
553 free_plugin_tex(tex->plugin);
555 if (tex->coba) MEM_freeN(tex->coba);
556 if (tex->env) BKE_free_envmap(tex->env);
557 if (tex->pd) BKE_free_pointdensity(tex->pd);
558 if (tex->vd) BKE_free_voxeldata(tex->vd);
559 if (tex->ot) BKE_free_oceantex(tex->ot);
560 BKE_free_animdata((struct ID *)tex);
562 BKE_previewimg_free(&tex->preview);
563 BKE_icon_delete((struct ID *)tex);
567 ntreeFreeTree(tex->nodetree);
568 MEM_freeN(tex->nodetree);
572 /* ------------------------------------------------------------------------- */
574 void default_tex(Tex *tex)
580 tex->type = TEX_CLOUDS;
582 tex->flag = TEX_CHECKER_ODD;
583 tex->imaflag = TEX_INTERPOL | TEX_MIPMAP | TEX_USEALPHA;
584 tex->extend = TEX_REPEAT;
585 tex->cropxmin = tex->cropymin = 0.0;
586 tex->cropxmax = tex->cropymax = 1.0;
587 tex->texfilter = TXF_EWA;
589 tex->xrepeat = tex->yrepeat = 1;
594 tex->noisesize = 0.25;
597 tex->nabla = 0.025; // also in do_versions
600 tex->saturation = 1.0;
601 tex->filtersize = 1.0;
605 /* newnoise: init. */
607 tex->noisebasis2 = 0;
610 tex->mg_lacunarity = 2.0;
611 tex->mg_octaves = 2.0;
612 tex->mg_offset = 1.0;
614 tex->ns_outscale = 1.0;
616 tex->dist_amount = 1.0;
619 tex->vn_w2 = tex->vn_w3 = tex->vn_w4 = 0.0;
625 tex->env->stype = ENV_ANIM;
626 tex->env->clipsta = 0.1;
627 tex->env->clipend = 100;
628 tex->env->cuberes = 600;
633 tex->pd->radius = 0.3f;
634 tex->pd->falloff_type = TEX_PD_FALLOFF_STD;
638 tex->vd->resol[0] = tex->vd->resol[1] = tex->vd->resol[2] = 0;
639 tex->vd->interp_type = TEX_VD_LINEAR;
640 tex->vd->file_format = TEX_VD_SMOKE;
644 tex->ot->output = TEX_OCN_DISPLACEMENT;
645 tex->ot->object = NULL;
649 varstr = pit->varstr;
651 for (a = 0; a < pit->vars; a++, varstr++) {
652 pit->data[a] = varstr->def;
657 tex->iuser.fie_ima = 2;
659 tex->iuser.frames = 100;
665 void tex_set_type(Tex *tex, int type)
671 tex->vd = BKE_add_voxeldata();
673 case TEX_POINTDENSITY:
675 tex->pd = BKE_add_pointdensity();
678 if (tex->env == NULL)
679 tex->env = BKE_add_envmap();
683 tex->ot = BKE_add_oceantex();
690 /* ------------------------------------------------------------------------- */
692 Tex *add_texture(const char *name)
694 Main *bmain = G.main;
697 tex = BKE_libblock_alloc(&bmain->tex, ID_TE, name);
704 /* ------------------------------------------------------------------------- */
706 void default_mtex(MTex *mtex)
708 mtex->texco = TEXCO_ORCO;
709 mtex->mapto = MAP_COL;
711 mtex->projx = PROJ_X;
712 mtex->projy = PROJ_Y;
713 mtex->projz = PROJ_Z;
714 mtex->mapping = MTEX_FLAT;
722 mtex->texflag = MTEX_3TAP_BUMP | MTEX_BUMP_OBJECTSPACE;
723 mtex->colormodel = 0;
729 mtex->blendtype = MTEX_BLEND;
734 mtex->colspecfac = 1.0f;
735 mtex->mirrfac = 1.0f;
736 mtex->alphafac = 1.0f;
737 mtex->difffac = 1.0f;
738 mtex->specfac = 1.0f;
739 mtex->emitfac = 1.0f;
740 mtex->hardfac = 1.0f;
741 mtex->raymirrfac = 1.0f;
742 mtex->translfac = 1.0f;
744 mtex->colemitfac = 1.0f;
745 mtex->colreflfac = 1.0f;
746 mtex->coltransfac = 1.0f;
747 mtex->densfac = 1.0f;
748 mtex->scatterfac = 1.0f;
749 mtex->reflfac = 1.0f;
750 mtex->shadowfac = 1.0f;
751 mtex->zenupfac = 1.0f;
752 mtex->zendownfac = 1.0f;
753 mtex->blendfac = 1.0f;
754 mtex->timefac = 1.0f;
755 mtex->lengthfac = 1.0f;
756 mtex->clumpfac = 1.0f;
757 mtex->kinkfac = 1.0f;
758 mtex->roughfac = 1.0f;
759 mtex->padensfac = 1.0f;
760 mtex->lifefac = 1.0f;
761 mtex->sizefac = 1.0f;
762 mtex->ivelfac = 1.0f;
763 mtex->dampfac = 1.0f;
764 mtex->gravityfac = 1.0f;
765 mtex->fieldfac = 1.0f;
766 mtex->normapspace = MTEX_NSPACE_TANGENT;
770 /* ------------------------------------------------------------------------- */
776 mtex = MEM_callocN(sizeof(MTex), "add_mtex");
783 /* slot -1 for first free ID */
784 MTex *add_mtex_id(ID *id, int slot)
789 give_active_mtex(id, &mtex_ar, &act);
791 if (mtex_ar == NULL) {
796 /* find first free */
798 for (i = 0; i < MAX_MTEX; i++) {
809 /* make sure slot is valid */
810 if (slot < 0 || slot >= MAX_MTEX) {
816 id_us_min((ID *)mtex_ar[slot]->tex);
817 MEM_freeN(mtex_ar[slot]);
818 mtex_ar[slot] = NULL;
821 mtex_ar[slot] = add_mtex();
823 return mtex_ar[slot];
826 /* ------------------------------------------------------------------------- */
828 Tex *BKE_texture_copy(Tex *tex)
832 texn = BKE_libblock_copy(&tex->id);
833 if (texn->type == TEX_IMAGE) id_us_plus((ID *)texn->ima);
834 else texn->ima = NULL;
837 texn->plugin = MEM_dupallocN(texn->plugin);
838 open_plugin_tex(texn->plugin);
841 if (texn->coba) texn->coba = MEM_dupallocN(texn->coba);
842 if (texn->env) texn->env = BKE_copy_envmap(texn->env);
843 if (texn->pd) texn->pd = BKE_copy_pointdensity(texn->pd);
844 if (texn->vd) texn->vd = MEM_dupallocN(texn->vd);
845 if (texn->ot) texn->ot = BKE_copy_oceantex(texn->ot);
846 if (tex->preview) texn->preview = BKE_previewimg_copy(tex->preview);
849 if (tex->nodetree->execdata) {
850 ntreeTexEndExecTree(tex->nodetree->execdata, 1);
852 texn->nodetree = ntreeCopyTree(tex->nodetree);
858 /* texture copy without adding to main dbase */
859 Tex *localize_texture(Tex *tex)
863 texn = BKE_libblock_copy(&tex->id);
864 BLI_remlink(&G.main->tex, texn);
866 /* image texture: BKE_texture_free also doesn't decrease */
869 texn->plugin = MEM_dupallocN(texn->plugin);
870 open_plugin_tex(texn->plugin);
873 if (texn->coba) texn->coba = MEM_dupallocN(texn->coba);
875 texn->env = BKE_copy_envmap(texn->env);
876 id_us_min(&texn->env->ima->id);
878 if (texn->pd) texn->pd = BKE_copy_pointdensity(texn->pd);
880 texn->vd = MEM_dupallocN(texn->vd);
881 if (texn->vd->dataset)
882 texn->vd->dataset = MEM_dupallocN(texn->vd->dataset);
885 texn->ot = BKE_copy_oceantex(tex->ot);
888 texn->preview = NULL;
891 texn->nodetree = ntreeLocalize(tex->nodetree);
898 /* ------------------------------------------------------------------------- */
900 static void extern_local_texture(Tex *tex)
902 id_lib_extern((ID *)tex->ima);
905 void BKE_texture_make_local(Tex *tex)
907 Main *bmain = G.main;
912 ParticleSettings *pa;
913 int a, is_local = FALSE, is_lib = FALSE;
915 /* - only lib users: do nothing
916 * - only local users: set flag
920 if (tex->id.lib == NULL) return;
922 if (tex->id.us == 1) {
923 id_clear_lib_data(bmain, &tex->id);
924 extern_local_texture(tex);
928 ma = bmain->mat.first;
930 for (a = 0; a < MAX_MTEX; a++) {
931 if (ma->mtex[a] && ma->mtex[a]->tex == tex) {
932 if (ma->id.lib) is_lib = TRUE;
933 else is_local = TRUE;
938 la = bmain->lamp.first;
940 for (a = 0; a < MAX_MTEX; a++) {
941 if (la->mtex[a] && la->mtex[a]->tex == tex) {
942 if (la->id.lib) is_lib = TRUE;
943 else is_local = TRUE;
948 wrld = bmain->world.first;
950 for (a = 0; a < MAX_MTEX; a++) {
951 if (wrld->mtex[a] && wrld->mtex[a]->tex == tex) {
952 if (wrld->id.lib) is_lib = TRUE;
953 else is_local = TRUE;
956 wrld = wrld->id.next;
958 br = bmain->brush.first;
960 if (br->mtex.tex == tex) {
961 if (br->id.lib) is_lib = TRUE;
962 else is_local = TRUE;
966 pa = bmain->particle.first;
968 for (a = 0; a < MAX_MTEX; a++) {
969 if (pa->mtex[a] && pa->mtex[a]->tex == tex) {
970 if (pa->id.lib) is_lib = TRUE;
971 else is_local = TRUE;
977 if (is_local && is_lib == FALSE) {
978 id_clear_lib_data(bmain, &tex->id);
979 extern_local_texture(tex);
981 else if (is_local && is_lib) {
982 Tex *tex_new = BKE_texture_copy(tex);
986 /* Remap paths of new ID using old library as base. */
987 BKE_id_lib_local_paths(bmain, tex->id.lib, &tex_new->id);
989 ma = bmain->mat.first;
991 for (a = 0; a < MAX_MTEX; a++) {
992 if (ma->mtex[a] && ma->mtex[a]->tex == tex) {
993 if (ma->id.lib == NULL) {
994 ma->mtex[a]->tex = tex_new;
1002 la = bmain->lamp.first;
1004 for (a = 0; a < MAX_MTEX; a++) {
1005 if (la->mtex[a] && la->mtex[a]->tex == tex) {
1006 if (la->id.lib == NULL) {
1007 la->mtex[a]->tex = tex_new;
1015 wrld = bmain->world.first;
1017 for (a = 0; a < MAX_MTEX; a++) {
1018 if (wrld->mtex[a] && wrld->mtex[a]->tex == tex) {
1019 if (wrld->id.lib == NULL) {
1020 wrld->mtex[a]->tex = tex_new;
1026 wrld = wrld->id.next;
1028 br = bmain->brush.first;
1030 if (br->mtex.tex == tex) {
1031 if (br->id.lib == NULL) {
1032 br->mtex.tex = tex_new;
1039 pa = bmain->particle.first;
1041 for (a = 0; a < MAX_MTEX; a++) {
1042 if (pa->mtex[a] && pa->mtex[a]->tex == tex) {
1043 if (pa->id.lib == NULL) {
1044 pa->mtex[a]->tex = tex_new;
1055 /* ------------------------------------------------------------------------- */
1057 void autotexname(Tex *tex)
1059 Main *bmain = G.main;
1060 char texstr[20][15] = {"None", "Clouds", "Wood", "Marble", "Magic", "Blend",
1061 "Stucci", "Noise", "Image", "Plugin", "EnvMap", "Musgrave",
1062 "Voronoi", "DistNoise", "Point Density", "Voxel Data", "Ocean", "", "", ""};
1064 char di[FILE_MAXDIR], fi[FILE_MAXFILE];
1067 if (tex->use_nodes) {
1068 new_id(&bmain->tex, (ID *)tex, "Noddy");
1071 if (tex->type == TEX_IMAGE) {
1074 BLI_strncpy(di, ima->name, sizeof(di));
1075 BLI_splitdirstring(di, fi);
1078 new_id(&bmain->tex, (ID *)tex, di);
1080 else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
1082 else if (tex->type == TEX_PLUGIN && tex->plugin) new_id(&bmain->tex, (ID *)tex, tex->plugin->pname);
1083 else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
1088 /* ------------------------------------------------------------------------- */
1090 Tex *give_current_object_texture(Object *ob)
1092 Material *ma, *node_ma;
1095 if (ob == NULL) return NULL;
1096 if (ob->totcol == 0 && !(ob->type == OB_LAMP)) return NULL;
1098 if (ob->type == OB_LAMP) {
1099 tex = give_current_lamp_texture(ob->data);
1102 ma = give_current_material(ob, ob->actcol);
1104 if ((node_ma = give_node_material(ma)))
1107 tex = give_current_material_texture(ma);
1113 Tex *give_current_lamp_texture(Lamp *la)
1119 mtex = la->mtex[(int)(la->texact)];
1120 if (mtex) tex = mtex->tex;
1126 void set_current_lamp_texture(Lamp *la, Tex *newtex)
1128 int act = la->texact;
1130 if (la->mtex[act] && la->mtex[act]->tex)
1131 id_us_min(&la->mtex[act]->tex->id);
1134 if (!la->mtex[act]) {
1135 la->mtex[act] = add_mtex();
1136 la->mtex[act]->texco = TEXCO_GLOB;
1139 la->mtex[act]->tex = newtex;
1140 id_us_plus(&newtex->id);
1142 else if (la->mtex[act]) {
1143 MEM_freeN(la->mtex[act]);
1144 la->mtex[act] = NULL;
1148 bNode *give_current_material_texture_node(Material *ma)
1150 if (ma && ma->use_nodes && ma->nodetree)
1151 return nodeGetActiveID(ma->nodetree, ID_TE);
1156 Tex *give_current_material_texture(Material *ma)
1162 if (ma && ma->use_nodes && ma->nodetree) {
1163 /* first check texture, then material, this works together
1164 * with a hack that clears the active ID flag for textures on
1165 * making a material node active */
1166 node = nodeGetActiveID(ma->nodetree, ID_TE);
1169 tex = (Tex *)node->id;
1175 mtex = ma->mtex[(int)(ma->texact)];
1176 if (mtex) tex = mtex->tex;
1182 int give_active_mtex(ID *id, MTex ***mtex_ar, short *act)
1184 switch (GS(id->name)) {
1186 *mtex_ar = ((Material *)id)->mtex;
1187 if (act) *act = (((Material *)id)->texact);
1190 *mtex_ar = ((World *)id)->mtex;
1191 if (act) *act = (((World *)id)->texact);
1194 *mtex_ar = ((Lamp *)id)->mtex;
1195 if (act) *act = (((Lamp *)id)->texact);
1198 *mtex_ar = ((ParticleSettings *)id)->mtex;
1199 if (act) *act = (((ParticleSettings *)id)->texact);
1210 void set_active_mtex(ID *id, short act)
1212 if (act < 0) act = 0;
1213 else if (act >= MAX_MTEX) act = MAX_MTEX - 1;
1215 switch (GS(id->name)) {
1217 ((Material *)id)->texact = act;
1220 ((World *)id)->texact = act;
1223 ((Lamp *)id)->texact = act;
1226 ((ParticleSettings *)id)->texact = act;
1231 void set_current_material_texture(Material *ma, Tex *newtex)
1236 if (ma && ma->use_nodes && ma->nodetree) {
1237 node = nodeGetActiveID(ma->nodetree, ID_TE);
1240 tex = (Tex *)node->id;
1241 id_us_min(&tex->id);
1242 node->id = &newtex->id;
1243 id_us_plus(&newtex->id);
1248 int act = (int)ma->texact;
1250 tex = (ma->mtex[act]) ? ma->mtex[act]->tex : NULL;
1251 id_us_min(&tex->id);
1255 ma->mtex[act] = add_mtex();
1257 ma->mtex[act]->tex = newtex;
1258 id_us_plus(&newtex->id);
1260 else if (ma->mtex[act]) {
1261 MEM_freeN(ma->mtex[act]);
1262 ma->mtex[act] = NULL;
1267 int has_current_material_texture(Material *ma)
1271 if (ma && ma->use_nodes && ma->nodetree) {
1272 node = nodeGetActiveID(ma->nodetree, ID_TE);
1278 return (ma != NULL);
1281 Tex *give_current_world_texture(World *world)
1286 if (!world) return NULL;
1288 mtex = world->mtex[(int)(world->texact)];
1289 if (mtex) tex = mtex->tex;
1294 void set_current_world_texture(World *wo, Tex *newtex)
1296 int act = wo->texact;
1298 if (wo->mtex[act] && wo->mtex[act]->tex)
1299 id_us_min(&wo->mtex[act]->tex->id);
1302 if (!wo->mtex[act]) {
1303 wo->mtex[act] = add_mtex();
1304 wo->mtex[act]->texco = TEXCO_VIEW;
1307 wo->mtex[act]->tex = newtex;
1308 id_us_plus(&newtex->id);
1310 else if (wo->mtex[act]) {
1311 MEM_freeN(wo->mtex[act]);
1312 wo->mtex[act] = NULL;
1316 Tex *give_current_brush_texture(Brush *br)
1318 return br->mtex.tex;
1321 void set_current_brush_texture(Brush *br, Tex *newtex)
1324 id_us_min(&br->mtex.tex->id);
1327 br->mtex.tex = newtex;
1328 id_us_plus(&newtex->id);
1332 Tex *give_current_particle_texture(ParticleSettings *part)
1337 if (!part) return NULL;
1339 mtex = part->mtex[(int)(part->texact)];
1340 if (mtex) tex = mtex->tex;
1345 void set_current_particle_texture(ParticleSettings *part, Tex *newtex)
1347 int act = part->texact;
1349 if (part->mtex[act] && part->mtex[act]->tex)
1350 id_us_min(&part->mtex[act]->tex->id);
1353 if (!part->mtex[act]) {
1354 part->mtex[act] = add_mtex();
1355 part->mtex[act]->texco = TEXCO_ORCO;
1356 part->mtex[act]->blendtype = MTEX_MUL;
1359 part->mtex[act]->tex = newtex;
1360 id_us_plus(&newtex->id);
1362 else if (part->mtex[act]) {
1363 MEM_freeN(part->mtex[act]);
1364 part->mtex[act] = NULL;
1368 /* ------------------------------------------------------------------------- */
1370 EnvMap *BKE_add_envmap(void)
1374 env = MEM_callocN(sizeof(EnvMap), "envmap");
1375 env->type = ENV_CUBE;
1376 env->stype = ENV_ANIM;
1378 env->clipend = 100.0;
1380 env->viewscale = 0.5;
1385 /* ------------------------------------------------------------------------- */
1387 EnvMap *BKE_copy_envmap(EnvMap *env)
1392 envn = MEM_dupallocN(env);
1394 for (a = 0; a < 6; a++) envn->cube[a] = NULL;
1395 if (envn->ima) id_us_plus((ID *)envn->ima);
1400 /* ------------------------------------------------------------------------- */
1402 void BKE_free_envmapdata(EnvMap *env)
1406 for (part = 0; part < 6; part++) {
1407 if (env->cube[part])
1408 IMB_freeImBuf(env->cube[part]);
1409 env->cube[part] = NULL;
1414 /* ------------------------------------------------------------------------- */
1416 void BKE_free_envmap(EnvMap *env)
1419 BKE_free_envmapdata(env);
1424 /* ------------------------------------------------------------------------- */
1426 PointDensity *BKE_add_pointdensity(void)
1430 pd = MEM_callocN(sizeof(PointDensity), "pointdensity");
1433 pd->falloff_type = TEX_PD_FALLOFF_STD;
1434 pd->falloff_softness = 2.0;
1435 pd->source = TEX_PD_PSYS;
1436 pd->point_tree = NULL;
1437 pd->point_data = NULL;
1438 pd->noise_size = 0.5f;
1439 pd->noise_depth = 1;
1440 pd->noise_fac = 1.0f;
1441 pd->noise_influence = TEX_PD_NOISE_STATIC;
1442 pd->coba = add_colorband(1);
1443 pd->speed_scale = 1.0f;
1447 pd->psys_cache_space = TEX_PD_WORLDSPACE;
1448 pd->falloff_curve = curvemapping_add(1, 0, 0, 1, 1);
1450 pd->falloff_curve->preset = CURVE_PRESET_LINE;
1451 pd->falloff_curve->cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
1452 curvemap_reset(pd->falloff_curve->cm, &pd->falloff_curve->clipr, pd->falloff_curve->preset, CURVEMAP_SLOPE_POSITIVE);
1453 curvemapping_changed(pd->falloff_curve, 0);
1458 PointDensity *BKE_copy_pointdensity(PointDensity *pd)
1462 pdn = MEM_dupallocN(pd);
1463 pdn->point_tree = NULL;
1464 pdn->point_data = NULL;
1465 if (pdn->coba) pdn->coba = MEM_dupallocN(pdn->coba);
1466 pdn->falloff_curve = curvemapping_copy(pdn->falloff_curve); /* can be NULL */
1470 void BKE_free_pointdensitydata(PointDensity *pd)
1472 if (pd->point_tree) {
1473 BLI_bvhtree_free(pd->point_tree);
1474 pd->point_tree = NULL;
1476 if (pd->point_data) {
1477 MEM_freeN(pd->point_data);
1478 pd->point_data = NULL;
1481 MEM_freeN(pd->coba);
1485 curvemapping_free(pd->falloff_curve); /* can be NULL */
1488 void BKE_free_pointdensity(PointDensity *pd)
1490 BKE_free_pointdensitydata(pd);
1494 /* ------------------------------------------------------------------------- */
1496 void BKE_free_voxeldatadata(VoxelData *vd)
1499 MEM_freeN(vd->dataset);
1505 void BKE_free_voxeldata(VoxelData *vd)
1507 BKE_free_voxeldatadata(vd);
1511 VoxelData *BKE_add_voxeldata(void)
1515 vd = MEM_callocN(sizeof(VoxelData), "voxeldata");
1517 vd->resol[0] = vd->resol[1] = vd->resol[2] = 1;
1518 vd->interp_type = TEX_VD_LINEAR;
1519 vd->file_format = TEX_VD_SMOKE;
1520 vd->int_multiplier = 1.0;
1521 vd->extend = TEX_CLIP;
1523 vd->cachedframe = -1;
1529 VoxelData *BKE_copy_voxeldata(VoxelData *vd)
1533 vdn = MEM_dupallocN(vd);
1534 vdn->dataset = NULL;
1539 /* ------------------------------------------------------------------------- */
1541 OceanTex *BKE_add_oceantex(void)
1545 ot = MEM_callocN(sizeof(struct OceanTex), "ocean texture");
1546 ot->output = TEX_OCN_DISPLACEMENT;
1552 OceanTex *BKE_copy_oceantex(struct OceanTex *ot)
1554 OceanTex *otn = MEM_dupallocN(ot);
1559 void BKE_free_oceantex(struct OceanTex *ot)
1565 /* ------------------------------------------------------------------------- */
1566 int BKE_texture_dependsOnTime(const struct Tex *texture)
1568 if (texture->plugin) {
1569 // assume all plugins depend on time
1572 else if (texture->ima &&
1573 ELEM(texture->ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE))
1577 else if (texture->adt) {
1578 // assume anything in adt means the texture is animated
1581 else if (texture->type == TEX_NOISE) {
1582 // noise always varies with time
1588 /* ------------------------------------------------------------------------- */