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