- use static vars and functions where possible.
[blender.git] / source / blender / blenkernel / intern / material.c
1
2 /*  material.c
3  *
4  * 
5  * $Id$
6  *
7  * ***** BEGIN GPL 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.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL LICENSE BLOCK *****
31  */
32
33 /** \file blender/blenkernel/intern/material.c
34  *  \ingroup bke
35  */
36
37
38 #include <string.h>
39 #include <math.h>
40
41 #include "MEM_guardedalloc.h"
42
43 #include "DNA_curve_types.h"
44 #include "DNA_material_types.h"
45 #include "DNA_mesh_types.h"
46 #include "DNA_meta_types.h"
47 #include "DNA_node_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_scene_types.h"
50
51 #include "BLI_math.h"           
52 #include "BLI_listbase.h"               
53 #include "BLI_utildefines.h"
54
55 #include "BKE_animsys.h"
56 #include "BKE_displist.h"
57 #include "BKE_global.h"
58 #include "BKE_icons.h"
59 #include "BKE_library.h"
60 #include "BKE_main.h"
61 #include "BKE_material.h"
62 #include "BKE_mesh.h"
63 #include "BKE_node.h"
64 #include "BKE_curve.h"
65
66 #include "GPU_material.h"
67
68 /* used in UI and render */
69 Material defmaterial;
70
71 /* called on startup, creator.c */
72 void init_def_material(void)
73 {
74         init_material(&defmaterial);
75 }
76
77 /* not material itself */
78 void free_material(Material *ma)
79 {
80         MTex *mtex;
81         int a;
82         
83         for(a=0; a<MAX_MTEX; a++) {
84                 mtex= ma->mtex[a];
85                 if(mtex && mtex->tex) mtex->tex->id.us--;
86                 if(mtex) MEM_freeN(mtex);
87         }
88         
89         if(ma->ramp_col) MEM_freeN(ma->ramp_col);
90         if(ma->ramp_spec) MEM_freeN(ma->ramp_spec);
91         
92         BKE_free_animdata((ID *)ma);
93         
94         if(ma->preview)
95                 BKE_previewimg_free(&ma->preview);
96         BKE_icon_delete((struct ID*)ma);
97         ma->id.icon_id = 0;
98         
99         /* is no lib link block, but material extension */
100         if(ma->nodetree) {
101                 ntreeFreeTree(ma->nodetree);
102                 MEM_freeN(ma->nodetree);
103         }
104
105         if(ma->gpumaterial.first)
106                 GPU_material_free(ma);
107 }
108
109 void init_material(Material *ma)
110 {
111         ma->r= ma->g= ma->b= ma->ref= 0.8;
112         ma->specr= ma->specg= ma->specb= 1.0;
113         ma->mirr= ma->mirg= ma->mirb= 1.0;
114         ma->spectra= 1.0;
115         ma->amb= 1.0;
116         ma->alpha= 1.0;
117         ma->spec= ma->hasize= 0.5;
118         ma->har= 50;
119         ma->starc= ma->ringc= 4;
120         ma->linec= 12;
121         ma->flarec= 1;
122         ma->flaresize= ma->subsize= 1.0;
123         ma->flareboost= 1;
124         ma->seed2= 6;
125         ma->friction= 0.5;
126         ma->refrac= 4.0;
127         ma->roughness= 0.5;
128         ma->param[0]= 0.5;
129         ma->param[1]= 0.1;
130         ma->param[2]= 0.5;
131         ma->param[3]= 0.1;
132         ma->rms= 0.1;
133         ma->darkness= 1.0;      
134         
135         ma->strand_sta= ma->strand_end= 1.0f;
136         
137         ma->ang= 1.0;
138         ma->ray_depth= 2;
139         ma->ray_depth_tra= 2;
140         ma->fresnel_mir= 0.0;
141         ma->fresnel_tra= 0.0;
142         ma->fresnel_tra_i= 1.25;
143         ma->fresnel_mir_i= 1.25;
144         ma->tx_limit= 0.0;
145         ma->tx_falloff= 1.0;
146         ma->shad_alpha= 1.0f;
147         
148         ma->gloss_mir = ma->gloss_tra= 1.0;
149         ma->samp_gloss_mir = ma->samp_gloss_tra= 18;
150         ma->adapt_thresh_mir = ma->adapt_thresh_tra = 0.005;
151         ma->dist_mir = 0.0;
152         ma->fadeto_mir = MA_RAYMIR_FADETOSKY;
153         
154         ma->rampfac_col= 1.0;
155         ma->rampfac_spec= 1.0;
156         ma->pr_lamp= 3;                 /* two lamps, is bits */
157         ma->pr_type= MA_SPHERE;
158
159         ma->sss_radius[0]= 1.0f;
160         ma->sss_radius[1]= 1.0f;
161         ma->sss_radius[2]= 1.0f;
162         ma->sss_col[0]= 1.0f;
163         ma->sss_col[1]= 1.0f;
164         ma->sss_col[2]= 1.0f;
165         ma->sss_error= 0.05f;
166         ma->sss_scale= 0.1f;
167         ma->sss_ior= 1.3f;
168         ma->sss_colfac= 1.0f;
169         ma->sss_texfac= 0.0f;
170         ma->sss_front= 1.0f;
171         ma->sss_back= 1.0f;
172
173         ma->vol.density = 1.0f;
174         ma->vol.emission = 0.0f;
175         ma->vol.scattering = 1.0f;
176         ma->vol.reflection = 1.0f;
177         ma->vol.transmission_col[0] = ma->vol.transmission_col[1] = ma->vol.transmission_col[2] = 1.0f;
178         ma->vol.reflection_col[0] = ma->vol.reflection_col[1] = ma->vol.reflection_col[2] = 1.0f;
179         ma->vol.emission_col[0] = ma->vol.emission_col[1] = ma->vol.emission_col[2] = 1.0f;
180         ma->vol.density_scale = 1.0f;
181         ma->vol.depth_cutoff = 0.01f;
182         ma->vol.stepsize_type = MA_VOL_STEP_RANDOMIZED;
183         ma->vol.stepsize = 0.2f;
184         ma->vol.shade_type = MA_VOL_SHADE_SHADED;
185         ma->vol.shadeflag |= MA_VOL_PRECACHESHADING;
186         ma->vol.precache_resolution = 50;
187         ma->vol.ms_spread = 0.2f;
188         ma->vol.ms_diff = 1.f;
189         ma->vol.ms_intensity = 1.f;
190         
191         ma->mode= MA_TRACEBLE|MA_SHADBUF|MA_SHADOW|MA_RAYBIAS|MA_TANGENT_STR|MA_ZTRANSP;
192         ma->shade_flag= MA_APPROX_OCCLUSION;
193         ma->preview = NULL;
194 }
195
196 Material *add_material(const char *name)
197 {
198         Material *ma;
199
200         ma= alloc_libblock(&G.main->mat, ID_MA, name);
201         
202         init_material(ma);
203         
204         return ma;      
205 }
206
207 /* XXX keep synced with next function */
208 Material *copy_material(Material *ma)
209 {
210         Material *man;
211         int a;
212         
213         man= copy_libblock(ma);
214         
215         id_lib_extern((ID *)man->group);
216         
217         for(a=0; a<MAX_MTEX; a++) {
218                 if(ma->mtex[a]) {
219                         man->mtex[a]= MEM_mallocN(sizeof(MTex), "copymaterial");
220                         memcpy(man->mtex[a], ma->mtex[a], sizeof(MTex));
221                         id_us_plus((ID *)man->mtex[a]->tex);
222                 }
223         }
224         
225         if(ma->ramp_col) man->ramp_col= MEM_dupallocN(ma->ramp_col);
226         if(ma->ramp_spec) man->ramp_spec= MEM_dupallocN(ma->ramp_spec);
227         
228         if (ma->preview) man->preview = BKE_previewimg_copy(ma->preview);
229
230         if(ma->nodetree) {
231                 man->nodetree= ntreeCopyTree(ma->nodetree);     /* 0 == full new tree */
232         }
233
234         man->gpumaterial.first= man->gpumaterial.last= NULL;
235         
236         return man;
237 }
238
239 /* XXX (see above) material copy without adding to main dbase */
240 Material *localize_material(Material *ma)
241 {
242         Material *man;
243         int a;
244         
245         man= copy_libblock(ma);
246         BLI_remlink(&G.main->mat, man);
247
248         /* no increment for texture ID users, in previewrender.c it prevents decrement */
249         for(a=0; a<MAX_MTEX; a++) {
250                 if(ma->mtex[a]) {
251                         man->mtex[a]= MEM_mallocN(sizeof(MTex), "copymaterial");
252                         memcpy(man->mtex[a], ma->mtex[a], sizeof(MTex));
253                 }
254         }
255         
256         if(ma->ramp_col) man->ramp_col= MEM_dupallocN(ma->ramp_col);
257         if(ma->ramp_spec) man->ramp_spec= MEM_dupallocN(ma->ramp_spec);
258         
259         man->preview = NULL;
260         
261         if(ma->nodetree)
262                 man->nodetree= ntreeLocalize(ma->nodetree);
263         
264         man->gpumaterial.first= man->gpumaterial.last= NULL;
265         
266         return man;
267 }
268
269 static void extern_local_material(Material *ma)
270 {
271         int i;
272         for(i=0; i < MAX_MTEX; i++) {
273                 if(ma->mtex[i]) id_lib_extern((ID *)ma->mtex[i]->tex);
274         }
275 }
276
277 void make_local_material(Material *ma)
278 {
279         Main *bmain= G.main;
280         Object *ob;
281         Mesh *me;
282         Curve *cu;
283         MetaBall *mb;
284         Material *man;
285         int a, local=0, lib=0;
286
287         /* - only lib users: do nothing
288                 * - only local users: set flag
289                 * - mixed: make copy
290                 */
291         
292         if(ma->id.lib==NULL) return;
293         if(ma->id.us==1) {
294                 ma->id.lib= NULL;
295                 ma->id.flag= LIB_LOCAL;
296
297                 new_id(&bmain->mat, (ID *)ma, NULL);
298                 extern_local_material(ma);
299                 return;
300         }
301         
302         /* test objects */
303         ob= bmain->object.first;
304         while(ob) {
305                 if(ob->mat) {
306                         for(a=0; a<ob->totcol; a++) {
307                                 if(ob->mat[a]==ma) {
308                                         if(ob->id.lib) lib= 1;
309                                         else local= 1;
310                                 }
311                         }
312                 }
313                 ob= ob->id.next;
314         }
315         /* test meshes */
316         me= bmain->mesh.first;
317         while(me) {
318                 if(me->mat) {
319                         for(a=0; a<me->totcol; a++) {
320                                 if(me->mat[a]==ma) {
321                                         if(me->id.lib) lib= 1;
322                                         else local= 1;
323                                 }
324                         }
325                 }
326                 me= me->id.next;
327         }
328         /* test curves */
329         cu= bmain->curve.first;
330         while(cu) {
331                 if(cu->mat) {
332                         for(a=0; a<cu->totcol; a++) {
333                                 if(cu->mat[a]==ma) {
334                                         if(cu->id.lib) lib= 1;
335                                         else local= 1;
336                                 }
337                         }
338                 }
339                 cu= cu->id.next;
340         }
341         /* test mballs */
342         mb= bmain->mball.first;
343         while(mb) {
344                 if(mb->mat) {
345                         for(a=0; a<mb->totcol; a++) {
346                                 if(mb->mat[a]==ma) {
347                                         if(mb->id.lib) lib= 1;
348                                         else local= 1;
349                                 }
350                         }
351                 }
352                 mb= mb->id.next;
353         }
354         
355         if(local && lib==0) {
356                 ma->id.lib= NULL;
357                 ma->id.flag= LIB_LOCAL;
358
359                 new_id(&bmain->mat, (ID *)ma, NULL);
360                 extern_local_material(ma);
361         }
362         else if(local && lib) {
363                 
364                 man= copy_material(ma);
365                 man->id.us= 0;
366                 
367                 /* do objects */
368                 ob= bmain->object.first;
369                 while(ob) {
370                         if(ob->mat) {
371                                 for(a=0; a<ob->totcol; a++) {
372                                         if(ob->mat[a]==ma) {
373                                                 if(ob->id.lib==NULL) {
374                                                         ob->mat[a]= man;
375                                                         man->id.us++;
376                                                         ma->id.us--;
377                                                 }
378                                         }
379                                 }
380                         }
381                         ob= ob->id.next;
382                 }
383                 /* do meshes */
384                 me= bmain->mesh.first;
385                 while(me) {
386                         if(me->mat) {
387                                 for(a=0; a<me->totcol; a++) {
388                                         if(me->mat[a]==ma) {
389                                                 if(me->id.lib==NULL) {
390                                                         me->mat[a]= man;
391                                                         man->id.us++;
392                                                         ma->id.us--;
393                                                 }
394                                         }
395                                 }
396                         }
397                         me= me->id.next;
398                 }
399                 /* do curves */
400                 cu= bmain->curve.first;
401                 while(cu) {
402                         if(cu->mat) {
403                                 for(a=0; a<cu->totcol; a++) {
404                                         if(cu->mat[a]==ma) {
405                                                 if(cu->id.lib==NULL) {
406                                                         cu->mat[a]= man;
407                                                         man->id.us++;
408                                                         ma->id.us--;
409                                                 }
410                                         }
411                                 }
412                         }
413                         cu= cu->id.next;
414                 }
415                 /* do mballs */
416                 mb= bmain->mball.first;
417                 while(mb) {
418                         if(mb->mat) {
419                                 for(a=0; a<mb->totcol; a++) {
420                                         if(mb->mat[a]==ma) {
421                                                 if(mb->id.lib==NULL) {
422                                                         mb->mat[a]= man;
423                                                         man->id.us++;
424                                                         ma->id.us--;
425                                                 }
426                                         }
427                                 }
428                         }
429                         mb= mb->id.next;
430                 }
431         }
432 }
433
434 /* for curve, mball, mesh types */
435 void extern_local_matarar(struct Material **matar, short totcol)
436 {
437         short i;
438         for(i= 0; i < totcol; i++) {
439                 id_lib_extern((ID *)matar[i]);
440         }
441 }
442
443 Material ***give_matarar(Object *ob)
444 {
445         Mesh *me;
446         Curve *cu;
447         MetaBall *mb;
448         
449         if(ob->type==OB_MESH) {
450                 me= ob->data;
451                 return &(me->mat);
452         }
453         else if ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF) {
454                 cu= ob->data;
455                 return &(cu->mat);
456         }
457         else if(ob->type==OB_MBALL) {
458                 mb= ob->data;
459                 return &(mb->mat);
460         }
461         return NULL;
462 }
463
464 short *give_totcolp(Object *ob)
465 {
466         Mesh *me;
467         Curve *cu;
468         MetaBall *mb;
469         
470         if(ob->type==OB_MESH) {
471                 me= ob->data;
472                 return &(me->totcol);
473         }
474         else if ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF) {
475                 cu= ob->data;
476                 return &(cu->totcol);
477         }
478         else if(ob->type==OB_MBALL) {
479                 mb= ob->data;
480                 return &(mb->totcol);
481         }
482         return NULL;
483 }
484
485 /* same as above but for ID's */
486 Material ***give_matarar_id(ID *id)
487 {
488         switch(GS(id->name)) {
489         case ID_ME:
490                 return &(((Mesh *)id)->mat);
491                 break;
492         case ID_CU:
493                 return &(((Curve *)id)->mat);
494                 break;
495         case ID_MB:
496                 return &(((MetaBall *)id)->mat);
497                 break;
498         }
499         return NULL;
500 }
501
502 short *give_totcolp_id(ID *id)
503 {
504         switch(GS(id->name)) {
505         case ID_ME:
506                 return &(((Mesh *)id)->totcol);
507                 break;
508         case ID_CU:
509                 return &(((Curve *)id)->totcol);
510                 break;
511         case ID_MB:
512                 return &(((MetaBall *)id)->totcol);
513                 break;
514         }
515         return NULL;
516 }
517
518 static void data_delete_material_index_id(ID *id, int index)
519 {
520         switch(GS(id->name)) {
521         case ID_ME:
522                 mesh_delete_material_index((Mesh *)id, index);
523                 break;
524         case ID_CU:
525                 curve_delete_material_index((Curve *)id, index);
526                 break;
527         case ID_MB:
528                 /* meta-elems dont have materials atm */
529                 break;
530         }
531 }
532
533 void material_append_id(ID *id, Material *ma)
534 {
535         Material ***matar;
536         if((matar= give_matarar_id(id))) {
537                 short *totcol= give_totcolp_id(id);
538                 Material **mat= MEM_callocN(sizeof(void *) * ((*totcol) + 1), "newmatar");
539                 if(*totcol) memcpy(mat, *matar, sizeof(void *) * (*totcol));
540                 if(*matar) MEM_freeN(*matar);
541
542                 *matar= mat;
543                 (*matar)[(*totcol)++]= ma;
544
545                 id_us_plus((ID *)ma);
546                 test_object_materials(id);
547         }
548 }
549
550 Material *material_pop_id(ID *id, int index, int remove_material_slot)
551 {
552         Material *ret= NULL;
553         Material ***matar;
554         if((matar= give_matarar_id(id))) {
555                 short *totcol= give_totcolp_id(id);
556                 if(index >= 0 && index < (*totcol)) {
557                         ret= (*matar)[index];
558                         id_us_min((ID *)ret);
559
560                         if (remove_material_slot) {
561                                 if(*totcol <= 1) {
562                                         *totcol= 0;
563                                         MEM_freeN(*matar);
564                                         *matar= NULL;
565                                 }
566                                 else {
567                                         Material **mat;
568                                         if(index + 1 != (*totcol))
569                                                 memmove((*matar)+index, (*matar)+(index+1), sizeof(void *) * ((*totcol) - (index + 1)));
570
571                                         (*totcol)--;
572                                         
573                                         mat= MEM_callocN(sizeof(void *) * (*totcol), "newmatar");
574                                         memcpy(mat, *matar, sizeof(void *) * (*totcol));
575                                         MEM_freeN(*matar);
576
577                                         *matar= mat;
578                                         test_object_materials(id);
579                                 }
580
581                                 /* decrease mat_nr index */
582                                 data_delete_material_index_id(id, index);
583                         }
584
585                         /* don't remove material slot, only clear it*/
586                         else
587                                 (*matar)[index]= NULL;
588                 }
589         }
590         
591         return ret;
592 }
593
594 Material *give_current_material(Object *ob, int act)
595 {
596         Material ***matarar, *ma;
597         short *totcolp;
598         
599         if(ob==NULL) return NULL;
600         
601         /* if object cannot have material, totcolp==NULL */
602         totcolp= give_totcolp(ob);
603         if(totcolp==NULL || ob->totcol==0) return NULL;
604         
605         if(act<0) {
606                 printf("no!\n");
607         }
608         
609         if(act>ob->totcol) act= ob->totcol;
610         else if(act<=0) act= 1;
611
612         if(ob->matbits && ob->matbits[act-1]) { /* in object */
613                 ma= ob->mat[act-1];
614         }
615         else {                                                          /* in data */
616
617                 /* check for inconsistency */
618                 if(*totcolp < ob->totcol)
619                         ob->totcol= *totcolp;
620                 if(act>ob->totcol) act= ob->totcol;
621
622                 matarar= give_matarar(ob);
623                 
624                 if(matarar && *matarar) ma= (*matarar)[act-1];
625                 else ma= NULL;
626                 
627         }
628         
629         return ma;
630 }
631
632 ID *material_from(Object *ob, int act)
633 {
634
635         if(ob==NULL) return NULL;
636
637         if(ob->totcol==0) return ob->data;
638         if(act==0) act= 1;
639
640         if(ob->matbits[act-1]) return (ID *)ob;
641         else return ob->data;
642 }
643
644 Material *give_node_material(Material *ma)
645 {
646         if(ma && ma->use_nodes && ma->nodetree) {
647                 bNode *node= nodeGetActiveID(ma->nodetree, ID_MA);
648
649                 if(node)
650                         return (Material *)node->id;
651         }
652
653         return NULL;
654 }
655
656 /* GS reads the memory pointed at in a specific ordering. There are,
657  * however two definitions for it. I have jotted them down here, both,
658  * but I think the first one is actually used. The thing is that
659  * big-endian systems might read this the wrong way round. OTOH, we
660  * constructed the IDs that are read out with this macro explicitly as
661  * well. I expect we'll sort it out soon... */
662
663 /* from blendef: */
664 #define GS(a)   (*((short *)(a)))
665
666 /* from misc_util: flip the bytes from x  */
667 /*  #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
668
669 void resize_object_material(Object *ob, const short totcol)
670 {
671         Material **newmatar;
672         char *newmatbits;
673
674         if(totcol==0) {
675                 if(ob->totcol) {
676                         MEM_freeN(ob->mat);
677                         MEM_freeN(ob->matbits);
678                         ob->mat= NULL;
679                         ob->matbits= NULL;
680                 }
681         }
682         else if(ob->totcol<totcol) {
683                 newmatar= MEM_callocN(sizeof(void *)*totcol, "newmatar");
684                 newmatbits= MEM_callocN(sizeof(char)*totcol, "newmatbits");
685                 if(ob->totcol) {
686                         memcpy(newmatar, ob->mat, sizeof(void *)*ob->totcol);
687                         memcpy(newmatbits, ob->matbits, sizeof(char)*ob->totcol);
688                         MEM_freeN(ob->mat);
689                         MEM_freeN(ob->matbits);
690                 }
691                 ob->mat= newmatar;
692                 ob->matbits= newmatbits;
693         }
694         ob->totcol= totcol;
695         if(ob->totcol && ob->actcol==0) ob->actcol= 1;
696         if(ob->actcol>ob->totcol) ob->actcol= ob->totcol;
697 }
698
699 void test_object_materials(ID *id)
700 {
701         /* make the ob mat-array same size as 'ob->data' mat-array */
702         Object *ob;
703         short *totcol;
704
705         if(id==NULL || (totcol=give_totcolp_id(id))==NULL) {
706                 return;
707         }
708
709         for(ob= G.main->object.first; ob; ob= ob->id.next) {
710                 if(ob->data==id) {
711                         resize_object_material(ob, *totcol);
712                 }
713         }
714 }
715
716 void assign_material(Object *ob, Material *ma, int act)
717 {
718         Material *mao, **matar, ***matarar;
719         char *matbits;
720         short *totcolp;
721
722         if(act>MAXMAT) return;
723         if(act<1) act= 1;
724         
725         /* prevent crashing when using accidentally */
726         BLI_assert(ob->id.lib == NULL);
727         if(ob->id.lib) return;
728         
729         /* test arraylens */
730         
731         totcolp= give_totcolp(ob);
732         matarar= give_matarar(ob);
733         
734         if(totcolp==NULL || matarar==NULL) return;
735         
736         if(act > *totcolp) {
737                 matar= MEM_callocN(sizeof(void *)*act, "matarray1");
738
739                 if(*totcolp) {
740                         memcpy(matar, *matarar, sizeof(void *)*(*totcolp));
741                         MEM_freeN(*matarar);
742                 }
743
744                 *matarar= matar;
745                 *totcolp= act;
746         }
747         
748         if(act > ob->totcol) {
749                 matar= MEM_callocN(sizeof(void *)*act, "matarray2");
750                 matbits= MEM_callocN(sizeof(char)*act, "matbits1");
751                 if( ob->totcol) {
752                         memcpy(matar, ob->mat, sizeof(void *)*( ob->totcol ));
753                         memcpy(matbits, ob->matbits, sizeof(char)*(*totcolp));
754                         MEM_freeN(ob->mat);
755                         MEM_freeN(ob->matbits);
756                 }
757                 ob->mat= matar;
758                 ob->matbits= matbits;
759                 ob->totcol= act;
760
761                 /* copy object/mesh linking, or assign based on userpref */
762                 if(ob->actcol)
763                         ob->matbits[act-1]= ob->matbits[ob->actcol-1];
764                 else
765                         ob->matbits[act-1]= (U.flag & USER_MAT_ON_OB)? 1: 0;
766         }
767         
768         /* do it */
769
770         if(ob->matbits[act-1]) {        /* in object */
771                 mao= ob->mat[act-1];
772                 if(mao) mao->id.us--;
773                 ob->mat[act-1]= ma;
774         }
775         else {  /* in data */
776                 mao= (*matarar)[act-1];
777                 if(mao) mao->id.us--;
778                 (*matarar)[act-1]= ma;
779         }
780
781         if(ma)
782                 id_us_plus((ID *)ma);
783         test_object_materials(ob->data);
784 }
785
786 /* XXX - this calls many more update calls per object then are needed, could be optimized */
787 void assign_matarar(struct Object *ob, struct Material ***matar, int totcol)
788 {
789         int i, actcol_orig= ob->actcol;
790
791         while(object_remove_material_slot(ob)) {};
792
793         /* now we have the right number of slots */
794         for(i=0; i<totcol; i++)
795                 assign_material(ob, (*matar)[i], i+1);
796
797         if(actcol_orig > ob->totcol)
798                 actcol_orig= ob->totcol;
799
800         ob->actcol= actcol_orig;
801 }
802
803
804 int find_material_index(Object *ob, Material *ma)
805 {
806         Material ***matarar;
807         short a, *totcolp;
808         
809         if(ma==NULL) return 0;
810         
811         totcolp= give_totcolp(ob);
812         matarar= give_matarar(ob);
813         
814         if(totcolp==NULL || matarar==NULL) return 0;
815         
816         for(a=0; a<*totcolp; a++)
817                 if((*matarar)[a]==ma)
818                    break;
819         if(a<*totcolp)
820                 return a+1;
821         return 0;          
822 }
823
824 int object_add_material_slot(Object *ob)
825 {
826         if(ob==NULL) return FALSE;
827         if(ob->totcol>=MAXMAT) return FALSE;
828         
829         assign_material(ob, NULL, ob->totcol+1);
830         ob->actcol= ob->totcol;
831         return TRUE;
832 }
833
834 static void do_init_render_material(Material *ma, int r_mode, float *amb)
835 {
836         MTex *mtex;
837         int a, needuv=0, needtang=0;
838         
839         if(ma->flarec==0) ma->flarec= 1;
840
841         /* add all texcoflags from mtex, texco and mapto were cleared in advance */
842         for(a=0; a<MAX_MTEX; a++) {
843                 
844                 /* separate tex switching */
845                 if(ma->septex & (1<<a)) continue;
846
847                 mtex= ma->mtex[a];
848                 if(mtex && mtex->tex && (mtex->tex->type | (mtex->tex->use_nodes && mtex->tex->nodetree) )) {
849                         
850                         ma->texco |= mtex->texco;
851                         ma->mapto |= mtex->mapto;
852
853                         /* always get derivatives for these textures */
854                         if ELEM3(mtex->tex->type, TEX_IMAGE, TEX_PLUGIN, TEX_ENVMAP) ma->texco |= TEXCO_OSA;
855                         else if(mtex->texflag & (MTEX_COMPAT_BUMP|MTEX_3TAP_BUMP|MTEX_5TAP_BUMP)) ma->texco |= TEXCO_OSA;
856                         
857                         if(ma->texco & (TEXCO_ORCO|TEXCO_REFL|TEXCO_NORM|TEXCO_STRAND|TEXCO_STRESS)) needuv= 1;
858                         else if(ma->texco & (TEXCO_GLOB|TEXCO_UV|TEXCO_OBJECT|TEXCO_SPEED)) needuv= 1;
859                         else if(ma->texco & (TEXCO_LAVECTOR|TEXCO_VIEW|TEXCO_STICKY)) needuv= 1;
860
861                         if((ma->mapto & MAP_NORM) && (mtex->normapspace == MTEX_NSPACE_TANGENT))
862                                 needtang= 1;
863                 }
864         }
865
866         if(needtang) ma->mode |= MA_NORMAP_TANG;
867         else ma->mode &= ~MA_NORMAP_TANG;
868         
869         if(ma->mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)) {
870                 needuv= 1;
871                 if(r_mode & R_OSA) ma->texco |= TEXCO_OSA;              /* for texfaces */
872         }
873         if(needuv) ma->texco |= NEED_UV;
874         
875         /* since the raytracer doesnt recalc O structs for each ray, we have to preset them all */
876         if(r_mode & R_RAYTRACE) {
877                 if((ma->mode & (MA_RAYMIRROR|MA_SHADOW_TRA)) || ((ma->mode & MA_TRANSP) && (ma->mode & MA_RAYTRANSP))) {
878                         ma->texco |= NEED_UV|TEXCO_ORCO|TEXCO_REFL|TEXCO_NORM;
879                         if(r_mode & R_OSA) ma->texco |= TEXCO_OSA;
880                 }
881         }
882         
883         if(amb) {
884                 ma->ambr= ma->amb*amb[0];
885                 ma->ambg= ma->amb*amb[1];
886                 ma->ambb= ma->amb*amb[2];
887         }       
888         /* will become or-ed result of all node modes */
889         ma->mode_l= ma->mode;
890         ma->mode_l &= ~MA_SHLESS;
891
892         if(ma->strand_surfnor > 0.0f)
893                 ma->mode_l |= MA_STR_SURFDIFF;
894
895         /* parses the geom+tex nodes */
896         if(ma->nodetree && ma->use_nodes)
897                 ntreeShaderGetTexcoMode(ma->nodetree, r_mode, &ma->texco, &ma->mode_l);
898 }
899
900 static void init_render_nodetree(bNodeTree *ntree, Material *basemat, int r_mode, float *amb)
901 {
902         bNode *node;
903         
904         for(node=ntree->nodes.first; node; node= node->next) {
905                 if(node->id) {
906                         if(GS(node->id->name)==ID_MA) {
907                                 Material *ma= (Material *)node->id;
908                                 if(ma!=basemat) {
909                                         do_init_render_material(ma, r_mode, amb);
910                                         basemat->texco |= ma->texco;
911                                         basemat->mode_l |= ma->mode_l & ~(MA_TRANSP|MA_ZTRANSP|MA_RAYTRANSP); 
912                                 }
913                         }
914                         else if(node->type==NODE_GROUP)
915                                 init_render_nodetree((bNodeTree *)node->id, basemat, r_mode, amb);
916                 }
917         }
918 }
919
920 void init_render_material(Material *mat, int r_mode, float *amb)
921 {
922         
923         do_init_render_material(mat, r_mode, amb);
924         
925         if(mat->nodetree && mat->use_nodes) {
926                 init_render_nodetree(mat->nodetree, mat, r_mode, amb);
927                 
928                 ntreeBeginExecTree(mat->nodetree); /* has internal flag to detect it only does it once */
929         }
930 }
931
932 void init_render_materials(Main *bmain, int r_mode, float *amb)
933 {
934         Material *ma;
935         
936         /* clear these flags before going over materials, to make sure they
937          * are cleared only once, otherwise node materials contained in other
938          * node materials can go wrong */
939         for(ma= bmain->mat.first; ma; ma= ma->id.next) {
940                 if(ma->id.us) {
941                         ma->texco= 0;
942                         ma->mapto= 0;
943                 }
944         }
945
946         /* two steps, first initialize, then or the flags for layers */
947         for(ma= bmain->mat.first; ma; ma= ma->id.next) {
948                 /* is_used flag comes back in convertblender.c */
949                 ma->flag &= ~MA_IS_USED;
950                 if(ma->id.us) 
951                         init_render_material(ma, r_mode, amb);
952         }
953         
954         do_init_render_material(&defmaterial, r_mode, amb);
955 }
956
957 /* only needed for nodes now */
958 void end_render_material(Material *mat)
959 {
960         if(mat && mat->nodetree && mat->use_nodes)
961                 ntreeEndExecTree(mat->nodetree); /* has internal flag to detect it only does it once */
962 }
963
964 void end_render_materials(Main *bmain)
965 {
966         Material *ma;
967         for(ma= bmain->mat.first; ma; ma= ma->id.next)
968                 if(ma->id.us) 
969                         end_render_material(ma);
970 }
971
972 static int material_in_nodetree(bNodeTree *ntree, Material *mat)
973 {
974         bNode *node;
975
976         for(node=ntree->nodes.first; node; node= node->next) {
977                 if(node->id && GS(node->id->name)==ID_MA) {
978                         if(node->id==(ID*)mat)
979                                 return 1;
980                 }
981                 else if(node->type==NODE_GROUP)
982                         if(material_in_nodetree((bNodeTree*)node->id, mat))
983                                 return 1;
984         }
985
986         return 0;
987 }
988
989 int material_in_material(Material *parmat, Material *mat)
990 {
991         if(parmat==mat)
992                 return 1;
993         else if(parmat->nodetree && parmat->use_nodes)
994                 return material_in_nodetree(parmat->nodetree, mat);
995         else
996                 return 0;
997 }
998         
999 /* ****************** */
1000
1001 static char colname_array[125][20]= {
1002 "Black","DarkRed","HalfRed","Red","Red",
1003 "DarkGreen","DarkOlive","Brown","Chocolate","OrangeRed",
1004 "HalfGreen","GreenOlive","DryOlive","Goldenrod","DarkOrange",
1005 "LightGreen","Chartreuse","YellowGreen","Yellow","Gold",
1006 "Green","LawnGreen","GreenYellow","LightOlive","Yellow",
1007 "DarkBlue","DarkPurple","HotPink","VioletPink","RedPink",
1008 "SlateGray","DarkGrey","PalePurple","IndianRed","Tomato",
1009 "SeaGreen","PaleGreen","GreenKhaki","LightBrown","LightSalmon",
1010 "SpringGreen","PaleGreen","MediumOlive","YellowBrown","LightGold",
1011 "LightGreen","LightGreen","LightGreen","GreenYellow","PaleYellow",
1012 "HalfBlue","DarkSky","HalfMagenta","VioletRed","DeepPink",
1013 "SteelBlue","SkyBlue","Orchid","LightHotPink","HotPink",
1014 "SeaGreen","SlateGray","MediumGrey","Burlywood","LightPink",
1015 "SpringGreen","Aquamarine","PaleGreen","Khaki","PaleOrange",
1016 "SpringGreen","SeaGreen","PaleGreen","PaleWhite","YellowWhite",
1017 "LightBlue","Purple","MediumOrchid","Magenta","Magenta",
1018 "RoyalBlue","SlateBlue","MediumOrchid","Orchid","Magenta",
1019 "DeepSkyBlue","LightSteelBlue","LightSkyBlue","Violet","LightPink",
1020 "Cyan","DarkTurquoise","SkyBlue","Grey","Snow",
1021 "Mint","Mint","Aquamarine","MintCream","Ivory",
1022 "Blue","Blue","DarkMagenta","DarkOrchid","Magenta",
1023 "SkyBlue","RoyalBlue","LightSlateBlue","MediumOrchid","Magenta",
1024 "DodgerBlue","SteelBlue","MediumPurple","PalePurple","Plum",
1025 "DeepSkyBlue","PaleBlue","LightSkyBlue","PalePurple","Thistle",
1026 "Cyan","ColdBlue","PaleTurquoise","GhostWhite","White"
1027 };
1028
1029 void automatname(Material *ma)
1030 {
1031         int nr, r, g, b;
1032         float ref;
1033         
1034         if(ma==NULL) return;
1035         if(ma->mode & MA_SHLESS) ref= 1.0;
1036         else ref= ma->ref;
1037         
1038         r= (int)(4.99f*(ref*ma->r));
1039         g= (int)(4.99f*(ref*ma->g));
1040         b= (int)(4.99f*(ref*ma->b));
1041         nr= r + 5*g + 25*b;
1042         if(nr>124) nr= 124;
1043         new_id(&G.main->mat, (ID *)ma, colname_array[nr]);
1044         
1045 }
1046
1047
1048 int object_remove_material_slot(Object *ob)
1049 {
1050         Material *mao, ***matarar;
1051         Object *obt;
1052         short *totcolp;
1053         int a, actcol;
1054         
1055         if(ob==NULL || ob->totcol==0) return FALSE;
1056         
1057         /* take a mesh/curve/mball as starting point, remove 1 index,
1058          * AND with all objects that share the ob->data
1059          * 
1060          * after that check indices in mesh/curve/mball!!!
1061          */
1062         
1063         totcolp= give_totcolp(ob);
1064         matarar= give_matarar(ob);
1065
1066         if(*matarar==NULL) return FALSE;
1067
1068         /* we delete the actcol */
1069         if(ob->totcol) {
1070                 mao= (*matarar)[ob->actcol-1];
1071                 if(mao) mao->id.us--;
1072         }
1073         
1074         for(a=ob->actcol; a<ob->totcol; a++)
1075                 (*matarar)[a-1]= (*matarar)[a];
1076         (*totcolp)--;
1077         
1078         if(*totcolp==0) {
1079                 MEM_freeN(*matarar);
1080                 *matarar= NULL;
1081         }
1082         
1083         actcol= ob->actcol;
1084         obt= G.main->object.first;
1085         while(obt) {
1086         
1087                 if(obt->data==ob->data) {
1088                         
1089                         /* WATCH IT: do not use actcol from ob or from obt (can become zero) */
1090                         mao= obt->mat[actcol-1];
1091                         if(mao) mao->id.us--;
1092                 
1093                         for(a=actcol; a<obt->totcol; a++) {
1094                                 obt->mat[a-1]= obt->mat[a];
1095                                 obt->matbits[a-1]= obt->matbits[a];
1096                         }
1097                         obt->totcol--;
1098                         if(obt->actcol > obt->totcol) obt->actcol= obt->totcol;
1099                         
1100                         if(obt->totcol==0) {
1101                                 MEM_freeN(obt->mat);
1102                                 MEM_freeN(obt->matbits);
1103                                 obt->mat= NULL;
1104                                 obt->matbits= NULL;
1105                         }
1106                 }
1107                 obt= obt->id.next;
1108         }
1109
1110         /* check indices from mesh */
1111         if (ELEM4(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT)) {
1112                 data_delete_material_index_id((ID *)ob->data, actcol-1);
1113                 freedisplist(&ob->disp);
1114         }
1115
1116         return TRUE;
1117 }
1118
1119
1120 /* r g b = current value, col = new value, fac==0 is no change */
1121 /* if g==NULL, it only does r channel */
1122 void ramp_blend(int type, float *r, float *g, float *b, float fac, float *col)
1123 {
1124         float tmp, facm= 1.0f-fac;
1125         
1126         switch (type) {
1127                 case MA_RAMP_BLEND:
1128                         *r = facm*(*r) + fac*col[0];
1129                         if(g) {
1130                                 *g = facm*(*g) + fac*col[1];
1131                                 *b = facm*(*b) + fac*col[2];
1132                         }
1133                                 break;
1134                 case MA_RAMP_ADD:
1135                         *r += fac*col[0];
1136                         if(g) {
1137                                 *g += fac*col[1];
1138                                 *b += fac*col[2];
1139                         }
1140                                 break;
1141                 case MA_RAMP_MULT:
1142                         *r *= (facm + fac*col[0]);
1143                         if(g) {
1144                                 *g *= (facm + fac*col[1]);
1145                                 *b *= (facm + fac*col[2]);
1146                         }
1147                                 break;
1148                 case MA_RAMP_SCREEN:
1149                         *r = 1.0f - (facm + fac*(1.0f - col[0])) * (1.0f - *r);
1150                         if(g) {
1151                                 *g = 1.0f - (facm + fac*(1.0f - col[1])) * (1.0f - *g);
1152                                 *b = 1.0f - (facm + fac*(1.0f - col[2])) * (1.0f - *b);
1153                         }
1154                                 break;
1155                 case MA_RAMP_OVERLAY:
1156                         if(*r < 0.5f)
1157                                 *r *= (facm + 2.0f*fac*col[0]);
1158                         else
1159                                 *r = 1.0f - (facm + 2.0f*fac*(1.0f - col[0])) * (1.0f - *r);
1160                         if(g) {
1161                                 if(*g < 0.5f)
1162                                         *g *= (facm + 2.0f*fac*col[1]);
1163                                 else
1164                                         *g = 1.0f - (facm + 2.0f*fac*(1.0f - col[1])) * (1.0f - *g);
1165                                 if(*b < 0.5f)
1166                                         *b *= (facm + 2.0f*fac*col[2]);
1167                                 else
1168                                         *b = 1.0f - (facm + 2.0f*fac*(1.0f - col[2])) * (1.0f - *b);
1169                         }
1170                                 break;
1171                 case MA_RAMP_SUB:
1172                         *r -= fac*col[0];
1173                         if(g) {
1174                                 *g -= fac*col[1];
1175                                 *b -= fac*col[2];
1176                         }
1177                                 break;
1178                 case MA_RAMP_DIV:
1179                         if(col[0]!=0.0f)
1180                                 *r = facm*(*r) + fac*(*r)/col[0];
1181                         if(g) {
1182                                 if(col[1]!=0.0f)
1183                                         *g = facm*(*g) + fac*(*g)/col[1];
1184                                 if(col[2]!=0.0f)
1185                                         *b = facm*(*b) + fac*(*b)/col[2];
1186                         }
1187                                 break;
1188                 case MA_RAMP_DIFF:
1189                         *r = facm*(*r) + fac*fabsf(*r-col[0]);
1190                         if(g) {
1191                                 *g = facm*(*g) + fac*fabsf(*g-col[1]);
1192                                 *b = facm*(*b) + fac*fabsf(*b-col[2]);
1193                         }
1194                                 break;
1195                 case MA_RAMP_DARK:
1196                         tmp=col[0]+((1-col[0])*facm); 
1197                         if(tmp < *r) *r= tmp; 
1198                         if(g) { 
1199                                 tmp=col[1]+((1-col[1])*facm); 
1200                                 if(tmp < *g) *g= tmp; 
1201                                 tmp=col[2]+((1-col[2])*facm); 
1202                                 if(tmp < *b) *b= tmp; 
1203                         } 
1204                                 break; 
1205                 case MA_RAMP_LIGHT:
1206                         tmp= fac*col[0];
1207                         if(tmp > *r) *r= tmp; 
1208                                 if(g) {
1209                                         tmp= fac*col[1];
1210                                         if(tmp > *g) *g= tmp; 
1211                                         tmp= fac*col[2];
1212                                         if(tmp > *b) *b= tmp; 
1213                                 }
1214                                         break;  
1215                 case MA_RAMP_DODGE:                     
1216                         
1217                                 
1218                         if(*r !=0.0f){
1219                                 tmp = 1.0f - fac*col[0];
1220                                 if(tmp <= 0.0f)
1221                                         *r = 1.0f;
1222                                 else if ((tmp = (*r) / tmp)> 1.0f)
1223                                         *r = 1.0f;
1224                                 else 
1225                                         *r = tmp;
1226                         }
1227                         if(g) {
1228                                 if(*g !=0.0f){
1229                                         tmp = 1.0f - fac*col[1];
1230                                         if(tmp <= 0.0f )
1231                                                 *g = 1.0f;
1232                                         else if ((tmp = (*g) / tmp) > 1.0f )
1233                                                 *g = 1.0f;
1234                                         else
1235                                                 *g = tmp;
1236                                 }
1237                                 if(*b !=0.0f){
1238                                         tmp = 1.0f - fac*col[2];
1239                                         if(tmp <= 0.0f)
1240                                                 *b = 1.0f;
1241                                         else if ((tmp = (*b) / tmp) > 1.0f )
1242                                                 *b = 1.0f;
1243                                         else
1244                                                 *b = tmp;
1245                                 }
1246
1247                         }
1248                                 break;  
1249                 case MA_RAMP_BURN:
1250                         
1251                         tmp = facm + fac*col[0];
1252                         
1253                         if(tmp <= 0.0f)
1254                                 *r = 0.0f;
1255                         else if (( tmp = (1.0f - (1.0f - (*r)) / tmp )) < 0.0f)
1256                                         *r = 0.0f;
1257                         else if (tmp > 1.0f)
1258                                 *r=1.0f;
1259                         else 
1260                                 *r = tmp; 
1261
1262                         if(g) {
1263                                 tmp = facm + fac*col[1];
1264                                 if(tmp <= 0.0f)
1265                                         *g = 0.0f;
1266                                 else if (( tmp = (1.0f - (1.0f - (*g)) / tmp )) < 0.0f )
1267                                                 *g = 0.0f;
1268                                 else if(tmp >1.0f)
1269                                         *g=1.0f;
1270                                 else
1271                                         *g = tmp;
1272                                         
1273                                         tmp = facm + fac*col[2];
1274                                         if(tmp <= 0.0f)
1275                                         *b = 0.0f;
1276                                 else if (( tmp = (1.0f - (1.0f - (*b)) / tmp )) < 0.0f  )
1277                                                 *b = 0.0f;
1278                                 else if(tmp >1.0f)
1279                                         *b= 1.0f;
1280                                 else
1281                                         *b = tmp;
1282                         }
1283                                 break;
1284                 case MA_RAMP_HUE:               
1285                         if(g){
1286                                 float rH,rS,rV;
1287                                 float colH,colS,colV; 
1288                                 float tmpr,tmpg,tmpb;
1289                                 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1290                                 if(colS!=0 ){
1291                                         rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1292                                         hsv_to_rgb( colH , rS, rV, &tmpr, &tmpg, &tmpb);
1293                                         *r = facm*(*r) + fac*tmpr;  
1294                                         *g = facm*(*g) + fac*tmpg; 
1295                                         *b = facm*(*b) + fac*tmpb;
1296                                 }
1297                         }
1298                                 break;
1299                 case MA_RAMP_SAT:               
1300                         if(g){
1301                                 float rH,rS,rV;
1302                                 float colH,colS,colV;
1303                                 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1304                                 if(rS!=0){
1305                                         rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1306                                         hsv_to_rgb( rH, (facm*rS +fac*colS), rV, r, g, b);
1307                                 }
1308                         }
1309                                 break;
1310                 case MA_RAMP_VAL:               
1311                         if(g){
1312                                 float rH,rS,rV;
1313                                 float colH,colS,colV;
1314                                 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1315                                 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1316                                 hsv_to_rgb( rH, rS, (facm*rV +fac*colV), r, g, b);
1317                         }
1318                                 break;
1319                 case MA_RAMP_COLOR:             
1320                         if(g){
1321                                 float rH,rS,rV;
1322                                 float colH,colS,colV;
1323                                 float tmpr,tmpg,tmpb;
1324                                 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1325                                 if(colS!=0){
1326                                         rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1327                                         hsv_to_rgb( colH, colS, rV, &tmpr, &tmpg, &tmpb);
1328                                         *r = facm*(*r) + fac*tmpr;
1329                                         *g = facm*(*g) + fac*tmpg;
1330                                         *b = facm*(*b) + fac*tmpb;
1331                                 }
1332                         }
1333                                 break;
1334                 case MA_RAMP_SOFT: 
1335                         if (g){ 
1336                                 float scr, scg, scb; 
1337
1338                                 /* first calculate non-fac based Screen mix */ 
1339                                 scr = 1.0f - (1.0f - col[0]) * (1.0f - *r); 
1340                                 scg = 1.0f - (1.0f - col[1]) * (1.0f - *g); 
1341                                 scb = 1.0f - (1.0f - col[2]) * (1.0f - *b); 
1342
1343                                 *r = facm*(*r) + fac*(((1.0f - *r) * col[0] * (*r)) + (*r * scr)); 
1344                                 *g = facm*(*g) + fac*(((1.0f - *g) * col[1] * (*g)) + (*g * scg)); 
1345                                 *b = facm*(*b) + fac*(((1.0f - *b) * col[2] * (*b)) + (*b * scb)); 
1346                         } 
1347                                 break; 
1348                 case MA_RAMP_LINEAR: 
1349                         if (col[0] > 0.5f)  
1350                                 *r = *r + fac*(2.0f*(col[0]-0.5f)); 
1351                         else  
1352                                 *r = *r + fac*(2.0f*(col[0]) - 1.0f); 
1353                         if (g){ 
1354                                 if (col[1] > 0.5f)  
1355                                         *g = *g + fac*(2.0f*(col[1]-0.5f)); 
1356                                 else  
1357                                         *g = *g + fac*(2.0f*(col[1]) -1.0f); 
1358                                 if (col[2] > 0.5f)  
1359                                         *b = *b + fac*(2.0f*(col[2]-0.5f)); 
1360                                 else  
1361                                         *b = *b + fac*(2.0f*(col[2]) - 1.0f); 
1362                         } 
1363                                 break; 
1364         }       
1365 }
1366
1367 /* copy/paste buffer, if we had a propper py api that would be better */
1368 static Material matcopybuf;
1369 static short matcopied= 0;
1370
1371 void clear_matcopybuf(void)
1372 {
1373         memset(&matcopybuf, 0, sizeof(Material));
1374         matcopied= 0;
1375 }
1376
1377 void free_matcopybuf(void)
1378 {
1379         int a;
1380
1381         for(a=0; a<MAX_MTEX; a++) {
1382                 if(matcopybuf.mtex[a]) {
1383                         MEM_freeN(matcopybuf.mtex[a]);
1384                         matcopybuf.mtex[a]= NULL;
1385                 }
1386         }
1387
1388         if(matcopybuf.ramp_col) MEM_freeN(matcopybuf.ramp_col);
1389         if(matcopybuf.ramp_spec) MEM_freeN(matcopybuf.ramp_spec);
1390
1391         matcopybuf.ramp_col= NULL;
1392         matcopybuf.ramp_spec= NULL;
1393
1394         if(matcopybuf.nodetree) {
1395                 ntreeFreeTree(matcopybuf.nodetree);
1396                 MEM_freeN(matcopybuf.nodetree);
1397                 matcopybuf.nodetree= NULL;
1398         }
1399
1400         matcopied= 0;
1401 }
1402
1403 void copy_matcopybuf(Material *ma)
1404 {
1405         int a;
1406         MTex *mtex;
1407
1408         if(matcopied)
1409                 free_matcopybuf();
1410
1411         memcpy(&matcopybuf, ma, sizeof(Material));
1412         if(matcopybuf.ramp_col) matcopybuf.ramp_col= MEM_dupallocN(matcopybuf.ramp_col);
1413         if(matcopybuf.ramp_spec) matcopybuf.ramp_spec= MEM_dupallocN(matcopybuf.ramp_spec);
1414
1415         for(a=0; a<MAX_MTEX; a++) {
1416                 mtex= matcopybuf.mtex[a];
1417                 if(mtex) {
1418                         matcopybuf.mtex[a]= MEM_dupallocN(mtex);
1419                 }
1420         }
1421         matcopybuf.nodetree= ntreeCopyTree(ma->nodetree);
1422         matcopybuf.preview= NULL;
1423         matcopybuf.gpumaterial.first= matcopybuf.gpumaterial.last= NULL;
1424         matcopied= 1;
1425 }
1426
1427 void paste_matcopybuf(Material *ma)
1428 {
1429         int a;
1430         MTex *mtex;
1431         ID id;
1432
1433         if(matcopied==0)
1434                 return;
1435         /* free current mat */
1436         if(ma->ramp_col) MEM_freeN(ma->ramp_col);
1437         if(ma->ramp_spec) MEM_freeN(ma->ramp_spec);
1438         for(a=0; a<MAX_MTEX; a++) {
1439                 mtex= ma->mtex[a];
1440                 if(mtex && mtex->tex) mtex->tex->id.us--;
1441                 if(mtex) MEM_freeN(mtex);
1442         }
1443
1444         if(ma->nodetree) {
1445                 ntreeFreeTree(ma->nodetree);
1446                 MEM_freeN(ma->nodetree);
1447         }
1448
1449         GPU_material_free(ma);
1450
1451         id= (ma->id);
1452         memcpy(ma, &matcopybuf, sizeof(Material));
1453         (ma->id)= id;
1454
1455         if(matcopybuf.ramp_col) ma->ramp_col= MEM_dupallocN(matcopybuf.ramp_col);
1456         if(matcopybuf.ramp_spec) ma->ramp_spec= MEM_dupallocN(matcopybuf.ramp_spec);
1457
1458         for(a=0; a<MAX_MTEX; a++) {
1459                 mtex= ma->mtex[a];
1460                 if(mtex) {
1461                         ma->mtex[a]= MEM_dupallocN(mtex);
1462                         if(mtex->tex) id_us_plus((ID *)mtex->tex);
1463                 }
1464         }
1465
1466         ma->nodetree= ntreeCopyTree(matcopybuf.nodetree);
1467 }