Merge with -r 22620:23107.
[blender.git] / source / blender / blenkernel / intern / material.c
1
2 /*  material.c
3  *
4  * 
5  * $Id$
6  *
7  * ***** BEGIN GPL LICENSE BLOCK *****
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL LICENSE BLOCK *****
31  */
32
33 #include <string.h>
34 #include <math.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_curve_types.h"
39 #include "DNA_material_types.h"
40 #include "DNA_mesh_types.h"
41 #include "DNA_meta_types.h"
42 #include "DNA_node_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_texture_types.h"
46 #include "DNA_userdef_types.h"
47
48 #include "BLI_blenlib.h"
49 #include "BLI_arithb.h"         
50
51 #include "BKE_animsys.h"
52 #include "BKE_blender.h"
53 #include "BKE_displist.h"
54 #include "BKE_global.h"
55 #include "BKE_icons.h"
56 #include "BKE_library.h"
57 #include "BKE_main.h"
58 #include "BKE_material.h"
59 #include "BKE_mesh.h"
60 #include "BKE_node.h"
61 #include "BKE_utildefines.h"
62
63 #ifndef DISABLE_PYTHON
64 #include "BPY_extern.h"
65 #endif
66
67 #include "GPU_material.h"
68
69 /* used in UI and render */
70 Material defmaterial;
71
72 /* called on startup, creator.c */
73 void init_def_material(void)
74 {
75         init_material(&defmaterial);
76 }
77
78 /* not material itself */
79 void free_material(Material *ma)
80 {
81         MTex *mtex;
82         int a;
83         
84         for(a=0; a<MAX_MTEX; a++) {
85                 mtex= ma->mtex[a];
86                 if(mtex && mtex->tex) mtex->tex->id.us--;
87                 if(mtex) MEM_freeN(mtex);
88         }
89         
90         if(ma->ramp_col) MEM_freeN(ma->ramp_col);
91         if(ma->ramp_spec) MEM_freeN(ma->ramp_spec);
92         
93         BKE_free_animdata((ID *)ma);
94         
95         BKE_previewimg_free(&ma->preview);
96         BKE_icon_delete((struct ID*)ma);
97         ma->id.icon_id = 0;
98         
99         /* is no lib link block, but material extension */
100         if(ma->nodetree) {
101                 ntreeFreeTree(ma->nodetree);
102                 MEM_freeN(ma->nodetree);
103         }
104
105         if(ma->gpumaterial.first)
106                 GPU_material_free(ma);
107 }
108
109 void init_material(Material *ma)
110 {
111         ma->r= ma->g= ma->b= ma->ref= 0.8;
112         ma->specr= ma->specg= ma->specb= 1.0;
113         ma->mirr= ma->mirg= ma->mirb= 1.0;
114         ma->spectra= 1.0;
115         ma->amb= 0.5;
116         ma->alpha= 1.0;
117         ma->spec= ma->hasize= 0.5;
118         ma->har= 50;
119         ma->starc= ma->ringc= 4;
120         ma->linec= 12;
121         ma->flarec= 1;
122         ma->flaresize= ma->subsize= 1.0;
123         ma->flareboost= 1;
124         ma->seed2= 6;
125         ma->friction= 0.5;
126         ma->refrac= 4.0;
127         ma->roughness= 0.5;
128         ma->param[0]= 0.5;
129         ma->param[1]= 0.1;
130         ma->param[2]= 0.5;
131         ma->param[3]= 0.1;
132         ma->rms= 0.1;
133         ma->darkness= 1.0;      
134         
135         ma->strand_sta= ma->strand_end= 1.0f;
136         
137         ma->ang= 1.0;
138         ma->ray_depth= 2;
139         ma->ray_depth_tra= 2;
140         ma->fresnel_mir= 0.0;
141         ma->fresnel_tra= 0.0;
142         ma->fresnel_tra_i= 1.25;
143         ma->fresnel_mir_i= 1.25;
144         ma->tx_limit= 0.0;
145         ma->tx_falloff= 1.0;
146         ma->shad_alpha= 1.0f;
147         
148         ma->gloss_mir = ma->gloss_tra= 1.0;
149         ma->samp_gloss_mir = ma->samp_gloss_tra= 18;
150         ma->adapt_thresh_mir = ma->adapt_thresh_tra = 0.005;
151         ma->dist_mir = 0.0;
152         ma->fadeto_mir = MA_RAYMIR_FADETOSKY;
153         
154         ma->rampfac_col= 1.0;
155         ma->rampfac_spec= 1.0;
156         ma->pr_lamp= 3;                 /* two lamps, is bits */
157         ma->pr_type= MA_SPHERE;
158
159         ma->sss_radius[0]= 1.0f;
160         ma->sss_radius[1]= 1.0f;
161         ma->sss_radius[2]= 1.0f;
162         ma->sss_col[0]= 0.8f;
163         ma->sss_col[1]= 0.8f;
164         ma->sss_col[2]= 0.8f;
165         ma->sss_error= 0.05f;
166         ma->sss_scale= 0.1f;
167         ma->sss_ior= 1.3f;
168         ma->sss_colfac= 1.0f;
169         ma->sss_texfac= 0.0f;
170         ma->sss_front= 1.0f;
171         ma->sss_back= 1.0f;
172
173         ma->vol.density = 1.0f;
174         ma->vol.emission = 0.0f;
175         ma->vol.absorption = 1.0f;
176         ma->vol.scattering = 1.0f;
177         ma->vol.emission_col[0] = ma->vol.emission_col[1] = ma->vol.emission_col[2] = 1.0f;
178         ma->vol.absorption_col[0] = ma->vol.absorption_col[1] = ma->vol.absorption_col[2] = 0.0f;
179         ma->vol.density_scale = 1.0f;
180         ma->vol.depth_cutoff = 0.01f;
181         ma->vol.stepsize_type = MA_VOL_STEP_RANDOMIZED;
182         ma->vol.stepsize = 0.2f;
183         ma->vol.shade_stepsize = 0.2f;
184         ma->vol.shade_type = MA_VOL_SHADE_SINGLE;
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 int find_material_index(Object *ob, Material *ma)
629 {
630         Material ***matarar;
631         short a, *totcolp;
632         
633         if(ma==NULL) return 0;
634         
635         totcolp= give_totcolp(ob);
636         matarar= give_matarar(ob);
637         
638         if(totcolp==NULL || matarar==NULL) return 0;
639         
640         for(a=0; a<*totcolp; a++)
641                 if((*matarar)[a]==ma)
642                    break;
643         if(a<*totcolp)
644                 return a+1;
645         return 0;          
646 }
647
648 void object_add_material_slot(Object *ob)
649 {
650         Material *ma;
651         
652         if(ob==0) return;
653         if(ob->totcol>=MAXMAT) return;
654         
655         ma= give_current_material(ob, ob->actcol);
656
657         assign_material(ob, ma, ob->totcol+1);
658         ob->actcol= ob->totcol;
659 }
660
661 static void do_init_render_material(Material *ma, int r_mode, float *amb)
662 {
663         MTex *mtex;
664         int a, needuv=0, needtang=0;
665         
666         if(ma->flarec==0) ma->flarec= 1;
667
668         /* add all texcoflags from mtex, texco and mapto were cleared in advance */
669         for(a=0; a<MAX_MTEX; a++) {
670                 
671                 /* separate tex switching */
672                 if(ma->septex & (1<<a)) continue;
673
674                 mtex= ma->mtex[a];
675                 if(mtex && mtex->tex && (mtex->tex->type | (mtex->tex->use_nodes && mtex->tex->nodetree) )) {
676                         
677                         ma->texco |= mtex->texco;
678                         ma->mapto |= mtex->mapto;
679                         if(r_mode & R_OSA) {
680                                 if ELEM3(mtex->tex->type, TEX_IMAGE, TEX_PLUGIN, TEX_ENVMAP) ma->texco |= TEXCO_OSA;
681                                 else if(mtex->texflag & MTEX_NEW_BUMP) ma->texco |= TEXCO_OSA; // NEWBUMP: need texture derivatives for procedurals as well
682                         }
683                         
684                         if(ma->texco & (TEXCO_ORCO|TEXCO_REFL|TEXCO_NORM|TEXCO_STRAND|TEXCO_STRESS)) needuv= 1;
685                         else if(ma->texco & (TEXCO_GLOB|TEXCO_UV|TEXCO_OBJECT|TEXCO_SPEED)) needuv= 1;
686                         else if(ma->texco & (TEXCO_LAVECTOR|TEXCO_VIEW|TEXCO_STICKY)) needuv= 1;
687
688                         if((ma->mapto & MAP_NORM) && (mtex->normapspace == MTEX_NSPACE_TANGENT))
689                                 needtang= 1;
690                 }
691         }
692
693         if(needtang) ma->mode |= MA_NORMAP_TANG;
694         else ma->mode &= ~MA_NORMAP_TANG;
695         
696         if(ma->mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)) {
697                 needuv= 1;
698                 if(r_mode & R_OSA) ma->texco |= TEXCO_OSA;              /* for texfaces */
699         }
700         if(needuv) ma->texco |= NEED_UV;
701         
702         /* since the raytracer doesnt recalc O structs for each ray, we have to preset them all */
703         if(r_mode & R_RAYTRACE) {
704                 if((ma->mode & (MA_RAYMIRROR|MA_SHADOW_TRA)) || ((ma->mode && MA_TRANSP) && (ma->mode & MA_RAYTRANSP))) { 
705                         ma->texco |= NEED_UV|TEXCO_ORCO|TEXCO_REFL|TEXCO_NORM;
706                         if(r_mode & R_OSA) ma->texco |= TEXCO_OSA;
707                 }
708         }
709         
710         if(amb) {
711                 ma->ambr= ma->amb*amb[0];
712                 ma->ambg= ma->amb*amb[1];
713                 ma->ambb= ma->amb*amb[2];
714         }       
715         /* will become or-ed result of all node modes */
716         ma->mode_l= ma->mode;
717         ma->mode_l &= ~MA_SHLESS;
718
719         if(ma->strand_surfnor > 0.0f)
720                 ma->mode_l |= MA_STR_SURFDIFF;
721 }
722
723 static void init_render_nodetree(bNodeTree *ntree, Material *basemat, int r_mode, float *amb)
724 {
725         bNode *node;
726         
727         for(node=ntree->nodes.first; node; node= node->next) {
728                 if(node->id) {
729                         if(GS(node->id->name)==ID_MA) {
730                                 Material *ma= (Material *)node->id;
731                                 if(ma!=basemat) {
732                                         do_init_render_material(ma, r_mode, amb);
733                                         basemat->texco |= ma->texco;
734                                         basemat->mode_l |= ma->mode_l;
735                                 }
736                         }
737                         else if(node->type==NODE_GROUP)
738                                 init_render_nodetree((bNodeTree *)node->id, basemat, r_mode, amb);
739                 }
740         }
741         /* parses the geom+tex nodes */
742         ntreeShaderGetTexcoMode(ntree, r_mode, &basemat->texco, &basemat->mode_l);
743 }
744
745 void init_render_material(Material *mat, int r_mode, float *amb)
746 {
747         
748         do_init_render_material(mat, r_mode, amb);
749         
750         if(mat->nodetree && mat->use_nodes) {
751                 init_render_nodetree(mat->nodetree, mat, r_mode, amb);
752                 
753                 ntreeBeginExecTree(mat->nodetree); /* has internal flag to detect it only does it once */
754         }
755 }
756
757 void init_render_materials(int r_mode, float *amb)
758 {
759         Material *ma;
760         
761         /* clear these flags before going over materials, to make sure they
762          * are cleared only once, otherwise node materials contained in other
763          * node materials can go wrong */
764         for(ma= G.main->mat.first; ma; ma= ma->id.next) {
765                 if(ma->id.us) {
766                         ma->texco= 0;
767                         ma->mapto= 0;
768                 }
769         }
770
771         /* two steps, first initialize, then or the flags for layers */
772         for(ma= G.main->mat.first; ma; ma= ma->id.next) {
773                 /* is_used flag comes back in convertblender.c */
774                 ma->flag &= ~MA_IS_USED;
775                 if(ma->id.us) 
776                         init_render_material(ma, r_mode, amb);
777         }
778         
779         do_init_render_material(&defmaterial, r_mode, amb);
780 }
781
782 /* only needed for nodes now */
783 void end_render_material(Material *mat)
784 {
785         if(mat && mat->nodetree && mat->use_nodes)
786                 ntreeEndExecTree(mat->nodetree); /* has internal flag to detect it only does it once */
787 }
788
789 void end_render_materials(void)
790 {
791         Material *ma;
792         for(ma= G.main->mat.first; ma; ma= ma->id.next)
793                 if(ma->id.us) 
794                         end_render_material(ma);
795 }
796
797 static int material_in_nodetree(bNodeTree *ntree, Material *mat)
798 {
799         bNode *node;
800
801         for(node=ntree->nodes.first; node; node= node->next) {
802                 if(node->id && GS(node->id->name)==ID_MA) {
803                         if(node->id==(ID*)mat)
804                                 return 1;
805                 }
806                 else if(node->type==NODE_GROUP)
807                         if(material_in_nodetree((bNodeTree*)node->id, mat))
808                                 return 1;
809         }
810
811         return 0;
812 }
813
814 int material_in_material(Material *parmat, Material *mat)
815 {
816         if(parmat==mat)
817                 return 1;
818         else if(parmat->nodetree && parmat->use_nodes)
819                 return material_in_nodetree(parmat->nodetree, mat);
820         else
821                 return 0;
822 }
823         
824 /* ****************** */
825
826 char colname_array[125][20]= {
827 "Black","DarkRed","HalfRed","Red","Red",
828 "DarkGreen","DarkOlive","Brown","Chocolate","OrangeRed",
829 "HalfGreen","GreenOlive","DryOlive","Goldenrod","DarkOrange",
830 "LightGreen","Chartreuse","YellowGreen","Yellow","Gold",
831 "Green","LawnGreen","GreenYellow","LightOlive","Yellow",
832 "DarkBlue","DarkPurple","HotPink","VioletPink","RedPink",
833 "SlateGray","DarkGrey","PalePurple","IndianRed","Tomato",
834 "SeaGreen","PaleGreen","GreenKhaki","LightBrown","LightSalmon",
835 "SpringGreen","PaleGreen","MediumOlive","YellowBrown","LightGold",
836 "LightGreen","LightGreen","LightGreen","GreenYellow","PaleYellow",
837 "HalfBlue","DarkSky","HalfMagenta","VioletRed","DeepPink",
838 "SteelBlue","SkyBlue","Orchid","LightHotPink","HotPink",
839 "SeaGreen","SlateGray","MediumGrey","Burlywood","LightPink",
840 "SpringGreen","Aquamarine","PaleGreen","Khaki","PaleOrange",
841 "SpringGreen","SeaGreen","PaleGreen","PaleWhite","YellowWhite",
842 "LightBlue","Purple","MediumOrchid","Magenta","Magenta",
843 "RoyalBlue","SlateBlue","MediumOrchid","Orchid","Magenta",
844 "DeepSkyBlue","LightSteelBlue","LightSkyBlue","Violet","LightPink",
845 "Cyan","DarkTurquoise","SkyBlue","Grey","Snow",
846 "Mint","Mint","Aquamarine","MintCream","Ivory",
847 "Blue","Blue","DarkMagenta","DarkOrchid","Magenta",
848 "SkyBlue","RoyalBlue","LightSlateBlue","MediumOrchid","Magenta",
849 "DodgerBlue","SteelBlue","MediumPurple","PalePurple","Plum",
850 "DeepSkyBlue","PaleBlue","LightSkyBlue","PalePurple","Thistle",
851 "Cyan","ColdBlue","PaleTurquoise","GhostWhite","White"
852 };
853
854 void automatname(Material *ma)
855 {
856         int nr, r, g, b;
857         float ref;
858         
859         if(ma==0) return;
860         if(ma->mode & MA_SHLESS) ref= 1.0;
861         else ref= ma->ref;
862         
863         r= (int)(4.99*(ref*ma->r));
864         g= (int)(4.99*(ref*ma->g));
865         b= (int)(4.99*(ref*ma->b));
866         nr= r + 5*g + 25*b;
867         if(nr>124) nr= 124;
868         new_id(&G.main->mat, (ID *)ma, colname_array[nr]);
869         
870 }
871
872
873 void object_remove_material_slot(Object *ob)
874 {
875         Material *mao, ***matarar;
876         Object *obt;
877         Curve *cu;
878         Nurb *nu;
879         short *totcolp;
880         int a, actcol;
881         
882         if(ob==NULL || ob->totcol==0) return;
883         
884         /* take a mesh/curve/mball as starting point, remove 1 index,
885          * AND with all objects that share the ob->data
886          * 
887          * after that check indices in mesh/curve/mball!!!
888          */
889         
890         totcolp= give_totcolp(ob);
891         matarar= give_matarar(ob);
892
893         /* we delete the actcol */
894         if(ob->totcol) {
895                 mao= (*matarar)[ob->actcol-1];
896                 if(mao) mao->id.us--;
897         }
898         
899         for(a=ob->actcol; a<ob->totcol; a++)
900                 (*matarar)[a-1]= (*matarar)[a];
901         (*totcolp)--;
902         
903         if(*totcolp==0) {
904                 MEM_freeN(*matarar);
905                 *matarar= 0;
906         }
907         
908         actcol= ob->actcol;
909         obt= G.main->object.first;
910         while(obt) {
911         
912                 if(obt->data==ob->data) {
913                         
914                         /* WATCH IT: do not use actcol from ob or from obt (can become zero) */
915                         mao= obt->mat[actcol-1];
916                         if(mao) mao->id.us--;
917                 
918                         for(a=actcol; a<obt->totcol; a++) {
919                                 obt->mat[a-1]= obt->mat[a];
920                                 obt->matbits[a-1]= obt->matbits[a];
921                         }
922                         obt->totcol--;
923                         if(obt->actcol > obt->totcol) obt->actcol= obt->totcol;
924                         
925                         if(obt->totcol==0) {
926                                 MEM_freeN(obt->mat);
927                                 MEM_freeN(obt->matbits);
928                                 obt->mat= 0;
929                                 obt->matbits= NULL;
930                         }
931                 }
932                 obt= obt->id.next;
933         }
934
935         /* check indices from mesh */
936
937         if(ob->type==OB_MESH) {
938                 Mesh *me= get_mesh(ob);
939                 mesh_delete_material_index(me, actcol-1);
940                 freedisplist(&ob->disp);
941         }
942         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
943                 cu= ob->data;
944                 nu= cu->nurb.first;
945                 
946                 while(nu) {
947                         if(nu->mat_nr && nu->mat_nr>=actcol-1) {
948                                 nu->mat_nr--;
949                                 if (ob->type == OB_CURVE) nu->charidx--;
950                         }
951                         nu= nu->next;
952                 }
953                 freedisplist(&ob->disp);
954         }
955 }
956
957
958 /* r g b = current value, col = new value, fac==0 is no change */
959 /* if g==NULL, it only does r channel */
960 void ramp_blend(int type, float *r, float *g, float *b, float fac, float *col)
961 {
962         float tmp, facm= 1.0-fac;
963         
964         switch (type) {
965                 case MA_RAMP_BLEND:
966                         *r = facm*(*r) + fac*col[0];
967                         if(g) {
968                                 *g = facm*(*g) + fac*col[1];
969                                 *b = facm*(*b) + fac*col[2];
970                         }
971                                 break;
972                 case MA_RAMP_ADD:
973                         *r += fac*col[0];
974                         if(g) {
975                                 *g += fac*col[1];
976                                 *b += fac*col[2];
977                         }
978                                 break;
979                 case MA_RAMP_MULT:
980                         *r *= (facm + fac*col[0]);
981                         if(g) {
982                                 *g *= (facm + fac*col[1]);
983                                 *b *= (facm + fac*col[2]);
984                         }
985                                 break;
986                 case MA_RAMP_SCREEN:
987                         *r = 1.0 - (facm + fac*(1.0 - col[0])) * (1.0 - *r);
988                         if(g) {
989                                 *g = 1.0 - (facm + fac*(1.0 - col[1])) * (1.0 - *g);
990                                 *b = 1.0 - (facm + fac*(1.0 - col[2])) * (1.0 - *b);
991                         }
992                                 break;
993                 case MA_RAMP_OVERLAY:
994                         if(*r < 0.5f)
995                                 *r *= (facm + 2.0f*fac*col[0]);
996                         else
997                                 *r = 1.0 - (facm + 2.0f*fac*(1.0 - col[0])) * (1.0 - *r);
998                         if(g) {
999                                 if(*g < 0.5f)
1000                                         *g *= (facm + 2.0f*fac*col[1]);
1001                                 else
1002                                         *g = 1.0 - (facm + 2.0f*fac*(1.0 - col[1])) * (1.0 - *g);
1003                                 if(*b < 0.5f)
1004                                         *b *= (facm + 2.0f*fac*col[2]);
1005                                 else
1006                                         *b = 1.0 - (facm + 2.0f*fac*(1.0 - col[2])) * (1.0 - *b);
1007                         }
1008                                 break;
1009                 case MA_RAMP_SUB:
1010                         *r -= fac*col[0];
1011                         if(g) {
1012                                 *g -= fac*col[1];
1013                                 *b -= fac*col[2];
1014                         }
1015                                 break;
1016                 case MA_RAMP_DIV:
1017                         if(col[0]!=0.0)
1018                                 *r = facm*(*r) + fac*(*r)/col[0];
1019                         if(g) {
1020                                 if(col[1]!=0.0)
1021                                         *g = facm*(*g) + fac*(*g)/col[1];
1022                                 if(col[2]!=0.0)
1023                                         *b = facm*(*b) + fac*(*b)/col[2];
1024                         }
1025                                 break;
1026                 case MA_RAMP_DIFF:
1027                         *r = facm*(*r) + fac*fabs(*r-col[0]);
1028                         if(g) {
1029                                 *g = facm*(*g) + fac*fabs(*g-col[1]);
1030                                 *b = facm*(*b) + fac*fabs(*b-col[2]);
1031                         }
1032                                 break;
1033                 case MA_RAMP_DARK:
1034             tmp=col[0]+((1-col[0])*facm); 
1035             if(tmp < *r) *r= tmp; 
1036             if(g) { 
1037                 tmp=col[1]+((1-col[1])*facm); 
1038                 if(tmp < *g) *g= tmp; 
1039                 tmp=col[2]+((1-col[2])*facm); 
1040                 if(tmp < *b) *b= tmp; 
1041             } 
1042                 break; 
1043                 case MA_RAMP_LIGHT:
1044                         tmp= fac*col[0];
1045                         if(tmp > *r) *r= tmp; 
1046                                 if(g) {
1047                                         tmp= fac*col[1];
1048                                         if(tmp > *g) *g= tmp; 
1049                                         tmp= fac*col[2];
1050                                         if(tmp > *b) *b= tmp; 
1051                                 }
1052                                         break;  
1053                 case MA_RAMP_DODGE:                     
1054                         
1055                                 
1056                         if(*r !=0.0){
1057                                 tmp = 1.0 - fac*col[0];
1058                                 if(tmp <= 0.0)
1059                                         *r = 1.0;
1060                                 else if ((tmp = (*r) / tmp)> 1.0)
1061                                         *r = 1.0;
1062                                 else 
1063                                         *r = tmp;
1064                         }
1065                         if(g) {
1066                                 if(*g !=0.0){
1067                                         tmp = 1.0 - fac*col[1];
1068                                         if(tmp <= 0.0 )
1069                                                 *g = 1.0;
1070                                         else if ((tmp = (*g) / tmp) > 1.0 )
1071                                                 *g = 1.0;
1072                                         else
1073                                                 *g = tmp;
1074                                 }
1075                                 if(*b !=0.0){
1076                                         tmp = 1.0 - fac*col[2];
1077                                         if(tmp <= 0.0)
1078                                                 *b = 1.0;
1079                                         else if ((tmp = (*b) / tmp) > 1.0 )
1080                                                 *b = 1.0;
1081                                         else
1082                                                 *b = tmp;
1083                                 }
1084
1085                         }
1086                                 break;  
1087                 case MA_RAMP_BURN:
1088                         
1089                         tmp = facm + fac*col[0];
1090                         
1091                         if(tmp <= 0.0)
1092                                 *r = 0.0;
1093                         else if (( tmp = (1.0 - (1.0 - (*r)) / tmp )) < 0.0)
1094                                 *r = 0.0;
1095                         else if (tmp > 1.0)
1096                                 *r=1.0;
1097                         else 
1098                                 *r = tmp; 
1099
1100                         if(g) {
1101                                 tmp = facm + fac*col[1];
1102                                 if(tmp <= 0.0)
1103                                         *g = 0.0;
1104                                 else if (( tmp = (1.0 - (1.0 - (*g)) / tmp )) < 0.0 )
1105                                         *g = 0.0;
1106                                 else if(tmp >1.0)
1107                                         *g=1.0;
1108                                 else
1109                                         *g = tmp;
1110                                         
1111                                 tmp = facm + fac*col[2];
1112                                 if(tmp <= 0.0)
1113                                         *b = 0.0;
1114                                 else if (( tmp = (1.0 - (1.0 - (*b)) / tmp )) < 0.0  )
1115                                         *b = 0.0;
1116                                 else if(tmp >1.0)
1117                                         *b= 1.0;
1118                                 else
1119                                         *b = tmp;
1120                         }
1121                                 break;
1122                 case MA_RAMP_HUE:               
1123                         if(g){
1124                                 float rH,rS,rV;
1125                                 float colH,colS,colV; 
1126                                 float tmpr,tmpg,tmpb;
1127                                 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1128                                 if(colS!=0 ){
1129                                         rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1130                                         hsv_to_rgb( colH , rS, rV, &tmpr, &tmpg, &tmpb);
1131                                         *r = facm*(*r) + fac*tmpr;  
1132                                         *g = facm*(*g) + fac*tmpg; 
1133                                         *b = facm*(*b) + fac*tmpb;
1134                                 }
1135                         }
1136                                 break;
1137                 case MA_RAMP_SAT:               
1138                         if(g){
1139                                 float rH,rS,rV;
1140                                 float colH,colS,colV;
1141                                 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1142                                 if(rS!=0){
1143                                         rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1144                                         hsv_to_rgb( rH, (facm*rS +fac*colS), rV, r, g, b);
1145                                 }
1146                         }
1147                                 break;
1148                 case MA_RAMP_VAL:               
1149                         if(g){
1150                                 float rH,rS,rV;
1151                                 float colH,colS,colV;
1152                                 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1153                                 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1154                                 hsv_to_rgb( rH, rS, (facm*rV +fac*colV), r, g, b);
1155                         }
1156                                 break;
1157                 case MA_RAMP_COLOR:             
1158                         if(g){
1159                                 float rH,rS,rV;
1160                                 float colH,colS,colV;
1161                                 float tmpr,tmpg,tmpb;
1162                                 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1163                                 if(colS!=0){
1164                                         rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1165                                         hsv_to_rgb( colH, colS, rV, &tmpr, &tmpg, &tmpb);
1166                                         *r = facm*(*r) + fac*tmpr;
1167                                         *g = facm*(*g) + fac*tmpg;
1168                                         *b = facm*(*b) + fac*tmpb;
1169                                 }
1170                         }
1171                                 break;
1172         case MA_RAMP_SOFT: 
1173             if (g){ 
1174                 float scr, scg, scb; 
1175                  
1176                 /* first calculate non-fac based Screen mix */ 
1177                 scr = 1.0 - ((1.0 - col[0])) * (1.0 - *r); 
1178                 scg = 1.0 - ((1.0 - col[1])) * (1.0 - *g); 
1179                 scb = 1.0 - ((1.0 - col[2])) * (1.0 - *b); 
1180                  
1181                 *r = facm*(*r) + fac*(((1.0 - *r) * col[0] * (*r)) + (*r * scr)); 
1182                 *g = facm*(*g) + fac*(((1.0 - *g) * col[1] * (*g)) + (*g * scg)); 
1183                 *b = facm*(*b) + fac*(((1.0 - *b) * col[2] * (*b)) + (*b * scb)); 
1184             } 
1185                 break; 
1186         case MA_RAMP_LINEAR: 
1187             if (col[0] > 0.5)  
1188                 *r = *r + fac*(2*(col[0]-0.5)); 
1189             else  
1190                 *r = *r + fac*(2*(col[0]) - 1); 
1191             if (g){ 
1192                 if (col[1] > 0.5)  
1193                     *g = *g + fac*(2*(col[1]-0.5)); 
1194                 else  
1195                     *g = *g + fac*(2*(col[1]) -1); 
1196                 if (col[2] > 0.5)  
1197                     *b = *b + fac*(2*(col[2]-0.5)); 
1198                 else  
1199                     *b = *b + fac*(2*(col[2]) - 1); 
1200             } 
1201                 break; 
1202         }       
1203 }
1204
1205