copy icon in the material buttons list view so you can copy the current set of materi...
[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(ob->totcol)
634                 object_remove_material_slot(ob);
635
636         /* now we have the right number of slots */
637         for(i=0; i<totcol; i++)
638                 assign_material(ob, (*matar)[i], i+1);
639
640         if(actcol_orig > ob->totcol)
641                 actcol_orig= ob->totcol;
642
643         ob->actcol= actcol_orig;
644 }
645
646
647 int find_material_index(Object *ob, Material *ma)
648 {
649         Material ***matarar;
650         short a, *totcolp;
651         
652         if(ma==NULL) return 0;
653         
654         totcolp= give_totcolp(ob);
655         matarar= give_matarar(ob);
656         
657         if(totcolp==NULL || matarar==NULL) return 0;
658         
659         for(a=0; a<*totcolp; a++)
660                 if((*matarar)[a]==ma)
661                    break;
662         if(a<*totcolp)
663                 return a+1;
664         return 0;          
665 }
666
667 void object_add_material_slot(Object *ob)
668 {
669         Material *ma;
670         
671         if(ob==0) return;
672         if(ob->totcol>=MAXMAT) return;
673         
674         ma= give_current_material(ob, ob->actcol);
675
676         assign_material(ob, ma, ob->totcol+1);
677         ob->actcol= ob->totcol;
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 void 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;
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         /* we delete the actcol */
913         if(ob->totcol) {
914                 mao= (*matarar)[ob->actcol-1];
915                 if(mao) mao->id.us--;
916         }
917         
918         for(a=ob->actcol; a<ob->totcol; a++)
919                 (*matarar)[a-1]= (*matarar)[a];
920         (*totcolp)--;
921         
922         if(*totcolp==0) {
923                 MEM_freeN(*matarar);
924                 *matarar= 0;
925         }
926         
927         actcol= ob->actcol;
928         obt= G.main->object.first;
929         while(obt) {
930         
931                 if(obt->data==ob->data) {
932                         
933                         /* WATCH IT: do not use actcol from ob or from obt (can become zero) */
934                         mao= obt->mat[actcol-1];
935                         if(mao) mao->id.us--;
936                 
937                         for(a=actcol; a<obt->totcol; a++) {
938                                 obt->mat[a-1]= obt->mat[a];
939                                 obt->matbits[a-1]= obt->matbits[a];
940                         }
941                         obt->totcol--;
942                         if(obt->actcol > obt->totcol) obt->actcol= obt->totcol;
943                         
944                         if(obt->totcol==0) {
945                                 MEM_freeN(obt->mat);
946                                 MEM_freeN(obt->matbits);
947                                 obt->mat= 0;
948                                 obt->matbits= NULL;
949                         }
950                 }
951                 obt= obt->id.next;
952         }
953
954         /* check indices from mesh */
955
956         if(ob->type==OB_MESH) {
957                 Mesh *me= get_mesh(ob);
958                 mesh_delete_material_index(me, actcol-1);
959                 freedisplist(&ob->disp);
960         }
961         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
962                 cu= ob->data;
963                 nu= cu->nurb.first;
964                 
965                 while(nu) {
966                         if(nu->mat_nr && nu->mat_nr>=actcol-1) {
967                                 nu->mat_nr--;
968                                 if (ob->type == OB_CURVE) nu->charidx--;
969                         }
970                         nu= nu->next;
971                 }
972                 freedisplist(&ob->disp);
973         }
974 }
975
976
977 /* r g b = current value, col = new value, fac==0 is no change */
978 /* if g==NULL, it only does r channel */
979 void ramp_blend(int type, float *r, float *g, float *b, float fac, float *col)
980 {
981         float tmp, facm= 1.0-fac;
982         
983         switch (type) {
984                 case MA_RAMP_BLEND:
985                         *r = facm*(*r) + fac*col[0];
986                         if(g) {
987                                 *g = facm*(*g) + fac*col[1];
988                                 *b = facm*(*b) + fac*col[2];
989                         }
990                                 break;
991                 case MA_RAMP_ADD:
992                         *r += fac*col[0];
993                         if(g) {
994                                 *g += fac*col[1];
995                                 *b += fac*col[2];
996                         }
997                                 break;
998                 case MA_RAMP_MULT:
999                         *r *= (facm + fac*col[0]);
1000                         if(g) {
1001                                 *g *= (facm + fac*col[1]);
1002                                 *b *= (facm + fac*col[2]);
1003                         }
1004                                 break;
1005                 case MA_RAMP_SCREEN:
1006                         *r = 1.0 - (facm + fac*(1.0 - col[0])) * (1.0 - *r);
1007                         if(g) {
1008                                 *g = 1.0 - (facm + fac*(1.0 - col[1])) * (1.0 - *g);
1009                                 *b = 1.0 - (facm + fac*(1.0 - col[2])) * (1.0 - *b);
1010                         }
1011                                 break;
1012                 case MA_RAMP_OVERLAY:
1013                         if(*r < 0.5f)
1014                                 *r *= (facm + 2.0f*fac*col[0]);
1015                         else
1016                                 *r = 1.0 - (facm + 2.0f*fac*(1.0 - col[0])) * (1.0 - *r);
1017                         if(g) {
1018                                 if(*g < 0.5f)
1019                                         *g *= (facm + 2.0f*fac*col[1]);
1020                                 else
1021                                         *g = 1.0 - (facm + 2.0f*fac*(1.0 - col[1])) * (1.0 - *g);
1022                                 if(*b < 0.5f)
1023                                         *b *= (facm + 2.0f*fac*col[2]);
1024                                 else
1025                                         *b = 1.0 - (facm + 2.0f*fac*(1.0 - col[2])) * (1.0 - *b);
1026                         }
1027                                 break;
1028                 case MA_RAMP_SUB:
1029                         *r -= fac*col[0];
1030                         if(g) {
1031                                 *g -= fac*col[1];
1032                                 *b -= fac*col[2];
1033                         }
1034                                 break;
1035                 case MA_RAMP_DIV:
1036                         if(col[0]!=0.0)
1037                                 *r = facm*(*r) + fac*(*r)/col[0];
1038                         if(g) {
1039                                 if(col[1]!=0.0)
1040                                         *g = facm*(*g) + fac*(*g)/col[1];
1041                                 if(col[2]!=0.0)
1042                                         *b = facm*(*b) + fac*(*b)/col[2];
1043                         }
1044                                 break;
1045                 case MA_RAMP_DIFF:
1046                         *r = facm*(*r) + fac*fabs(*r-col[0]);
1047                         if(g) {
1048                                 *g = facm*(*g) + fac*fabs(*g-col[1]);
1049                                 *b = facm*(*b) + fac*fabs(*b-col[2]);
1050                         }
1051                                 break;
1052                 case MA_RAMP_DARK:
1053             tmp=col[0]+((1-col[0])*facm); 
1054             if(tmp < *r) *r= tmp; 
1055             if(g) { 
1056                 tmp=col[1]+((1-col[1])*facm); 
1057                 if(tmp < *g) *g= tmp; 
1058                 tmp=col[2]+((1-col[2])*facm); 
1059                 if(tmp < *b) *b= tmp; 
1060             } 
1061                 break; 
1062                 case MA_RAMP_LIGHT:
1063                         tmp= fac*col[0];
1064                         if(tmp > *r) *r= tmp; 
1065                                 if(g) {
1066                                         tmp= fac*col[1];
1067                                         if(tmp > *g) *g= tmp; 
1068                                         tmp= fac*col[2];
1069                                         if(tmp > *b) *b= tmp; 
1070                                 }
1071                                         break;  
1072                 case MA_RAMP_DODGE:                     
1073                         
1074                                 
1075                         if(*r !=0.0){
1076                                 tmp = 1.0 - fac*col[0];
1077                                 if(tmp <= 0.0)
1078                                         *r = 1.0;
1079                                 else if ((tmp = (*r) / tmp)> 1.0)
1080                                         *r = 1.0;
1081                                 else 
1082                                         *r = tmp;
1083                         }
1084                         if(g) {
1085                                 if(*g !=0.0){
1086                                         tmp = 1.0 - fac*col[1];
1087                                         if(tmp <= 0.0 )
1088                                                 *g = 1.0;
1089                                         else if ((tmp = (*g) / tmp) > 1.0 )
1090                                                 *g = 1.0;
1091                                         else
1092                                                 *g = tmp;
1093                                 }
1094                                 if(*b !=0.0){
1095                                         tmp = 1.0 - fac*col[2];
1096                                         if(tmp <= 0.0)
1097                                                 *b = 1.0;
1098                                         else if ((tmp = (*b) / tmp) > 1.0 )
1099                                                 *b = 1.0;
1100                                         else
1101                                                 *b = tmp;
1102                                 }
1103
1104                         }
1105                                 break;  
1106                 case MA_RAMP_BURN:
1107                         
1108                         tmp = facm + fac*col[0];
1109                         
1110                         if(tmp <= 0.0)
1111                                 *r = 0.0;
1112                         else if (( tmp = (1.0 - (1.0 - (*r)) / tmp )) < 0.0)
1113                                 *r = 0.0;
1114                         else if (tmp > 1.0)
1115                                 *r=1.0;
1116                         else 
1117                                 *r = tmp; 
1118
1119                         if(g) {
1120                                 tmp = facm + fac*col[1];
1121                                 if(tmp <= 0.0)
1122                                         *g = 0.0;
1123                                 else if (( tmp = (1.0 - (1.0 - (*g)) / tmp )) < 0.0 )
1124                                         *g = 0.0;
1125                                 else if(tmp >1.0)
1126                                         *g=1.0;
1127                                 else
1128                                         *g = tmp;
1129                                         
1130                                 tmp = facm + fac*col[2];
1131                                 if(tmp <= 0.0)
1132                                         *b = 0.0;
1133                                 else if (( tmp = (1.0 - (1.0 - (*b)) / tmp )) < 0.0  )
1134                                         *b = 0.0;
1135                                 else if(tmp >1.0)
1136                                         *b= 1.0;
1137                                 else
1138                                         *b = tmp;
1139                         }
1140                                 break;
1141                 case MA_RAMP_HUE:               
1142                         if(g){
1143                                 float rH,rS,rV;
1144                                 float colH,colS,colV; 
1145                                 float tmpr,tmpg,tmpb;
1146                                 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1147                                 if(colS!=0 ){
1148                                         rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1149                                         hsv_to_rgb( colH , rS, rV, &tmpr, &tmpg, &tmpb);
1150                                         *r = facm*(*r) + fac*tmpr;  
1151                                         *g = facm*(*g) + fac*tmpg; 
1152                                         *b = facm*(*b) + fac*tmpb;
1153                                 }
1154                         }
1155                                 break;
1156                 case MA_RAMP_SAT:               
1157                         if(g){
1158                                 float rH,rS,rV;
1159                                 float colH,colS,colV;
1160                                 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1161                                 if(rS!=0){
1162                                         rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1163                                         hsv_to_rgb( rH, (facm*rS +fac*colS), rV, r, g, b);
1164                                 }
1165                         }
1166                                 break;
1167                 case MA_RAMP_VAL:               
1168                         if(g){
1169                                 float rH,rS,rV;
1170                                 float colH,colS,colV;
1171                                 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1172                                 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1173                                 hsv_to_rgb( rH, rS, (facm*rV +fac*colV), r, g, b);
1174                         }
1175                                 break;
1176                 case MA_RAMP_COLOR:             
1177                         if(g){
1178                                 float rH,rS,rV;
1179                                 float colH,colS,colV;
1180                                 float tmpr,tmpg,tmpb;
1181                                 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1182                                 if(colS!=0){
1183                                         rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1184                                         hsv_to_rgb( colH, colS, rV, &tmpr, &tmpg, &tmpb);
1185                                         *r = facm*(*r) + fac*tmpr;
1186                                         *g = facm*(*g) + fac*tmpg;
1187                                         *b = facm*(*b) + fac*tmpb;
1188                                 }
1189                         }
1190                                 break;
1191         case MA_RAMP_SOFT: 
1192             if (g){ 
1193                 float scr, scg, scb; 
1194                  
1195                 /* first calculate non-fac based Screen mix */ 
1196                 scr = 1.0 - ((1.0 - col[0])) * (1.0 - *r); 
1197                 scg = 1.0 - ((1.0 - col[1])) * (1.0 - *g); 
1198                 scb = 1.0 - ((1.0 - col[2])) * (1.0 - *b); 
1199                  
1200                 *r = facm*(*r) + fac*(((1.0 - *r) * col[0] * (*r)) + (*r * scr)); 
1201                 *g = facm*(*g) + fac*(((1.0 - *g) * col[1] * (*g)) + (*g * scg)); 
1202                 *b = facm*(*b) + fac*(((1.0 - *b) * col[2] * (*b)) + (*b * scb)); 
1203             } 
1204                 break; 
1205         case MA_RAMP_LINEAR: 
1206             if (col[0] > 0.5)  
1207                 *r = *r + fac*(2*(col[0]-0.5)); 
1208             else  
1209                 *r = *r + fac*(2*(col[0]) - 1); 
1210             if (g){ 
1211                 if (col[1] > 0.5)  
1212                     *g = *g + fac*(2*(col[1]-0.5)); 
1213                 else  
1214                     *g = *g + fac*(2*(col[1]) -1); 
1215                 if (col[2] > 0.5)  
1216                     *b = *b + fac*(2*(col[2]-0.5)); 
1217                 else  
1218                     *b = *b + fac*(2*(col[2]) - 1); 
1219             } 
1220                 break; 
1221         }       
1222 }
1223
1224