Subsurface scattering:
[blender.git] / source / blender / blenkernel / intern / material.c
1
2 /*  material.c
3  *
4  * 
5  * $Id$
6  *
7  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
8  *
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.
16  *
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.
21  *
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.
25  *
26  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
27  * All rights reserved.
28  *
29  * The Original Code is: all of this file.
30  *
31  * Contributor(s): none yet.
32  *
33  * ***** END GPL/BL DUAL LICENSE BLOCK *****
34  */
35
36 #include <string.h>
37 #include <math.h>
38
39 #include "MEM_guardedalloc.h"
40
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"
49
50 #include "BLI_blenlib.h"
51 #include "BLI_arithb.h"         
52
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"
64
65 #include "BPY_extern.h"
66
67 /* used in UI and render */
68 Material defmaterial;
69
70 /* called on startup, creator.c */
71 void init_def_material(void)
72 {
73         init_material(&defmaterial);
74 }
75
76 /* not material itself */
77 void free_material(Material *ma)
78 {
79         MTex *mtex;
80         int a;
81
82         BPY_free_scriptlink(&ma->scriptlink);
83         
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);
88         }
89         
90         if(ma->ramp_col) MEM_freeN(ma->ramp_col);
91         if(ma->ramp_spec) MEM_freeN(ma->ramp_spec);
92         
93         BKE_icon_delete((struct ID*)ma);
94         ma->id.icon_id = 0;
95         
96         /* is no lib link block, but material extension */
97         if(ma->nodetree) {
98                 ntreeFreeTree(ma->nodetree);
99                 MEM_freeN(ma->nodetree);
100         }
101 }
102
103 void init_material(Material *ma)
104 {
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;      
128         
129         ma->strand_sta= ma->strand_end= 1.0f;
130         
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;
141         
142         ma->rampfac_col= 1.0;
143         ma->rampfac_spec= 1.0;
144         ma->pr_lamp= 3;                 /* two lamps, is bits */
145         ma->pr_type= MA_SPHERE;
146
147         ma->sss_radius[0]= 1.0f;
148         ma->sss_radius[1]= 1.0f;
149         ma->sss_radius[2]= 1.0f;
150         ma->sss_col[0]= 0.8f;
151         ma->sss_col[1]= 0.8f;
152         ma->sss_col[2]= 0.8f;
153         ma->sss_error= 0.05f;
154         ma->sss_scale= 0.1f;
155         ma->sss_ior= 1.3f;
156         ma->sss_colfac= 1.0f;
157         ma->sss_texfac= 0.0f;
158         ma->sss_front= 1.0f;
159         ma->sss_back= 1.0f;
160
161         ma->mode= MA_TRACEBLE|MA_SHADBUF|MA_SHADOW|MA_RADIO|MA_RAYBIAS|MA_TANGENT_STR;
162 }
163
164 Material *add_material(char *name)
165 {
166         Material *ma;
167
168         ma= alloc_libblock(&G.main->mat, ID_MA, name);
169         
170         init_material(ma);
171         
172         return ma;      
173 }
174
175 Material *copy_material(Material *ma)
176 {
177         Material *man;
178         int a;
179         
180         man= copy_libblock(ma);
181         
182         id_us_plus((ID *)man->ipo);
183         id_us_plus((ID *)man->group);
184         
185         
186         for(a=0; a<MAX_MTEX; a++) {
187                 if(ma->mtex[a]) {
188                         man->mtex[a]= MEM_mallocN(sizeof(MTex), "copymaterial");
189                         memcpy(man->mtex[a], ma->mtex[a], sizeof(MTex));
190                         id_us_plus((ID *)man->mtex[a]->tex);
191                 }
192         }
193         
194         BPY_copy_scriptlink(&ma->scriptlink);
195         
196         if(ma->ramp_col) man->ramp_col= MEM_dupallocN(ma->ramp_col);
197         if(ma->ramp_spec) man->ramp_spec= MEM_dupallocN(ma->ramp_spec);
198         
199         if(ma->nodetree) {
200                 man->nodetree= ntreeCopyTree(ma->nodetree, 0);  /* 0 == full new tree */
201         }
202         
203         return man;
204 }
205
206 void make_local_material(Material *ma)
207 {
208         Object *ob;
209         Mesh *me;
210         Curve *cu;
211         MetaBall *mb;
212         Material *man;
213         int a, local=0, lib=0;
214
215         /* - only lib users: do nothing
216             * - only local users: set flag
217             * - mixed: make copy
218             */
219         
220         if(ma->id.lib==0) return;
221         if(ma->id.us==1) {
222                 ma->id.lib= 0;
223                 ma->id.flag= LIB_LOCAL;
224                 new_id(0, (ID *)ma, 0);
225                 for(a=0; a<MAX_MTEX; a++) {
226                         if(ma->mtex[a]) id_lib_extern((ID *)ma->mtex[a]->tex);
227                 }
228                 
229                 return;
230         }
231         
232         /* test objects */
233         ob= G.main->object.first;
234         while(ob) {
235                 if(ob->mat) {
236                         for(a=0; a<ob->totcol; a++) {
237                                 if(ob->mat[a]==ma) {
238                                         if(ob->id.lib) lib= 1;
239                                         else local= 1;
240                                 }
241                         }
242                 }
243                 ob= ob->id.next;
244         }
245         /* test meshes */
246         me= G.main->mesh.first;
247         while(me) {
248                 if(me->mat) {
249                         for(a=0; a<me->totcol; a++) {
250                                 if(me->mat[a]==ma) {
251                                         if(me->id.lib) lib= 1;
252                                         else local= 1;
253                                 }
254                         }
255                 }
256                 me= me->id.next;
257         }
258         /* test curves */
259         cu= G.main->curve.first;
260         while(cu) {
261                 if(cu->mat) {
262                         for(a=0; a<cu->totcol; a++) {
263                                 if(cu->mat[a]==ma) {
264                                         if(cu->id.lib) lib= 1;
265                                         else local= 1;
266                                 }
267                         }
268                 }
269                 cu= cu->id.next;
270         }
271         /* test mballs */
272         mb= G.main->mball.first;
273         while(mb) {
274                 if(mb->mat) {
275                         for(a=0; a<mb->totcol; a++) {
276                                 if(mb->mat[a]==ma) {
277                                         if(mb->id.lib) lib= 1;
278                                         else local= 1;
279                                 }
280                         }
281                 }
282                 mb= mb->id.next;
283         }
284         
285         if(local && lib==0) {
286                 ma->id.lib= 0;
287                 ma->id.flag= LIB_LOCAL;
288                 
289                 for(a=0; a<MAX_MTEX; a++) {
290                         if(ma->mtex[a]) id_lib_extern((ID *)ma->mtex[a]->tex);
291                 }
292                 
293                 new_id(0, (ID *)ma, 0);
294         }
295         else if(local && lib) {
296                 
297                 man= copy_material(ma);
298                 man->id.us= 0;
299                 
300                 /* do objects */
301                 ob= G.main->object.first;
302                 while(ob) {
303                         if(ob->mat) {
304                                 for(a=0; a<ob->totcol; a++) {
305                                         if(ob->mat[a]==ma) {
306                                                 if(ob->id.lib==0) {
307                                                         ob->mat[a]= man;
308                                                         man->id.us++;
309                                                         ma->id.us--;
310                                                 }
311                                         }
312                                 }
313                         }
314                         ob= ob->id.next;
315                 }
316                 /* do meshes */
317                 me= G.main->mesh.first;
318                 while(me) {
319                         if(me->mat) {
320                                 for(a=0; a<me->totcol; a++) {
321                                         if(me->mat[a]==ma) {
322                                                 if(me->id.lib==0) {
323                                                         me->mat[a]= man;
324                                                         man->id.us++;
325                                                         ma->id.us--;
326                                                 }
327                                         }
328                                 }
329                         }
330                         me= me->id.next;
331                 }
332                 /* do curves */
333                 cu= G.main->curve.first;
334                 while(cu) {
335                         if(cu->mat) {
336                                 for(a=0; a<cu->totcol; a++) {
337                                         if(cu->mat[a]==ma) {
338                                                 if(cu->id.lib==0) {
339                                                         cu->mat[a]= man;
340                                                         man->id.us++;
341                                                         ma->id.us--;
342                                                 }
343                                         }
344                                 }
345                         }
346                         cu= cu->id.next;
347                 }
348                 /* do mballs */
349                 mb= G.main->mball.first;
350                 while(mb) {
351                         if(mb->mat) {
352                                 for(a=0; a<mb->totcol; a++) {
353                                         if(mb->mat[a]==ma) {
354                                                 if(mb->id.lib==0) {
355                                                         mb->mat[a]= man;
356                                                         man->id.us++;
357                                                         ma->id.us--;
358                                                 }
359                                         }
360                                 }
361                         }
362                         mb= mb->id.next;
363                 }
364         }
365 }
366
367 Material ***give_matarar(Object *ob)
368 {
369         Mesh *me;
370         Curve *cu;
371         MetaBall *mb;
372         
373         if(ob->type==OB_MESH) {
374                 me= ob->data;
375                 return &(me->mat);
376         }
377         else if ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF) {
378                 cu= ob->data;
379                 return &(cu->mat);
380         }
381         else if(ob->type==OB_MBALL) {
382                 mb= ob->data;
383                 return &(mb->mat);
384         }
385         return NULL;
386 }
387
388 short *give_totcolp(Object *ob)
389 {
390         Mesh *me;
391         Curve *cu;
392         MetaBall *mb;
393         
394         if(ob->type==OB_MESH) {
395                 me= ob->data;
396                 return &(me->totcol);
397         }
398         else if ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF) {
399                 cu= ob->data;
400                 return &(cu->totcol);
401         }
402         else if(ob->type==OB_MBALL) {
403                 mb= ob->data;
404                 return &(mb->totcol);
405         }
406         return NULL;
407 }
408
409 Material *give_current_material(Object *ob, int act)
410 {
411         Material ***matarar, *ma;
412         short *totcolp;
413         
414         if(ob==NULL) return NULL;
415         
416         /* if object cannot have material, totcolp==NULL */
417         totcolp= give_totcolp(ob);
418         if(totcolp==NULL || ob->totcol==0) return NULL;
419         
420         if(act>ob->totcol) act= ob->totcol;
421         else if(act<=0) act= 1;
422
423         if( BTST(ob->colbits, act-1) ) {        /* in object */
424                 ma= ob->mat[act-1];
425         }
426         else {                                                          /* in data */
427
428                 /* check for inconsistancy */
429                 if(*totcolp < ob->totcol)
430                         ob->totcol= *totcolp;
431                 if(act>ob->totcol) act= ob->totcol;
432
433                 matarar= give_matarar(ob);
434                 
435                 if(matarar && *matarar) ma= (*matarar)[act-1];
436                 else ma= 0;
437                 
438         }
439         
440         return ma;
441 }
442
443 ID *material_from(Object *ob, int act)
444 {
445
446         if(ob==0) return 0;
447
448         if(ob->totcol==0) return ob->data;
449         if(act==0) act= 1;
450
451         if( BTST(ob->colbits, act-1) ) return (ID *)ob;
452         else return ob->data;
453 }
454
455 /* GS reads the memory pointed at in a specific ordering. There are,
456  * however two definitions for it. I have jotted them down here, both,
457  * but I think the first one is actually used. The thing is that
458  * big-endian systems might read this the wrong way round. OTOH, we
459  * constructed the IDs that are read out with this macro explicitly as
460  * well. I expect we'll sort it out soon... */
461
462 /* from blendef: */
463 #define GS(a)   (*((short *)(a)))
464
465 /* from misc_util: flip the bytes from x  */
466 /*  #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
467
468 void test_object_materials(ID *id)
469 {
470         /* make the ob mat-array same size as 'ob->data' mat-array */
471         Object *ob;
472         Mesh *me;
473         Curve *cu;
474         MetaBall *mb;
475         Material **newmatar;
476         int totcol=0;
477
478         if(id==0) return;
479
480         if( GS(id->name)==ID_ME ) {
481                 me= (Mesh *)id;
482                 totcol= me->totcol;
483         }
484         else if( GS(id->name)==ID_CU ) {
485                 cu= (Curve *)id;
486                 totcol= cu->totcol;
487         }
488         else if( GS(id->name)==ID_MB ) {
489                 mb= (MetaBall *)id;
490                 totcol= mb->totcol;
491         }
492         else return;
493
494         ob= G.main->object.first;
495         while(ob) {
496                 
497                 if(ob->data==id) {
498                 
499                         if(totcol==0) {
500                                 if(ob->totcol) {
501                                         MEM_freeN(ob->mat);
502                                         ob->mat= 0;
503                                 }
504                         }
505                         else if(ob->totcol<totcol) {
506                                 newmatar= MEM_callocN(sizeof(void *)*totcol, "newmatar");
507                                 if(ob->totcol) {
508                                         memcpy(newmatar, ob->mat, sizeof(void *)*ob->totcol);
509                                         MEM_freeN(ob->mat);
510                                 }
511                                 ob->mat= newmatar;
512                         }
513                         ob->totcol= totcol;
514                         if(ob->totcol && ob->actcol==0) ob->actcol= 1;
515                         if(ob->actcol>ob->totcol) ob->actcol= ob->totcol;
516                 }
517                 ob= ob->id.next;
518         }
519 }
520
521
522 void assign_material(Object *ob, Material *ma, int act)
523 {
524         Material *mao, **matar, ***matarar;
525         short *totcolp;
526
527         if(act>MAXMAT) return;
528         if(act<1) act= 1;
529         
530         /* test arraylens */
531         
532         totcolp= give_totcolp(ob);
533         matarar= give_matarar(ob);
534         
535         if(totcolp==0 || matarar==0) return;
536         
537         if( act > *totcolp) {
538                 matar= MEM_callocN(sizeof(void *)*act, "matarray1");
539                 if( *totcolp) {
540                         memcpy(matar, *matarar, sizeof(void *)*( *totcolp ));
541                         MEM_freeN(*matarar);
542                 }
543                 *matarar= matar;
544                 *totcolp= act;
545         }
546         
547         if(act > ob->totcol) {
548                 matar= MEM_callocN(sizeof(void *)*act, "matarray2");
549                 if( ob->totcol) {
550                         memcpy(matar, ob->mat, sizeof(void *)*( ob->totcol ));
551                         MEM_freeN(ob->mat);
552                 }
553                 ob->mat= matar;
554                 ob->totcol= act;
555         }
556         
557         /* do it */
558
559         if( BTST(ob->colbits, act-1) ) {        /* in object */
560                 mao= ob->mat[act-1];
561                 if(mao) mao->id.us--;
562                 ob->mat[act-1]= ma;
563         }
564         else {  /* in data */
565                 mao= (*matarar)[act-1];
566                 if(mao) mao->id.us--;
567                 (*matarar)[act-1]= ma;
568         }
569         id_us_plus((ID *)ma);
570         test_object_materials(ob->data);
571 }
572
573 void new_material_to_objectdata(Object *ob)
574 {
575         Material *ma;
576         
577         if(ob==0) return;
578         if(ob->totcol>=MAXMAT) return;
579         
580         ma= give_current_material(ob, ob->actcol);
581         if(ma==NULL)
582                 ma= add_material("Material");
583         else
584                 ma= copy_material(ma);
585         
586         ma->id.us= 0; /* eeh... */
587         
588         if(ob->actcol) {
589                 if( BTST(ob->colbits, ob->actcol-1) ) {
590                         ob->colbits= BSET(ob->colbits, ob->totcol);
591                 }
592         }
593         
594         assign_material(ob, ma, ob->totcol+1);
595         ob->actcol= ob->totcol;
596 }
597
598 static void do_init_render_material(Material *ma, int r_mode, float *amb)
599 {
600         MTex *mtex;
601         int a, needuv=0;
602         
603         if(ma->flarec==0) ma->flarec= 1;
604
605         /* add all texcoflags from mtex */
606         ma->texco= 0;
607         ma->mapto= 0;
608         for(a=0; a<MAX_MTEX; a++) {
609                 
610                 /* separate tex switching */
611                 if(ma->septex & (1<<a)) continue;
612
613                 mtex= ma->mtex[a];
614                 if(mtex && mtex->tex && mtex->tex->type) {
615                         
616                         ma->texco |= mtex->texco;
617                         ma->mapto |= mtex->mapto;
618                         if(r_mode & R_OSA) {
619                                 if ELEM3(mtex->tex->type, TEX_IMAGE, TEX_PLUGIN, TEX_ENVMAP) ma->texco |= TEXCO_OSA;
620                         }
621                         
622                         if(ma->texco & (TEXCO_ORCO|TEXCO_REFL|TEXCO_NORM|TEXCO_STRAND|TEXCO_STRESS)) needuv= 1;
623                         else if(ma->texco & (TEXCO_GLOB|TEXCO_UV|TEXCO_OBJECT|TEXCO_SPEED)) needuv= 1;
624                         else if(ma->texco & (TEXCO_LAVECTOR|TEXCO_VIEW|TEXCO_STICKY)) needuv= 1;
625                 }
626         }
627         
628         if(r_mode & R_RADIO)
629                 if(ma->mode & MA_RADIO) needuv= 1;
630         
631         if(ma->mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)) {
632                 needuv= 1;
633                 if(r_mode & R_OSA) ma->texco |= TEXCO_OSA;              /* for texfaces */
634         }
635         if(needuv) ma->texco |= NEED_UV;
636         
637         /* since the raytracer doesnt recalc O structs for each ray, we have to preset them all */
638         if(r_mode & R_RAYTRACE) {
639                 if(ma->mode & (MA_RAYMIRROR|MA_RAYTRANSP|MA_SHADOW_TRA)) { 
640                         ma->texco |= NEED_UV|TEXCO_ORCO|TEXCO_REFL|TEXCO_NORM;
641                         if(r_mode & R_OSA) ma->texco |= TEXCO_OSA;
642                 }
643         }
644         
645         if(amb) {
646                 ma->ambr= ma->amb*amb[0];
647                 ma->ambg= ma->amb*amb[1];
648                 ma->ambb= ma->amb*amb[2];
649         }       
650         /* will become or-ed result of all node modes */
651         ma->mode_l= ma->mode;
652         ma->mode_l &= ~MA_SHLESS;
653 }
654
655 static void init_render_nodetree(bNodeTree *ntree, Material *basemat, int r_mode, float *amb)
656 {
657         bNode *node;
658         
659         for(node=ntree->nodes.first; node; node= node->next) {
660                 if(node->id) {
661                         if(GS(node->id->name)==ID_MA) {
662                                 Material *ma= (Material *)node->id;
663                                 if(ma!=basemat) {
664                                         do_init_render_material(ma, r_mode, amb);
665                                         basemat->texco |= ma->texco;
666                                         basemat->mode_l |= ma->mode_l;
667                                 }
668                         }
669                         else if(node->type==NODE_GROUP)
670                                 init_render_nodetree((bNodeTree *)node->id, basemat, r_mode, amb);
671                 }
672         }
673         /* parses the geom+tex nodes */
674         ntreeShaderGetTexcoMode(ntree, r_mode, &basemat->texco, &basemat->mode_l);
675 }
676
677 void init_render_material(Material *mat, int r_mode, float *amb)
678 {
679         
680         do_init_render_material(mat, r_mode, amb);
681         
682         if(mat->nodetree && mat->use_nodes) {
683                 init_render_nodetree(mat->nodetree, mat, r_mode, amb);
684                 
685                 ntreeBeginExecTree(mat->nodetree); /* has internal flag to detect it only does it once */
686         }
687 }
688
689 void init_render_materials(int r_mode, float *amb)
690 {
691         Material *ma;
692         
693         /* two steps, first initialize, then or the flags for layers */
694         for(ma= G.main->mat.first; ma; ma= ma->id.next) {
695                 /* is_used flag comes back in convertblender.c */
696                 ma->flag &= ~MA_IS_USED;
697                 if(ma->id.us) 
698                         init_render_material(ma, r_mode, amb);
699         }
700         
701         do_init_render_material(&defmaterial, r_mode, amb);
702 }
703
704 /* only needed for nodes now */
705 void end_render_material(Material *mat)
706 {
707         if(mat && mat->nodetree && mat->use_nodes)
708                 ntreeEndExecTree(mat->nodetree); /* has internal flag to detect it only does it once */
709 }
710
711 void end_render_materials(void)
712 {
713         Material *ma;
714         for(ma= G.main->mat.first; ma; ma= ma->id.next)
715                 if(ma->id.us) 
716                         end_render_material(ma);
717 }
718
719 static int material_in_nodetree(bNodeTree *ntree, Material *mat)
720 {
721         bNode *node;
722
723         for(node=ntree->nodes.first; node; node= node->next) {
724                 if(node->id && GS(node->id->name)==ID_MA) {
725                         if(node->id==(ID*)mat)
726                                 return 1;
727                 }
728                 else if(node->type==NODE_GROUP)
729                         if(material_in_nodetree((bNodeTree*)node->id, mat))
730                                 return 1;
731         }
732
733         return 0;
734 }
735
736 int material_in_material(Material *parmat, Material *mat)
737 {
738         if(parmat==mat)
739                 return 1;
740         else if(parmat->nodetree && parmat->use_nodes)
741                 return material_in_nodetree(parmat->nodetree, mat);
742         else
743                 return 0;
744 }
745         
746 /* ****************** */
747
748 char colname_array[125][20]= {
749 "Black","DarkRed","HalveRed","Red","Red",
750 "DarkGreen","DarkOlive","Brown","Chocolate","OrangeRed",
751 "HalveGreen","GreenOlive","DryOlive","Goldenrod","DarkOrange",
752 "LightGreen","Chartreuse","YellowGreen","Yellow","Gold",
753 "Green","LawnGreen","GreenYellow","LightOlive","Yellow",
754 "DarkBlue","DarkPurple","HotPink","VioletPink","RedPink",
755 "SlateGray","DarkGrey","PalePurple","IndianRed","Tomato",
756 "SeaGreen","PaleGreen","GreenKhaki","LightBrown","LightSalmon",
757 "SpringGreen","PaleGreen","MediumOlive","YellowBrown","LightGold",
758 "LightGreen","LightGreen","LightGreen","GreenYellow","PaleYellow",
759 "HalveBlue","DarkSky","HalveMagenta","VioletRed","DeepPink",
760 "SteelBlue","SkyBlue","Orchid","LightHotPink","HotPink",
761 "SeaGreen","SlateGray","MediumGrey","Burlywood","LightPink",
762 "SpringGreen","Aquamarine","PaleGreen","Khaki","PaleOrange",
763 "SpringGreen","SeaGreen","PaleGreen","PaleWhite","YellowWhite",
764 "LightBlue","Purple","MediumOrchid","Magenta","Magenta",
765 "RoyalBlue","SlateBlue","MediumOrchid","Orchid","Magenta",
766 "DeepSkyBlue","LightSteelBlue","LightSkyBlue","Violet","LightPink",
767 "Cyan","DarkTurquoise","SkyBlue","Grey","Snow",
768 "Mint","Mint","Aquamarine","MintCream","Ivory",
769 "Blue","Blue","DarkMagenta","DarkOrchid","Magenta",
770 "SkyBlue","RoyalBlue","LightSlateBlue","MediumOrchid","Magenta",
771 "DodgerBlue","SteelBlue","MediumPurple","PalePurple","Plum",
772 "DeepSkyBlue","PaleBlue","LightSkyBlue","PalePurple","Thistle",
773 "Cyan","ColdBlue","PaleTurquoise","GhostWhite","White"
774 };
775
776 void automatname(Material *ma)
777 {
778         int nr, r, g, b;
779         float ref;
780         
781         if(ma==0) return;
782         if(ma->mode & MA_SHLESS) ref= 1.0;
783         else ref= ma->ref;
784         
785         r= (int)(4.99*(ref*ma->r));
786         g= (int)(4.99*(ref*ma->g));
787         b= (int)(4.99*(ref*ma->b));
788         nr= r + 5*g + 25*b;
789         if(nr>124) nr= 124;
790         new_id(&G.main->mat, (ID *)ma, colname_array[nr]);
791         
792 }
793
794
795 void delete_material_index()
796 {
797         Material *mao, ***matarar;
798         Object *ob, *obt;
799         Curve *cu;
800         Nurb *nu;
801         short *totcolp;
802         int a, actcol;
803         
804         if(G.obedit) {
805                 error("Unable to perform function in EditMode");
806                 return;
807         }
808         ob= ((G.scene->basact)? (G.scene->basact->object) : 0) ;
809         if(ob==0 || ob->totcol==0) return;
810         
811         /* take a mesh/curve/mball as starting point, remove 1 index,
812          * AND with all objects that share the ob->data
813          * 
814          * after that check indices in mesh/curve/mball!!!
815          */
816         
817         totcolp= give_totcolp(ob);
818         matarar= give_matarar(ob);
819
820         /* we delete the actcol */
821         if(ob->totcol) {
822                 mao= (*matarar)[ob->actcol-1];
823                 if(mao) mao->id.us--;
824         }
825         
826         for(a=ob->actcol; a<ob->totcol; a++) {
827                 (*matarar)[a-1]= (*matarar)[a];
828         }
829         (*totcolp)--;
830         
831         if(*totcolp==0) {
832                 MEM_freeN(*matarar);
833                 *matarar= 0;
834         }
835         
836         actcol= ob->actcol;
837         obt= G.main->object.first;
838         while(obt) {
839         
840                 if(obt->data==ob->data) {
841                         
842                         /* WATCH IT: do not use actcol from ob or from obt (can become zero) */
843                         mao= obt->mat[actcol-1];
844                         if(mao) mao->id.us--;
845                 
846                         for(a=actcol; a<obt->totcol; a++) obt->mat[a-1]= obt->mat[a];
847                         obt->totcol--;
848                         if(obt->actcol > obt->totcol) obt->actcol= obt->totcol;
849                         
850                         if(obt->totcol==0) {
851                                 MEM_freeN(obt->mat);
852                                 obt->mat= 0;
853                         }
854                 }
855                 obt= obt->id.next;
856         }
857
858         /* check indices from mesh */
859
860         if(ob->type==OB_MESH) {
861                 Mesh *me= get_mesh(ob);
862                 mesh_delete_material_index(me, actcol-1);
863                 freedisplist(&ob->disp);
864         }
865         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
866                 cu= ob->data;
867                 nu= cu->nurb.first;
868                 
869                 while(nu) {
870                         if(nu->mat_nr && nu->mat_nr>=actcol-1) {
871                                 nu->mat_nr--;
872                                 if (ob->type == OB_CURVE) nu->charidx--;
873                         }
874                         nu= nu->next;
875                 }
876                 freedisplist(&ob->disp);
877         }
878 }
879
880
881 /* r g b = current value, col = new value, fac==0 is no change */
882 /* if g==NULL, it only does r channel */
883 void ramp_blend(int type, float *r, float *g, float *b, float fac, float *col)
884 {
885         float tmp, facm= 1.0-fac;
886         
887         switch (type) {
888                 case MA_RAMP_BLEND:
889                         *r = facm*(*r) + fac*col[0];
890                         if(g) {
891                                 *g = facm*(*g) + fac*col[1];
892                                 *b = facm*(*b) + fac*col[2];
893                         }
894                                 break;
895                 case MA_RAMP_ADD:
896                         *r += fac*col[0];
897                         if(g) {
898                                 *g += fac*col[1];
899                                 *b += fac*col[2];
900                         }
901                                 break;
902                 case MA_RAMP_MULT:
903                         *r *= (facm + fac*col[0]);
904                         if(g) {
905                                 *g *= (facm + fac*col[1]);
906                                 *b *= (facm + fac*col[2]);
907                         }
908                                 break;
909                 case MA_RAMP_SCREEN:
910                         *r = 1.0 - (facm + fac*(1.0 - col[0])) * (1.0 - *r);
911                         if(g) {
912                                 *g = 1.0 - (facm + fac*(1.0 - col[1])) * (1.0 - *g);
913                                 *b = 1.0 - (facm + fac*(1.0 - col[2])) * (1.0 - *b);
914                         }
915                                 break;
916                 case MA_RAMP_OVERLAY:
917                         if(*r < 0.5f)
918                                 *r *= (facm + 2.0f*fac*col[0]);
919                         else
920                                 *r = 1.0 - (facm + 2.0f*fac*(1.0 - col[0])) * (1.0 - *r);
921                         if(g) {
922                                 if(*g < 0.5f)
923                                         *g *= (facm + 2.0f*fac*col[1]);
924                                 else
925                                         *g = 1.0 - (facm + 2.0f*fac*(1.0 - col[1])) * (1.0 - *g);
926                                 if(*b < 0.5f)
927                                         *b *= (facm + 2.0f*fac*col[2]);
928                                 else
929                                         *b = 1.0 - (facm + 2.0f*fac*(1.0 - col[2])) * (1.0 - *b);
930                         }
931                                 break;
932                 case MA_RAMP_SUB:
933                         *r -= fac*col[0];
934                         if(g) {
935                                 *g -= fac*col[1];
936                                 *b -= fac*col[2];
937                         }
938                                 break;
939                 case MA_RAMP_DIV:
940                         if(col[0]!=0.0)
941                                 *r = facm*(*r) + fac*(*r)/col[0];
942                         if(g) {
943                                 if(col[1]!=0.0)
944                                         *g = facm*(*g) + fac*(*g)/col[1];
945                                 if(col[2]!=0.0)
946                                         *b = facm*(*b) + fac*(*b)/col[2];
947                         }
948                                 break;
949                 case MA_RAMP_DIFF:
950                         *r = facm*(*r) + fac*fabs(*r-col[0]);
951                         if(g) {
952                                 *g = facm*(*g) + fac*fabs(*g-col[1]);
953                                 *b = facm*(*b) + fac*fabs(*b-col[2]);
954                         }
955                                 break;
956                 case MA_RAMP_DARK:
957                         tmp= fac*col[0];
958                         if(tmp < *r) *r= tmp; 
959                                 if(g) {
960                                         tmp= fac*col[1];
961                                         if(tmp < *g) *g= tmp; 
962                                         tmp= fac*col[2];
963                                         if(tmp < *b) *b= tmp; 
964                                 }
965                                         break;
966                 case MA_RAMP_LIGHT:
967                         tmp= fac*col[0];
968                         if(tmp > *r) *r= tmp; 
969                                 if(g) {
970                                         tmp= fac*col[1];
971                                         if(tmp > *g) *g= tmp; 
972                                         tmp= fac*col[2];
973                                         if(tmp > *b) *b= tmp; 
974                                 }
975                                         break;  
976                 case MA_RAMP_DODGE:                     
977                         
978                                 
979                         if(*r !=0.0){
980                                 tmp = 1.0 - fac*col[0];
981                                 if(tmp <= 0.0)
982                                         *r = 1.0;
983                                 else if ((tmp = (*r) / tmp)> 1.0)
984                                         *r = 1.0;
985                                 else 
986                                         *r = tmp;
987                         }
988                         if(g) {
989                                 if(*g !=0.0){
990                                         tmp = 1.0 - fac*col[1];
991                                         if(tmp <= 0.0 )
992                                                 *g = 1.0;
993                                         else if ((tmp = (*g) / tmp) > 1.0 )
994                                                 *g = 1.0;
995                                         else
996                                                 *g = tmp;
997                                 }
998                                 if(*b !=0.0){
999                                         tmp = 1.0 - fac*col[2];
1000                                         if(tmp <= 0.0)
1001                                                 *b = 1.0;
1002                                         else if ((tmp = (*b) / tmp) > 1.0 )
1003                                                 *b = 1.0;
1004                                         else
1005                                                 *b = tmp;
1006                                 }
1007
1008                         }
1009                                 break;  
1010                 case MA_RAMP_BURN:
1011                         
1012                         tmp = facm + fac*col[0];
1013                         
1014                         if(tmp <= 0.0)
1015                                 *r = 0.0;
1016                         else if (( tmp = (1.0 - (1.0 - (*r)) / tmp )) < 0.0)
1017                                 *r = 0.0;
1018                         else if (tmp > 1.0)
1019                                 *r=1.0;
1020                         else 
1021                                 *r = tmp; 
1022
1023                         if(g) {
1024                                 tmp = facm + fac*col[1];
1025                                 if(tmp <= 0.0)
1026                                         *g = 0.0;
1027                                 else if (( tmp = (1.0 - (1.0 - (*g)) / tmp )) < 0.0 )
1028                                         *g = 0.0;
1029                                 else if(tmp >1.0)
1030                                         *g=1.0;
1031                                 else
1032                                         *g = tmp;
1033                                         
1034                                 tmp = facm + fac*col[2];
1035                                 if(tmp <= 0.0)
1036                                         *b = 0.0;
1037                                 else if (( tmp = (1.0 - (1.0 - (*b)) / tmp )) < 0.0  )
1038                                         *b = 0.0;
1039                                 else if(tmp >1.0)
1040                                         *b= 1.0;
1041                                 else
1042                                         *b = tmp;
1043                         }
1044                                 break;
1045                 case MA_RAMP_HUE:               
1046                         if(g){
1047                                 float rH,rS,rV;
1048                                 float colH,colS,colV; 
1049                                 float tmpr,tmpg,tmpb;
1050                                 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1051                                 if(colS!=0 ){
1052                                         rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1053                                         hsv_to_rgb( colH , rS, rV, &tmpr, &tmpg, &tmpb);
1054                                         *r = facm*(*r) + fac*tmpr;  
1055                                         *g = facm*(*g) + fac*tmpg; 
1056                                         *b = facm*(*b) + fac*tmpb;
1057                                 }
1058                         }
1059                                 break;
1060                 case MA_RAMP_SAT:               
1061                         if(g){
1062                                 float rH,rS,rV;
1063                                 float colH,colS,colV;
1064                                 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1065                                 if(rS!=0){
1066                                         rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1067                                         hsv_to_rgb( rH, (facm*rS +fac*colS), rV, r, g, b);
1068                                 }
1069                         }
1070                                 break;
1071                 case MA_RAMP_VAL:               
1072                         if(g){
1073                                 float rH,rS,rV;
1074                                 float colH,colS,colV;
1075                                 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1076                                 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1077                                 hsv_to_rgb( rH, rS, (facm*rV +fac*colV), r, g, b);
1078                         }
1079                                 break;
1080                 case MA_RAMP_COLOR:             
1081                         if(g){
1082                                 float rH,rS,rV;
1083                                 float colH,colS,colV;
1084                                 float tmpr,tmpg,tmpb;
1085                                 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1086                                 if(colS!=0){
1087                                         rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1088                                         hsv_to_rgb( colH, colS, rV, &tmpr, &tmpg, &tmpb);
1089                                         *r = facm*(*r) + fac*tmpr;
1090                                         *g = facm*(*g) + fac*tmpg;
1091                                         *b = facm*(*b) + fac*tmpb;
1092                                 }
1093                         }
1094                                 break;
1095         }
1096         
1097 }
1098
1099