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