Merge branch 'db/rev'
[plumiferos.git] / source / blender / blenkernel / intern / material.c
blobc2b2ed2a909f6a9d5750b2f532b9b3d69a2857c2
2 /* material.c
4 *
5 * $Id: material.c 12708 2007-11-28 18:43:09Z ton $
7 * ***** BEGIN GPL/BL DUAL 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. The Blender
13 * Foundation also sells licenses for use in proprietary software under
14 * the Blender License. See http://www.blender.org/BL/ for information
15 * about this.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
27 * All rights reserved.
29 * The Original Code is: all of this file.
31 * Contributor(s): none yet.
33 * ***** END GPL/BL DUAL LICENSE BLOCK *****
36 #include <string.h>
37 #include <math.h>
39 #include "MEM_guardedalloc.h"
41 #include "DNA_curve_types.h"
42 #include "DNA_material_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_meta_types.h"
45 #include "DNA_node_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_texture_types.h"
50 #include "BLI_blenlib.h"
51 #include "BLI_arithb.h"
53 #include "BKE_bad_level_calls.h"
54 #include "BKE_blender.h"
55 #include "BKE_displist.h"
56 #include "BKE_global.h"
57 #include "BKE_icons.h"
58 #include "BKE_library.h"
59 #include "BKE_main.h"
60 #include "BKE_material.h"
61 #include "BKE_mesh.h"
62 #include "BKE_node.h"
63 #include "BKE_utildefines.h"
65 #include "BPY_extern.h"
67 /* used in UI and render */
68 Material defmaterial;
70 /* called on startup, creator.c */
71 void init_def_material(void)
73 init_material(&defmaterial);
76 /* not material itself */
77 void free_material(Material *ma)
79 MTex *mtex;
80 int a;
82 BPY_free_scriptlink(&ma->scriptlink);
84 for(a=0; a<MAX_MTEX; a++) {
85 mtex= ma->mtex[a];
86 if(mtex && mtex->tex) mtex->tex->id.us--;
87 if(mtex) MEM_freeN(mtex);
90 if(ma->ramp_col) MEM_freeN(ma->ramp_col);
91 if(ma->ramp_spec) MEM_freeN(ma->ramp_spec);
93 BKE_icon_delete((struct ID*)ma);
94 ma->id.icon_id = 0;
96 /* is no lib link block, but material extension */
97 if(ma->nodetree) {
98 ntreeFreeTree(ma->nodetree);
99 MEM_freeN(ma->nodetree);
103 void init_material(Material *ma)
105 ma->r= ma->g= ma->b= ma->ref= 0.8;
106 ma->specr= ma->specg= ma->specb= 1.0;
107 ma->mirr= ma->mirg= ma->mirb= 1.0;
108 ma->spectra= 1.0;
109 ma->amb= 0.5;
110 ma->alpha= 1.0;
111 ma->spec= ma->hasize= 0.5;
112 ma->har= 50;
113 ma->starc= ma->ringc= 4;
114 ma->linec= 12;
115 ma->flarec= 1;
116 ma->flaresize= ma->subsize= 1.0;
117 ma->flareboost= 1;
118 ma->seed2= 6;
119 ma->friction= 0.5;
120 ma->refrac= 4.0;
121 ma->roughness= 0.5;
122 ma->param[0]= 0.5;
123 ma->param[1]= 0.1;
124 ma->param[2]= 0.5;
125 ma->param[3]= 0.1;
126 ma->rms= 0.1;
127 ma->darkness= 1.0;
129 ma->strand_sta= ma->strand_end= 1.0f;
131 ma->ang= 1.0;
132 ma->ray_depth= 2;
133 ma->ray_depth_tra= 2;
134 ma->fresnel_mir= 0.0;
135 ma->fresnel_tra= 0.0;
136 ma->fresnel_tra_i= 1.25;
137 ma->fresnel_mir_i= 1.25;
138 ma->tx_limit= 0.0;
139 ma->tx_falloff= 1.0;
140 ma->shad_alpha= 1.0f;
142 ma->gloss_mir = ma->gloss_tra= 1.0;
143 ma->samp_gloss_mir = ma->samp_gloss_tra= 18;
144 ma->adapt_thresh_mir = ma->adapt_thresh_tra = 0.005;
145 ma->dist_mir = 0.0;
146 ma->fadeto_mir = MA_RAYMIR_FADETOSKY;
148 ma->rampfac_col= 1.0;
149 ma->rampfac_spec= 1.0;
150 ma->pr_lamp= 3; /* two lamps, is bits */
151 ma->pr_type= MA_SPHERE;
153 ma->sss_radius[0]= 1.0f;
154 ma->sss_radius[1]= 1.0f;
155 ma->sss_radius[2]= 1.0f;
156 ma->sss_col[0]= 0.8f;
157 ma->sss_col[1]= 0.8f;
158 ma->sss_col[2]= 0.8f;
159 ma->sss_error= 0.05f;
160 ma->sss_scale= 0.1f;
161 ma->sss_ior= 1.3f;
162 ma->sss_colfac= 1.0f;
163 ma->sss_texfac= 0.0f;
164 ma->sss_front= 1.0f;
165 ma->sss_back= 1.0f;
167 ma->mode= MA_TRACEBLE|MA_SHADBUF|MA_SHADOW|MA_RADIO|MA_RAYBIAS|MA_TANGENT_STR;
170 Material *add_material(char *name)
172 Material *ma;
174 ma= alloc_libblock(&G.main->mat, ID_MA, name);
176 init_material(ma);
178 return ma;
181 Material *copy_material(Material *ma)
183 Material *man;
184 int a;
186 man= copy_libblock(ma);
188 id_us_plus((ID *)man->ipo);
189 id_us_plus((ID *)man->group);
192 for(a=0; a<MAX_MTEX; a++) {
193 if(ma->mtex[a]) {
194 man->mtex[a]= MEM_mallocN(sizeof(MTex), "copymaterial");
195 memcpy(man->mtex[a], ma->mtex[a], sizeof(MTex));
196 id_us_plus((ID *)man->mtex[a]->tex);
200 BPY_copy_scriptlink(&ma->scriptlink);
202 if(ma->ramp_col) man->ramp_col= MEM_dupallocN(ma->ramp_col);
203 if(ma->ramp_spec) man->ramp_spec= MEM_dupallocN(ma->ramp_spec);
205 if(ma->nodetree) {
206 man->nodetree= ntreeCopyTree(ma->nodetree, 0); /* 0 == full new tree */
209 return man;
212 void make_local_material(Material *ma)
214 Object *ob;
215 Mesh *me;
216 Curve *cu;
217 MetaBall *mb;
218 Material *man;
219 int a, local=0, lib=0;
221 /* - only lib users: do nothing
222 * - only local users: set flag
223 * - mixed: make copy
226 if(ma->id.lib==0) return;
227 if(ma->id.us==1) {
228 ma->id.lib= 0;
229 ma->id.flag= LIB_LOCAL;
230 new_id(0, (ID *)ma, 0);
231 for(a=0; a<MAX_MTEX; a++) {
232 if(ma->mtex[a]) id_lib_extern((ID *)ma->mtex[a]->tex);
235 return;
238 /* test objects */
239 ob= G.main->object.first;
240 while(ob) {
241 if(ob->mat) {
242 for(a=0; a<ob->totcol; a++) {
243 if(ob->mat[a]==ma) {
244 if(ob->id.lib) lib= 1;
245 else local= 1;
249 ob= ob->id.next;
251 /* test meshes */
252 me= G.main->mesh.first;
253 while(me) {
254 if(me->mat) {
255 for(a=0; a<me->totcol; a++) {
256 if(me->mat[a]==ma) {
257 if(me->id.lib) lib= 1;
258 else local= 1;
262 me= me->id.next;
264 /* test curves */
265 cu= G.main->curve.first;
266 while(cu) {
267 if(cu->mat) {
268 for(a=0; a<cu->totcol; a++) {
269 if(cu->mat[a]==ma) {
270 if(cu->id.lib) lib= 1;
271 else local= 1;
275 cu= cu->id.next;
277 /* test mballs */
278 mb= G.main->mball.first;
279 while(mb) {
280 if(mb->mat) {
281 for(a=0; a<mb->totcol; a++) {
282 if(mb->mat[a]==ma) {
283 if(mb->id.lib) lib= 1;
284 else local= 1;
288 mb= mb->id.next;
291 if(local && lib==0) {
292 ma->id.lib= 0;
293 ma->id.flag= LIB_LOCAL;
295 for(a=0; a<MAX_MTEX; a++) {
296 if(ma->mtex[a]) id_lib_extern((ID *)ma->mtex[a]->tex);
299 new_id(0, (ID *)ma, 0);
301 else if(local && lib) {
303 man= copy_material(ma);
304 man->id.us= 0;
306 /* do objects */
307 ob= G.main->object.first;
308 while(ob) {
309 if(ob->mat) {
310 for(a=0; a<ob->totcol; a++) {
311 if(ob->mat[a]==ma) {
312 if(ob->id.lib==0) {
313 ob->mat[a]= man;
314 man->id.us++;
315 ma->id.us--;
320 ob= ob->id.next;
322 /* do meshes */
323 me= G.main->mesh.first;
324 while(me) {
325 if(me->mat) {
326 for(a=0; a<me->totcol; a++) {
327 if(me->mat[a]==ma) {
328 if(me->id.lib==0) {
329 me->mat[a]= man;
330 man->id.us++;
331 ma->id.us--;
336 me= me->id.next;
338 /* do curves */
339 cu= G.main->curve.first;
340 while(cu) {
341 if(cu->mat) {
342 for(a=0; a<cu->totcol; a++) {
343 if(cu->mat[a]==ma) {
344 if(cu->id.lib==0) {
345 cu->mat[a]= man;
346 man->id.us++;
347 ma->id.us--;
352 cu= cu->id.next;
354 /* do mballs */
355 mb= G.main->mball.first;
356 while(mb) {
357 if(mb->mat) {
358 for(a=0; a<mb->totcol; a++) {
359 if(mb->mat[a]==ma) {
360 if(mb->id.lib==0) {
361 mb->mat[a]= man;
362 man->id.us++;
363 ma->id.us--;
368 mb= mb->id.next;
373 Material ***give_matarar(Object *ob)
375 Mesh *me;
376 Curve *cu;
377 MetaBall *mb;
379 if(ob->type==OB_MESH) {
380 me= ob->data;
381 return &(me->mat);
383 else if ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF) {
384 cu= ob->data;
385 return &(cu->mat);
387 else if(ob->type==OB_MBALL) {
388 mb= ob->data;
389 return &(mb->mat);
391 return NULL;
394 short *give_totcolp(Object *ob)
396 Mesh *me;
397 Curve *cu;
398 MetaBall *mb;
400 if(ob->type==OB_MESH) {
401 me= ob->data;
402 return &(me->totcol);
404 else if ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF) {
405 cu= ob->data;
406 return &(cu->totcol);
408 else if(ob->type==OB_MBALL) {
409 mb= ob->data;
410 return &(mb->totcol);
412 return NULL;
415 Material *give_current_material(Object *ob, int act)
417 Material ***matarar, *ma;
418 short *totcolp;
420 if(ob==NULL) return NULL;
422 /* if object cannot have material, totcolp==NULL */
423 totcolp= give_totcolp(ob);
424 if(totcolp==NULL || ob->totcol==0) return NULL;
426 if(act>ob->totcol) act= ob->totcol;
427 else if(act<=0) act= 1;
429 if( BTST(ob->colbits, act-1) ) { /* in object */
430 ma= ob->mat[act-1];
432 else { /* in data */
434 /* check for inconsistancy */
435 if(*totcolp < ob->totcol)
436 ob->totcol= *totcolp;
437 if(act>ob->totcol) act= ob->totcol;
439 matarar= give_matarar(ob);
441 if(matarar && *matarar) ma= (*matarar)[act-1];
442 else ma= 0;
446 return ma;
449 ID *material_from(Object *ob, int act)
452 if(ob==0) return 0;
454 if(ob->totcol==0) return ob->data;
455 if(act==0) act= 1;
457 if( BTST(ob->colbits, act-1) ) return (ID *)ob;
458 else return ob->data;
461 /* GS reads the memory pointed at in a specific ordering. There are,
462 * however two definitions for it. I have jotted them down here, both,
463 * but I think the first one is actually used. The thing is that
464 * big-endian systems might read this the wrong way round. OTOH, we
465 * constructed the IDs that are read out with this macro explicitly as
466 * well. I expect we'll sort it out soon... */
468 /* from blendef: */
469 #define GS(a) (*((short *)(a)))
471 /* from misc_util: flip the bytes from x */
472 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
474 void test_object_materials(ID *id)
476 /* make the ob mat-array same size as 'ob->data' mat-array */
477 Object *ob;
478 Mesh *me;
479 Curve *cu;
480 MetaBall *mb;
481 Material **newmatar;
482 int totcol=0;
484 if(id==0) return;
486 if( GS(id->name)==ID_ME ) {
487 me= (Mesh *)id;
488 totcol= me->totcol;
490 else if( GS(id->name)==ID_CU ) {
491 cu= (Curve *)id;
492 totcol= cu->totcol;
494 else if( GS(id->name)==ID_MB ) {
495 mb= (MetaBall *)id;
496 totcol= mb->totcol;
498 else return;
500 ob= G.main->object.first;
501 while(ob) {
503 if(ob->data==id) {
505 if(totcol==0) {
506 if(ob->totcol) {
507 MEM_freeN(ob->mat);
508 ob->mat= 0;
511 else if(ob->totcol<totcol) {
512 newmatar= MEM_callocN(sizeof(void *)*totcol, "newmatar");
513 if(ob->totcol) {
514 memcpy(newmatar, ob->mat, sizeof(void *)*ob->totcol);
515 MEM_freeN(ob->mat);
517 ob->mat= newmatar;
519 ob->totcol= totcol;
520 if(ob->totcol && ob->actcol==0) ob->actcol= 1;
521 if(ob->actcol>ob->totcol) ob->actcol= ob->totcol;
523 ob= ob->id.next;
528 void assign_material(Object *ob, Material *ma, int act)
530 Material *mao, **matar, ***matarar;
531 short *totcolp;
533 if(act>MAXMAT) return;
534 if(act<1) act= 1;
536 /* test arraylens */
538 totcolp= give_totcolp(ob);
539 matarar= give_matarar(ob);
541 if(totcolp==0 || matarar==0) return;
543 if( act > *totcolp) {
544 matar= MEM_callocN(sizeof(void *)*act, "matarray1");
545 if( *totcolp) {
546 memcpy(matar, *matarar, sizeof(void *)*( *totcolp ));
547 MEM_freeN(*matarar);
549 *matarar= matar;
550 *totcolp= act;
553 if(act > ob->totcol) {
554 matar= MEM_callocN(sizeof(void *)*act, "matarray2");
555 if( ob->totcol) {
556 memcpy(matar, ob->mat, sizeof(void *)*( ob->totcol ));
557 MEM_freeN(ob->mat);
559 ob->mat= matar;
560 ob->totcol= act;
563 /* do it */
565 if( BTST(ob->colbits, act-1) ) { /* in object */
566 mao= ob->mat[act-1];
567 if(mao) mao->id.us--;
568 ob->mat[act-1]= ma;
570 else { /* in data */
571 mao= (*matarar)[act-1];
572 if(mao) mao->id.us--;
573 (*matarar)[act-1]= ma;
575 id_us_plus((ID *)ma);
576 test_object_materials(ob->data);
579 int find_material_index(Object *ob, Material *ma)
581 Material ***matarar;
582 short a, *totcolp;
584 if(ma==NULL) return 0;
586 totcolp= give_totcolp(ob);
587 matarar= give_matarar(ob);
589 if(totcolp==NULL || matarar==NULL) return 0;
591 for(a=0; a<*totcolp; a++)
592 if((*matarar)[a]==ma)
593 break;
594 if(a<*totcolp)
595 return a+1;
596 return 0;
599 void new_material_to_objectdata(Object *ob)
601 Material *ma;
603 if(ob==0) return;
604 if(ob->totcol>=MAXMAT) return;
606 ma= give_current_material(ob, ob->actcol);
607 if(ma==NULL)
608 ma= add_material("Material");
609 else
610 ma= copy_material(ma);
612 ma->id.us= 0; /* eeh... */
614 if(ob->actcol) {
615 if( BTST(ob->colbits, ob->actcol-1) ) {
616 ob->colbits= BSET(ob->colbits, ob->totcol);
620 assign_material(ob, ma, ob->totcol+1);
621 ob->actcol= ob->totcol;
624 static void do_init_render_material(Material *ma, int r_mode, float *amb)
626 MTex *mtex;
627 int a, needuv=0;
629 if(ma->flarec==0) ma->flarec= 1;
631 /* add all texcoflags from mtex, texco and mapto were cleared in advance */
632 for(a=0; a<MAX_MTEX; a++) {
634 /* separate tex switching */
635 if(ma->septex & (1<<a)) continue;
637 mtex= ma->mtex[a];
638 if(mtex && mtex->tex && mtex->tex->type) {
640 ma->texco |= mtex->texco;
641 ma->mapto |= mtex->mapto;
642 if(r_mode & R_OSA) {
643 if ELEM3(mtex->tex->type, TEX_IMAGE, TEX_PLUGIN, TEX_ENVMAP) ma->texco |= TEXCO_OSA;
646 if(ma->texco & (TEXCO_ORCO|TEXCO_REFL|TEXCO_NORM|TEXCO_STRAND|TEXCO_STRESS)) needuv= 1;
647 else if(ma->texco & (TEXCO_GLOB|TEXCO_UV|TEXCO_OBJECT|TEXCO_SPEED)) needuv= 1;
648 else if(ma->texco & (TEXCO_LAVECTOR|TEXCO_VIEW|TEXCO_STICKY)) needuv= 1;
652 if(r_mode & R_RADIO)
653 if(ma->mode & MA_RADIO) needuv= 1;
655 if(ma->mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)) {
656 needuv= 1;
657 if(r_mode & R_OSA) ma->texco |= TEXCO_OSA; /* for texfaces */
659 if(needuv) ma->texco |= NEED_UV;
661 /* since the raytracer doesnt recalc O structs for each ray, we have to preset them all */
662 if(r_mode & R_RAYTRACE) {
663 if(ma->mode & (MA_RAYMIRROR|MA_RAYTRANSP|MA_SHADOW_TRA)) {
664 ma->texco |= NEED_UV|TEXCO_ORCO|TEXCO_REFL|TEXCO_NORM;
665 if(r_mode & R_OSA) ma->texco |= TEXCO_OSA;
669 if(amb) {
670 ma->ambr= ma->amb*amb[0];
671 ma->ambg= ma->amb*amb[1];
672 ma->ambb= ma->amb*amb[2];
674 /* will become or-ed result of all node modes */
675 ma->mode_l= ma->mode;
676 ma->mode_l &= ~MA_SHLESS;
679 static void init_render_nodetree(bNodeTree *ntree, Material *basemat, int r_mode, float *amb)
681 bNode *node;
683 for(node=ntree->nodes.first; node; node= node->next) {
684 if(node->id) {
685 if(GS(node->id->name)==ID_MA) {
686 Material *ma= (Material *)node->id;
687 if(ma!=basemat) {
688 do_init_render_material(ma, r_mode, amb);
689 basemat->texco |= ma->texco;
690 basemat->mode_l |= ma->mode_l;
693 else if(node->type==NODE_GROUP)
694 init_render_nodetree((bNodeTree *)node->id, basemat, r_mode, amb);
697 /* parses the geom+tex nodes */
698 ntreeShaderGetTexcoMode(ntree, r_mode, &basemat->texco, &basemat->mode_l);
701 void init_render_material(Material *mat, int r_mode, float *amb)
704 do_init_render_material(mat, r_mode, amb);
706 if(mat->nodetree && mat->use_nodes) {
707 init_render_nodetree(mat->nodetree, mat, r_mode, amb);
709 ntreeBeginExecTree(mat->nodetree); /* has internal flag to detect it only does it once */
713 void init_render_materials(int r_mode, float *amb)
715 Material *ma;
717 /* clear these flags before going over materials, to make sure they
718 * are cleared only once, otherwise node materials contained in other
719 * node materials can go wrong */
720 for(ma= G.main->mat.first; ma; ma= ma->id.next) {
721 if(ma->id.us) {
722 ma->texco= 0;
723 ma->mapto= 0;
727 /* two steps, first initialize, then or the flags for layers */
728 for(ma= G.main->mat.first; ma; ma= ma->id.next) {
729 /* is_used flag comes back in convertblender.c */
730 ma->flag &= ~MA_IS_USED;
731 if(ma->id.us)
732 init_render_material(ma, r_mode, amb);
735 do_init_render_material(&defmaterial, r_mode, amb);
738 /* only needed for nodes now */
739 void end_render_material(Material *mat)
741 if(mat && mat->nodetree && mat->use_nodes)
742 ntreeEndExecTree(mat->nodetree); /* has internal flag to detect it only does it once */
745 void end_render_materials(void)
747 Material *ma;
748 for(ma= G.main->mat.first; ma; ma= ma->id.next)
749 if(ma->id.us)
750 end_render_material(ma);
753 static int material_in_nodetree(bNodeTree *ntree, Material *mat)
755 bNode *node;
757 for(node=ntree->nodes.first; node; node= node->next) {
758 if(node->id && GS(node->id->name)==ID_MA) {
759 if(node->id==(ID*)mat)
760 return 1;
762 else if(node->type==NODE_GROUP)
763 if(material_in_nodetree((bNodeTree*)node->id, mat))
764 return 1;
767 return 0;
770 int material_in_material(Material *parmat, Material *mat)
772 if(parmat==mat)
773 return 1;
774 else if(parmat->nodetree && parmat->use_nodes)
775 return material_in_nodetree(parmat->nodetree, mat);
776 else
777 return 0;
780 /* ****************** */
782 char colname_array[125][20]= {
783 "Black","DarkRed","HalveRed","Red","Red",
784 "DarkGreen","DarkOlive","Brown","Chocolate","OrangeRed",
785 "HalveGreen","GreenOlive","DryOlive","Goldenrod","DarkOrange",
786 "LightGreen","Chartreuse","YellowGreen","Yellow","Gold",
787 "Green","LawnGreen","GreenYellow","LightOlive","Yellow",
788 "DarkBlue","DarkPurple","HotPink","VioletPink","RedPink",
789 "SlateGray","DarkGrey","PalePurple","IndianRed","Tomato",
790 "SeaGreen","PaleGreen","GreenKhaki","LightBrown","LightSalmon",
791 "SpringGreen","PaleGreen","MediumOlive","YellowBrown","LightGold",
792 "LightGreen","LightGreen","LightGreen","GreenYellow","PaleYellow",
793 "HalveBlue","DarkSky","HalveMagenta","VioletRed","DeepPink",
794 "SteelBlue","SkyBlue","Orchid","LightHotPink","HotPink",
795 "SeaGreen","SlateGray","MediumGrey","Burlywood","LightPink",
796 "SpringGreen","Aquamarine","PaleGreen","Khaki","PaleOrange",
797 "SpringGreen","SeaGreen","PaleGreen","PaleWhite","YellowWhite",
798 "LightBlue","Purple","MediumOrchid","Magenta","Magenta",
799 "RoyalBlue","SlateBlue","MediumOrchid","Orchid","Magenta",
800 "DeepSkyBlue","LightSteelBlue","LightSkyBlue","Violet","LightPink",
801 "Cyan","DarkTurquoise","SkyBlue","Grey","Snow",
802 "Mint","Mint","Aquamarine","MintCream","Ivory",
803 "Blue","Blue","DarkMagenta","DarkOrchid","Magenta",
804 "SkyBlue","RoyalBlue","LightSlateBlue","MediumOrchid","Magenta",
805 "DodgerBlue","SteelBlue","MediumPurple","PalePurple","Plum",
806 "DeepSkyBlue","PaleBlue","LightSkyBlue","PalePurple","Thistle",
807 "Cyan","ColdBlue","PaleTurquoise","GhostWhite","White"
810 void automatname(Material *ma)
812 int nr, r, g, b;
813 float ref;
815 if(ma==0) return;
816 if(ma->mode & MA_SHLESS) ref= 1.0;
817 else ref= ma->ref;
819 r= (int)(4.99*(ref*ma->r));
820 g= (int)(4.99*(ref*ma->g));
821 b= (int)(4.99*(ref*ma->b));
822 nr= r + 5*g + 25*b;
823 if(nr>124) nr= 124;
824 new_id(&G.main->mat, (ID *)ma, colname_array[nr]);
829 void delete_material_index()
831 Material *mao, ***matarar;
832 Object *ob, *obt;
833 Curve *cu;
834 Nurb *nu;
835 short *totcolp;
836 int a, actcol;
838 if(G.obedit) {
839 error("Unable to perform function in EditMode");
840 return;
842 ob= ((G.scene->basact)? (G.scene->basact->object) : 0) ;
843 if(ob==0 || ob->totcol==0) return;
845 /* take a mesh/curve/mball as starting point, remove 1 index,
846 * AND with all objects that share the ob->data
848 * after that check indices in mesh/curve/mball!!!
851 totcolp= give_totcolp(ob);
852 matarar= give_matarar(ob);
854 /* we delete the actcol */
855 if(ob->totcol) {
856 mao= (*matarar)[ob->actcol-1];
857 if(mao) mao->id.us--;
860 for(a=ob->actcol; a<ob->totcol; a++) {
861 (*matarar)[a-1]= (*matarar)[a];
863 (*totcolp)--;
865 if(*totcolp==0) {
866 MEM_freeN(*matarar);
867 *matarar= 0;
870 actcol= ob->actcol;
871 obt= G.main->object.first;
872 while(obt) {
874 if(obt->data==ob->data) {
876 /* WATCH IT: do not use actcol from ob or from obt (can become zero) */
877 mao= obt->mat[actcol-1];
878 if(mao) mao->id.us--;
880 for(a=actcol; a<obt->totcol; a++) obt->mat[a-1]= obt->mat[a];
881 obt->totcol--;
882 if(obt->actcol > obt->totcol) obt->actcol= obt->totcol;
884 if(obt->totcol==0) {
885 MEM_freeN(obt->mat);
886 obt->mat= 0;
889 obt= obt->id.next;
892 /* check indices from mesh */
894 if(ob->type==OB_MESH) {
895 Mesh *me= get_mesh(ob);
896 mesh_delete_material_index(me, actcol-1);
897 freedisplist(&ob->disp);
899 else if ELEM(ob->type, OB_CURVE, OB_SURF) {
900 cu= ob->data;
901 nu= cu->nurb.first;
903 while(nu) {
904 if(nu->mat_nr && nu->mat_nr>=actcol-1) {
905 nu->mat_nr--;
906 if (ob->type == OB_CURVE) nu->charidx--;
908 nu= nu->next;
910 freedisplist(&ob->disp);
915 /* r g b = current value, col = new value, fac==0 is no change */
916 /* if g==NULL, it only does r channel */
917 void ramp_blend(int type, float *r, float *g, float *b, float fac, float *col)
919 float tmp, facm= 1.0-fac;
921 switch (type) {
922 case MA_RAMP_BLEND:
923 *r = facm*(*r) + fac*col[0];
924 if(g) {
925 *g = facm*(*g) + fac*col[1];
926 *b = facm*(*b) + fac*col[2];
928 break;
929 case MA_RAMP_ADD:
930 *r += fac*col[0];
931 if(g) {
932 *g += fac*col[1];
933 *b += fac*col[2];
935 break;
936 case MA_RAMP_MULT:
937 *r *= (facm + fac*col[0]);
938 if(g) {
939 *g *= (facm + fac*col[1]);
940 *b *= (facm + fac*col[2]);
942 break;
943 case MA_RAMP_SCREEN:
944 *r = 1.0 - (facm + fac*(1.0 - col[0])) * (1.0 - *r);
945 if(g) {
946 *g = 1.0 - (facm + fac*(1.0 - col[1])) * (1.0 - *g);
947 *b = 1.0 - (facm + fac*(1.0 - col[2])) * (1.0 - *b);
949 break;
950 case MA_RAMP_OVERLAY:
951 if(*r < 0.5f)
952 *r *= (facm + 2.0f*fac*col[0]);
953 else
954 *r = 1.0 - (facm + 2.0f*fac*(1.0 - col[0])) * (1.0 - *r);
955 if(g) {
956 if(*g < 0.5f)
957 *g *= (facm + 2.0f*fac*col[1]);
958 else
959 *g = 1.0 - (facm + 2.0f*fac*(1.0 - col[1])) * (1.0 - *g);
960 if(*b < 0.5f)
961 *b *= (facm + 2.0f*fac*col[2]);
962 else
963 *b = 1.0 - (facm + 2.0f*fac*(1.0 - col[2])) * (1.0 - *b);
965 break;
966 case MA_RAMP_SUB:
967 *r -= fac*col[0];
968 if(g) {
969 *g -= fac*col[1];
970 *b -= fac*col[2];
972 break;
973 case MA_RAMP_DIV:
974 if(col[0]!=0.0)
975 *r = facm*(*r) + fac*(*r)/col[0];
976 if(g) {
977 if(col[1]!=0.0)
978 *g = facm*(*g) + fac*(*g)/col[1];
979 if(col[2]!=0.0)
980 *b = facm*(*b) + fac*(*b)/col[2];
982 break;
983 case MA_RAMP_DIFF:
984 *r = facm*(*r) + fac*fabs(*r-col[0]);
985 if(g) {
986 *g = facm*(*g) + fac*fabs(*g-col[1]);
987 *b = facm*(*b) + fac*fabs(*b-col[2]);
989 break;
990 case MA_RAMP_DARK:
991 tmp= fac*col[0];
992 if(tmp < *r) *r= tmp;
993 if(g) {
994 tmp= fac*col[1];
995 if(tmp < *g) *g= tmp;
996 tmp= fac*col[2];
997 if(tmp < *b) *b= tmp;
999 break;
1000 case MA_RAMP_LIGHT:
1001 tmp= fac*col[0];
1002 if(tmp > *r) *r= tmp;
1003 if(g) {
1004 tmp= fac*col[1];
1005 if(tmp > *g) *g= tmp;
1006 tmp= fac*col[2];
1007 if(tmp > *b) *b= tmp;
1009 break;
1010 case MA_RAMP_DODGE:
1013 if(*r !=0.0){
1014 tmp = 1.0 - fac*col[0];
1015 if(tmp <= 0.0)
1016 *r = 1.0;
1017 else if ((tmp = (*r) / tmp)> 1.0)
1018 *r = 1.0;
1019 else
1020 *r = tmp;
1022 if(g) {
1023 if(*g !=0.0){
1024 tmp = 1.0 - fac*col[1];
1025 if(tmp <= 0.0 )
1026 *g = 1.0;
1027 else if ((tmp = (*g) / tmp) > 1.0 )
1028 *g = 1.0;
1029 else
1030 *g = tmp;
1032 if(*b !=0.0){
1033 tmp = 1.0 - fac*col[2];
1034 if(tmp <= 0.0)
1035 *b = 1.0;
1036 else if ((tmp = (*b) / tmp) > 1.0 )
1037 *b = 1.0;
1038 else
1039 *b = tmp;
1043 break;
1044 case MA_RAMP_BURN:
1046 tmp = facm + fac*col[0];
1048 if(tmp <= 0.0)
1049 *r = 0.0;
1050 else if (( tmp = (1.0 - (1.0 - (*r)) / tmp )) < 0.0)
1051 *r = 0.0;
1052 else if (tmp > 1.0)
1053 *r=1.0;
1054 else
1055 *r = tmp;
1057 if(g) {
1058 tmp = facm + fac*col[1];
1059 if(tmp <= 0.0)
1060 *g = 0.0;
1061 else if (( tmp = (1.0 - (1.0 - (*g)) / tmp )) < 0.0 )
1062 *g = 0.0;
1063 else if(tmp >1.0)
1064 *g=1.0;
1065 else
1066 *g = tmp;
1068 tmp = facm + fac*col[2];
1069 if(tmp <= 0.0)
1070 *b = 0.0;
1071 else if (( tmp = (1.0 - (1.0 - (*b)) / tmp )) < 0.0 )
1072 *b = 0.0;
1073 else if(tmp >1.0)
1074 *b= 1.0;
1075 else
1076 *b = tmp;
1078 break;
1079 case MA_RAMP_HUE:
1080 if(g){
1081 float rH,rS,rV;
1082 float colH,colS,colV;
1083 float tmpr,tmpg,tmpb;
1084 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1085 if(colS!=0 ){
1086 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1087 hsv_to_rgb( colH , rS, rV, &tmpr, &tmpg, &tmpb);
1088 *r = facm*(*r) + fac*tmpr;
1089 *g = facm*(*g) + fac*tmpg;
1090 *b = facm*(*b) + fac*tmpb;
1093 break;
1094 case MA_RAMP_SAT:
1095 if(g){
1096 float rH,rS,rV;
1097 float colH,colS,colV;
1098 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1099 if(rS!=0){
1100 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1101 hsv_to_rgb( rH, (facm*rS +fac*colS), rV, r, g, b);
1104 break;
1105 case MA_RAMP_VAL:
1106 if(g){
1107 float rH,rS,rV;
1108 float colH,colS,colV;
1109 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1110 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1111 hsv_to_rgb( rH, rS, (facm*rV +fac*colV), r, g, b);
1113 break;
1114 case MA_RAMP_COLOR:
1115 if(g){
1116 float rH,rS,rV;
1117 float colH,colS,colV;
1118 float tmpr,tmpg,tmpb;
1119 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1120 if(colS!=0){
1121 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1122 hsv_to_rgb( colH, colS, rV, &tmpr, &tmpg, &tmpb);
1123 *r = facm*(*r) + fac*tmpr;
1124 *g = facm*(*g) + fac*tmpg;
1125 *b = facm*(*b) + fac*tmpb;
1128 break;