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