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