2ad3da9f3a0885db8b82d090c39faefcc84797d9
[blender.git] / source / blender / blenkernel / intern / material.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/material.c
29  *  \ingroup bke
30  */
31
32
33 #include <string.h>
34 #include <math.h>
35 #include <stddef.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "DNA_curve_types.h"
40 #include "DNA_material_types.h"
41 #include "DNA_mesh_types.h"
42 #include "DNA_meshdata_types.h"
43 #include "DNA_customdata_types.h"
44 #include "DNA_ID.h"
45 #include "DNA_meta_types.h"
46 #include "DNA_node_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
49
50 #include "BLI_math.h"           
51 #include "BLI_listbase.h"               
52 #include "BLI_utildefines.h"
53 #include "BLI_bpath.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_image.h"
60 #include "BKE_library.h"
61 #include "BKE_main.h"
62 #include "BKE_material.h"
63 #include "BKE_mesh.h"
64 #include "BKE_node.h"
65 #include "BKE_curve.h"
66
67 #include "GPU_material.h"
68
69 /* used in UI and render */
70 Material defmaterial;
71
72 /* called on startup, creator.c */
73 void init_def_material(void)
74 {
75         init_material(&defmaterial);
76 }
77
78 /* not material itself */
79 void free_material(Material *ma)
80 {
81         MTex *mtex;
82         int a;
83         
84         for(a=0; a<MAX_MTEX; a++) {
85                 mtex= ma->mtex[a];
86                 if(mtex && mtex->tex) mtex->tex->id.us--;
87                 if(mtex) MEM_freeN(mtex);
88         }
89         
90         if(ma->ramp_col) MEM_freeN(ma->ramp_col);
91         if(ma->ramp_spec) MEM_freeN(ma->ramp_spec);
92         
93         BKE_free_animdata((ID *)ma);
94         
95         if(ma->preview)
96                 BKE_previewimg_free(&ma->preview);
97         BKE_icon_delete((struct ID*)ma);
98         ma->id.icon_id = 0;
99         
100         /* is no lib link block, but material extension */
101         if(ma->nodetree) {
102                 ntreeFreeTree(ma->nodetree);
103                 MEM_freeN(ma->nodetree);
104         }
105
106         if(ma->gpumaterial.first)
107                 GPU_material_free(ma);
108 }
109
110 void init_material(Material *ma)
111 {
112         ma->r= ma->g= ma->b= ma->ref= 0.8;
113         ma->specr= ma->specg= ma->specb= 1.0;
114         ma->mirr= ma->mirg= ma->mirb= 1.0;
115         ma->spectra= 1.0;
116         ma->amb= 1.0;
117         ma->alpha= 1.0;
118         ma->spec= ma->hasize= 0.5;
119         ma->har= 50;
120         ma->starc= ma->ringc= 4;
121         ma->linec= 12;
122         ma->flarec= 1;
123         ma->flaresize= ma->subsize= 1.0;
124         ma->flareboost= 1;
125         ma->seed2= 6;
126         ma->friction= 0.5;
127         ma->refrac= 4.0;
128         ma->roughness= 0.5;
129         ma->param[0]= 0.5;
130         ma->param[1]= 0.1;
131         ma->param[2]= 0.5;
132         ma->param[3]= 0.1;
133         ma->rms= 0.1;
134         ma->darkness= 1.0;      
135         
136         ma->strand_sta= ma->strand_end= 1.0f;
137         
138         ma->ang= 1.0;
139         ma->ray_depth= 2;
140         ma->ray_depth_tra= 2;
141         ma->fresnel_mir= 0.0;
142         ma->fresnel_tra= 0.0;
143         ma->fresnel_tra_i= 1.25;
144         ma->fresnel_mir_i= 1.25;
145         ma->tx_limit= 0.0;
146         ma->tx_falloff= 1.0;
147         ma->shad_alpha= 1.0f;
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->game.flag = GEMAT_BACKCULL;
193         ma->game.alpha_blend=0;
194         ma->game.face_orientation=0;
195         
196         ma->mode= MA_TRACEBLE|MA_SHADBUF|MA_SHADOW|MA_RAYBIAS|MA_TANGENT_STR|MA_ZTRANSP;
197         ma->shade_flag= MA_APPROX_OCCLUSION;
198         ma->preview = NULL;
199 }
200
201 Material *add_material(const char *name)
202 {
203         Material *ma;
204
205         ma= alloc_libblock(&G.main->mat, ID_MA, name);
206         
207         init_material(ma);
208         
209         return ma;      
210 }
211
212 /* XXX keep synced with next function */
213 Material *copy_material(Material *ma)
214 {
215         Material *man;
216         int a;
217         
218         man= copy_libblock(ma);
219         
220         id_lib_extern((ID *)man->group);
221         
222         for(a=0; a<MAX_MTEX; a++) {
223                 if(ma->mtex[a]) {
224                         man->mtex[a]= MEM_mallocN(sizeof(MTex), "copymaterial");
225                         memcpy(man->mtex[a], ma->mtex[a], sizeof(MTex));
226                         id_us_plus((ID *)man->mtex[a]->tex);
227                 }
228         }
229         
230         if(ma->ramp_col) man->ramp_col= MEM_dupallocN(ma->ramp_col);
231         if(ma->ramp_spec) man->ramp_spec= MEM_dupallocN(ma->ramp_spec);
232         
233         if (ma->preview) man->preview = BKE_previewimg_copy(ma->preview);
234
235         if(ma->nodetree) {
236                 man->nodetree= ntreeCopyTree(ma->nodetree);     /* 0 == full new tree */
237         }
238
239         man->gpumaterial.first= man->gpumaterial.last= NULL;
240         
241         return man;
242 }
243
244 /* XXX (see above) material copy without adding to main dbase */
245 Material *localize_material(Material *ma)
246 {
247         Material *man;
248         int a;
249         
250         man= copy_libblock(ma);
251         BLI_remlink(&G.main->mat, man);
252
253         /* no increment for texture ID users, in previewrender.c it prevents decrement */
254         for(a=0; a<MAX_MTEX; a++) {
255                 if(ma->mtex[a]) {
256                         man->mtex[a]= MEM_mallocN(sizeof(MTex), "copymaterial");
257                         memcpy(man->mtex[a], ma->mtex[a], sizeof(MTex));
258                 }
259         }
260         
261         if(ma->ramp_col) man->ramp_col= MEM_dupallocN(ma->ramp_col);
262         if(ma->ramp_spec) man->ramp_spec= MEM_dupallocN(ma->ramp_spec);
263         
264         man->preview = NULL;
265         
266         if(ma->nodetree)
267                 man->nodetree= ntreeLocalize(ma->nodetree);
268         
269         man->gpumaterial.first= man->gpumaterial.last= NULL;
270         
271         return man;
272 }
273
274 static void extern_local_material(Material *ma)
275 {
276         int i;
277         for(i=0; i < MAX_MTEX; i++) {
278                 if(ma->mtex[i]) id_lib_extern((ID *)ma->mtex[i]->tex);
279         }
280 }
281
282 void make_local_material(Material *ma)
283 {
284         Main *bmain= G.main;
285         Object *ob;
286         Mesh *me;
287         Curve *cu;
288         MetaBall *mb;
289         int a, is_local= FALSE, is_lib= FALSE;
290
291         /* - only lib users: do nothing
292                 * - only local users: set flag
293                 * - mixed: make copy
294                 */
295         
296         if(ma->id.lib==NULL) return;
297
298         /* One local user; set flag and return. */
299         if(ma->id.us==1) {
300                 id_clear_lib_data(bmain, &ma->id);
301                 extern_local_material(ma);
302                 return;
303         }
304
305         /* Check which other IDs reference this one to determine if it's used by
306            lib or local */
307         /* test objects */
308         ob= bmain->object.first;
309         while(ob) {
310                 if(ob->mat) {
311                         for(a=0; a<ob->totcol; a++) {
312                                 if(ob->mat[a]==ma) {
313                                         if(ob->id.lib) is_lib= TRUE;
314                                         else is_local= TRUE;
315                                 }
316                         }
317                 }
318                 ob= ob->id.next;
319         }
320         /* test meshes */
321         me= bmain->mesh.first;
322         while(me) {
323                 if(me->mat) {
324                         for(a=0; a<me->totcol; a++) {
325                                 if(me->mat[a]==ma) {
326                                         if(me->id.lib) is_lib= TRUE;
327                                         else is_local= TRUE;
328                                 }
329                         }
330                 }
331                 me= me->id.next;
332         }
333         /* test curves */
334         cu= bmain->curve.first;
335         while(cu) {
336                 if(cu->mat) {
337                         for(a=0; a<cu->totcol; a++) {
338                                 if(cu->mat[a]==ma) {
339                                         if(cu->id.lib) is_lib= TRUE;
340                                         else is_local= TRUE;
341                                 }
342                         }
343                 }
344                 cu= cu->id.next;
345         }
346         /* test mballs */
347         mb= bmain->mball.first;
348         while(mb) {
349                 if(mb->mat) {
350                         for(a=0; a<mb->totcol; a++) {
351                                 if(mb->mat[a]==ma) {
352                                         if(mb->id.lib) is_lib= TRUE;
353                                         else is_local= TRUE;
354                                 }
355                         }
356                 }
357                 mb= mb->id.next;
358         }
359
360         /* Only local users. */
361         if(is_local && is_lib == FALSE) {
362                 id_clear_lib_data(bmain, &ma->id);
363                 extern_local_material(ma);
364         }
365         /* Both user and local, so copy. */
366         else if(is_local && is_lib) {
367                 char *bpath_user_data[2]= {bmain->name, ma->id.lib->filepath};
368                 Material *man= copy_material(ma);
369
370                 man->id.us= 0;
371
372                 /* Remap paths of new ID using old library as base. */
373                 bpath_traverse_id(bmain, &man->id, bpath_relocate_visitor, 0, bpath_user_data);
374
375                 /* do objects */
376                 ob= bmain->object.first;
377                 while(ob) {
378                         if(ob->mat) {
379                                 for(a=0; a<ob->totcol; a++) {
380                                         if(ob->mat[a]==ma) {
381                                                 if(ob->id.lib==NULL) {
382                                                         ob->mat[a]= man;
383                                                         man->id.us++;
384                                                         ma->id.us--;
385                                                 }
386                                         }
387                                 }
388                         }
389                         ob= ob->id.next;
390                 }
391                 /* do meshes */
392                 me= bmain->mesh.first;
393                 while(me) {
394                         if(me->mat) {
395                                 for(a=0; a<me->totcol; a++) {
396                                         if(me->mat[a]==ma) {
397                                                 if(me->id.lib==NULL) {
398                                                         me->mat[a]= man;
399                                                         man->id.us++;
400                                                         ma->id.us--;
401                                                 }
402                                         }
403                                 }
404                         }
405                         me= me->id.next;
406                 }
407                 /* do curves */
408                 cu= bmain->curve.first;
409                 while(cu) {
410                         if(cu->mat) {
411                                 for(a=0; a<cu->totcol; a++) {
412                                         if(cu->mat[a]==ma) {
413                                                 if(cu->id.lib==NULL) {
414                                                         cu->mat[a]= man;
415                                                         man->id.us++;
416                                                         ma->id.us--;
417                                                 }
418                                         }
419                                 }
420                         }
421                         cu= cu->id.next;
422                 }
423                 /* do mballs */
424                 mb= bmain->mball.first;
425                 while(mb) {
426                         if(mb->mat) {
427                                 for(a=0; a<mb->totcol; a++) {
428                                         if(mb->mat[a]==ma) {
429                                                 if(mb->id.lib==NULL) {
430                                                         mb->mat[a]= man;
431                                                         man->id.us++;
432                                                         ma->id.us--;
433                                                 }
434                                         }
435                                 }
436                         }
437                         mb= mb->id.next;
438                 }
439         }
440 }
441
442 /* for curve, mball, mesh types */
443 void extern_local_matarar(struct Material **matar, short totcol)
444 {
445         short i;
446         for(i= 0; i < totcol; i++) {
447                 id_lib_extern((ID *)matar[i]);
448         }
449 }
450
451 Material ***give_matarar(Object *ob)
452 {
453         Mesh *me;
454         Curve *cu;
455         MetaBall *mb;
456         
457         if(ob->type==OB_MESH) {
458                 me= ob->data;
459                 return &(me->mat);
460         }
461         else if ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF) {
462                 cu= ob->data;
463                 return &(cu->mat);
464         }
465         else if(ob->type==OB_MBALL) {
466                 mb= ob->data;
467                 return &(mb->mat);
468         }
469         return NULL;
470 }
471
472 short *give_totcolp(Object *ob)
473 {
474         Mesh *me;
475         Curve *cu;
476         MetaBall *mb;
477         
478         if(ob->type==OB_MESH) {
479                 me= ob->data;
480                 return &(me->totcol);
481         }
482         else if ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF) {
483                 cu= ob->data;
484                 return &(cu->totcol);
485         }
486         else if(ob->type==OB_MBALL) {
487                 mb= ob->data;
488                 return &(mb->totcol);
489         }
490         return NULL;
491 }
492
493 /* same as above but for ID's */
494 Material ***give_matarar_id(ID *id)
495 {
496         switch(GS(id->name)) {
497         case ID_ME:
498                 return &(((Mesh *)id)->mat);
499                 break;
500         case ID_CU:
501                 return &(((Curve *)id)->mat);
502                 break;
503         case ID_MB:
504                 return &(((MetaBall *)id)->mat);
505                 break;
506         }
507         return NULL;
508 }
509
510 short *give_totcolp_id(ID *id)
511 {
512         switch(GS(id->name)) {
513         case ID_ME:
514                 return &(((Mesh *)id)->totcol);
515                 break;
516         case ID_CU:
517                 return &(((Curve *)id)->totcol);
518                 break;
519         case ID_MB:
520                 return &(((MetaBall *)id)->totcol);
521                 break;
522         }
523         return NULL;
524 }
525
526 static void data_delete_material_index_id(ID *id, short index)
527 {
528         switch(GS(id->name)) {
529         case ID_ME:
530                 mesh_delete_material_index((Mesh *)id, index);
531                 break;
532         case ID_CU:
533                 curve_delete_material_index((Curve *)id, index);
534                 break;
535         case ID_MB:
536                 /* meta-elems dont have materials atm */
537                 break;
538         }
539 }
540
541 void material_append_id(ID *id, Material *ma)
542 {
543         Material ***matar;
544         if((matar= give_matarar_id(id))) {
545                 short *totcol= give_totcolp_id(id);
546                 Material **mat= MEM_callocN(sizeof(void *) * ((*totcol) + 1), "newmatar");
547                 if(*totcol) memcpy(mat, *matar, sizeof(void *) * (*totcol));
548                 if(*matar) MEM_freeN(*matar);
549
550                 *matar= mat;
551                 (*matar)[(*totcol)++]= ma;
552
553                 id_us_plus((ID *)ma);
554                 test_object_materials(id);
555         }
556 }
557
558 Material *material_pop_id(ID *id, int index_i, int remove_material_slot)
559 {
560         short index= (short)index_i;
561         Material *ret= NULL;
562         Material ***matar;
563         if((matar= give_matarar_id(id))) {
564                 short *totcol= give_totcolp_id(id);
565                 if(index >= 0 && index < (*totcol)) {
566                         ret= (*matar)[index];
567                         id_us_min((ID *)ret);
568
569                         if (remove_material_slot) {
570                                 if(*totcol <= 1) {
571                                         *totcol= 0;
572                                         MEM_freeN(*matar);
573                                         *matar= NULL;
574                                 }
575                                 else {
576                                         Material **mat;
577                                         if(index + 1 != (*totcol))
578                                                 memmove((*matar)+index, (*matar)+(index+1), sizeof(void *) * ((*totcol) - (index + 1)));
579
580                                         (*totcol)--;
581                                         
582                                         mat= MEM_callocN(sizeof(void *) * (*totcol), "newmatar");
583                                         memcpy(mat, *matar, sizeof(void *) * (*totcol));
584                                         MEM_freeN(*matar);
585
586                                         *matar= mat;
587                                         test_object_materials(id);
588                                 }
589
590                                 /* decrease mat_nr index */
591                                 data_delete_material_index_id(id, index);
592                         }
593
594                         /* don't remove material slot, only clear it*/
595                         else
596                                 (*matar)[index]= NULL;
597                 }
598         }
599         
600         return ret;
601 }
602
603 Material *give_current_material(Object *ob, short act)
604 {
605         Material ***matarar, *ma;
606         short *totcolp;
607         
608         if(ob==NULL) return NULL;
609         
610         /* if object cannot have material, totcolp==NULL */
611         totcolp= give_totcolp(ob);
612         if(totcolp==NULL || ob->totcol==0) return NULL;
613         
614         if(act<0) {
615                 printf("no!\n");
616         }
617         
618         if(act>ob->totcol) act= ob->totcol;
619         else if(act<=0) act= 1;
620
621         if(ob->matbits && ob->matbits[act-1]) { /* in object */
622                 ma= ob->mat[act-1];
623         }
624         else {                                                          /* in data */
625
626                 /* check for inconsistency */
627                 if(*totcolp < ob->totcol)
628                         ob->totcol= *totcolp;
629                 if(act>ob->totcol) act= ob->totcol;
630
631                 matarar= give_matarar(ob);
632                 
633                 if(matarar && *matarar) ma= (*matarar)[act-1];
634                 else ma= NULL;
635                 
636         }
637         
638         return ma;
639 }
640
641 ID *material_from(Object *ob, short act)
642 {
643
644         if(ob==NULL) return NULL;
645
646         if(ob->totcol==0) return ob->data;
647         if(act==0) act= 1;
648
649         if(ob->matbits[act-1]) return (ID *)ob;
650         else return ob->data;
651 }
652
653 Material *give_node_material(Material *ma)
654 {
655         if(ma && ma->use_nodes && ma->nodetree) {
656                 bNode *node= nodeGetActiveID(ma->nodetree, ID_MA);
657
658                 if(node)
659                         return (Material *)node->id;
660         }
661
662         return NULL;
663 }
664
665 /* GS reads the memory pointed at in a specific ordering. There are,
666  * however two definitions for it. I have jotted them down here, both,
667  * but I think the first one is actually used. The thing is that
668  * big-endian systems might read this the wrong way round. OTOH, we
669  * constructed the IDs that are read out with this macro explicitly as
670  * well. I expect we'll sort it out soon... */
671
672 /* from blendef: */
673 #define GS(a)   (*((short *)(a)))
674
675 /* from misc_util: flip the bytes from x  */
676 /*  #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
677
678 void resize_object_material(Object *ob, const short totcol)
679 {
680         Material **newmatar;
681         char *newmatbits;
682
683         if(totcol==0) {
684                 if(ob->totcol) {
685                         MEM_freeN(ob->mat);
686                         MEM_freeN(ob->matbits);
687                         ob->mat= NULL;
688                         ob->matbits= NULL;
689                 }
690         }
691         else if(ob->totcol<totcol) {
692                 newmatar= MEM_callocN(sizeof(void *)*totcol, "newmatar");
693                 newmatbits= MEM_callocN(sizeof(char)*totcol, "newmatbits");
694                 if(ob->totcol) {
695                         memcpy(newmatar, ob->mat, sizeof(void *)*ob->totcol);
696                         memcpy(newmatbits, ob->matbits, sizeof(char)*ob->totcol);
697                         MEM_freeN(ob->mat);
698                         MEM_freeN(ob->matbits);
699                 }
700                 ob->mat= newmatar;
701                 ob->matbits= newmatbits;
702         }
703         ob->totcol= totcol;
704         if(ob->totcol && ob->actcol==0) ob->actcol= 1;
705         if(ob->actcol>ob->totcol) ob->actcol= ob->totcol;
706 }
707
708 void test_object_materials(ID *id)
709 {
710         /* make the ob mat-array same size as 'ob->data' mat-array */
711         Object *ob;
712         short *totcol;
713
714         if(id==NULL || (totcol=give_totcolp_id(id))==NULL) {
715                 return;
716         }
717
718         for(ob= G.main->object.first; ob; ob= ob->id.next) {
719                 if(ob->data==id) {
720                         resize_object_material(ob, *totcol);
721                 }
722         }
723 }
724
725 void assign_material_id(ID *id, Material *ma, short act)
726 {
727         Material *mao, **matar, ***matarar;
728         short *totcolp;
729
730         if(act>MAXMAT) return;
731         if(act<1) act= 1;
732
733         /* prevent crashing when using accidentally */
734         BLI_assert(id->lib == NULL);
735         if(id->lib) return;
736
737         /* test arraylens */
738
739         totcolp= give_totcolp_id(id);
740         matarar= give_matarar_id(id);
741
742         if(totcolp==NULL || matarar==NULL) return;
743
744         if(act > *totcolp) {
745                 matar= MEM_callocN(sizeof(void *)*act, "matarray1");
746
747                 if(*totcolp) {
748                         memcpy(matar, *matarar, sizeof(void *)*(*totcolp));
749                         MEM_freeN(*matarar);
750                 }
751
752                 *matarar= matar;
753                 *totcolp= act;
754         }
755
756         /* in data */
757         mao= (*matarar)[act-1];
758         if(mao) mao->id.us--;
759         (*matarar)[act-1]= ma;
760
761         if(ma)
762                 id_us_plus((ID *)ma);
763
764         test_object_materials(id);
765 }
766
767 void assign_material(Object *ob, Material *ma, short act)
768 {
769         Material *mao, **matar, ***matarar;
770         char *matbits;
771         short *totcolp;
772
773         if(act>MAXMAT) return;
774         if(act<1) act= 1;
775         
776         /* prevent crashing when using accidentally */
777         BLI_assert(ob->id.lib == NULL);
778         if(ob->id.lib) return;
779         
780         /* test arraylens */
781         
782         totcolp= give_totcolp(ob);
783         matarar= give_matarar(ob);
784         
785         if(totcolp==NULL || matarar==NULL) return;
786         
787         if(act > *totcolp) {
788                 matar= MEM_callocN(sizeof(void *)*act, "matarray1");
789
790                 if(*totcolp) {
791                         memcpy(matar, *matarar, sizeof(void *)*(*totcolp));
792                         MEM_freeN(*matarar);
793                 }
794
795                 *matarar= matar;
796                 *totcolp= act;
797         }
798         
799         if(act > ob->totcol) {
800                 matar= MEM_callocN(sizeof(void *)*act, "matarray2");
801                 matbits= MEM_callocN(sizeof(char)*act, "matbits1");
802                 if( ob->totcol) {
803                         memcpy(matar, ob->mat, sizeof(void *)*( ob->totcol ));
804                         memcpy(matbits, ob->matbits, sizeof(char)*(*totcolp));
805                         MEM_freeN(ob->mat);
806                         MEM_freeN(ob->matbits);
807                 }
808                 ob->mat= matar;
809                 ob->matbits= matbits;
810                 ob->totcol= act;
811
812                 /* copy object/mesh linking, or assign based on userpref */
813                 if(ob->actcol)
814                         ob->matbits[act-1]= ob->matbits[ob->actcol-1];
815                 else
816                         ob->matbits[act-1]= (U.flag & USER_MAT_ON_OB)? 1: 0;
817         }
818         
819         /* do it */
820
821         if(ob->matbits[act-1]) {        /* in object */
822                 mao= ob->mat[act-1];
823                 if(mao) mao->id.us--;
824                 ob->mat[act-1]= ma;
825         }
826         else {  /* in data */
827                 mao= (*matarar)[act-1];
828                 if(mao) mao->id.us--;
829                 (*matarar)[act-1]= ma;
830         }
831
832         if(ma)
833                 id_us_plus((ID *)ma);
834         test_object_materials(ob->data);
835 }
836
837 /* XXX - this calls many more update calls per object then are needed, could be optimized */
838 void assign_matarar(struct Object *ob, struct Material ***matar, short totcol)
839 {
840         int actcol_orig= ob->actcol;
841         short i;
842
843         while(object_remove_material_slot(ob)) {};
844
845         /* now we have the right number of slots */
846         for(i=0; i<totcol; i++)
847                 assign_material(ob, (*matar)[i], i+1);
848
849         if(actcol_orig > ob->totcol)
850                 actcol_orig= ob->totcol;
851
852         ob->actcol= actcol_orig;
853 }
854
855
856 short find_material_index(Object *ob, Material *ma)
857 {
858         Material ***matarar;
859         short a, *totcolp;
860         
861         if(ma==NULL) return 0;
862         
863         totcolp= give_totcolp(ob);
864         matarar= give_matarar(ob);
865         
866         if(totcolp==NULL || matarar==NULL) return 0;
867         
868         for(a=0; a<*totcolp; a++)
869                 if((*matarar)[a]==ma)
870                    break;
871         if(a<*totcolp)
872                 return a+1;
873         return 0;          
874 }
875
876 int object_add_material_slot(Object *ob)
877 {
878         if(ob==NULL) return FALSE;
879         if(ob->totcol>=MAXMAT) return FALSE;
880         
881         assign_material(ob, NULL, ob->totcol+1);
882         ob->actcol= ob->totcol;
883         return TRUE;
884 }
885
886 static void do_init_render_material(Material *ma, int r_mode, float *amb)
887 {
888         MTex *mtex;
889         int a, needuv=0, needtang=0;
890         
891         if(ma->flarec==0) ma->flarec= 1;
892
893         /* add all texcoflags from mtex, texco and mapto were cleared in advance */
894         for(a=0; a<MAX_MTEX; a++) {
895                 
896                 /* separate tex switching */
897                 if(ma->septex & (1<<a)) continue;
898
899                 mtex= ma->mtex[a];
900                 if(mtex && mtex->tex && (mtex->tex->type | (mtex->tex->use_nodes && mtex->tex->nodetree) )) {
901                         
902                         ma->texco |= mtex->texco;
903                         ma->mapto |= mtex->mapto;
904
905                         /* always get derivatives for these textures */
906                         if ELEM3(mtex->tex->type, TEX_IMAGE, TEX_PLUGIN, TEX_ENVMAP) ma->texco |= TEXCO_OSA;
907                         else if(mtex->texflag & (MTEX_COMPAT_BUMP|MTEX_3TAP_BUMP|MTEX_5TAP_BUMP)) ma->texco |= TEXCO_OSA;
908                         
909                         if(ma->texco & (TEXCO_ORCO|TEXCO_REFL|TEXCO_NORM|TEXCO_STRAND|TEXCO_STRESS)) needuv= 1;
910                         else if(ma->texco & (TEXCO_GLOB|TEXCO_UV|TEXCO_OBJECT|TEXCO_SPEED)) needuv= 1;
911                         else if(ma->texco & (TEXCO_LAVECTOR|TEXCO_VIEW|TEXCO_STICKY)) needuv= 1;
912
913                         if((ma->mapto & MAP_NORM) && (mtex->normapspace == MTEX_NSPACE_TANGENT))
914                                 needtang= 1;
915                 }
916         }
917
918         if(needtang) ma->mode |= MA_NORMAP_TANG;
919         else ma->mode &= ~MA_NORMAP_TANG;
920         
921         if(ma->mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)) {
922                 needuv= 1;
923                 if(r_mode & R_OSA) ma->texco |= TEXCO_OSA;              /* for texfaces */
924         }
925         if(needuv) ma->texco |= NEED_UV;
926         
927         /* since the raytracer doesnt recalc O structs for each ray, we have to preset them all */
928         if(r_mode & R_RAYTRACE) {
929                 if((ma->mode & (MA_RAYMIRROR|MA_SHADOW_TRA)) || ((ma->mode & MA_TRANSP) && (ma->mode & MA_RAYTRANSP))) {
930                         ma->texco |= NEED_UV|TEXCO_ORCO|TEXCO_REFL|TEXCO_NORM;
931                         if(r_mode & R_OSA) ma->texco |= TEXCO_OSA;
932                 }
933         }
934         
935         if(amb) {
936                 ma->ambr= ma->amb*amb[0];
937                 ma->ambg= ma->amb*amb[1];
938                 ma->ambb= ma->amb*amb[2];
939         }       
940         /* will become or-ed result of all node modes */
941         ma->mode_l= ma->mode;
942         ma->mode_l &= ~MA_SHLESS;
943
944         if(ma->strand_surfnor > 0.0f)
945                 ma->mode_l |= MA_STR_SURFDIFF;
946
947         /* parses the geom+tex nodes */
948         if(ma->nodetree && ma->use_nodes)
949                 ntreeShaderGetTexcoMode(ma->nodetree, r_mode, &ma->texco, &ma->mode_l);
950 }
951
952 static void init_render_nodetree(bNodeTree *ntree, Material *basemat, int r_mode, float *amb)
953 {
954         bNode *node;
955         
956         for(node=ntree->nodes.first; node; node= node->next) {
957                 if(node->id) {
958                         if(GS(node->id->name)==ID_MA) {
959                                 Material *ma= (Material *)node->id;
960                                 if(ma!=basemat) {
961                                         do_init_render_material(ma, r_mode, amb);
962                                         basemat->texco |= ma->texco;
963                                         basemat->mode_l |= ma->mode_l & ~(MA_TRANSP|MA_ZTRANSP|MA_RAYTRANSP); 
964                                 }
965                         }
966                         else if(node->type==NODE_GROUP)
967                                 init_render_nodetree((bNodeTree *)node->id, basemat, r_mode, amb);
968                 }
969         }
970 }
971
972 void init_render_material(Material *mat, int r_mode, float *amb)
973 {
974         
975         do_init_render_material(mat, r_mode, amb);
976         
977         if(mat->nodetree && mat->use_nodes) {
978                 init_render_nodetree(mat->nodetree, mat, r_mode, amb);
979                 
980                 if (!mat->nodetree->execdata)
981                         mat->nodetree->execdata = ntreeShaderBeginExecTree(mat->nodetree, 1);
982         }
983 }
984
985 void init_render_materials(Main *bmain, int r_mode, float *amb)
986 {
987         Material *ma;
988         
989         /* clear these flags before going over materials, to make sure they
990          * are cleared only once, otherwise node materials contained in other
991          * node materials can go wrong */
992         for(ma= bmain->mat.first; ma; ma= ma->id.next) {
993                 if(ma->id.us) {
994                         ma->texco= 0;
995                         ma->mapto= 0;
996                 }
997         }
998
999         /* two steps, first initialize, then or the flags for layers */
1000         for(ma= bmain->mat.first; ma; ma= ma->id.next) {
1001                 /* is_used flag comes back in convertblender.c */
1002                 ma->flag &= ~MA_IS_USED;
1003                 if(ma->id.us) 
1004                         init_render_material(ma, r_mode, amb);
1005         }
1006         
1007         do_init_render_material(&defmaterial, r_mode, amb);
1008 }
1009
1010 /* only needed for nodes now */
1011 void end_render_material(Material *mat)
1012 {
1013         if(mat && mat->nodetree && mat->use_nodes) {
1014                 if (mat->nodetree->execdata)
1015                         ntreeShaderEndExecTree(mat->nodetree->execdata, 1);
1016         }
1017 }
1018
1019 void end_render_materials(Main *bmain)
1020 {
1021         Material *ma;
1022         for(ma= bmain->mat.first; ma; ma= ma->id.next)
1023                 if(ma->id.us) 
1024                         end_render_material(ma);
1025 }
1026
1027 static int material_in_nodetree(bNodeTree *ntree, Material *mat)
1028 {
1029         bNode *node;
1030
1031         for(node=ntree->nodes.first; node; node= node->next) {
1032                 if(node->id && GS(node->id->name)==ID_MA) {
1033                         if(node->id==(ID*)mat)
1034                                 return 1;
1035                 }
1036                 else if(node->type==NODE_GROUP)
1037                         if(material_in_nodetree((bNodeTree*)node->id, mat))
1038                                 return 1;
1039         }
1040
1041         return 0;
1042 }
1043
1044 int material_in_material(Material *parmat, Material *mat)
1045 {
1046         if(parmat==mat)
1047                 return 1;
1048         else if(parmat->nodetree && parmat->use_nodes)
1049                 return material_in_nodetree(parmat->nodetree, mat);
1050         else
1051                 return 0;
1052 }
1053         
1054 /* ****************** */
1055
1056 static char colname_array[125][20]= {
1057 "Black","DarkRed","HalfRed","Red","Red",
1058 "DarkGreen","DarkOlive","Brown","Chocolate","OrangeRed",
1059 "HalfGreen","GreenOlive","DryOlive","Goldenrod","DarkOrange",
1060 "LightGreen","Chartreuse","YellowGreen","Yellow","Gold",
1061 "Green","LawnGreen","GreenYellow","LightOlive","Yellow",
1062 "DarkBlue","DarkPurple","HotPink","VioletPink","RedPink",
1063 "SlateGray","DarkGrey","PalePurple","IndianRed","Tomato",
1064 "SeaGreen","PaleGreen","GreenKhaki","LightBrown","LightSalmon",
1065 "SpringGreen","PaleGreen","MediumOlive","YellowBrown","LightGold",
1066 "LightGreen","LightGreen","LightGreen","GreenYellow","PaleYellow",
1067 "HalfBlue","DarkSky","HalfMagenta","VioletRed","DeepPink",
1068 "SteelBlue","SkyBlue","Orchid","LightHotPink","HotPink",
1069 "SeaGreen","SlateGray","MediumGrey","Burlywood","LightPink",
1070 "SpringGreen","Aquamarine","PaleGreen","Khaki","PaleOrange",
1071 "SpringGreen","SeaGreen","PaleGreen","PaleWhite","YellowWhite",
1072 "LightBlue","Purple","MediumOrchid","Magenta","Magenta",
1073 "RoyalBlue","SlateBlue","MediumOrchid","Orchid","Magenta",
1074 "DeepSkyBlue","LightSteelBlue","LightSkyBlue","Violet","LightPink",
1075 "Cyan","DarkTurquoise","SkyBlue","Grey","Snow",
1076 "Mint","Mint","Aquamarine","MintCream","Ivory",
1077 "Blue","Blue","DarkMagenta","DarkOrchid","Magenta",
1078 "SkyBlue","RoyalBlue","LightSlateBlue","MediumOrchid","Magenta",
1079 "DodgerBlue","SteelBlue","MediumPurple","PalePurple","Plum",
1080 "DeepSkyBlue","PaleBlue","LightSkyBlue","PalePurple","Thistle",
1081 "Cyan","ColdBlue","PaleTurquoise","GhostWhite","White"
1082 };
1083
1084 void automatname(Material *ma)
1085 {
1086         int nr, r, g, b;
1087         float ref;
1088         
1089         if(ma==NULL) return;
1090         if(ma->mode & MA_SHLESS) ref= 1.0;
1091         else ref= ma->ref;
1092         
1093         r= (int)(4.99f*(ref*ma->r));
1094         g= (int)(4.99f*(ref*ma->g));
1095         b= (int)(4.99f*(ref*ma->b));
1096         nr= r + 5*g + 25*b;
1097         if(nr>124) nr= 124;
1098         new_id(&G.main->mat, (ID *)ma, colname_array[nr]);
1099         
1100 }
1101
1102
1103 int object_remove_material_slot(Object *ob)
1104 {
1105         Material *mao, ***matarar;
1106         Object *obt;
1107         short *totcolp;
1108         short a, actcol;
1109         
1110         if (ob==NULL || ob->totcol==0) {
1111                 return FALSE;
1112         }
1113
1114         /* this should never happen and used to crash */
1115         if (ob->actcol <= 0) {
1116                 printf("%s: invalid material index %d, report a bug!\n", __func__, ob->actcol);
1117                 BLI_assert(0);
1118                 return FALSE;
1119         }
1120
1121         /* take a mesh/curve/mball as starting point, remove 1 index,
1122          * AND with all objects that share the ob->data
1123          * 
1124          * after that check indices in mesh/curve/mball!!!
1125          */
1126         
1127         totcolp= give_totcolp(ob);
1128         matarar= give_matarar(ob);
1129
1130         if(*matarar==NULL) return FALSE;
1131
1132         /* we delete the actcol */
1133         mao= (*matarar)[ob->actcol-1];
1134         if(mao) mao->id.us--;
1135         
1136         for(a=ob->actcol; a<ob->totcol; a++)
1137                 (*matarar)[a-1]= (*matarar)[a];
1138         (*totcolp)--;
1139         
1140         if(*totcolp==0) {
1141                 MEM_freeN(*matarar);
1142                 *matarar= NULL;
1143         }
1144         
1145         actcol= ob->actcol;
1146         obt= G.main->object.first;
1147         while(obt) {
1148         
1149                 if(obt->data==ob->data) {
1150                         
1151                         /* WATCH IT: do not use actcol from ob or from obt (can become zero) */
1152                         mao= obt->mat[actcol-1];
1153                         if(mao) mao->id.us--;
1154                 
1155                         for(a=actcol; a<obt->totcol; a++) {
1156                                 obt->mat[a-1]= obt->mat[a];
1157                                 obt->matbits[a-1]= obt->matbits[a];
1158                         }
1159                         obt->totcol--;
1160                         if(obt->actcol > obt->totcol) obt->actcol= obt->totcol;
1161                         
1162                         if(obt->totcol==0) {
1163                                 MEM_freeN(obt->mat);
1164                                 MEM_freeN(obt->matbits);
1165                                 obt->mat= NULL;
1166                                 obt->matbits= NULL;
1167                         }
1168                 }
1169                 obt= obt->id.next;
1170         }
1171
1172         /* check indices from mesh */
1173         if (ELEM4(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT)) {
1174                 data_delete_material_index_id((ID *)ob->data, actcol-1);
1175                 freedisplist(&ob->disp);
1176         }
1177
1178         return TRUE;
1179 }
1180
1181
1182 /* r g b = current value, col = new value, fac==0 is no change */
1183 /* if g==NULL, it only does r channel */
1184 void ramp_blend(int type, float *r, float *g, float *b, float fac, const float col[3])
1185 {
1186         float tmp, facm= 1.0f-fac;
1187         
1188         switch (type) {
1189                 case MA_RAMP_BLEND:
1190                         *r = facm*(*r) + fac*col[0];
1191                         if(g) {
1192                                 *g = facm*(*g) + fac*col[1];
1193                                 *b = facm*(*b) + fac*col[2];
1194                         }
1195                                 break;
1196                 case MA_RAMP_ADD:
1197                         *r += fac*col[0];
1198                         if(g) {
1199                                 *g += fac*col[1];
1200                                 *b += fac*col[2];
1201                         }
1202                                 break;
1203                 case MA_RAMP_MULT:
1204                         *r *= (facm + fac*col[0]);
1205                         if(g) {
1206                                 *g *= (facm + fac*col[1]);
1207                                 *b *= (facm + fac*col[2]);
1208                         }
1209                                 break;
1210                 case MA_RAMP_SCREEN:
1211                         *r = 1.0f - (facm + fac*(1.0f - col[0])) * (1.0f - *r);
1212                         if(g) {
1213                                 *g = 1.0f - (facm + fac*(1.0f - col[1])) * (1.0f - *g);
1214                                 *b = 1.0f - (facm + fac*(1.0f - col[2])) * (1.0f - *b);
1215                         }
1216                                 break;
1217                 case MA_RAMP_OVERLAY:
1218                         if(*r < 0.5f)
1219                                 *r *= (facm + 2.0f*fac*col[0]);
1220                         else
1221                                 *r = 1.0f - (facm + 2.0f*fac*(1.0f - col[0])) * (1.0f - *r);
1222                         if(g) {
1223                                 if(*g < 0.5f)
1224                                         *g *= (facm + 2.0f*fac*col[1]);
1225                                 else
1226                                         *g = 1.0f - (facm + 2.0f*fac*(1.0f - col[1])) * (1.0f - *g);
1227                                 if(*b < 0.5f)
1228                                         *b *= (facm + 2.0f*fac*col[2]);
1229                                 else
1230                                         *b = 1.0f - (facm + 2.0f*fac*(1.0f - col[2])) * (1.0f - *b);
1231                         }
1232                                 break;
1233                 case MA_RAMP_SUB:
1234                         *r -= fac*col[0];
1235                         if(g) {
1236                                 *g -= fac*col[1];
1237                                 *b -= fac*col[2];
1238                         }
1239                                 break;
1240                 case MA_RAMP_DIV:
1241                         if(col[0]!=0.0f)
1242                                 *r = facm*(*r) + fac*(*r)/col[0];
1243                         if(g) {
1244                                 if(col[1]!=0.0f)
1245                                         *g = facm*(*g) + fac*(*g)/col[1];
1246                                 if(col[2]!=0.0f)
1247                                         *b = facm*(*b) + fac*(*b)/col[2];
1248                         }
1249                                 break;
1250                 case MA_RAMP_DIFF:
1251                         *r = facm*(*r) + fac*fabsf(*r-col[0]);
1252                         if(g) {
1253                                 *g = facm*(*g) + fac*fabsf(*g-col[1]);
1254                                 *b = facm*(*b) + fac*fabsf(*b-col[2]);
1255                         }
1256                                 break;
1257                 case MA_RAMP_DARK:
1258                         tmp=col[0]+((1-col[0])*facm); 
1259                         if(tmp < *r) *r= tmp; 
1260                         if(g) { 
1261                                 tmp=col[1]+((1-col[1])*facm); 
1262                                 if(tmp < *g) *g= tmp; 
1263                                 tmp=col[2]+((1-col[2])*facm); 
1264                                 if(tmp < *b) *b= tmp; 
1265                         } 
1266                                 break; 
1267                 case MA_RAMP_LIGHT:
1268                         tmp= fac*col[0];
1269                         if(tmp > *r) *r= tmp; 
1270                                 if(g) {
1271                                         tmp= fac*col[1];
1272                                         if(tmp > *g) *g= tmp; 
1273                                         tmp= fac*col[2];
1274                                         if(tmp > *b) *b= tmp; 
1275                                 }
1276                                         break;  
1277                 case MA_RAMP_DODGE:                     
1278                         
1279                                 
1280                         if(*r !=0.0f){
1281                                 tmp = 1.0f - fac*col[0];
1282                                 if(tmp <= 0.0f)
1283                                         *r = 1.0f;
1284                                 else if ((tmp = (*r) / tmp)> 1.0f)
1285                                         *r = 1.0f;
1286                                 else 
1287                                         *r = tmp;
1288                         }
1289                         if(g) {
1290                                 if(*g !=0.0f){
1291                                         tmp = 1.0f - fac*col[1];
1292                                         if(tmp <= 0.0f )
1293                                                 *g = 1.0f;
1294                                         else if ((tmp = (*g) / tmp) > 1.0f )
1295                                                 *g = 1.0f;
1296                                         else
1297                                                 *g = tmp;
1298                                 }
1299                                 if(*b !=0.0f){
1300                                         tmp = 1.0f - fac*col[2];
1301                                         if(tmp <= 0.0f)
1302                                                 *b = 1.0f;
1303                                         else if ((tmp = (*b) / tmp) > 1.0f )
1304                                                 *b = 1.0f;
1305                                         else
1306                                                 *b = tmp;
1307                                 }
1308
1309                         }
1310                                 break;  
1311                 case MA_RAMP_BURN:
1312                         
1313                         tmp = facm + fac*col[0];
1314                         
1315                         if(tmp <= 0.0f)
1316                                 *r = 0.0f;
1317                         else if (( tmp = (1.0f - (1.0f - (*r)) / tmp )) < 0.0f)
1318                                         *r = 0.0f;
1319                         else if (tmp > 1.0f)
1320                                 *r=1.0f;
1321                         else 
1322                                 *r = tmp; 
1323
1324                         if(g) {
1325                                 tmp = facm + fac*col[1];
1326                                 if(tmp <= 0.0f)
1327                                         *g = 0.0f;
1328                                 else if (( tmp = (1.0f - (1.0f - (*g)) / tmp )) < 0.0f )
1329                                                 *g = 0.0f;
1330                                 else if(tmp >1.0f)
1331                                         *g=1.0f;
1332                                 else
1333                                         *g = tmp;
1334
1335                                         tmp = facm + fac*col[2];
1336                                         if(tmp <= 0.0f)
1337                                         *b = 0.0f;
1338                                 else if (( tmp = (1.0f - (1.0f - (*b)) / tmp )) < 0.0f  )
1339                                                 *b = 0.0f;
1340                                 else if(tmp >1.0f)
1341                                         *b= 1.0f;
1342                                 else
1343                                         *b = tmp;
1344                         }
1345                                 break;
1346                 case MA_RAMP_HUE:               
1347                         if(g){
1348                                 float rH,rS,rV;
1349                                 float colH,colS,colV; 
1350                                 float tmpr,tmpg,tmpb;
1351                                 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1352                                 if(colS!=0 ){
1353                                         rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1354                                         hsv_to_rgb( colH , rS, rV, &tmpr, &tmpg, &tmpb);
1355                                         *r = facm*(*r) + fac*tmpr;  
1356                                         *g = facm*(*g) + fac*tmpg; 
1357                                         *b = facm*(*b) + fac*tmpb;
1358                                 }
1359                         }
1360                                 break;
1361                 case MA_RAMP_SAT:               
1362                         if(g){
1363                                 float rH,rS,rV;
1364                                 float colH,colS,colV;
1365                                 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1366                                 if(rS!=0){
1367                                         rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1368                                         hsv_to_rgb( rH, (facm*rS +fac*colS), rV, r, g, b);
1369                                 }
1370                         }
1371                                 break;
1372                 case MA_RAMP_VAL:               
1373                         if(g){
1374                                 float rH,rS,rV;
1375                                 float colH,colS,colV;
1376                                 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1377                                 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1378                                 hsv_to_rgb( rH, rS, (facm*rV +fac*colV), r, g, b);
1379                         }
1380                                 break;
1381                 case MA_RAMP_COLOR:             
1382                         if(g){
1383                                 float rH,rS,rV;
1384                                 float colH,colS,colV;
1385                                 float tmpr,tmpg,tmpb;
1386                                 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1387                                 if(colS!=0){
1388                                         rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1389                                         hsv_to_rgb( colH, colS, rV, &tmpr, &tmpg, &tmpb);
1390                                         *r = facm*(*r) + fac*tmpr;
1391                                         *g = facm*(*g) + fac*tmpg;
1392                                         *b = facm*(*b) + fac*tmpb;
1393                                 }
1394                         }
1395                                 break;
1396                 case MA_RAMP_SOFT: 
1397                         if (g){ 
1398                                 float scr, scg, scb; 
1399
1400                                 /* first calculate non-fac based Screen mix */ 
1401                                 scr = 1.0f - (1.0f - col[0]) * (1.0f - *r); 
1402                                 scg = 1.0f - (1.0f - col[1]) * (1.0f - *g); 
1403                                 scb = 1.0f - (1.0f - col[2]) * (1.0f - *b); 
1404
1405                                 *r = facm*(*r) + fac*(((1.0f - *r) * col[0] * (*r)) + (*r * scr)); 
1406                                 *g = facm*(*g) + fac*(((1.0f - *g) * col[1] * (*g)) + (*g * scg)); 
1407                                 *b = facm*(*b) + fac*(((1.0f - *b) * col[2] * (*b)) + (*b * scb)); 
1408                         } 
1409                                 break; 
1410                 case MA_RAMP_LINEAR: 
1411                         if (col[0] > 0.5f)  
1412                                 *r = *r + fac*(2.0f*(col[0]-0.5f)); 
1413                         else  
1414                                 *r = *r + fac*(2.0f*(col[0]) - 1.0f); 
1415                         if (g){ 
1416                                 if (col[1] > 0.5f)  
1417                                         *g = *g + fac*(2.0f*(col[1]-0.5f)); 
1418                                 else  
1419                                         *g = *g + fac*(2.0f*(col[1]) -1.0f); 
1420                                 if (col[2] > 0.5f)  
1421                                         *b = *b + fac*(2.0f*(col[2]-0.5f)); 
1422                                 else  
1423                                         *b = *b + fac*(2.0f*(col[2]) - 1.0f); 
1424                         } 
1425                                 break; 
1426         }       
1427 }
1428
1429 /* copy/paste buffer, if we had a propper py api that would be better */
1430 static Material matcopybuf;
1431 static short matcopied= 0;
1432
1433 void clear_matcopybuf(void)
1434 {
1435         memset(&matcopybuf, 0, sizeof(Material));
1436         matcopied= 0;
1437 }
1438
1439 void free_matcopybuf(void)
1440 {
1441         int a;
1442
1443         for(a=0; a<MAX_MTEX; a++) {
1444                 if(matcopybuf.mtex[a]) {
1445                         MEM_freeN(matcopybuf.mtex[a]);
1446                         matcopybuf.mtex[a]= NULL;
1447                 }
1448         }
1449
1450         if(matcopybuf.ramp_col) MEM_freeN(matcopybuf.ramp_col);
1451         if(matcopybuf.ramp_spec) MEM_freeN(matcopybuf.ramp_spec);
1452
1453         matcopybuf.ramp_col= NULL;
1454         matcopybuf.ramp_spec= NULL;
1455
1456         if(matcopybuf.nodetree) {
1457                 ntreeFreeTree(matcopybuf.nodetree);
1458                 MEM_freeN(matcopybuf.nodetree);
1459                 matcopybuf.nodetree= NULL;
1460         }
1461
1462         matcopied= 0;
1463 }
1464
1465 void copy_matcopybuf(Material *ma)
1466 {
1467         int a;
1468         MTex *mtex;
1469
1470         if(matcopied)
1471                 free_matcopybuf();
1472
1473         memcpy(&matcopybuf, ma, sizeof(Material));
1474         if(matcopybuf.ramp_col) matcopybuf.ramp_col= MEM_dupallocN(matcopybuf.ramp_col);
1475         if(matcopybuf.ramp_spec) matcopybuf.ramp_spec= MEM_dupallocN(matcopybuf.ramp_spec);
1476
1477         for(a=0; a<MAX_MTEX; a++) {
1478                 mtex= matcopybuf.mtex[a];
1479                 if(mtex) {
1480                         matcopybuf.mtex[a]= MEM_dupallocN(mtex);
1481                 }
1482         }
1483         matcopybuf.nodetree= ntreeCopyTree(ma->nodetree);
1484         matcopybuf.preview= NULL;
1485         matcopybuf.gpumaterial.first= matcopybuf.gpumaterial.last= NULL;
1486         matcopied= 1;
1487 }
1488
1489 void paste_matcopybuf(Material *ma)
1490 {
1491         int a;
1492         MTex *mtex;
1493         ID id;
1494
1495         if(matcopied==0)
1496                 return;
1497         /* free current mat */
1498         if(ma->ramp_col) MEM_freeN(ma->ramp_col);
1499         if(ma->ramp_spec) MEM_freeN(ma->ramp_spec);
1500         for(a=0; a<MAX_MTEX; a++) {
1501                 mtex= ma->mtex[a];
1502                 if(mtex && mtex->tex) mtex->tex->id.us--;
1503                 if(mtex) MEM_freeN(mtex);
1504         }
1505
1506         if(ma->nodetree) {
1507                 ntreeFreeTree(ma->nodetree);
1508                 MEM_freeN(ma->nodetree);
1509         }
1510
1511         GPU_material_free(ma);
1512
1513         id= (ma->id);
1514         memcpy(ma, &matcopybuf, sizeof(Material));
1515         (ma->id)= id;
1516
1517         if(matcopybuf.ramp_col) ma->ramp_col= MEM_dupallocN(matcopybuf.ramp_col);
1518         if(matcopybuf.ramp_spec) ma->ramp_spec= MEM_dupallocN(matcopybuf.ramp_spec);
1519
1520         for(a=0; a<MAX_MTEX; a++) {
1521                 mtex= ma->mtex[a];
1522                 if(mtex) {
1523                         ma->mtex[a]= MEM_dupallocN(mtex);
1524                         if(mtex->tex) id_us_plus((ID *)mtex->tex);
1525                 }
1526         }
1527
1528         ma->nodetree= ntreeCopyTree(matcopybuf.nodetree);
1529 }
1530
1531
1532 /*********************** texface to material convert functions **********************/
1533 /* encode all the TF information into a single int */
1534 static int encode_tfaceflag(MTFace *tf, int convertall)
1535 {
1536         /* calculate the flag */
1537         int flag = tf->mode;
1538
1539         /* options that change the material offline render */   
1540         if (!convertall) {
1541                 flag &= ~TF_OBCOL;
1542         }
1543
1544         /* clean flags that are not being converted */
1545         flag &= ~TF_TEX;
1546         flag &= ~TF_SHAREDVERT;
1547         flag &= ~TF_SHAREDCOL;
1548         flag &= ~TF_CONVERTED;
1549
1550         /* light tface flag is ignored in GLSL mode */
1551         flag &= ~TF_LIGHT;
1552         
1553         /* 15 is how big the flag can be - hardcoded here and in decode_tfaceflag() */
1554         flag |= tf->transp << 15;
1555         
1556         /* increase 1 so flag 0 is different than no flag yet */
1557         return flag + 1;
1558 }
1559
1560 /* set the material options based in the tface flag */
1561 static void decode_tfaceflag(Material *ma, int flag, int convertall)
1562 {
1563         int alphablend; 
1564         GameSettings *game= &ma->game;
1565
1566         /* flag is shifted in 1 to make 0 != no flag yet (see encode_tfaceflag) */
1567         flag -= 1;
1568
1569         alphablend = flag >> 15; //encoded in the encode_tfaceflag function
1570         (*game).flag = 0;
1571         
1572         /* General Material Options */
1573         if ((flag & TF_DYNAMIC)==0) (*game).flag        |= GEMAT_NOPHYSICS;
1574         
1575         /* Material Offline Rendering Properties */
1576         if (convertall) {
1577                 if (flag & TF_OBCOL) ma->shade_flag |= MA_OBCOLOR;
1578         }
1579         
1580         /* Special Face Properties */
1581         if ((flag & TF_TWOSIDE)==0) (*game).flag |= GEMAT_BACKCULL;
1582         if (flag & TF_INVISIBLE)(*game).flag |= GEMAT_INVISIBLE;
1583         if (flag & TF_BMFONT) (*game).flag |= GEMAT_TEXT;
1584         
1585         /* Face Orientation */
1586         if (flag & TF_BILLBOARD) (*game).face_orientation |= GEMAT_HALO;
1587         else if (flag & TF_BILLBOARD2) (*game).face_orientation |= GEMAT_BILLBOARD;
1588         else if (flag & TF_SHADOW) (*game).face_orientation |= GEMAT_SHADOW;
1589         
1590         /* Alpha Blend */
1591         if (flag & TF_ALPHASORT && ELEM(alphablend, TF_ALPHA, TF_ADD)) (*game).alpha_blend = GEMAT_ALPHA_SORT;
1592         else if (alphablend & TF_ALPHA) (*game).alpha_blend = GEMAT_ALPHA;
1593         else if (alphablend & TF_ADD) (*game).alpha_blend = GEMAT_ADD;
1594         else if (alphablend & TF_CLIP) (*game).alpha_blend = GEMAT_CLIP;
1595 }
1596
1597 /* boolean check to see if the mesh needs a material */
1598 static int check_tfaceneedmaterial(int flag)
1599 {
1600         // check if the flags we have are not deprecated != than default material options
1601         // also if only flags are visible and collision see if all objects using this mesh have this option in physics
1602
1603         /* flag is shifted in 1 to make 0 != no flag yet (see encode_tfaceflag) */
1604         flag -=1;
1605
1606         // deprecated flags
1607         flag &= ~TF_OBCOL;
1608         flag &= ~TF_SHAREDVERT;
1609         flag &= ~TF_SHAREDCOL;
1610
1611         /* light tface flag is ignored in GLSL mode */
1612         flag &= ~TF_LIGHT;
1613         
1614         // automatic detected if tex image has alpha
1615         flag &= ~(TF_ALPHA << 15);
1616         // automatic detected if using texture
1617         flag &= ~TF_TEX;
1618
1619         // settings for the default NoMaterial
1620         if (flag == TF_DYNAMIC)
1621                 return 0;
1622
1623         else
1624                 return 1;
1625 }
1626
1627 /* return number of digits of an integer */
1628 // XXX to be optmized or replaced by an equivalent blender internal function
1629 static int integer_getdigits(int number)
1630 {
1631         int i=0;
1632         if (number == 0) return 1;
1633
1634         while (number != 0){
1635                 number = (int)(number/10);
1636                 i++;
1637         }
1638         return i;
1639 }
1640
1641 static void calculate_tface_materialname(char *matname, char *newname, int flag)
1642 {
1643         // if flag has only light and collision and material matches those values
1644         // you can do strcpy(name, mat_name);
1645         // otherwise do:
1646         int digits = integer_getdigits(flag);
1647         /* clamp the old name, remove the MA prefix and add the .TF.flag suffix
1648         e.g. matname = "MALoooooooooooooongName"; newname = "Loooooooooooooon.TF.2" */
1649         sprintf(newname, "%.*s.TF.%0*d", MAX_ID_NAME-(digits+5), matname, digits, flag);
1650 }
1651
1652 /* returns -1 if no match */
1653 static short mesh_getmaterialnumber(Mesh *me, Material *ma)
1654 {
1655         short a;
1656
1657         for (a=0; a<me->totcol; a++) {
1658                 if (me->mat[a] == ma) {
1659                         return a;
1660                 }
1661         }
1662
1663         return -1;
1664 }
1665
1666 /* append material */
1667 static short mesh_addmaterial(Mesh *me, Material *ma)
1668 {
1669         material_append_id(&me->id, NULL);
1670         me->mat[me->totcol-1]= ma;
1671
1672         id_us_plus(&ma->id);
1673
1674         return me->totcol-1;
1675 }
1676
1677 static void set_facetexture_flags(Material *ma, Image *image)
1678 {
1679         if(image) {
1680                 ma->mode |= MA_FACETEXTURE;
1681                 /* we could check if the texture has alpha, but then more meshes sharing the same
1682                  * material may need it. Let's make it simple. */
1683                 if(BKE_image_has_alpha(image))
1684                         ma->mode |= MA_FACETEXTURE_ALPHA;
1685         }
1686 }
1687
1688 /* returns material number */
1689 static short convert_tfacenomaterial(Main *main, Mesh *me, MTFace *tf, int flag)
1690 {
1691         Material *ma;
1692         char idname[MAX_ID_NAME];
1693         short mat_nr= -1;
1694         
1695         /* new material, the name uses the flag*/
1696         sprintf(idname, "MAMaterial.TF.%0*d", integer_getdigits(flag), flag);
1697         
1698         if ((ma= BLI_findstring(&main->mat, idname+2, offsetof(ID, name)+2))) {
1699                 mat_nr= mesh_getmaterialnumber(me, ma);
1700                 /* assign the material to the mesh */
1701                 if(mat_nr == -1) mat_nr= mesh_addmaterial(me, ma);
1702
1703                 /* if needed set "Face Textures [Alpha]" Material options */
1704                 set_facetexture_flags(ma, tf->tpage);
1705         }
1706         /* create a new material */
1707         else {
1708                 ma= add_material(idname+2);
1709
1710                 if(ma){
1711                         printf("TexFace Convert: Material \"%s\" created.\n", idname+2);
1712                         mat_nr= mesh_addmaterial(me, ma);
1713                         
1714                         /* if needed set "Face Textures [Alpha]" Material options */
1715                         set_facetexture_flags(ma, tf->tpage);
1716
1717                         decode_tfaceflag(ma, flag, 1);
1718                         // the final decoding will happen after, outside the main loop
1719                         // for now store the flag into the material and change light/tex/collision 
1720                         // store the flag as a negative number
1721                         ma->game.flag = -flag;
1722                         id_us_min((ID *)ma);    
1723                 }
1724                 else printf("Error: Unable to create Material \"%s\" for Mesh \"%s\".", idname+2, me->id.name+2);
1725         }
1726
1727         /* set as converted, no need to go bad to this face */
1728         tf->mode |= TF_CONVERTED;       
1729         return mat_nr;
1730 }
1731
1732 /* Function to fully convert materials */
1733 static void convert_tfacematerial(Main *main, Material *ma)
1734 {
1735         Mesh *me;
1736         Material *mat_new;
1737         MFace *mf;
1738         MTFace *tf;
1739         int flag, index;
1740         int a;
1741         short mat_nr;
1742         CustomDataLayer *cdl;
1743         char idname[MAX_ID_NAME];
1744
1745         for(me=main->mesh.first; me; me=me->id.next){
1746                 /* check if this mesh uses this material */
1747                 for(a=0;a<me->totcol;a++)
1748                         if(me->mat[a] == ma) break;
1749                         
1750                 /* no material found */
1751                 if (a == me->totcol) continue;
1752
1753                 /* get the active tface layer */
1754                 index= CustomData_get_active_layer_index(&me->fdata, CD_MTFACE);
1755                 cdl= (index == -1)? NULL: &me->fdata.layers[index];
1756                 if (!cdl) continue;
1757
1758                 /* loop over all the faces and stop at the ones that use the material*/
1759                 for(a=0, mf=me->mface; a<me->totface; a++, mf++) {
1760                         if(me->mat[mf->mat_nr] != ma) continue;
1761
1762                         /* texface data for this face */
1763                         tf = ((MTFace*)cdl->data) + a;
1764                         flag = encode_tfaceflag(tf, 1);
1765
1766                         /* the name of the new material */
1767                         calculate_tface_materialname(ma->id.name, (char *)&idname, flag);
1768
1769                         if ((mat_new= BLI_findstring(&main->mat, idname+2, offsetof(ID, name)+2))) {
1770                                 /* material already existent, see if the mesh has it */
1771                                 mat_nr = mesh_getmaterialnumber(me, mat_new);
1772                                 /* material is not in the mesh, add it */
1773                                 if(mat_nr == -1) mat_nr= mesh_addmaterial(me, mat_new);
1774                         }
1775                         /* create a new material */
1776                         else {
1777                                 mat_new=copy_material(ma);
1778                                 if(mat_new){
1779                                         /* rename the material*/
1780                                         strcpy(mat_new->id.name, idname);
1781                                         id_us_min((ID *)mat_new);       
1782
1783                                         mat_nr= mesh_addmaterial(me, mat_new);
1784                                         decode_tfaceflag(mat_new, flag, 1);
1785                                 }
1786                                 else {
1787                                         printf("Error: Unable to create Material \"%s\" for Mesh \"%s.", idname+2, me->id.name+2);
1788                                         mat_nr = mf->mat_nr;
1789                                         continue;
1790                                 }
1791                         }
1792                         
1793                         /* if the material has a texture but no texture channel
1794                          * set "Face Textures [Alpha]" Material options 
1795                          * actually we need to run it always, because of old behavior
1796                          * of using face texture if any texture channel was present (multitex) */
1797                         //if((!mat_new->mtex[0]) && (!mat_new->mtex[0]->tex))
1798                         set_facetexture_flags(mat_new, tf->tpage);
1799
1800                         /* set the material number to the face*/
1801                         mf->mat_nr = mat_nr;
1802                 }
1803                 /* remove material from mesh */
1804                 for(a=0;a<me->totcol;)
1805                         if(me->mat[a] == ma) material_pop_id(&me->id, a, 1);else a++;
1806         }
1807 }
1808
1809
1810 #define MAT_BGE_DISPUTED -99999
1811
1812 int do_version_tface(Main *main, int fileload)
1813 {
1814         Mesh *me;
1815         Material *ma;
1816         MFace *mf;
1817         MTFace *tf;
1818         CustomDataLayer *cdl;
1819         int a;
1820         int flag;
1821         int index;
1822
1823         /* sometimes mesh has no materials but will need a new one. In those
1824          * cases we need to ignore the mf->mat_nr and only look at the face
1825          * mode because it can be zero as uninitialized or the 1st created material
1826          */
1827         int nomaterialslots;
1828
1829         /* alert to user to check the console */
1830         int nowarning = 1;
1831
1832         /* mark all the materials to conversion with a flag
1833          * if there is tface create a complete flag for that storing in flag
1834          * if there is tface and flag > 0: creates a new flag based on this face
1835          * if flags are different set flag to -1  
1836          */
1837         
1838         /* 1st part: marking mesh materials to update */
1839         for(me=main->mesh.first; me; me=me->id.next){
1840                 if (me->id.lib) continue;
1841
1842                 /* get the active tface layer */
1843                 index= CustomData_get_active_layer_index(&me->fdata, CD_MTFACE);
1844                 cdl= (index == -1)? NULL: &me->fdata.layers[index];
1845                 if (!cdl) continue;
1846
1847                 nomaterialslots = (me->totcol==0?1:0);
1848                 
1849                 /* loop over all the faces*/
1850                 for(a=0, mf=me->mface; a<me->totface; a++, mf++) {
1851                         /* texface data for this face */
1852                         tf = ((MTFace*)cdl->data) + a;
1853
1854                         /* conversion should happen only once */
1855                         if (fileload)
1856                                 tf->mode &= ~TF_CONVERTED;
1857                         else {
1858                                 if((tf->mode & TF_CONVERTED)) continue;
1859                                 else tf->mode |= TF_CONVERTED;
1860                         }
1861                         
1862                         /* no material slots */
1863                         if(nomaterialslots) {
1864                                 flag = encode_tfaceflag(tf, 1);
1865                                 
1866                                 /* create/find a new material and assign to the face */
1867                                 if (check_tfaceneedmaterial(flag)) {
1868                                         mf->mat_nr= convert_tfacenomaterial(main, me, tf, flag);
1869                                 }
1870                                 /* else mark them as no-material to be reverted to 0 later */
1871                                 else {
1872                                         mf->mat_nr = -1;
1873                                 }
1874                         }
1875                         else if(mf->mat_nr < me->totcol) {
1876                                 ma= me->mat[mf->mat_nr];
1877                                 
1878                                 /* no material create one if necessary */
1879                                 if(!ma) {
1880                                         /* find a new material and assign to the face */
1881                                         flag = encode_tfaceflag(tf, 1);
1882
1883                                         /* create/find a new material and assign to the face */
1884                                         if (check_tfaceneedmaterial(flag))
1885                                                 mf->mat_nr= convert_tfacenomaterial(main, me, tf, flag);
1886
1887                                         continue;
1888                                 }
1889
1890                                 /* we can't read from this if it comes from a library,
1891                                  * at doversion time: direct_link might not have happened on it,
1892                                  * so ma->mtex is not pointing to valid memory yet.
1893                                  * later we could, but it's better not */
1894                                 else if(ma->id.lib)
1895                                         continue;
1896                                 
1897                                 /* material already marked as disputed */
1898                                 else if(ma->game.flag == MAT_BGE_DISPUTED)
1899                                         continue;
1900
1901                                 /* found a material */
1902                                 else {
1903                                         flag = encode_tfaceflag(tf, ((fileload)?0:1));
1904
1905                                         /* first time changing this material */
1906                                         if (ma->game.flag == 0)
1907                                                 ma->game.flag= -flag;
1908                         
1909                                         /* mark material as disputed */
1910                                         else if (ma->game.flag != -flag) {
1911                                                 ma->game.flag = MAT_BGE_DISPUTED;
1912                                                 continue;
1913                                         }
1914                         
1915                                         /* material ok so far */
1916                                         else {
1917                                                 ma->game.flag = -flag;
1918                                                 
1919                                                 /* some people uses multitexture with TexFace by creating a texture
1920                                                  * channel which not neccessarly the tf->tpage image. But the game engine
1921                                                  * was enabling it. Now it's required to set "Face Texture [Alpha] in the
1922                                                  * material settings. */
1923                                                 if(!fileload)
1924                                                         set_facetexture_flags(ma, tf->tpage);
1925                                         }
1926                                 }
1927                         }
1928                         else
1929                                 continue;
1930                 }
1931
1932                 /* if we didn't have material slot and now we do, we need to
1933                  * make sure the materials are correct */
1934                 if(nomaterialslots) {
1935                         if (me->totcol>0) {
1936                                 for(a=0, mf=me->mface; a<me->totface; a++, mf++) {
1937                                         if (mf->mat_nr == -1) {
1938                                                 /* texface data for this face */
1939                                                 tf = ((MTFace*)cdl->data) + a;
1940                                                 mf->mat_nr= convert_tfacenomaterial(main, me, tf, encode_tfaceflag(tf, 1));
1941                                         }
1942                                 }
1943                         }
1944                         else {
1945                                 for(a=0, mf=me->mface; a<me->totface; a++, mf++) {
1946                                         mf->mat_nr=0;
1947                                 }
1948                         }
1949                 }
1950
1951         }
1952         
1953         /* 2nd part - conversion */
1954         /* skip library files */
1955
1956         /* we shouldn't loop through the materials created in the loop. make the loop stop at its original length) */
1957         for (ma= main->mat.first, a=0; ma; ma= ma->id.next, a++) {
1958                 if (ma->id.lib) continue;
1959
1960                 /* disputed material */
1961                 if (ma->game.flag == MAT_BGE_DISPUTED) {
1962                         ma->game.flag = 0;
1963                         if (fileload) {
1964                                 printf("Warning: material \"%s\" skipped - to convert old game texface to material go to the Help menu.\n", ma->id.name+2);
1965                                 nowarning = 0;
1966                         }
1967                         else
1968                                 convert_tfacematerial(main, ma);
1969                         continue;       
1970                 }
1971         
1972                 /* no conflicts in this material - 90% of cases
1973                  * convert from tface system to material */
1974                 else if (ma->game.flag < 0) {
1975                         decode_tfaceflag(ma, -(ma->game.flag), 1);
1976
1977                         /* material is good make sure all faces using
1978                          * this material are set to converted */
1979                         if (fileload) {
1980                                 for(me=main->mesh.first; me; me=me->id.next){
1981                                         /* check if this mesh uses this material */
1982                                         for(a=0;a<me->totcol;a++)
1983                                                 if(me->mat[a] == ma) break;
1984                                                 
1985                                         /* no material found */
1986                                         if (a == me->totcol) continue;
1987                         
1988                                         /* get the active tface layer */
1989                                         index= CustomData_get_active_layer_index(&me->fdata, CD_MTFACE);
1990                                         cdl= (index == -1)? NULL: &me->fdata.layers[index];
1991                                         if (!cdl) continue;
1992                         
1993                                         /* loop over all the faces and stop at the ones that use the material*/
1994                                         for (a=0, mf=me->mface; a<me->totface; a++, mf++) {
1995                                                 if (me->mat[mf->mat_nr] == ma) {
1996                                                         /* texface data for this face */
1997                                                         tf = ((MTFace*)cdl->data) + a;
1998                                                         tf->mode |= TF_CONVERTED;
1999                                                 }
2000                                         }
2001                                 }
2002                         }
2003                 }
2004                 /* material is not used by faces with texface
2005                  * set the default flag - do it only once */
2006                 else
2007                          if (fileload)
2008                                         ma->game.flag = GEMAT_BACKCULL;
2009         }
2010
2011         return nowarning;
2012 }
2013