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