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