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