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