7 * ***** BEGIN GPL LICENSE BLOCK *****
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): none yet.
30 * ***** END GPL LICENSE BLOCK *****
33 /** \file blender/blenkernel/intern/material.c
41 #include "MEM_guardedalloc.h"
43 #include "DNA_curve_types.h"
44 #include "DNA_material_types.h"
45 #include "DNA_mesh_types.h"
46 #include "DNA_meta_types.h"
47 #include "DNA_node_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_scene_types.h"
52 #include "BLI_listbase.h"
53 #include "BLI_utildefines.h"
55 #include "BKE_animsys.h"
56 #include "BKE_displist.h"
57 #include "BKE_global.h"
58 #include "BKE_icons.h"
59 #include "BKE_library.h"
61 #include "BKE_material.h"
66 #include "GPU_material.h"
68 /* used in UI and render */
71 /* called on startup, creator.c */
72 void init_def_material(void)
74 init_material(&defmaterial);
77 /* not material itself */
78 void free_material(Material *ma)
83 for(a=0; a<MAX_MTEX; a++) {
85 if(mtex && mtex->tex) mtex->tex->id.us--;
86 if(mtex) MEM_freeN(mtex);
89 if(ma->ramp_col) MEM_freeN(ma->ramp_col);
90 if(ma->ramp_spec) MEM_freeN(ma->ramp_spec);
92 BKE_free_animdata((ID *)ma);
95 BKE_previewimg_free(&ma->preview);
96 BKE_icon_delete((struct ID*)ma);
99 /* is no lib link block, but material extension */
101 ntreeFreeTree(ma->nodetree);
102 MEM_freeN(ma->nodetree);
105 if(ma->gpumaterial.first)
106 GPU_material_free(ma);
109 void init_material(Material *ma)
111 ma->r= ma->g= ma->b= ma->ref= 0.8;
112 ma->specr= ma->specg= ma->specb= 1.0;
113 ma->mirr= ma->mirg= ma->mirb= 1.0;
117 ma->spec= ma->hasize= 0.5;
119 ma->starc= ma->ringc= 4;
122 ma->flaresize= ma->subsize= 1.0;
135 ma->strand_sta= ma->strand_end= 1.0f;
139 ma->ray_depth_tra= 2;
140 ma->fresnel_mir= 0.0;
141 ma->fresnel_tra= 0.0;
142 ma->fresnel_tra_i= 1.25;
143 ma->fresnel_mir_i= 1.25;
146 ma->shad_alpha= 1.0f;
148 ma->gloss_mir = ma->gloss_tra= 1.0;
149 ma->samp_gloss_mir = ma->samp_gloss_tra= 18;
150 ma->adapt_thresh_mir = ma->adapt_thresh_tra = 0.005;
152 ma->fadeto_mir = MA_RAYMIR_FADETOSKY;
154 ma->rampfac_col= 1.0;
155 ma->rampfac_spec= 1.0;
156 ma->pr_lamp= 3; /* two lamps, is bits */
157 ma->pr_type= MA_SPHERE;
159 ma->sss_radius[0]= 1.0f;
160 ma->sss_radius[1]= 1.0f;
161 ma->sss_radius[2]= 1.0f;
162 ma->sss_col[0]= 1.0f;
163 ma->sss_col[1]= 1.0f;
164 ma->sss_col[2]= 1.0f;
165 ma->sss_error= 0.05f;
168 ma->sss_colfac= 1.0f;
169 ma->sss_texfac= 0.0f;
173 ma->vol.density = 1.0f;
174 ma->vol.emission = 0.0f;
175 ma->vol.scattering = 1.0f;
176 ma->vol.reflection = 1.0f;
177 ma->vol.transmission_col[0] = ma->vol.transmission_col[1] = ma->vol.transmission_col[2] = 1.0f;
178 ma->vol.reflection_col[0] = ma->vol.reflection_col[1] = ma->vol.reflection_col[2] = 1.0f;
179 ma->vol.emission_col[0] = ma->vol.emission_col[1] = ma->vol.emission_col[2] = 1.0f;
180 ma->vol.density_scale = 1.0f;
181 ma->vol.depth_cutoff = 0.01f;
182 ma->vol.stepsize_type = MA_VOL_STEP_RANDOMIZED;
183 ma->vol.stepsize = 0.2f;
184 ma->vol.shade_type = MA_VOL_SHADE_SHADED;
185 ma->vol.shadeflag |= MA_VOL_PRECACHESHADING;
186 ma->vol.precache_resolution = 50;
187 ma->vol.ms_spread = 0.2f;
188 ma->vol.ms_diff = 1.f;
189 ma->vol.ms_intensity = 1.f;
191 ma->mode= MA_TRACEBLE|MA_SHADBUF|MA_SHADOW|MA_RAYBIAS|MA_TANGENT_STR|MA_ZTRANSP;
192 ma->shade_flag= MA_APPROX_OCCLUSION;
196 Material *add_material(const char *name)
200 ma= alloc_libblock(&G.main->mat, ID_MA, name);
207 /* XXX keep synced with next function */
208 Material *copy_material(Material *ma)
213 man= copy_libblock(ma);
215 id_lib_extern((ID *)man->group);
217 for(a=0; a<MAX_MTEX; a++) {
219 man->mtex[a]= MEM_mallocN(sizeof(MTex), "copymaterial");
220 memcpy(man->mtex[a], ma->mtex[a], sizeof(MTex));
221 id_us_plus((ID *)man->mtex[a]->tex);
225 if(ma->ramp_col) man->ramp_col= MEM_dupallocN(ma->ramp_col);
226 if(ma->ramp_spec) man->ramp_spec= MEM_dupallocN(ma->ramp_spec);
228 if (ma->preview) man->preview = BKE_previewimg_copy(ma->preview);
231 man->nodetree= ntreeCopyTree(ma->nodetree); /* 0 == full new tree */
234 man->gpumaterial.first= man->gpumaterial.last= NULL;
239 /* XXX (see above) material copy without adding to main dbase */
240 Material *localize_material(Material *ma)
245 man= copy_libblock(ma);
246 BLI_remlink(&G.main->mat, man);
248 /* no increment for texture ID users, in previewrender.c it prevents decrement */
249 for(a=0; a<MAX_MTEX; a++) {
251 man->mtex[a]= MEM_mallocN(sizeof(MTex), "copymaterial");
252 memcpy(man->mtex[a], ma->mtex[a], sizeof(MTex));
256 if(ma->ramp_col) man->ramp_col= MEM_dupallocN(ma->ramp_col);
257 if(ma->ramp_spec) man->ramp_spec= MEM_dupallocN(ma->ramp_spec);
262 man->nodetree= ntreeLocalize(ma->nodetree);
264 man->gpumaterial.first= man->gpumaterial.last= NULL;
269 static void extern_local_material(Material *ma)
272 for(i=0; i < MAX_MTEX; i++) {
273 if(ma->mtex[i]) id_lib_extern((ID *)ma->mtex[i]->tex);
277 void make_local_material(Material *ma)
285 int a, local=0, lib=0;
287 /* - only lib users: do nothing
288 * - only local users: set flag
292 if(ma->id.lib==NULL) return;
295 ma->id.flag= LIB_LOCAL;
297 new_id(&bmain->mat, (ID *)ma, NULL);
298 extern_local_material(ma);
303 ob= bmain->object.first;
306 for(a=0; a<ob->totcol; a++) {
308 if(ob->id.lib) lib= 1;
316 me= bmain->mesh.first;
319 for(a=0; a<me->totcol; a++) {
321 if(me->id.lib) lib= 1;
329 cu= bmain->curve.first;
332 for(a=0; a<cu->totcol; a++) {
334 if(cu->id.lib) lib= 1;
342 mb= bmain->mball.first;
345 for(a=0; a<mb->totcol; a++) {
347 if(mb->id.lib) lib= 1;
355 if(local && lib==0) {
357 ma->id.flag= LIB_LOCAL;
359 new_id(&bmain->mat, (ID *)ma, NULL);
360 extern_local_material(ma);
362 else if(local && lib) {
364 man= copy_material(ma);
368 ob= bmain->object.first;
371 for(a=0; a<ob->totcol; a++) {
373 if(ob->id.lib==NULL) {
384 me= bmain->mesh.first;
387 for(a=0; a<me->totcol; a++) {
389 if(me->id.lib==NULL) {
400 cu= bmain->curve.first;
403 for(a=0; a<cu->totcol; a++) {
405 if(cu->id.lib==NULL) {
416 mb= bmain->mball.first;
419 for(a=0; a<mb->totcol; a++) {
421 if(mb->id.lib==NULL) {
434 /* for curve, mball, mesh types */
435 void extern_local_matarar(struct Material **matar, short totcol)
438 for(i= 0; i < totcol; i++) {
439 id_lib_extern((ID *)matar[i]);
443 Material ***give_matarar(Object *ob)
449 if(ob->type==OB_MESH) {
453 else if ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF) {
457 else if(ob->type==OB_MBALL) {
464 short *give_totcolp(Object *ob)
470 if(ob->type==OB_MESH) {
472 return &(me->totcol);
474 else if ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF) {
476 return &(cu->totcol);
478 else if(ob->type==OB_MBALL) {
480 return &(mb->totcol);
485 /* same as above but for ID's */
486 Material ***give_matarar_id(ID *id)
488 switch(GS(id->name)) {
490 return &(((Mesh *)id)->mat);
493 return &(((Curve *)id)->mat);
496 return &(((MetaBall *)id)->mat);
502 short *give_totcolp_id(ID *id)
504 switch(GS(id->name)) {
506 return &(((Mesh *)id)->totcol);
509 return &(((Curve *)id)->totcol);
512 return &(((MetaBall *)id)->totcol);
518 void material_append_id(ID *id, Material *ma)
521 if((matar= give_matarar_id(id))) {
522 short *totcol= give_totcolp_id(id);
523 Material **mat= MEM_callocN(sizeof(void *) * ((*totcol) + 1), "newmatar");
524 if(*totcol) memcpy(mat, *matar, sizeof(void *) * (*totcol));
525 if(*matar) MEM_freeN(*matar);
528 (*matar)[(*totcol)++]= ma;
530 id_us_plus((ID *)ma);
531 test_object_materials(id);
535 Material *material_pop_id(ID *id, int index)
539 if((matar= give_matarar_id(id))) {
540 short *totcol= give_totcolp_id(id);
541 if(index >= 0 && index < (*totcol)) {
542 ret= (*matar)[index];
543 id_us_min((ID *)ret);
552 if(index + 1 != (*totcol))
553 memmove((*matar), (*matar) + 1, (*totcol) - (index + 1));
557 mat= MEM_callocN(sizeof(void *) * (*totcol), "newmatar");
558 memcpy(mat, *matar, sizeof(void *) * (*totcol));
562 test_object_materials(id);
570 Material *give_current_material(Object *ob, int act)
572 Material ***matarar, *ma;
575 if(ob==NULL) return NULL;
577 /* if object cannot have material, totcolp==NULL */
578 totcolp= give_totcolp(ob);
579 if(totcolp==NULL || ob->totcol==0) return NULL;
585 if(act>ob->totcol) act= ob->totcol;
586 else if(act<=0) act= 1;
588 if(ob->matbits && ob->matbits[act-1]) { /* in object */
593 /* check for inconsistency */
594 if(*totcolp < ob->totcol)
595 ob->totcol= *totcolp;
596 if(act>ob->totcol) act= ob->totcol;
598 matarar= give_matarar(ob);
600 if(matarar && *matarar) ma= (*matarar)[act-1];
608 ID *material_from(Object *ob, int act)
611 if(ob==NULL) return NULL;
613 if(ob->totcol==0) return ob->data;
616 if(ob->matbits[act-1]) return (ID *)ob;
617 else return ob->data;
620 Material *give_node_material(Material *ma)
622 if(ma && ma->use_nodes && ma->nodetree) {
623 bNode *node= nodeGetActiveID(ma->nodetree, ID_MA);
626 return (Material *)node->id;
632 /* GS reads the memory pointed at in a specific ordering. There are,
633 * however two definitions for it. I have jotted them down here, both,
634 * but I think the first one is actually used. The thing is that
635 * big-endian systems might read this the wrong way round. OTOH, we
636 * constructed the IDs that are read out with this macro explicitly as
637 * well. I expect we'll sort it out soon... */
640 #define GS(a) (*((short *)(a)))
642 /* from misc_util: flip the bytes from x */
643 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
645 void resize_object_material(Object *ob, const short totcol)
653 MEM_freeN(ob->matbits);
658 else if(ob->totcol<totcol) {
659 newmatar= MEM_callocN(sizeof(void *)*totcol, "newmatar");
660 newmatbits= MEM_callocN(sizeof(char)*totcol, "newmatbits");
662 memcpy(newmatar, ob->mat, sizeof(void *)*ob->totcol);
663 memcpy(newmatbits, ob->matbits, sizeof(char)*ob->totcol);
665 MEM_freeN(ob->matbits);
668 ob->matbits= newmatbits;
671 if(ob->totcol && ob->actcol==0) ob->actcol= 1;
672 if(ob->actcol>ob->totcol) ob->actcol= ob->totcol;
675 void test_object_materials(ID *id)
677 /* make the ob mat-array same size as 'ob->data' mat-array */
681 if(id==NULL || (totcol=give_totcolp_id(id))==NULL) {
685 for(ob= G.main->object.first; ob; ob= ob->id.next) {
687 resize_object_material(ob, *totcol);
692 void assign_material(Object *ob, Material *ma, int act)
694 Material *mao, **matar, ***matarar;
698 if(act>MAXMAT) return;
701 /* prevent crashing when using accidentally */
702 BLI_assert(ob->id.lib == NULL);
703 if(ob->id.lib) return;
707 totcolp= give_totcolp(ob);
708 matarar= give_matarar(ob);
710 if(totcolp==NULL || matarar==NULL) return;
713 matar= MEM_callocN(sizeof(void *)*act, "matarray1");
716 memcpy(matar, *matarar, sizeof(void *)*(*totcolp));
724 if(act > ob->totcol) {
725 matar= MEM_callocN(sizeof(void *)*act, "matarray2");
726 matbits= MEM_callocN(sizeof(char)*act, "matbits1");
728 memcpy(matar, ob->mat, sizeof(void *)*( ob->totcol ));
729 memcpy(matbits, ob->matbits, sizeof(char)*(*totcolp));
731 MEM_freeN(ob->matbits);
734 ob->matbits= matbits;
737 /* copy object/mesh linking, or assign based on userpref */
739 ob->matbits[act-1]= ob->matbits[ob->actcol-1];
741 ob->matbits[act-1]= (U.flag & USER_MAT_ON_OB)? 1: 0;
746 if(ob->matbits[act-1]) { /* in object */
748 if(mao) mao->id.us--;
752 mao= (*matarar)[act-1];
753 if(mao) mao->id.us--;
754 (*matarar)[act-1]= ma;
758 id_us_plus((ID *)ma);
759 test_object_materials(ob->data);
762 /* XXX - this calls many more update calls per object then are needed, could be optimized */
763 void assign_matarar(struct Object *ob, struct Material ***matar, int totcol)
765 int i, actcol_orig= ob->actcol;
767 while(object_remove_material_slot(ob)) {};
769 /* now we have the right number of slots */
770 for(i=0; i<totcol; i++)
771 assign_material(ob, (*matar)[i], i+1);
773 if(actcol_orig > ob->totcol)
774 actcol_orig= ob->totcol;
776 ob->actcol= actcol_orig;
780 int find_material_index(Object *ob, Material *ma)
785 if(ma==NULL) return 0;
787 totcolp= give_totcolp(ob);
788 matarar= give_matarar(ob);
790 if(totcolp==NULL || matarar==NULL) return 0;
792 for(a=0; a<*totcolp; a++)
793 if((*matarar)[a]==ma)
800 int object_add_material_slot(Object *ob)
802 if(ob==NULL) return FALSE;
803 if(ob->totcol>=MAXMAT) return FALSE;
805 assign_material(ob, NULL, ob->totcol+1);
806 ob->actcol= ob->totcol;
810 static void do_init_render_material(Material *ma, int r_mode, float *amb)
813 int a, needuv=0, needtang=0;
815 if(ma->flarec==0) ma->flarec= 1;
817 /* add all texcoflags from mtex, texco and mapto were cleared in advance */
818 for(a=0; a<MAX_MTEX; a++) {
820 /* separate tex switching */
821 if(ma->septex & (1<<a)) continue;
824 if(mtex && mtex->tex && (mtex->tex->type | (mtex->tex->use_nodes && mtex->tex->nodetree) )) {
826 ma->texco |= mtex->texco;
827 ma->mapto |= mtex->mapto;
829 /* always get derivatives for these textures */
830 if ELEM3(mtex->tex->type, TEX_IMAGE, TEX_PLUGIN, TEX_ENVMAP) ma->texco |= TEXCO_OSA;
831 else if(mtex->texflag & (MTEX_COMPAT_BUMP|MTEX_3TAP_BUMP|MTEX_5TAP_BUMP)) ma->texco |= TEXCO_OSA;
833 if(ma->texco & (TEXCO_ORCO|TEXCO_REFL|TEXCO_NORM|TEXCO_STRAND|TEXCO_STRESS)) needuv= 1;
834 else if(ma->texco & (TEXCO_GLOB|TEXCO_UV|TEXCO_OBJECT|TEXCO_SPEED)) needuv= 1;
835 else if(ma->texco & (TEXCO_LAVECTOR|TEXCO_VIEW|TEXCO_STICKY)) needuv= 1;
837 if((ma->mapto & MAP_NORM) && (mtex->normapspace == MTEX_NSPACE_TANGENT))
842 if(needtang) ma->mode |= MA_NORMAP_TANG;
843 else ma->mode &= ~MA_NORMAP_TANG;
845 if(ma->mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)) {
847 if(r_mode & R_OSA) ma->texco |= TEXCO_OSA; /* for texfaces */
849 if(needuv) ma->texco |= NEED_UV;
851 /* since the raytracer doesnt recalc O structs for each ray, we have to preset them all */
852 if(r_mode & R_RAYTRACE) {
853 if((ma->mode & (MA_RAYMIRROR|MA_SHADOW_TRA)) || ((ma->mode & MA_TRANSP) && (ma->mode & MA_RAYTRANSP))) {
854 ma->texco |= NEED_UV|TEXCO_ORCO|TEXCO_REFL|TEXCO_NORM;
855 if(r_mode & R_OSA) ma->texco |= TEXCO_OSA;
860 ma->ambr= ma->amb*amb[0];
861 ma->ambg= ma->amb*amb[1];
862 ma->ambb= ma->amb*amb[2];
864 /* will become or-ed result of all node modes */
865 ma->mode_l= ma->mode;
866 ma->mode_l &= ~MA_SHLESS;
868 if(ma->strand_surfnor > 0.0f)
869 ma->mode_l |= MA_STR_SURFDIFF;
871 /* parses the geom+tex nodes */
872 if(ma->nodetree && ma->use_nodes)
873 ntreeShaderGetTexcoMode(ma->nodetree, r_mode, &ma->texco, &ma->mode_l);
876 static void init_render_nodetree(bNodeTree *ntree, Material *basemat, int r_mode, float *amb)
880 for(node=ntree->nodes.first; node; node= node->next) {
882 if(GS(node->id->name)==ID_MA) {
883 Material *ma= (Material *)node->id;
885 do_init_render_material(ma, r_mode, amb);
886 basemat->texco |= ma->texco;
887 basemat->mode_l |= ma->mode_l & ~(MA_TRANSP|MA_ZTRANSP|MA_RAYTRANSP);
890 else if(node->type==NODE_GROUP)
891 init_render_nodetree((bNodeTree *)node->id, basemat, r_mode, amb);
896 void init_render_material(Material *mat, int r_mode, float *amb)
899 do_init_render_material(mat, r_mode, amb);
901 if(mat->nodetree && mat->use_nodes) {
902 init_render_nodetree(mat->nodetree, mat, r_mode, amb);
904 ntreeBeginExecTree(mat->nodetree); /* has internal flag to detect it only does it once */
908 void init_render_materials(Main *bmain, int r_mode, float *amb)
912 /* clear these flags before going over materials, to make sure they
913 * are cleared only once, otherwise node materials contained in other
914 * node materials can go wrong */
915 for(ma= bmain->mat.first; ma; ma= ma->id.next) {
922 /* two steps, first initialize, then or the flags for layers */
923 for(ma= bmain->mat.first; ma; ma= ma->id.next) {
924 /* is_used flag comes back in convertblender.c */
925 ma->flag &= ~MA_IS_USED;
927 init_render_material(ma, r_mode, amb);
930 do_init_render_material(&defmaterial, r_mode, amb);
933 /* only needed for nodes now */
934 void end_render_material(Material *mat)
936 if(mat && mat->nodetree && mat->use_nodes)
937 ntreeEndExecTree(mat->nodetree); /* has internal flag to detect it only does it once */
940 void end_render_materials(Main *bmain)
943 for(ma= bmain->mat.first; ma; ma= ma->id.next)
945 end_render_material(ma);
948 static int material_in_nodetree(bNodeTree *ntree, Material *mat)
952 for(node=ntree->nodes.first; node; node= node->next) {
953 if(node->id && GS(node->id->name)==ID_MA) {
954 if(node->id==(ID*)mat)
957 else if(node->type==NODE_GROUP)
958 if(material_in_nodetree((bNodeTree*)node->id, mat))
965 int material_in_material(Material *parmat, Material *mat)
969 else if(parmat->nodetree && parmat->use_nodes)
970 return material_in_nodetree(parmat->nodetree, mat);
975 /* ****************** */
977 static char colname_array[125][20]= {
978 "Black","DarkRed","HalfRed","Red","Red",
979 "DarkGreen","DarkOlive","Brown","Chocolate","OrangeRed",
980 "HalfGreen","GreenOlive","DryOlive","Goldenrod","DarkOrange",
981 "LightGreen","Chartreuse","YellowGreen","Yellow","Gold",
982 "Green","LawnGreen","GreenYellow","LightOlive","Yellow",
983 "DarkBlue","DarkPurple","HotPink","VioletPink","RedPink",
984 "SlateGray","DarkGrey","PalePurple","IndianRed","Tomato",
985 "SeaGreen","PaleGreen","GreenKhaki","LightBrown","LightSalmon",
986 "SpringGreen","PaleGreen","MediumOlive","YellowBrown","LightGold",
987 "LightGreen","LightGreen","LightGreen","GreenYellow","PaleYellow",
988 "HalfBlue","DarkSky","HalfMagenta","VioletRed","DeepPink",
989 "SteelBlue","SkyBlue","Orchid","LightHotPink","HotPink",
990 "SeaGreen","SlateGray","MediumGrey","Burlywood","LightPink",
991 "SpringGreen","Aquamarine","PaleGreen","Khaki","PaleOrange",
992 "SpringGreen","SeaGreen","PaleGreen","PaleWhite","YellowWhite",
993 "LightBlue","Purple","MediumOrchid","Magenta","Magenta",
994 "RoyalBlue","SlateBlue","MediumOrchid","Orchid","Magenta",
995 "DeepSkyBlue","LightSteelBlue","LightSkyBlue","Violet","LightPink",
996 "Cyan","DarkTurquoise","SkyBlue","Grey","Snow",
997 "Mint","Mint","Aquamarine","MintCream","Ivory",
998 "Blue","Blue","DarkMagenta","DarkOrchid","Magenta",
999 "SkyBlue","RoyalBlue","LightSlateBlue","MediumOrchid","Magenta",
1000 "DodgerBlue","SteelBlue","MediumPurple","PalePurple","Plum",
1001 "DeepSkyBlue","PaleBlue","LightSkyBlue","PalePurple","Thistle",
1002 "Cyan","ColdBlue","PaleTurquoise","GhostWhite","White"
1005 void automatname(Material *ma)
1010 if(ma==NULL) return;
1011 if(ma->mode & MA_SHLESS) ref= 1.0;
1014 r= (int)(4.99f*(ref*ma->r));
1015 g= (int)(4.99f*(ref*ma->g));
1016 b= (int)(4.99f*(ref*ma->b));
1019 new_id(&G.main->mat, (ID *)ma, colname_array[nr]);
1024 int object_remove_material_slot(Object *ob)
1026 Material *mao, ***matarar;
1033 if(ob==NULL || ob->totcol==0) return FALSE;
1035 /* take a mesh/curve/mball as starting point, remove 1 index,
1036 * AND with all objects that share the ob->data
1038 * after that check indices in mesh/curve/mball!!!
1041 totcolp= give_totcolp(ob);
1042 matarar= give_matarar(ob);
1044 if(*matarar==NULL) return FALSE;
1046 /* we delete the actcol */
1048 mao= (*matarar)[ob->actcol-1];
1049 if(mao) mao->id.us--;
1052 for(a=ob->actcol; a<ob->totcol; a++)
1053 (*matarar)[a-1]= (*matarar)[a];
1057 MEM_freeN(*matarar);
1062 obt= G.main->object.first;
1065 if(obt->data==ob->data) {
1067 /* WATCH IT: do not use actcol from ob or from obt (can become zero) */
1068 mao= obt->mat[actcol-1];
1069 if(mao) mao->id.us--;
1071 for(a=actcol; a<obt->totcol; a++) {
1072 obt->mat[a-1]= obt->mat[a];
1073 obt->matbits[a-1]= obt->matbits[a];
1076 if(obt->actcol > obt->totcol) obt->actcol= obt->totcol;
1078 if(obt->totcol==0) {
1079 MEM_freeN(obt->mat);
1080 MEM_freeN(obt->matbits);
1088 /* check indices from mesh */
1090 if(ob->type==OB_MESH) {
1091 Mesh *me= get_mesh(ob);
1092 mesh_delete_material_index(me, actcol-1);
1093 freedisplist(&ob->disp);
1095 else if ELEM(ob->type, OB_CURVE, OB_SURF) {
1100 if(nu->mat_nr && nu->mat_nr>=actcol-1) {
1102 if (ob->type == OB_CURVE) nu->charidx--;
1106 freedisplist(&ob->disp);
1113 /* r g b = current value, col = new value, fac==0 is no change */
1114 /* if g==NULL, it only does r channel */
1115 void ramp_blend(int type, float *r, float *g, float *b, float fac, float *col)
1117 float tmp, facm= 1.0f-fac;
1121 *r = facm*(*r) + fac*col[0];
1123 *g = facm*(*g) + fac*col[1];
1124 *b = facm*(*b) + fac*col[2];
1135 *r *= (facm + fac*col[0]);
1137 *g *= (facm + fac*col[1]);
1138 *b *= (facm + fac*col[2]);
1141 case MA_RAMP_SCREEN:
1142 *r = 1.0f - (facm + fac*(1.0f - col[0])) * (1.0f - *r);
1144 *g = 1.0f - (facm + fac*(1.0f - col[1])) * (1.0f - *g);
1145 *b = 1.0f - (facm + fac*(1.0f - col[2])) * (1.0f - *b);
1148 case MA_RAMP_OVERLAY:
1150 *r *= (facm + 2.0f*fac*col[0]);
1152 *r = 1.0f - (facm + 2.0f*fac*(1.0f - col[0])) * (1.0f - *r);
1155 *g *= (facm + 2.0f*fac*col[1]);
1157 *g = 1.0f - (facm + 2.0f*fac*(1.0f - col[1])) * (1.0f - *g);
1159 *b *= (facm + 2.0f*fac*col[2]);
1161 *b = 1.0f - (facm + 2.0f*fac*(1.0f - col[2])) * (1.0f - *b);
1173 *r = facm*(*r) + fac*(*r)/col[0];
1176 *g = facm*(*g) + fac*(*g)/col[1];
1178 *b = facm*(*b) + fac*(*b)/col[2];
1182 *r = facm*(*r) + fac*fabsf(*r-col[0]);
1184 *g = facm*(*g) + fac*fabsf(*g-col[1]);
1185 *b = facm*(*b) + fac*fabsf(*b-col[2]);
1189 tmp=col[0]+((1-col[0])*facm);
1190 if(tmp < *r) *r= tmp;
1192 tmp=col[1]+((1-col[1])*facm);
1193 if(tmp < *g) *g= tmp;
1194 tmp=col[2]+((1-col[2])*facm);
1195 if(tmp < *b) *b= tmp;
1200 if(tmp > *r) *r= tmp;
1203 if(tmp > *g) *g= tmp;
1205 if(tmp > *b) *b= tmp;
1212 tmp = 1.0f - fac*col[0];
1215 else if ((tmp = (*r) / tmp)> 1.0f)
1222 tmp = 1.0f - fac*col[1];
1225 else if ((tmp = (*g) / tmp) > 1.0f )
1231 tmp = 1.0f - fac*col[2];
1234 else if ((tmp = (*b) / tmp) > 1.0f )
1244 tmp = facm + fac*col[0];
1248 else if (( tmp = (1.0f - (1.0f - (*r)) / tmp )) < 0.0f)
1250 else if (tmp > 1.0f)
1256 tmp = facm + fac*col[1];
1259 else if (( tmp = (1.0f - (1.0f - (*g)) / tmp )) < 0.0f )
1266 tmp = facm + fac*col[2];
1269 else if (( tmp = (1.0f - (1.0f - (*b)) / tmp )) < 0.0f )
1280 float colH,colS,colV;
1281 float tmpr,tmpg,tmpb;
1282 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1284 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1285 hsv_to_rgb( colH , rS, rV, &tmpr, &tmpg, &tmpb);
1286 *r = facm*(*r) + fac*tmpr;
1287 *g = facm*(*g) + fac*tmpg;
1288 *b = facm*(*b) + fac*tmpb;
1295 float colH,colS,colV;
1296 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1298 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1299 hsv_to_rgb( rH, (facm*rS +fac*colS), rV, r, g, b);
1306 float colH,colS,colV;
1307 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1308 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1309 hsv_to_rgb( rH, rS, (facm*rV +fac*colV), r, g, b);
1315 float colH,colS,colV;
1316 float tmpr,tmpg,tmpb;
1317 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1319 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1320 hsv_to_rgb( colH, colS, rV, &tmpr, &tmpg, &tmpb);
1321 *r = facm*(*r) + fac*tmpr;
1322 *g = facm*(*g) + fac*tmpg;
1323 *b = facm*(*b) + fac*tmpb;
1329 float scr, scg, scb;
1331 /* first calculate non-fac based Screen mix */
1332 scr = 1.0f - (1.0f - col[0]) * (1.0f - *r);
1333 scg = 1.0f - (1.0f - col[1]) * (1.0f - *g);
1334 scb = 1.0f - (1.0f - col[2]) * (1.0f - *b);
1336 *r = facm*(*r) + fac*(((1.0f - *r) * col[0] * (*r)) + (*r * scr));
1337 *g = facm*(*g) + fac*(((1.0f - *g) * col[1] * (*g)) + (*g * scg));
1338 *b = facm*(*b) + fac*(((1.0f - *b) * col[2] * (*b)) + (*b * scb));
1341 case MA_RAMP_LINEAR:
1343 *r = *r + fac*(2.0f*(col[0]-0.5f));
1345 *r = *r + fac*(2.0f*(col[0]) - 1.0f);
1348 *g = *g + fac*(2.0f*(col[1]-0.5f));
1350 *g = *g + fac*(2.0f*(col[1]) -1.0f);
1352 *b = *b + fac*(2.0f*(col[2]-0.5f));
1354 *b = *b + fac*(2.0f*(col[2]) - 1.0f);
1360 /* copy/paste buffer, if we had a propper py api that would be better */
1361 Material matcopybuf;
1362 static short matcopied= 0;
1364 void clear_matcopybuf(void)
1366 memset(&matcopybuf, 0, sizeof(Material));
1370 void free_matcopybuf(void)
1374 for(a=0; a<MAX_MTEX; a++) {
1375 if(matcopybuf.mtex[a]) {
1376 MEM_freeN(matcopybuf.mtex[a]);
1377 matcopybuf.mtex[a]= NULL;
1381 if(matcopybuf.ramp_col) MEM_freeN(matcopybuf.ramp_col);
1382 if(matcopybuf.ramp_spec) MEM_freeN(matcopybuf.ramp_spec);
1384 matcopybuf.ramp_col= NULL;
1385 matcopybuf.ramp_spec= NULL;
1387 if(matcopybuf.nodetree) {
1388 ntreeFreeTree(matcopybuf.nodetree);
1389 MEM_freeN(matcopybuf.nodetree);
1390 matcopybuf.nodetree= NULL;
1396 void copy_matcopybuf(Material *ma)
1404 memcpy(&matcopybuf, ma, sizeof(Material));
1405 if(matcopybuf.ramp_col) matcopybuf.ramp_col= MEM_dupallocN(matcopybuf.ramp_col);
1406 if(matcopybuf.ramp_spec) matcopybuf.ramp_spec= MEM_dupallocN(matcopybuf.ramp_spec);
1408 for(a=0; a<MAX_MTEX; a++) {
1409 mtex= matcopybuf.mtex[a];
1411 matcopybuf.mtex[a]= MEM_dupallocN(mtex);
1414 matcopybuf.nodetree= ntreeCopyTree(ma->nodetree);
1415 matcopybuf.preview= NULL;
1416 matcopybuf.gpumaterial.first= matcopybuf.gpumaterial.last= NULL;
1420 void paste_matcopybuf(Material *ma)
1428 /* free current mat */
1429 if(ma->ramp_col) MEM_freeN(ma->ramp_col);
1430 if(ma->ramp_spec) MEM_freeN(ma->ramp_spec);
1431 for(a=0; a<MAX_MTEX; a++) {
1433 if(mtex && mtex->tex) mtex->tex->id.us--;
1434 if(mtex) MEM_freeN(mtex);
1438 ntreeFreeTree(ma->nodetree);
1439 MEM_freeN(ma->nodetree);
1442 GPU_material_free(ma);
1445 memcpy(ma, &matcopybuf, sizeof(Material));
1448 if(matcopybuf.ramp_col) ma->ramp_col= MEM_dupallocN(matcopybuf.ramp_col);
1449 if(matcopybuf.ramp_spec) ma->ramp_spec= MEM_dupallocN(matcopybuf.ramp_spec);
1451 for(a=0; a<MAX_MTEX; a++) {
1454 ma->mtex[a]= MEM_dupallocN(mtex);
1455 if(mtex->tex) id_us_plus((ID *)mtex->tex);
1459 ma->nodetree= ntreeCopyTree(matcopybuf.nodetree);