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