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