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 *****
36 #include "MEM_guardedalloc.h"
38 #include "DNA_curve_types.h"
39 #include "DNA_material_types.h"
40 #include "DNA_mesh_types.h"
41 #include "DNA_meta_types.h"
42 #include "DNA_node_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_scene_types.h"
48 #include "BKE_animsys.h"
49 #include "BKE_displist.h"
50 #include "BKE_global.h"
51 #include "BKE_icons.h"
52 #include "BKE_library.h"
54 #include "BKE_material.h"
57 #include "BKE_utildefines.h"
59 #ifndef DISABLE_PYTHON
60 #include "BPY_extern.h"
63 #include "GPU_material.h"
65 /* used in UI and render */
68 /* called on startup, creator.c */
69 void init_def_material(void)
71 init_material(&defmaterial);
74 /* not material itself */
75 void free_material(Material *ma)
80 for(a=0; a<MAX_MTEX; a++) {
82 if(mtex && mtex->tex) mtex->tex->id.us--;
83 if(mtex) MEM_freeN(mtex);
86 if(ma->ramp_col) MEM_freeN(ma->ramp_col);
87 if(ma->ramp_spec) MEM_freeN(ma->ramp_spec);
89 BKE_free_animdata((ID *)ma);
91 BKE_previewimg_free(&ma->preview);
92 BKE_icon_delete((struct ID*)ma);
95 /* is no lib link block, but material extension */
97 ntreeFreeTree(ma->nodetree);
98 MEM_freeN(ma->nodetree);
101 if(ma->gpumaterial.first)
102 GPU_material_free(ma);
105 void init_material(Material *ma)
107 ma->r= ma->g= ma->b= ma->ref= 0.8;
108 ma->specr= ma->specg= ma->specb= 1.0;
109 ma->mirr= ma->mirg= ma->mirb= 1.0;
113 ma->spec= ma->hasize= 0.5;
115 ma->starc= ma->ringc= 4;
118 ma->flaresize= ma->subsize= 1.0;
131 ma->strand_sta= ma->strand_end= 1.0f;
135 ma->ray_depth_tra= 2;
136 ma->fresnel_mir= 0.0;
137 ma->fresnel_tra= 0.0;
138 ma->fresnel_tra_i= 1.25;
139 ma->fresnel_mir_i= 1.25;
142 ma->shad_alpha= 1.0f;
145 ma->gloss_mir = ma->gloss_tra= 1.0;
146 ma->samp_gloss_mir = ma->samp_gloss_tra= 18;
147 ma->adapt_thresh_mir = ma->adapt_thresh_tra = 0.005;
149 ma->fadeto_mir = MA_RAYMIR_FADETOSKY;
151 ma->rampfac_col= 1.0;
152 ma->rampfac_spec= 1.0;
153 ma->pr_lamp= 3; /* two lamps, is bits */
154 ma->pr_type= MA_SPHERE;
156 ma->sss_radius[0]= 1.0f;
157 ma->sss_radius[1]= 1.0f;
158 ma->sss_radius[2]= 1.0f;
159 ma->sss_col[0]= 1.0f;
160 ma->sss_col[1]= 1.0f;
161 ma->sss_col[2]= 1.0f;
162 ma->sss_error= 0.05f;
165 ma->sss_colfac= 1.0f;
166 ma->sss_texfac= 0.0f;
170 ma->vol.density = 1.0f;
171 ma->vol.emission = 0.0f;
172 ma->vol.scattering = 1.0f;
173 ma->vol.reflection = 1.0f;
174 ma->vol.transmission_col[0] = ma->vol.transmission_col[1] = ma->vol.transmission_col[2] = 1.0f;
175 ma->vol.reflection_col[0] = ma->vol.reflection_col[1] = ma->vol.reflection_col[2] = 1.0f;
176 ma->vol.emission_col[0] = ma->vol.emission_col[1] = ma->vol.emission_col[2] = 1.0f;
177 ma->vol.density_scale = 1.0f;
178 ma->vol.depth_cutoff = 0.01f;
179 ma->vol.stepsize_type = MA_VOL_STEP_RANDOMIZED;
180 ma->vol.stepsize = 0.2f;
181 ma->vol.shade_type = MA_VOL_SHADE_SHADED;
182 ma->vol.shadeflag |= MA_VOL_PRECACHESHADING;
183 ma->vol.precache_resolution = 50;
184 ma->vol.ms_spread = 0.2f;
185 ma->vol.ms_diff = 1.f;
186 ma->vol.ms_intensity = 1.f;
188 ma->mode= MA_TRACEBLE|MA_SHADBUF|MA_SHADOW|MA_RAYBIAS|MA_TANGENT_STR|MA_ZTRANSP;
189 ma->shade_flag= MA_APPROX_OCCLUSION;
193 Material *add_material(char *name)
197 ma= alloc_libblock(&G.main->mat, ID_MA, name);
204 Material *copy_material(Material *ma)
209 man= copy_libblock(ma);
211 #if 0 // XXX old animation system
212 id_us_plus((ID *)man->ipo);
213 #endif // XXX old animation system
214 id_us_plus((ID *)man->group);
216 for(a=0; a<MAX_MTEX; a++) {
218 man->mtex[a]= MEM_mallocN(sizeof(MTex), "copymaterial");
219 memcpy(man->mtex[a], ma->mtex[a], sizeof(MTex));
220 id_us_plus((ID *)man->mtex[a]->tex);
224 if(ma->ramp_col) man->ramp_col= MEM_dupallocN(ma->ramp_col);
225 if(ma->ramp_spec) man->ramp_spec= MEM_dupallocN(ma->ramp_spec);
227 if (ma->preview) man->preview = BKE_previewimg_copy(ma->preview);
230 man->nodetree= ntreeCopyTree(ma->nodetree, 0); /* 0 == full new tree */
233 man->gpumaterial.first= man->gpumaterial.last= NULL;
238 void make_local_material(Material *ma)
245 int a, local=0, lib=0;
247 /* - only lib users: do nothing
248 * - only local users: set flag
252 if(ma->id.lib==0) return;
255 ma->id.flag= LIB_LOCAL;
256 new_id(0, (ID *)ma, 0);
257 for(a=0; a<MAX_MTEX; a++) {
258 if(ma->mtex[a]) id_lib_extern((ID *)ma->mtex[a]->tex);
265 ob= G.main->object.first;
268 for(a=0; a<ob->totcol; a++) {
270 if(ob->id.lib) lib= 1;
278 me= G.main->mesh.first;
281 for(a=0; a<me->totcol; a++) {
283 if(me->id.lib) lib= 1;
291 cu= G.main->curve.first;
294 for(a=0; a<cu->totcol; a++) {
296 if(cu->id.lib) lib= 1;
304 mb= G.main->mball.first;
307 for(a=0; a<mb->totcol; a++) {
309 if(mb->id.lib) lib= 1;
317 if(local && lib==0) {
319 ma->id.flag= LIB_LOCAL;
321 for(a=0; a<MAX_MTEX; a++) {
322 if(ma->mtex[a]) id_lib_extern((ID *)ma->mtex[a]->tex);
325 new_id(0, (ID *)ma, 0);
327 else if(local && lib) {
329 man= copy_material(ma);
333 ob= G.main->object.first;
336 for(a=0; a<ob->totcol; a++) {
349 me= G.main->mesh.first;
352 for(a=0; a<me->totcol; a++) {
365 cu= G.main->curve.first;
368 for(a=0; a<cu->totcol; a++) {
381 mb= G.main->mball.first;
384 for(a=0; a<mb->totcol; a++) {
399 Material ***give_matarar(Object *ob)
405 if(ob->type==OB_MESH) {
409 else if ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF) {
413 else if(ob->type==OB_MBALL) {
420 short *give_totcolp(Object *ob)
426 if(ob->type==OB_MESH) {
428 return &(me->totcol);
430 else if ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF) {
432 return &(cu->totcol);
434 else if(ob->type==OB_MBALL) {
436 return &(mb->totcol);
441 Material *give_current_material(Object *ob, int act)
443 Material ***matarar, *ma;
446 if(ob==NULL) return NULL;
448 /* if object cannot have material, totcolp==NULL */
449 totcolp= give_totcolp(ob);
450 if(totcolp==NULL || ob->totcol==0) return NULL;
452 if(act>ob->totcol) act= ob->totcol;
453 else if(act<=0) act= 1;
455 if(ob->matbits[act-1]) { /* in object */
460 /* check for inconsistancy */
461 if(*totcolp < ob->totcol)
462 ob->totcol= *totcolp;
463 if(act>ob->totcol) act= ob->totcol;
465 matarar= give_matarar(ob);
467 if(matarar && *matarar) ma= (*matarar)[act-1];
475 ID *material_from(Object *ob, int act)
480 if(ob->totcol==0) return ob->data;
483 if(ob->matbits[act-1]) return (ID *)ob;
484 else return ob->data;
487 Material *give_node_material(Material *ma)
489 if(ma && ma->use_nodes && ma->nodetree) {
490 bNode *node= nodeGetActiveID(ma->nodetree, ID_MA);
493 return (Material *)node->id;
499 /* GS reads the memory pointed at in a specific ordering. There are,
500 * however two definitions for it. I have jotted them down here, both,
501 * but I think the first one is actually used. The thing is that
502 * big-endian systems might read this the wrong way round. OTOH, we
503 * constructed the IDs that are read out with this macro explicitly as
504 * well. I expect we'll sort it out soon... */
507 #define GS(a) (*((short *)(a)))
509 /* from misc_util: flip the bytes from x */
510 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
512 void test_object_materials(ID *id)
514 /* make the ob mat-array same size as 'ob->data' mat-array */
525 if( GS(id->name)==ID_ME ) {
529 else if( GS(id->name)==ID_CU ) {
533 else if( GS(id->name)==ID_MB ) {
539 ob= G.main->object.first;
547 MEM_freeN(ob->matbits);
552 else if(ob->totcol<totcol) {
553 newmatar= MEM_callocN(sizeof(void *)*totcol, "newmatar");
554 newmatbits= MEM_callocN(sizeof(char)*totcol, "newmatbits");
556 memcpy(newmatar, ob->mat, sizeof(void *)*ob->totcol);
557 memcpy(newmatbits, ob->matbits, sizeof(char)*ob->totcol);
559 MEM_freeN(ob->matbits);
562 ob->matbits= newmatbits;
565 if(ob->totcol && ob->actcol==0) ob->actcol= 1;
566 if(ob->actcol>ob->totcol) ob->actcol= ob->totcol;
573 void assign_material(Object *ob, Material *ma, int act)
575 Material *mao, **matar, ***matarar;
579 if(act>MAXMAT) return;
584 totcolp= give_totcolp(ob);
585 matarar= give_matarar(ob);
587 if(totcolp==0 || matarar==0) return;
590 matar= MEM_callocN(sizeof(void *)*act, "matarray1");
593 memcpy(matar, *matarar, sizeof(void *)*(*totcolp));
601 if(act > ob->totcol) {
602 matar= MEM_callocN(sizeof(void *)*act, "matarray2");
603 matbits= MEM_callocN(sizeof(char)*act, "matbits1");
605 memcpy(matar, ob->mat, sizeof(void *)*( ob->totcol ));
606 memcpy(matbits, ob->matbits, sizeof(char)*(*totcolp));
608 MEM_freeN(ob->matbits);
611 ob->matbits= matbits;
614 /* copy object/mesh linking, or assign based on userpref */
616 ob->matbits[act-1]= ob->matbits[ob->actcol-1];
618 ob->matbits[act-1]= (U.flag & USER_MAT_ON_OB)? 1: 0;
623 if(ob->matbits[act-1]) { /* in object */
625 if(mao) mao->id.us--;
629 mao= (*matarar)[act-1];
630 if(mao) mao->id.us--;
631 (*matarar)[act-1]= ma;
635 id_us_plus((ID *)ma);
636 test_object_materials(ob->data);
639 /* XXX - this calls many more update calls per object then are needed, could be optimized */
640 void assign_matarar(struct Object *ob, struct Material ***matar, int totcol)
642 int i, actcol_orig= ob->actcol;
644 while(object_remove_material_slot(ob)) {};
646 /* now we have the right number of slots */
647 for(i=0; i<totcol; i++)
648 assign_material(ob, (*matar)[i], i+1);
650 if(actcol_orig > ob->totcol)
651 actcol_orig= ob->totcol;
653 ob->actcol= actcol_orig;
657 int find_material_index(Object *ob, Material *ma)
662 if(ma==NULL) return 0;
664 totcolp= give_totcolp(ob);
665 matarar= give_matarar(ob);
667 if(totcolp==NULL || matarar==NULL) return 0;
669 for(a=0; a<*totcolp; a++)
670 if((*matarar)[a]==ma)
677 int object_add_material_slot(Object *ob)
681 if(ob==0) return FALSE;
682 if(ob->totcol>=MAXMAT) return FALSE;
684 ma= give_current_material(ob, ob->actcol);
686 assign_material(ob, ma, ob->totcol+1);
687 ob->actcol= ob->totcol;
691 static void do_init_render_material(Material *ma, int r_mode, float *amb)
694 int a, needuv=0, needtang=0;
696 if(ma->flarec==0) ma->flarec= 1;
698 /* add all texcoflags from mtex, texco and mapto were cleared in advance */
699 for(a=0; a<MAX_MTEX; a++) {
701 /* separate tex switching */
702 if(ma->septex & (1<<a)) continue;
705 if(mtex && mtex->tex && (mtex->tex->type | (mtex->tex->use_nodes && mtex->tex->nodetree) )) {
707 ma->texco |= mtex->texco;
708 ma->mapto |= mtex->mapto;
710 if ELEM3(mtex->tex->type, TEX_IMAGE, TEX_PLUGIN, TEX_ENVMAP) ma->texco |= TEXCO_OSA;
711 else if(mtex->texflag & MTEX_NEW_BUMP) ma->texco |= TEXCO_OSA; // NEWBUMP: need texture derivatives for procedurals as well
714 if(ma->texco & (TEXCO_ORCO|TEXCO_REFL|TEXCO_NORM|TEXCO_STRAND|TEXCO_STRESS)) needuv= 1;
715 else if(ma->texco & (TEXCO_GLOB|TEXCO_UV|TEXCO_OBJECT|TEXCO_SPEED)) needuv= 1;
716 else if(ma->texco & (TEXCO_LAVECTOR|TEXCO_VIEW|TEXCO_STICKY)) needuv= 1;
718 if((ma->mapto & MAP_NORM) && (mtex->normapspace == MTEX_NSPACE_TANGENT))
723 if(needtang) ma->mode |= MA_NORMAP_TANG;
724 else ma->mode &= ~MA_NORMAP_TANG;
726 if(ma->mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)) {
728 if(r_mode & R_OSA) ma->texco |= TEXCO_OSA; /* for texfaces */
730 if(needuv) ma->texco |= NEED_UV;
732 /* since the raytracer doesnt recalc O structs for each ray, we have to preset them all */
733 if(r_mode & R_RAYTRACE) {
734 if((ma->mode & (MA_RAYMIRROR|MA_SHADOW_TRA)) || ((ma->mode && MA_TRANSP) && (ma->mode & MA_RAYTRANSP))) {
735 ma->texco |= NEED_UV|TEXCO_ORCO|TEXCO_REFL|TEXCO_NORM;
736 if(r_mode & R_OSA) ma->texco |= TEXCO_OSA;
741 ma->ambr= ma->amb*amb[0];
742 ma->ambg= ma->amb*amb[1];
743 ma->ambb= ma->amb*amb[2];
745 /* will become or-ed result of all node modes */
746 ma->mode_l= ma->mode;
747 ma->mode_l &= ~MA_SHLESS;
749 if(ma->strand_surfnor > 0.0f)
750 ma->mode_l |= MA_STR_SURFDIFF;
753 static void init_render_nodetree(bNodeTree *ntree, Material *basemat, int r_mode, float *amb)
757 for(node=ntree->nodes.first; node; node= node->next) {
759 if(GS(node->id->name)==ID_MA) {
760 Material *ma= (Material *)node->id;
762 do_init_render_material(ma, r_mode, amb);
763 basemat->texco |= ma->texco;
764 basemat->mode_l |= ma->mode_l;
767 else if(node->type==NODE_GROUP)
768 init_render_nodetree((bNodeTree *)node->id, basemat, r_mode, amb);
771 /* parses the geom+tex nodes */
772 ntreeShaderGetTexcoMode(ntree, r_mode, &basemat->texco, &basemat->mode_l);
775 void init_render_material(Material *mat, int r_mode, float *amb)
778 do_init_render_material(mat, r_mode, amb);
780 if(mat->nodetree && mat->use_nodes) {
781 init_render_nodetree(mat->nodetree, mat, r_mode, amb);
783 ntreeBeginExecTree(mat->nodetree); /* has internal flag to detect it only does it once */
787 void init_render_materials(int r_mode, float *amb)
791 /* clear these flags before going over materials, to make sure they
792 * are cleared only once, otherwise node materials contained in other
793 * node materials can go wrong */
794 for(ma= G.main->mat.first; ma; ma= ma->id.next) {
801 /* two steps, first initialize, then or the flags for layers */
802 for(ma= G.main->mat.first; ma; ma= ma->id.next) {
803 /* is_used flag comes back in convertblender.c */
804 ma->flag &= ~MA_IS_USED;
806 init_render_material(ma, r_mode, amb);
809 do_init_render_material(&defmaterial, r_mode, amb);
812 /* only needed for nodes now */
813 void end_render_material(Material *mat)
815 if(mat && mat->nodetree && mat->use_nodes)
816 ntreeEndExecTree(mat->nodetree); /* has internal flag to detect it only does it once */
819 void end_render_materials(void)
822 for(ma= G.main->mat.first; ma; ma= ma->id.next)
824 end_render_material(ma);
827 static int material_in_nodetree(bNodeTree *ntree, Material *mat)
831 for(node=ntree->nodes.first; node; node= node->next) {
832 if(node->id && GS(node->id->name)==ID_MA) {
833 if(node->id==(ID*)mat)
836 else if(node->type==NODE_GROUP)
837 if(material_in_nodetree((bNodeTree*)node->id, mat))
844 int material_in_material(Material *parmat, Material *mat)
848 else if(parmat->nodetree && parmat->use_nodes)
849 return material_in_nodetree(parmat->nodetree, mat);
854 /* ****************** */
856 char colname_array[125][20]= {
857 "Black","DarkRed","HalfRed","Red","Red",
858 "DarkGreen","DarkOlive","Brown","Chocolate","OrangeRed",
859 "HalfGreen","GreenOlive","DryOlive","Goldenrod","DarkOrange",
860 "LightGreen","Chartreuse","YellowGreen","Yellow","Gold",
861 "Green","LawnGreen","GreenYellow","LightOlive","Yellow",
862 "DarkBlue","DarkPurple","HotPink","VioletPink","RedPink",
863 "SlateGray","DarkGrey","PalePurple","IndianRed","Tomato",
864 "SeaGreen","PaleGreen","GreenKhaki","LightBrown","LightSalmon",
865 "SpringGreen","PaleGreen","MediumOlive","YellowBrown","LightGold",
866 "LightGreen","LightGreen","LightGreen","GreenYellow","PaleYellow",
867 "HalfBlue","DarkSky","HalfMagenta","VioletRed","DeepPink",
868 "SteelBlue","SkyBlue","Orchid","LightHotPink","HotPink",
869 "SeaGreen","SlateGray","MediumGrey","Burlywood","LightPink",
870 "SpringGreen","Aquamarine","PaleGreen","Khaki","PaleOrange",
871 "SpringGreen","SeaGreen","PaleGreen","PaleWhite","YellowWhite",
872 "LightBlue","Purple","MediumOrchid","Magenta","Magenta",
873 "RoyalBlue","SlateBlue","MediumOrchid","Orchid","Magenta",
874 "DeepSkyBlue","LightSteelBlue","LightSkyBlue","Violet","LightPink",
875 "Cyan","DarkTurquoise","SkyBlue","Grey","Snow",
876 "Mint","Mint","Aquamarine","MintCream","Ivory",
877 "Blue","Blue","DarkMagenta","DarkOrchid","Magenta",
878 "SkyBlue","RoyalBlue","LightSlateBlue","MediumOrchid","Magenta",
879 "DodgerBlue","SteelBlue","MediumPurple","PalePurple","Plum",
880 "DeepSkyBlue","PaleBlue","LightSkyBlue","PalePurple","Thistle",
881 "Cyan","ColdBlue","PaleTurquoise","GhostWhite","White"
884 void automatname(Material *ma)
890 if(ma->mode & MA_SHLESS) ref= 1.0;
893 r= (int)(4.99*(ref*ma->r));
894 g= (int)(4.99*(ref*ma->g));
895 b= (int)(4.99*(ref*ma->b));
898 new_id(&G.main->mat, (ID *)ma, colname_array[nr]);
903 int object_remove_material_slot(Object *ob)
905 Material *mao, ***matarar;
912 if(ob==NULL || ob->totcol==0) return FALSE;
914 /* take a mesh/curve/mball as starting point, remove 1 index,
915 * AND with all objects that share the ob->data
917 * after that check indices in mesh/curve/mball!!!
920 totcolp= give_totcolp(ob);
921 matarar= give_matarar(ob);
923 if(*matarar==NULL) return FALSE;
925 /* we delete the actcol */
927 mao= (*matarar)[ob->actcol-1];
928 if(mao) mao->id.us--;
931 for(a=ob->actcol; a<ob->totcol; a++)
932 (*matarar)[a-1]= (*matarar)[a];
941 obt= G.main->object.first;
944 if(obt->data==ob->data) {
946 /* WATCH IT: do not use actcol from ob or from obt (can become zero) */
947 mao= obt->mat[actcol-1];
948 if(mao) mao->id.us--;
950 for(a=actcol; a<obt->totcol; a++) {
951 obt->mat[a-1]= obt->mat[a];
952 obt->matbits[a-1]= obt->matbits[a];
955 if(obt->actcol > obt->totcol) obt->actcol= obt->totcol;
959 MEM_freeN(obt->matbits);
967 /* check indices from mesh */
969 if(ob->type==OB_MESH) {
970 Mesh *me= get_mesh(ob);
971 mesh_delete_material_index(me, actcol-1);
972 freedisplist(&ob->disp);
974 else if ELEM(ob->type, OB_CURVE, OB_SURF) {
979 if(nu->mat_nr && nu->mat_nr>=actcol-1) {
981 if (ob->type == OB_CURVE) nu->charidx--;
985 freedisplist(&ob->disp);
992 /* r g b = current value, col = new value, fac==0 is no change */
993 /* if g==NULL, it only does r channel */
994 void ramp_blend(int type, float *r, float *g, float *b, float fac, float *col)
996 float tmp, facm= 1.0f-fac;
1000 *r = facm*(*r) + fac*col[0];
1002 *g = facm*(*g) + fac*col[1];
1003 *b = facm*(*b) + fac*col[2];
1014 *r *= (facm + fac*col[0]);
1016 *g *= (facm + fac*col[1]);
1017 *b *= (facm + fac*col[2]);
1020 case MA_RAMP_SCREEN:
1021 *r = 1.0f - (facm + fac*(1.0f - col[0])) * (1.0f - *r);
1023 *g = 1.0f - (facm + fac*(1.0f - col[1])) * (1.0f - *g);
1024 *b = 1.0f - (facm + fac*(1.0f - col[2])) * (1.0f - *b);
1027 case MA_RAMP_OVERLAY:
1029 *r *= (facm + 2.0f*fac*col[0]);
1031 *r = 1.0f - (facm + 2.0f*fac*(1.0f - col[0])) * (1.0f - *r);
1034 *g *= (facm + 2.0f*fac*col[1]);
1036 *g = 1.0f - (facm + 2.0f*fac*(1.0f - col[1])) * (1.0f - *g);
1038 *b *= (facm + 2.0f*fac*col[2]);
1040 *b = 1.0f - (facm + 2.0f*fac*(1.0f - col[2])) * (1.0f - *b);
1052 *r = facm*(*r) + fac*(*r)/col[0];
1055 *g = facm*(*g) + fac*(*g)/col[1];
1057 *b = facm*(*b) + fac*(*b)/col[2];
1061 *r = facm*(*r) + fac*fabs(*r-col[0]);
1063 *g = facm*(*g) + fac*fabs(*g-col[1]);
1064 *b = facm*(*b) + fac*fabs(*b-col[2]);
1068 tmp=col[0]+((1-col[0])*facm);
1069 if(tmp < *r) *r= tmp;
1071 tmp=col[1]+((1-col[1])*facm);
1072 if(tmp < *g) *g= tmp;
1073 tmp=col[2]+((1-col[2])*facm);
1074 if(tmp < *b) *b= tmp;
1079 if(tmp > *r) *r= tmp;
1082 if(tmp > *g) *g= tmp;
1084 if(tmp > *b) *b= tmp;
1091 tmp = 1.0f - fac*col[0];
1094 else if ((tmp = (*r) / tmp)> 1.0f)
1101 tmp = 1.0f - fac*col[1];
1104 else if ((tmp = (*g) / tmp) > 1.0f )
1110 tmp = 1.0f - fac*col[2];
1113 else if ((tmp = (*b) / tmp) > 1.0f )
1123 tmp = facm + fac*col[0];
1127 else if (( tmp = (1.0f - (1.0f - (*r)) / tmp )) < 0.0f)
1129 else if (tmp > 1.0f)
1135 tmp = facm + fac*col[1];
1138 else if (( tmp = (1.0f - (1.0f - (*g)) / tmp )) < 0.0f )
1145 tmp = facm + fac*col[2];
1148 else if (( tmp = (1.0f - (1.0f - (*b)) / tmp )) < 0.0f )
1159 float colH,colS,colV;
1160 float tmpr,tmpg,tmpb;
1161 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1163 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1164 hsv_to_rgb( colH , rS, rV, &tmpr, &tmpg, &tmpb);
1165 *r = facm*(*r) + fac*tmpr;
1166 *g = facm*(*g) + fac*tmpg;
1167 *b = facm*(*b) + fac*tmpb;
1174 float colH,colS,colV;
1175 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1177 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1178 hsv_to_rgb( rH, (facm*rS +fac*colS), rV, r, g, b);
1185 float colH,colS,colV;
1186 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1187 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1188 hsv_to_rgb( rH, rS, (facm*rV +fac*colV), r, g, b);
1194 float colH,colS,colV;
1195 float tmpr,tmpg,tmpb;
1196 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1198 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1199 hsv_to_rgb( colH, colS, rV, &tmpr, &tmpg, &tmpb);
1200 *r = facm*(*r) + fac*tmpr;
1201 *g = facm*(*g) + fac*tmpg;
1202 *b = facm*(*b) + fac*tmpb;
1208 float scr, scg, scb;
1210 /* first calculate non-fac based Screen mix */
1211 scr = 1.0f - (1.0f - col[0]) * (1.0f - *r);
1212 scg = 1.0f - (1.0f - col[1]) * (1.0f - *g);
1213 scb = 1.0f - (1.0f - col[2]) * (1.0f - *b);
1215 *r = facm*(*r) + fac*(((1.0f - *r) * col[0] * (*r)) + (*r * scr));
1216 *g = facm*(*g) + fac*(((1.0f - *g) * col[1] * (*g)) + (*g * scg));
1217 *b = facm*(*b) + fac*(((1.0f - *b) * col[2] * (*b)) + (*b * scb));
1220 case MA_RAMP_LINEAR:
1222 *r = *r + fac*(2.0f*(col[0]-0.5f));
1224 *r = *r + fac*(2.0f*(col[0]) - 1.0f);
1227 *g = *g + fac*(2.0f*(col[1]-0.5f));
1229 *g = *g + fac*(2.0f*(col[1]) -1.0f);
1231 *b = *b + fac*(2.0f*(col[2]-0.5f));
1233 *b = *b + fac*(2.0f*(col[2]) - 1.0f);
1239 /* copy/paste buffer, if we had a propper py api that would be better */
1240 Material matcopybuf;
1241 // MTex mtexcopybuf;
1242 static short matcopied=0;
1244 void clear_matcopybuf(void)
1246 memset(&matcopybuf, 0, sizeof(Material));
1250 void free_matcopybuf(void)
1252 // extern MTex mtexcopybuf; /* buttons.c */
1255 for(a=0; a<MAX_MTEX; a++) {
1256 if(matcopybuf.mtex[a]) {
1257 MEM_freeN(matcopybuf.mtex[a]);
1258 matcopybuf.mtex[a]= NULL;
1262 if(matcopybuf.ramp_col) MEM_freeN(matcopybuf.ramp_col);
1263 if(matcopybuf.ramp_spec) MEM_freeN(matcopybuf.ramp_spec);
1265 matcopybuf.ramp_col= NULL;
1266 matcopybuf.ramp_spec= NULL;
1268 if(matcopybuf.nodetree) {
1269 ntreeFreeTree(matcopybuf.nodetree);
1270 MEM_freeN(matcopybuf.nodetree);
1271 matcopybuf.nodetree= NULL;
1273 // default_mtex(&mtexcopybuf);
1278 void copy_matcopybuf(Material *ma)
1286 memcpy(&matcopybuf, ma, sizeof(Material));
1287 if(matcopybuf.ramp_col) matcopybuf.ramp_col= MEM_dupallocN(matcopybuf.ramp_col);
1288 if(matcopybuf.ramp_spec) matcopybuf.ramp_spec= MEM_dupallocN(matcopybuf.ramp_spec);
1290 for(a=0; a<MAX_MTEX; a++) {
1291 mtex= matcopybuf.mtex[a];
1293 matcopybuf.mtex[a]= MEM_dupallocN(mtex);
1296 matcopybuf.nodetree= ntreeCopyTree(ma->nodetree, 0);
1297 matcopybuf.preview= NULL;
1298 matcopybuf.gpumaterial.first= matcopybuf.gpumaterial.last= NULL;
1302 void paste_matcopybuf(Material *ma)
1310 /* free current mat */
1311 if(ma->ramp_col) MEM_freeN(ma->ramp_col);
1312 if(ma->ramp_spec) MEM_freeN(ma->ramp_spec);
1313 for(a=0; a<MAX_MTEX; a++) {
1315 if(mtex && mtex->tex) mtex->tex->id.us--;
1316 if(mtex) MEM_freeN(mtex);
1320 ntreeFreeTree(ma->nodetree);
1321 MEM_freeN(ma->nodetree);
1324 GPU_materials_free(ma);
1327 memcpy(ma, &matcopybuf, sizeof(Material));
1330 if(matcopybuf.ramp_col) ma->ramp_col= MEM_dupallocN(matcopybuf.ramp_col);
1331 if(matcopybuf.ramp_spec) ma->ramp_spec= MEM_dupallocN(matcopybuf.ramp_spec);
1333 for(a=0; a<MAX_MTEX; a++) {
1336 ma->mtex[a]= MEM_dupallocN(mtex);
1337 if(mtex->tex) id_us_plus((ID *)mtex->tex);
1341 ma->nodetree= ntreeCopyTree(matcopybuf.nodetree, 0);
1344 BIF_preview_changed(ID_MA);
1345 BIF_undo_push("Paste material settings");
1346 scrarea_queue_winredraw(curarea);