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