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