6 * ***** BEGIN GPL LICENSE BLOCK *****
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23 * All rights reserved.
25 * The Original Code is: all of this file.
27 * Contributor(s): none yet.
29 * ***** END GPL LICENSE BLOCK *****
32 /** \file blender/blenkernel/intern/texture.c
42 #include "MEM_guardedalloc.h"
44 #include "PIL_dynlib.h"
46 #include "BLI_blenlib.h"
48 #include "BLI_kdopbvh.h"
49 #include "BLI_utildefines.h"
51 #include "DNA_key_types.h"
52 #include "DNA_object_types.h"
53 #include "DNA_lamp_types.h"
54 #include "DNA_material_types.h"
55 #include "DNA_world_types.h"
56 #include "DNA_brush_types.h"
57 #include "DNA_node_types.h"
58 #include "DNA_color_types.h"
59 #include "DNA_particle_types.h"
61 #include "IMB_imbuf.h"
63 #include "BKE_plugin_types.h"
64 #include "BKE_utildefines.h"
65 #include "BKE_global.h"
68 #include "BKE_library.h"
69 #include "BKE_image.h"
70 #include "BKE_material.h"
71 #include "BKE_texture.h"
73 #include "BKE_icons.h"
75 #include "BKE_animsys.h"
76 #include "BKE_colortools.h"
78 /* ------------------------------------------------------------------------- */
80 /* All support for plugin textures: */
81 int test_dlerr(const char *name, const char *symbol)
85 err= PIL_dynlib_get_error_as_string(NULL);
87 printf("var1: %s, var2: %s, var3: %s\n", name, symbol, err);
94 /* ------------------------------------------------------------------------- */
96 void open_plugin_tex(PluginTex *pit)
100 /* init all the happy variables */
108 pit->instance_init= NULL;
110 /* clear the error list */
111 PIL_dynlib_get_error_as_string(NULL);
113 /* no PIL_dynlib_close! multiple opened plugins... */
114 /* if(pit->handle) PIL_dynlib_close(pit->handle); */
115 /* pit->handle= 0; */
117 /* open the needed object */
118 pit->handle= PIL_dynlib_open(pit->name);
119 if(test_dlerr(pit->name, pit->name)) return;
121 if (pit->handle != NULL) {
122 /* find the address of the version function */
123 version= (int (*)(void)) PIL_dynlib_find_symbol(pit->handle, "plugin_tex_getversion");
124 if (test_dlerr(pit->name, "plugin_tex_getversion")) return;
126 if (version != NULL) {
127 pit->version= version();
128 if( pit->version >= 2 && pit->version <=6) {
129 int (*info_func)(PluginInfo *);
130 PluginInfo *info= (PluginInfo*) MEM_mallocN(sizeof(PluginInfo), "plugin_info");
132 info_func= (int (*)(PluginInfo *))PIL_dynlib_find_symbol(pit->handle, "plugin_getinfo");
133 if (!test_dlerr(pit->name, "plugin_getinfo")) {
134 info->instance_init = NULL;
138 pit->doit= (int(*)(void)) info->tex_doit;
139 pit->callback= (void(*)(unsigned short)) info->callback;
140 pit->stypes= info->stypes;
141 pit->vars= info->nvars;
142 pit->pname= info->name;
143 pit->stnames= info->snames;
144 pit->varstr= info->varstr;
145 pit->result= info->result;
146 pit->cfra= info->cfra;
147 pit->instance_init = info->instance_init;
148 if (info->init) info->init();
152 printf ("Plugin returned unrecognized version number\n");
159 /* ------------------------------------------------------------------------- */
161 /* very badlevel define to bypass linking with BIF_interface.h */
165 PluginTex *add_plugin_tex(char *str)
171 pit= MEM_callocN(sizeof(PluginTex), "plugintex");
173 strcpy(pit->name, str);
174 open_plugin_tex(pit);
176 if(pit->doit==NULL) {
177 if(pit->handle==NULL) {;} //XXX error("no plugin: %s", str);
178 else {;} //XXX error("in plugin: %s", str);
184 for(a=0; a<pit->vars; a++, varstr++) {
185 if( (varstr->type & FLO)==FLO)
186 pit->data[a]= varstr->def;
187 else if( (varstr->type & INT)==INT)
188 *((int *)(pit->data+a))= (int) varstr->def;
191 if (pit->instance_init)
192 pit->instance_init((void *) pit->data);
197 /* ------------------------------------------------------------------------- */
199 void free_plugin_tex(PluginTex *pit)
201 if(pit==NULL) return;
203 /* no PIL_dynlib_close: same plugin can be opened multiple times, 1 handle */
207 /* ****************** Mapping ******************* */
209 TexMapping *add_mapping(void)
211 TexMapping *texmap= MEM_callocN(sizeof(TexMapping), "Tex map");
213 texmap->size[0]= texmap->size[1]= texmap->size[2]= 1.0f;
214 texmap->max[0]= texmap->max[1]= texmap->max[2]= 1.0f;
215 unit_m4(texmap->mat);
220 void init_mapping(TexMapping *texmap)
222 float eul[3], smat[3][3], rmat[3][3], mat[3][3];
224 size_to_mat3( smat,texmap->size);
226 eul[0]= DEG2RADF(texmap->rot[0]);
227 eul[1]= DEG2RADF(texmap->rot[1]);
228 eul[2]= DEG2RADF(texmap->rot[2]);
229 eul_to_mat3( rmat,eul);
231 mul_m3_m3m3(mat, rmat, smat);
233 copy_m4_m3(texmap->mat, mat);
234 VECCOPY(texmap->mat[3], texmap->loc);
238 /* ****************** COLORBAND ******************* */
240 void init_colorband(ColorBand *coba, int rangetype)
244 coba->data[0].pos= 0.0;
245 coba->data[1].pos= 1.0;
248 coba->data[0].r= 0.0;
249 coba->data[0].g= 0.0;
250 coba->data[0].b= 0.0;
251 coba->data[0].a= 0.0;
253 coba->data[1].r= 1.0;
254 coba->data[1].g= 1.0;
255 coba->data[1].b= 1.0;
256 coba->data[1].a= 1.0;
259 coba->data[0].r= 0.0;
260 coba->data[0].g= 0.0;
261 coba->data[0].b= 0.0;
262 coba->data[0].a= 1.0;
264 coba->data[1].r= 1.0;
265 coba->data[1].g= 1.0;
266 coba->data[1].b= 1.0;
267 coba->data[1].a= 1.0;
270 for(a=2; a<MAXCOLORBAND; a++) {
271 coba->data[a].r= 0.5;
272 coba->data[a].g= 0.5;
273 coba->data[a].b= 0.5;
274 coba->data[a].a= 1.0;
275 coba->data[a].pos= 0.5;
282 ColorBand *add_colorband(int rangetype)
286 coba= MEM_callocN( sizeof(ColorBand), "colorband");
287 init_colorband(coba, rangetype);
292 /* ------------------------------------------------------------------------- */
294 int do_colorband(ColorBand *coba, float in, float out[4])
296 CBData *cbd1, *cbd2, *cbd0, *cbd3;
297 float fac, mfac, t[4];
300 if(coba==NULL || coba->tot==0) return 0;
310 if(in <= cbd1->pos && coba->ipotype<2) {
319 /* we're looking for first pos > in */
320 for(a=0; a<coba->tot; a++, cbd1++) if(cbd1->pos > in) break;
335 if(in >= cbd1->pos && coba->ipotype<2) {
343 if(cbd2->pos!=cbd1->pos)
344 fac= (in-cbd1->pos)/(cbd2->pos-cbd1->pos);
346 /* was setting to 0.0 in 2.56 & previous, but this
347 * is incorrect for the last element, see [#26732] */
348 fac= (a != coba->tot) ? 0.0f : 1.0f;
351 if (coba->ipotype==4) {
360 if(coba->ipotype>=2) {
361 /* ipo from right to left: 3 2 1 0 */
363 if(a>=coba->tot-1) cbd0= cbd1;
368 CLAMP(fac, 0.0f, 1.0f);
371 key_curve_position_weights(fac, t, KEY_CARDINAL);
373 key_curve_position_weights(fac, t, KEY_BSPLINE);
375 out[0]= t[3]*cbd3->r +t[2]*cbd2->r +t[1]*cbd1->r +t[0]*cbd0->r;
376 out[1]= t[3]*cbd3->g +t[2]*cbd2->g +t[1]*cbd1->g +t[0]*cbd0->g;
377 out[2]= t[3]*cbd3->b +t[2]*cbd2->b +t[1]*cbd1->b +t[0]*cbd0->b;
378 out[3]= t[3]*cbd3->a +t[2]*cbd2->a +t[1]*cbd1->a +t[0]*cbd0->a;
379 CLAMP(out[0], 0.0f, 1.0f);
380 CLAMP(out[1], 0.0f, 1.0f);
381 CLAMP(out[2], 0.0f, 1.0f);
382 CLAMP(out[3], 0.0f, 1.0f);
386 if(coba->ipotype==1) { /* EASE */
388 fac= 3.0f*mfac-2.0f*mfac*fac;
392 out[0]= mfac*cbd1->r + fac*cbd2->r;
393 out[1]= mfac*cbd1->g + fac*cbd2->g;
394 out[2]= mfac*cbd1->b + fac*cbd2->b;
395 out[3]= mfac*cbd1->a + fac*cbd2->a;
403 void colorband_table_RGBA(ColorBand *coba, float **array, int *size)
408 *array = MEM_callocN(sizeof(float)*(*size)*4, "ColorBand");
410 for(a=0; a<*size; a++)
411 do_colorband(coba, (float)a/(float)CM_TABLE, &(*array)[a*4]);
414 int vergcband(const void *a1, const void *a2)
416 const CBData *x1=a1, *x2=a2;
418 if( x1->pos > x2->pos ) return 1;
419 else if( x1->pos < x2->pos) return -1;
423 CBData *colorband_element_add(struct ColorBand *coba, float position)
427 if(coba->tot==MAXCOLORBAND) {
430 else if(coba->tot > 0) {
434 do_colorband(coba, position, col);
436 xnew = &coba->data[coba->tot];
437 xnew->pos = position;
446 coba->cur = coba->tot-1;
448 for(a = 0; a < coba->tot; a++)
449 coba->data[a].cur = a;
451 qsort(coba->data, coba->tot, sizeof(CBData), vergcband);
453 for(a = 0; a < coba->tot; a++) {
454 if(coba->data[a].cur == coba->cur) {
460 return coba->data + coba->cur;
463 int colorband_element_remove(struct ColorBand *coba, int index)
470 if(index < 0 || index >= coba->tot)
473 for(a = index; a < coba->tot; a++) {
474 coba->data[a] = coba->data[a + 1];
476 if(coba->cur) coba->cur--;
481 /* ******************* TEX ************************ */
483 void free_texture(Tex *tex)
485 free_plugin_tex(tex->plugin);
487 if(tex->coba) MEM_freeN(tex->coba);
488 if(tex->env) BKE_free_envmap(tex->env);
489 if(tex->pd) BKE_free_pointdensity(tex->pd);
490 if(tex->vd) BKE_free_voxeldata(tex->vd);
491 BKE_free_animdata((struct ID *)tex);
493 BKE_previewimg_free(&tex->preview);
494 BKE_icon_delete((struct ID*)tex);
498 ntreeFreeTree(tex->nodetree);
499 MEM_freeN(tex->nodetree);
503 /* ------------------------------------------------------------------------- */
505 void default_tex(Tex *tex)
511 tex->type= TEX_CLOUDS;
513 tex->flag= TEX_CHECKER_ODD;
514 tex->imaflag= TEX_INTERPOL|TEX_MIPMAP|TEX_USEALPHA;
515 tex->extend= TEX_REPEAT;
516 tex->cropxmin= tex->cropymin= 0.0;
517 tex->cropxmax= tex->cropymax= 1.0;
518 tex->texfilter = TXF_EWA;
520 tex->xrepeat= tex->yrepeat= 1;
525 tex->noisesize= 0.25;
528 tex->nabla= 0.025; // also in do_versions
531 tex->saturation= 1.0;
532 tex->filtersize= 1.0;
536 /* newnoise: init. */
538 tex->noisebasis2 = 0;
541 tex->mg_lacunarity = 2.0;
542 tex->mg_octaves = 2.0;
543 tex->mg_offset = 1.0;
545 tex->ns_outscale = 1.0;
547 tex->dist_amount = 1.0;
550 tex->vn_w2 = tex->vn_w3 = tex->vn_w4 = 0.0;
556 tex->env->stype=ENV_ANIM;
557 tex->env->clipsta=0.1;
558 tex->env->clipend=100;
559 tex->env->cuberes=600;
564 tex->pd->radius = 0.3f;
565 tex->pd->falloff_type = TEX_PD_FALLOFF_STD;
569 tex->vd->resol[0] = tex->vd->resol[1] = tex->vd->resol[2] = 0;
570 tex->vd->interp_type=TEX_VD_LINEAR;
571 tex->vd->file_format=TEX_VD_SMOKE;
577 for(a=0; a<pit->vars; a++, varstr++) {
578 pit->data[a] = varstr->def;
583 tex->iuser.fie_ima= 2;
585 tex->iuser.frames= 100;
591 void tex_set_type(Tex *tex, int type)
597 tex->vd = BKE_add_voxeldata();
599 case TEX_POINTDENSITY:
601 tex->pd = BKE_add_pointdensity();
604 if (tex->env == NULL)
605 tex->env = BKE_add_envmap();
612 /* ------------------------------------------------------------------------- */
614 Tex *add_texture(const char *name)
619 tex= alloc_libblock(&bmain->tex, ID_TE, name);
626 /* ------------------------------------------------------------------------- */
628 void default_mtex(MTex *mtex)
630 mtex->texco= TEXCO_ORCO;
631 mtex->mapto= MAP_COL;
636 mtex->mapping= MTEX_FLAT;
644 mtex->texflag= MTEX_3TAP_BUMP | MTEX_BUMP_OBJECTSPACE;
651 mtex->blendtype= MTEX_BLEND;
656 mtex->colspecfac= 1.0f;
658 mtex->alphafac= 1.0f;
663 mtex->raymirrfac= 1.0f;
664 mtex->translfac= 1.0f;
666 mtex->colemitfac= 1.0f;
667 mtex->colreflfac= 1.0f;
668 mtex->coltransfac= 1.0f;
670 mtex->scatterfac= 1.0f;
672 mtex->shadowfac= 1.0f;
673 mtex->zenupfac= 1.0f;
674 mtex->zendownfac= 1.0f;
675 mtex->blendfac= 1.0f;
677 mtex->lengthfac= 1.0f;
678 mtex->clumpfac= 1.0f;
680 mtex->roughfac= 1.0f;
681 mtex->padensfac= 1.0f;
686 mtex->gravityfac= 1.0f;
687 mtex->fieldfac= 1.0f;
688 mtex->normapspace= MTEX_NSPACE_TANGENT;
692 /* ------------------------------------------------------------------------- */
698 mtex= MEM_callocN(sizeof(MTex), "add_mtex");
705 /* slot -1 for first free ID */
706 MTex *add_mtex_id(ID *id, int slot)
711 give_active_mtex(id, &mtex_ar, &act);
718 /* find first free */
720 for (i= 0; i < MAX_MTEX; i++) {
731 /* make sure slot is valid */
732 if(slot < 0 || slot >= MAX_MTEX) {
738 id_us_min((ID *)mtex_ar[slot]->tex);
739 MEM_freeN(mtex_ar[slot]);
743 mtex_ar[slot]= add_mtex();
745 return mtex_ar[slot];
748 /* ------------------------------------------------------------------------- */
750 Tex *copy_texture(Tex *tex)
754 texn= copy_libblock(tex);
755 if(texn->type==TEX_IMAGE) id_us_plus((ID *)texn->ima);
756 else texn->ima= NULL;
759 texn->plugin= MEM_dupallocN(texn->plugin);
760 open_plugin_tex(texn->plugin);
763 if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
764 if(texn->env) texn->env= BKE_copy_envmap(texn->env);
765 if(texn->pd) texn->pd= MEM_dupallocN(texn->pd);
766 if(texn->vd) texn->vd= MEM_dupallocN(texn->vd);
768 if(tex->preview) texn->preview = BKE_previewimg_copy(tex->preview);
771 ntreeEndExecTree(tex->nodetree);
772 texn->nodetree= ntreeCopyTree(tex->nodetree);
778 /* texture copy without adding to main dbase */
779 Tex *localize_texture(Tex *tex)
783 texn= copy_libblock(tex);
784 BLI_remlink(&G.main->tex, texn);
786 /* image texture: free_texture also doesn't decrease */
789 texn->plugin= MEM_dupallocN(texn->plugin);
790 open_plugin_tex(texn->plugin);
793 if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
795 texn->env= BKE_copy_envmap(texn->env);
796 id_us_min(&texn->env->ima->id);
799 texn->pd= MEM_dupallocN(texn->pd);
801 texn->pd->point_tree = NULL;
802 texn->pd->coba= MEM_dupallocN(texn->pd->coba);
807 texn->vd= MEM_dupallocN(texn->vd);
808 if(texn->vd->dataset)
809 texn->vd->dataset= MEM_dupallocN(texn->vd->dataset);
812 texn->preview = NULL;
815 texn->nodetree= ntreeLocalize(tex->nodetree);
822 /* ------------------------------------------------------------------------- */
824 void make_local_texture(Tex *tex)
832 ParticleSettings *pa;
833 int a, local=0, lib=0;
835 /* - only lib users: do nothing
836 * - only local users: set flag
840 if(tex->id.lib==NULL) return;
842 /* special case: ima always local immediately */
844 tex->ima->id.lib= NULL;
845 tex->ima->id.flag= LIB_LOCAL;
846 new_id(&bmain->image, (ID *)tex->ima, NULL);
851 tex->id.flag= LIB_LOCAL;
852 new_id(&bmain->tex, (ID *)tex, NULL);
857 ma= bmain->mat.first;
859 for(a=0; a<MAX_MTEX; a++) {
860 if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
861 if(ma->id.lib) lib= 1;
867 la= bmain->lamp.first;
869 for(a=0; a<MAX_MTEX; a++) {
870 if(la->mtex[a] && la->mtex[a]->tex==tex) {
871 if(la->id.lib) lib= 1;
877 wrld= bmain->world.first;
879 for(a=0; a<MAX_MTEX; a++) {
880 if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
881 if(wrld->id.lib) lib= 1;
887 br= bmain->brush.first;
889 if(br->mtex.tex==tex) {
890 if(br->id.lib) lib= 1;
895 pa= bmain->particle.first;
897 for(a=0; a<MAX_MTEX; a++) {
898 if(pa->mtex[a] && pa->mtex[a]->tex==tex) {
899 if(pa->id.lib) lib= 1;
906 if(local && lib==0) {
908 tex->id.flag= LIB_LOCAL;
909 new_id(&bmain->tex, (ID *)tex, NULL);
911 else if(local && lib) {
912 texn= copy_texture(tex);
915 ma= bmain->mat.first;
917 for(a=0; a<MAX_MTEX; a++) {
918 if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
919 if(ma->id.lib==NULL) {
920 ma->mtex[a]->tex= texn;
928 la= bmain->lamp.first;
930 for(a=0; a<MAX_MTEX; a++) {
931 if(la->mtex[a] && la->mtex[a]->tex==tex) {
932 if(la->id.lib==NULL) {
933 la->mtex[a]->tex= texn;
941 wrld= bmain->world.first;
943 for(a=0; a<MAX_MTEX; a++) {
944 if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
945 if(wrld->id.lib==NULL) {
946 wrld->mtex[a]->tex= texn;
954 br= bmain->brush.first;
956 if(br->mtex.tex==tex) {
957 if(br->id.lib==NULL) {
965 pa= bmain->particle.first;
967 for(a=0; a<MAX_MTEX; a++) {
968 if(pa->mtex[a] && pa->mtex[a]->tex==tex) {
969 if(pa->id.lib==NULL) {
970 pa->mtex[a]->tex= texn;
981 /* ------------------------------------------------------------------------- */
983 void autotexname(Tex *tex)
986 char texstr[20][15]= {"None" , "Clouds" , "Wood", "Marble", "Magic" , "Blend",
987 "Stucci", "Noise" , "Image", "Plugin", "EnvMap" , "Musgrave",
988 "Voronoi", "DistNoise", "Point Density", "Voxel Data", "", "", "", ""};
990 char di[FILE_MAXDIR], fi[FILE_MAXFILE];
994 new_id(&bmain->tex, (ID *)tex, "Noddy");
997 if(tex->type==TEX_IMAGE) {
1000 strcpy(di, ima->name);
1001 BLI_splitdirstring(di, fi);
1004 new_id(&bmain->tex, (ID *)tex, di);
1006 else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
1008 else if(tex->type==TEX_PLUGIN && tex->plugin) new_id(&bmain->tex, (ID *)tex, tex->plugin->pname);
1009 else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
1013 /* ------------------------------------------------------------------------- */
1015 Tex *give_current_object_texture(Object *ob)
1020 if(ob==NULL) return NULL;
1021 if(ob->totcol==0 && !(ob->type==OB_LAMP)) return NULL;
1023 if(ob->type==OB_LAMP) {
1024 tex= give_current_lamp_texture(ob->data);
1026 ma= give_current_material(ob, ob->actcol);
1027 tex= give_current_material_texture(ma);
1033 Tex *give_current_lamp_texture(Lamp *la)
1039 mtex= la->mtex[(int)(la->texact)];
1040 if(mtex) tex= mtex->tex;
1046 void set_current_lamp_texture(Lamp *la, Tex *newtex)
1048 int act= la->texact;
1050 if(la->mtex[act] && la->mtex[act]->tex)
1051 id_us_min(&la->mtex[act]->tex->id);
1054 if(!la->mtex[act]) {
1055 la->mtex[act]= add_mtex();
1056 la->mtex[act]->texco= TEXCO_GLOB;
1059 la->mtex[act]->tex= newtex;
1060 id_us_plus(&newtex->id);
1062 else if(la->mtex[act]) {
1063 MEM_freeN(la->mtex[act]);
1064 la->mtex[act]= NULL;
1068 bNode *give_current_material_texture_node(Material *ma)
1070 if(ma && ma->use_nodes && ma->nodetree)
1071 return nodeGetActiveID(ma->nodetree, ID_TE);
1076 Tex *give_current_material_texture(Material *ma)
1082 if(ma && ma->use_nodes && ma->nodetree) {
1083 /* first check texture, then material, this works together
1084 with a hack that clears the active ID flag for textures on
1085 making a material node active */
1086 node= nodeGetActiveID(ma->nodetree, ID_TE);
1089 tex= (Tex *)node->id;
1093 node= nodeGetActiveID(ma->nodetree, ID_MA);
1095 ma= (Material*)node->id;
1097 mtex= ma->mtex[(int)(ma->texact)];
1098 if(mtex) tex= mtex->tex;
1106 mtex= ma->mtex[(int)(ma->texact)];
1107 if(mtex) tex= mtex->tex;
1113 int give_active_mtex(ID *id, MTex ***mtex_ar, short *act)
1115 switch(GS(id->name)) {
1117 *mtex_ar= ((Material *)id)->mtex;
1118 if(act) *act= (((Material *)id)->texact);
1121 *mtex_ar= ((World *)id)->mtex;
1122 if(act) *act= (((World *)id)->texact);
1125 *mtex_ar= ((Lamp *)id)->mtex;
1126 if(act) *act= (((Lamp *)id)->texact);
1129 *mtex_ar= ((ParticleSettings *)id)->mtex;
1130 if(act) *act= (((ParticleSettings *)id)->texact);
1141 void set_active_mtex(ID *id, short act)
1144 else if(act>=MAX_MTEX) act= MAX_MTEX-1;
1146 switch(GS(id->name)) {
1148 ((Material *)id)->texact= act;
1151 ((World *)id)->texact= act;
1154 ((Lamp *)id)->texact= act;
1157 ((ParticleSettings *)id)->texact= act;
1162 void set_current_material_texture(Material *ma, Tex *newtex)
1167 if(ma && ma->use_nodes && ma->nodetree) {
1168 node= nodeGetActiveID(ma->nodetree, ID_TE);
1171 tex= (Tex *)node->id;
1172 id_us_min(&tex->id);
1173 node->id= &newtex->id;
1174 id_us_plus(&newtex->id);
1178 node= nodeGetActiveID(ma->nodetree, ID_MA);
1180 ma= (Material*)node->id;
1184 int act= (int)ma->texact;
1186 tex= (ma->mtex[act])? ma->mtex[act]->tex: NULL;
1187 id_us_min(&tex->id);
1191 ma->mtex[act]= add_mtex();
1193 ma->mtex[act]->tex= newtex;
1194 id_us_plus(&newtex->id);
1196 else if(ma->mtex[act]) {
1197 MEM_freeN(ma->mtex[act]);
1198 ma->mtex[act]= NULL;
1203 Tex *give_current_world_texture(World *world)
1208 if(!world) return NULL;
1210 mtex= world->mtex[(int)(world->texact)];
1211 if(mtex) tex= mtex->tex;
1216 void set_current_world_texture(World *wo, Tex *newtex)
1218 int act= wo->texact;
1220 if(wo->mtex[act] && wo->mtex[act]->tex)
1221 id_us_min(&wo->mtex[act]->tex->id);
1224 if(!wo->mtex[act]) {
1225 wo->mtex[act]= add_mtex();
1226 wo->mtex[act]->texco= TEXCO_VIEW;
1229 wo->mtex[act]->tex= newtex;
1230 id_us_plus(&newtex->id);
1232 else if(wo->mtex[act]) {
1233 MEM_freeN(wo->mtex[act]);
1234 wo->mtex[act]= NULL;
1238 Tex *give_current_brush_texture(Brush *br)
1240 return br->mtex.tex;
1243 void set_current_brush_texture(Brush *br, Tex *newtex)
1246 id_us_min(&br->mtex.tex->id);
1249 br->mtex.tex= newtex;
1250 id_us_plus(&newtex->id);
1254 Tex *give_current_particle_texture(ParticleSettings *part)
1259 if(!part) return NULL;
1261 mtex= part->mtex[(int)(part->texact)];
1262 if(mtex) tex= mtex->tex;
1267 void set_current_particle_texture(ParticleSettings *part, Tex *newtex)
1269 int act= part->texact;
1271 if(part->mtex[act] && part->mtex[act]->tex)
1272 id_us_min(&part->mtex[act]->tex->id);
1275 if(!part->mtex[act]) {
1276 part->mtex[act]= add_mtex();
1277 part->mtex[act]->texco= TEXCO_ORCO;
1278 part->mtex[act]->blendtype= MTEX_MUL;
1281 part->mtex[act]->tex= newtex;
1282 id_us_plus(&newtex->id);
1284 else if(part->mtex[act]) {
1285 MEM_freeN(part->mtex[act]);
1286 part->mtex[act]= NULL;
1290 /* ------------------------------------------------------------------------- */
1292 EnvMap *BKE_add_envmap(void)
1296 env= MEM_callocN(sizeof(EnvMap), "envmap");
1297 env->type= ENV_CUBE;
1298 env->stype= ENV_ANIM;
1300 env->clipend= 100.0;
1302 env->viewscale = 0.5;
1307 /* ------------------------------------------------------------------------- */
1309 EnvMap *BKE_copy_envmap(EnvMap *env)
1314 envn= MEM_dupallocN(env);
1316 for(a=0; a<6; a++) envn->cube[a]= NULL;
1317 if(envn->ima) id_us_plus((ID *)envn->ima);
1322 /* ------------------------------------------------------------------------- */
1324 void BKE_free_envmapdata(EnvMap *env)
1328 for(part=0; part<6; part++) {
1330 IMB_freeImBuf(env->cube[part]);
1331 env->cube[part]= NULL;
1336 /* ------------------------------------------------------------------------- */
1338 void BKE_free_envmap(EnvMap *env)
1341 BKE_free_envmapdata(env);
1346 /* ------------------------------------------------------------------------- */
1348 PointDensity *BKE_add_pointdensity(void)
1352 pd= MEM_callocN(sizeof(PointDensity), "pointdensity");
1355 pd->falloff_type = TEX_PD_FALLOFF_STD;
1356 pd->falloff_softness = 2.0;
1357 pd->source = TEX_PD_PSYS;
1358 pd->point_tree = NULL;
1359 pd->point_data = NULL;
1360 pd->noise_size = 0.5f;
1361 pd->noise_depth = 1;
1362 pd->noise_fac = 1.0f;
1363 pd->noise_influence = TEX_PD_NOISE_STATIC;
1364 pd->coba = add_colorband(1);
1365 pd->speed_scale = 1.0f;
1369 pd->psys_cache_space= TEX_PD_WORLDSPACE;
1370 pd->falloff_curve = curvemapping_add(1, 0, 0, 1, 1);
1372 pd->falloff_curve->preset = CURVE_PRESET_LINE;
1373 pd->falloff_curve->cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
1374 curvemap_reset(pd->falloff_curve->cm, &pd->falloff_curve->clipr, pd->falloff_curve->preset, CURVEMAP_SLOPE_POSITIVE);
1375 curvemapping_changed(pd->falloff_curve, 0);
1380 PointDensity *BKE_copy_pointdensity(PointDensity *pd)
1384 pdn= MEM_dupallocN(pd);
1385 pdn->point_tree = NULL;
1386 pdn->point_data = NULL;
1387 if(pdn->coba) pdn->coba= MEM_dupallocN(pdn->coba);
1392 void BKE_free_pointdensitydata(PointDensity *pd)
1394 if (pd->point_tree) {
1395 BLI_bvhtree_free(pd->point_tree);
1396 pd->point_tree = NULL;
1398 if (pd->point_data) {
1399 MEM_freeN(pd->point_data);
1400 pd->point_data = NULL;
1403 MEM_freeN(pd->coba);
1408 void BKE_free_pointdensity(PointDensity *pd)
1410 BKE_free_pointdensitydata(pd);
1415 void BKE_free_voxeldatadata(struct VoxelData *vd)
1418 MEM_freeN(vd->dataset);
1424 void BKE_free_voxeldata(struct VoxelData *vd)
1426 BKE_free_voxeldatadata(vd);
1430 struct VoxelData *BKE_add_voxeldata(void)
1434 vd= MEM_callocN(sizeof(struct VoxelData), "voxeldata");
1436 vd->resol[0] = vd->resol[1] = vd->resol[2] = 1;
1437 vd->interp_type= TEX_VD_LINEAR;
1438 vd->file_format= TEX_VD_SMOKE;
1439 vd->int_multiplier = 1.0;
1440 vd->extend = TEX_CLIP;
1442 vd->cachedframe = -1;
1448 struct VoxelData *BKE_copy_voxeldata(struct VoxelData *vd)
1452 vdn= MEM_dupallocN(vd);
1453 vdn->dataset = NULL;
1459 /* ------------------------------------------------------------------------- */
1460 int BKE_texture_dependsOnTime(const struct Tex *texture)
1462 if(texture->plugin) {
1463 // assume all plugins depend on time
1466 else if( texture->ima &&
1467 ELEM(texture->ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
1470 else if(texture->adt) {
1471 // assume anything in adt means the texture is animated
1477 /* ------------------------------------------------------------------------- */