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