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