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