Initial revision
[blender.git] / source / blender / blenkernel / intern / material.c
1
2 /*  material.c        MIX MODEL
3  * 
4  *  maart 95
5  * 
6  * $Id$
7  *
8  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version. The Blender
14  * Foundation also sells licenses for use in proprietary software under
15  * the Blender License.  See http://www.blender.org/BL/ for information
16  * about this.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  *
27  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
28  * All rights reserved.
29  *
30  * The Original Code is: all of this file.
31  *
32  * Contributor(s): none yet.
33  *
34  * ***** END GPL/BL DUAL LICENSE BLOCK *****
35  */
36
37 #include <string.h>
38 #include "MEM_guardedalloc.h"
39
40 #include "DNA_material_types.h"
41 #include "DNA_texture_types.h"
42 #include "DNA_mesh_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_curve_types.h"
45 #include "DNA_meta_types.h"
46 #include "DNA_scene_types.h"
47
48 #include "BLI_blenlib.h"
49
50 #include "BKE_bad_level_calls.h"
51 #include "BKE_utildefines.h"
52
53 #include "BKE_global.h"
54 #include "BKE_main.h"
55
56 #include "BKE_mesh.h"
57 #include "BKE_library.h"
58 #include "BKE_displist.h"
59 #include "BPY_extern.h"
60 #include "BKE_material.h"
61
62 void free_material(Material *ma)
63 {
64         int a;
65         MTex *mtex;
66
67         BPY_free_scriptlink(&ma->scriptlink);
68         
69         if(ma->ren) MEM_freeN(ma->ren);
70         ma->ren= 0;
71         
72         for(a=0; a<8; a++) {
73                 mtex= ma->mtex[a];
74                 if(mtex && mtex->tex) mtex->tex->id.us--;
75                 if(mtex) MEM_freeN(mtex);
76         }
77 }
78
79 void init_material(Material *ma)
80 {
81         ma->lay= 1;
82         ma->r= ma->g= ma->b= ma->ref= 0.8f;
83         ma->specr= ma->specg= ma->specb= 1.0f;
84         ma->mirr= ma->mirg= ma->mirb= 1.0f;
85         ma->amb= 0.5f;
86         ma->alpha= 1.0f;
87         ma->spec= ma->hasize= 0.5f;
88         ma->har= 50;
89         ma->starc= ma->ringc= 4;
90         ma->linec= 12;
91         ma->flarec= 1;
92         ma->flaresize= ma->subsize= 1.0f;
93         ma->friction= 0.5f;
94         
95         ma->mode= MA_TRACEBLE+MA_SHADOW;        
96 }
97
98 Material *add_material(char *name)
99 {
100         Material *ma;
101
102         ma= alloc_libblock(&G.main->mat, ID_MA, name);
103         
104         init_material(ma);
105         
106         return ma;      
107 }
108
109 Material *copy_material(Material *ma)
110 {
111         Material *man;
112         int a;
113         
114         man= copy_libblock(ma);
115         
116         id_us_plus((ID *)man->ipo);
117         
118         for(a=0; a<8; a++) {
119                 if(ma->mtex[a]) {
120                         man->mtex[a]= MEM_mallocN(sizeof(MTex), "copymaterial");
121                         memcpy(man->mtex[a], ma->mtex[a], sizeof(MTex));
122                         id_us_plus((ID *)man->mtex[a]->tex);
123                 }
124         }
125         
126         BPY_copy_scriptlink(&ma->scriptlink);
127         
128         return man;
129 }
130
131 void make_local_material(Material *ma)
132 {
133         Object *ob;
134         Mesh *me;
135         Curve *cu;
136         MetaBall *mb;
137         Material *man;
138         int a, local=0, lib=0;
139         
140         /* - zijn er alleen lib users: niet doen
141          * - zijn er alleen locale users: flag zetten
142          * - mixed: copy
143          */
144         
145         if(ma->id.lib==0) return;
146         if(ma->id.us==1) {
147                 ma->id.lib= 0;
148                 ma->id.flag= LIB_LOCAL;
149                 new_id(0, (ID *)ma, 0);
150                 for(a=0; a<8; a++) {
151                         if(ma->mtex[a]) id_lib_extern((ID *)ma->mtex[a]->tex);
152                 }
153                 
154                 return;
155         }
156         
157         /* test objects */
158         ob= G.main->object.first;
159         while(ob) {
160                 if(ob->mat) {
161                         for(a=0; a<ob->totcol; a++) {
162                                 if(ob->mat[a]==ma) {
163                                         if(ob->id.lib) lib= 1;
164                                         else local= 1;
165                                 }
166                         }
167                 }
168                 ob= ob->id.next;
169         }
170         /* test meshes */
171         me= G.main->mesh.first;
172         while(me) {
173                 if(me->mat) {
174                         for(a=0; a<me->totcol; a++) {
175                                 if(me->mat[a]==ma) {
176                                         if(me->id.lib) lib= 1;
177                                         else local= 1;
178                                 }
179                         }
180                 }
181                 me= me->id.next;
182         }
183         /* test curves */
184         cu= G.main->curve.first;
185         while(cu) {
186                 if(cu->mat) {
187                         for(a=0; a<cu->totcol; a++) {
188                                 if(cu->mat[a]==ma) {
189                                         if(cu->id.lib) lib= 1;
190                                         else local= 1;
191                                 }
192                         }
193                 }
194                 cu= cu->id.next;
195         }
196         /* test mballs */
197         mb= G.main->mball.first;
198         while(mb) {
199                 if(mb->mat) {
200                         for(a=0; a<mb->totcol; a++) {
201                                 if(mb->mat[a]==ma) {
202                                         if(mb->id.lib) lib= 1;
203                                         else local= 1;
204                                 }
205                         }
206                 }
207                 mb= mb->id.next;
208         }
209         
210         if(local && lib==0) {
211                 ma->id.lib= 0;
212                 ma->id.flag= LIB_LOCAL;
213                 
214                 for(a=0; a<8; a++) {
215                         if(ma->mtex[a]) id_lib_extern((ID *)ma->mtex[a]->tex);
216                 }
217                 
218                 new_id(0, (ID *)ma, 0);
219         }
220         else if(local && lib) {
221                 man= copy_material(ma);
222                 man->id.us= 0;
223                 
224                 /* do objects */
225                 ob= G.main->object.first;
226                 while(ob) {
227                         if(ob->mat) {
228                                 for(a=0; a<ob->totcol; a++) {
229                                         if(ob->mat[a]==ma) {
230                                                 if(ob->id.lib==0) {
231                                                         ob->mat[a]= man;
232                                                         man->id.us++;
233                                                         ma->id.us--;
234                                                 }
235                                         }
236                                 }
237                         }
238                         ob= ob->id.next;
239                 }
240                 /* do meshes */
241                 me= G.main->mesh.first;
242                 while(me) {
243                         if(me->mat) {
244                                 for(a=0; a<me->totcol; a++) {
245                                         if(me->mat[a]==ma) {
246                                                 if(me->id.lib==0) {
247                                                         me->mat[a]= man;
248                                                         man->id.us++;
249                                                         ma->id.us--;
250                                                 }
251                                         }
252                                 }
253                         }
254                         me= me->id.next;
255                 }
256                 /* do curves */
257                 cu= G.main->curve.first;
258                 while(cu) {
259                         if(cu->mat) {
260                                 for(a=0; a<cu->totcol; a++) {
261                                         if(cu->mat[a]==ma) {
262                                                 if(cu->id.lib==0) {
263                                                         cu->mat[a]= man;
264                                                         man->id.us++;
265                                                         ma->id.us--;
266                                                 }
267                                         }
268                                 }
269                         }
270                         cu= cu->id.next;
271                 }
272                 /* do mballs */
273                 mb= G.main->mball.first;
274                 while(mb) {
275                         if(mb->mat) {
276                                 for(a=0; a<mb->totcol; a++) {
277                                         if(mb->mat[a]==ma) {
278                                                 if(mb->id.lib==0) {
279                                                         mb->mat[a]= man;
280                                                         man->id.us++;
281                                                         ma->id.us--;
282                                                 }
283                                         }
284                                 }
285                         }
286                         mb= mb->id.next;
287                 }
288         }
289 }
290
291 Material ***give_matarar(Object *ob)
292 {
293         Mesh *me;
294         Curve *cu;
295         MetaBall *mb;
296         
297         if(ob->type==OB_MESH) {
298                 me= ob->data;
299                 return &(me->mat);
300         }
301         else if ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF) {
302                 cu= ob->data;
303                 return &(cu->mat);
304         }
305         else if(ob->type==OB_MBALL) {
306                 mb= ob->data;
307                 return &(mb->mat);
308         }
309         return 0;
310 }
311
312 short *give_totcolp(Object *ob)
313 {
314         Mesh *me;
315         Curve *cu;
316         MetaBall *mb;
317         
318         if(ob->type==OB_MESH) {
319                 me= ob->data;
320                 return &(me->totcol);
321         }
322         else if ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF) {
323                 cu= ob->data;
324                 return &(cu->totcol);
325         }
326         else if(ob->type==OB_MBALL) {
327                 mb= ob->data;
328                 return &(mb->totcol);
329         }
330         return 0;
331 }
332
333 Material *give_current_material(Object *ob, int act)
334 {
335         Material ***matarar, *ma;
336         
337         if(ob==0) return 0;
338         if(ob->totcol==0) return 0;
339         
340         if(act>ob->totcol) act= ob->totcol;
341         else if(act==0) act= 1;
342
343         if( BTST(ob->colbits, act-1) ) {        /* aan object */
344                 ma= ob->mat[act-1];
345         }
346         else {                                                          /* aan data */
347                 matarar= give_matarar(ob);
348                 
349                 if(matarar && *matarar) ma= (*matarar)[act-1];
350                 else ma= 0;
351                 
352         }
353         
354         return ma;
355 }
356
357 ID *material_from(Object *ob, int act)
358 {
359
360         if(ob==0) return 0;
361
362         if(ob->totcol==0) return ob->data;
363         if(act==0) act= 1;
364
365         if( BTST(ob->colbits, act-1) ) return (ID *)ob;
366         else return ob->data;
367 }
368
369 /* GS reads the memory pointed at in a specific ordering. There are,
370  * however two definitions for it. I have jotted them down here, both,
371  * but I think the first one is actually used. The thing is that
372  * big-endian systems might read this the wrong way round. OTOH, we
373  * constructed the IDs that are read out with this macro explicitly as
374  * well. I expect we'll sort it out soon... */
375
376 /* from blendef: */
377 #define GS(a)   (*((short *)(a)))
378
379 /* from misc_util: flip the bytes from x  */
380 /*  #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
381
382 void test_object_materials(ID *id)
383 {
384         /* ob mat-array evenlang maken als obdata mat-array */
385         Object *ob;
386         Mesh *me;
387         Curve *cu;
388         MetaBall *mb;
389         Material **newmatar;
390         int totcol=0;
391
392         if(id==0) return;
393
394         if( GS(id->name)==ID_ME ) {
395                 me= (Mesh *)id;
396                 totcol= me->totcol;
397         }
398         else if( GS(id->name)==ID_CU ) {
399                 cu= (Curve *)id;
400                 totcol= cu->totcol;
401         }
402         else if( GS(id->name)==ID_MB ) {
403                 mb= (MetaBall *)id;
404                 totcol= mb->totcol;
405         }
406         else return;
407
408         ob= G.main->object.first;
409         while(ob) {
410                 
411                 if(ob->data==id) {
412                 
413                         if(totcol==0) {
414                                 if(ob->totcol) {
415                                         MEM_freeN(ob->mat);
416                                         ob->mat= 0;
417                                 }
418                         }
419                         else if(ob->totcol<totcol) {
420                                 newmatar= MEM_callocN(sizeof(void *)*totcol, "newmatar");
421                                 if(ob->totcol) {
422                                         memcpy(newmatar, ob->mat, sizeof(void *)*ob->totcol);
423                                         MEM_freeN(ob->mat);
424                                 }
425                                 ob->mat= newmatar;
426                         }
427                         ob->totcol= totcol;
428                         if(ob->totcol && ob->actcol==0) ob->actcol= 1;
429                         if(ob->actcol>ob->totcol) ob->actcol= ob->totcol;
430                 }
431                 ob= ob->id.next;
432         }
433 }
434
435
436 void assign_material(Object *ob, Material *ma, int act)
437 {
438         Material *mao, **matar, ***matarar;
439         short *totcolp;
440
441         if(act>MAXMAT) return;
442         if(act<1) act= 1;
443         
444         /* test arraylens */
445         
446         totcolp= give_totcolp(ob);
447         matarar= give_matarar(ob);
448         
449         if(totcolp==0 || matarar==0) return;
450         
451         if( act > *totcolp) {
452                 matar= MEM_callocN(sizeof(void *)*act, "matarray1");
453                 if( *totcolp) {
454                         memcpy(matar, *matarar, sizeof(void *)*( *totcolp ));
455                         MEM_freeN(*matarar);
456                 }
457                 *matarar= matar;
458                 *totcolp= act;
459         }
460         
461         if(act > ob->totcol) {
462                 matar= MEM_callocN(sizeof(void *)*act, "matarray2");
463                 if( ob->totcol) {
464                         memcpy(matar, ob->mat, sizeof(void *)*( ob->totcol ));
465                         MEM_freeN(ob->mat);
466                 }
467                 ob->mat= matar;
468                 ob->totcol= act;
469         }
470         
471         /* doe 't */
472
473         if( BTST(ob->colbits, act-1) ) {        /* aan object */
474                 mao= ob->mat[act-1];
475                 if(mao) mao->id.us--;
476                 ob->mat[act-1]= ma;
477         }
478         else {  /* aan data */
479                 mao= (*matarar)[act-1];
480                 if(mao) mao->id.us--;
481                 (*matarar)[act-1]= ma;
482         }
483         id_us_plus((ID *)ma);
484         test_object_materials(ob->data);
485 }
486
487 void new_material_to_objectdata(Object *ob)
488 {
489         Material *ma;
490         
491         if(ob==0) return;
492         if(ob->totcol>=MAXMAT) return;
493         
494         ma= give_current_material(ob, ob->actcol);
495         if(ma==0) {
496                 ma= add_material("Material");
497                 ma->id.us= 0;
498         }
499         
500         if(ob->actcol) {
501                 if( BTST(ob->colbits, ob->actcol-1) ) {
502                         ob->colbits= BSET(ob->colbits, ob->totcol);
503                 }
504         }
505         
506         assign_material(ob, ma, ob->totcol+1);
507         ob->actcol= ob->totcol;
508 }
509
510
511 void init_render_material(Material *ma)
512 {
513         MTex *mtex;
514         int a, needuv=0;
515         
516         if(ma->ren) return;
517
518         if(ma->flarec==0) ma->flarec= 1;
519
520         ma->ren= MEM_mallocN(sizeof(Material), "initrendermaterial");
521         memcpy(ma->ren, ma, sizeof(Material));
522         
523         /* alle texcoflags van mtex adden */
524         ma= ma->ren;
525         ma->texco= 0;
526         ma->mapto= 0;
527         for(a=0; a<8; a++) {
528                 mtex= ma->mtex[a];
529                 if(mtex && mtex->tex) {
530                 
531                         /* force std. ref mapping for envmap */
532                         if(mtex->tex->type==TEX_ENVMAP) {
533 /*                              mtex->texco= TEXCO_REFL; */
534 /*                              mtex->projx= PROJ_X; */
535 /*                              mtex->projy= PROJ_Y; */
536 /*                              mtex->projz= PROJ_Z; */
537 /*                              mtex->mapping= MTEX_FLAT; */
538                         }
539                         /* hier niet testen op mtex->object en mtex->texco op TEXCO_ORCO zetten: mtex is linked! */
540                         
541                         ma->texco |= mtex->texco;
542                         ma->mapto |= mtex->mapto;
543                         if(R.osa) {
544                                 if ELEM3(mtex->tex->type, TEX_IMAGE, TEX_PLUGIN, TEX_ENVMAP) ma->texco |= TEXCO_OSA;
545                         }
546                         
547                         if(ma->texco & (511)) needuv= 1;
548                         
549                         if(mtex->object) mtex->object->flag |= OB_DO_IMAT;
550                         
551                 }
552         }
553         if(ma->mode & MA_ZTRA) {
554                 /* if(ma->alpha==0.0 || ma->alpha==1.0) */
555                 R.flag |= R_ZTRA;
556         }
557         if(ma->mode & MA_VERTEXCOLP) ma->mode |= MA_VERTEXCOL; 
558         
559         if(ma->mode & (MA_VERTEXCOL|MA_FACETEXTURE)) {
560                 needuv= 1;
561                 if(R.osa) ma->texco |= TEXCO_OSA;               /* for texfaces */
562         }
563         if(needuv) ma->texco |= NEED_UV;
564
565         ma->ambr= ma->amb*R.wrld.ambr;
566         ma->ambg= ma->amb*R.wrld.ambg;
567         ma->ambb= ma->amb*R.wrld.ambb;
568         
569 }
570
571 void init_render_materials()
572 {
573         Material *ma;
574         
575         ma= G.main->mat.first;
576         while(ma) {
577                 if(ma->id.us) init_render_material(ma);
578                 ma= ma->id.next;
579         }
580         
581 }
582
583 void end_render_material(Material *ma)
584 {
585         
586         if(ma->ren) MEM_freeN(ma->ren);
587         ma->ren= 0;
588
589         if(ma->mode & (MA_VERTEXCOLP|MA_FACETEXTURE)) {
590                 if( !(ma->mode & MA_HALO) ) {
591                         ma->r= ma->g= ma->b= 1.0;
592                 }
593         }
594 }
595
596 void end_render_materials()
597 {
598         Material *ma;
599         
600         ma= G.main->mat.first;
601         while(ma) {
602                 if(ma->id.us) end_render_material(ma);
603                 ma= ma->id.next;
604         }
605         
606 }
607
608
609 /* ****************** */
610
611 char colname_array[125][20]= {
612 "Black","DarkRed","HalveRed","Red","Red",
613 "DarkGreen","DarkOlive","Brown","Chocolate","OrangeRed",
614 "HalveGreen","GreenOlive","DryOlive","Goldenrod","DarkOrange",
615 "LightGreen","Chartreuse","YellowGreen","Yellow","Gold",
616 "Green","LawnGreen","GreenYellow","LightOlive","Yellow",
617 "DarkBlue","DarkPurple","HotPink","VioletPink","RedPink",
618 "SlateGray","DarkGrey","PalePurple","IndianRed","Tomato",
619 "SeaGreen","PaleGreen","GreenKhaki","LightBrown","LightSalmon",
620 "SpringGreen","PaleGreen","MediumOlive","YellowBrown","LightGold",
621 "LightGreen","LightGreen","LightGreen","GreenYellow","PaleYellow",
622 "HalveBlue","DarkSky","HalveMagenta","VioletRed","DeepPink",
623 "SteelBlue","SkyBlue","Orchid","LightHotPink","HotPink",
624 "SeaGreen","SlateGray","MediumGrey","Burlywood","LightPink",
625 "SpringGreen","Aquamarine","PaleGreen","Khaki","PaleOrange",
626 "SpringGreen","SeaGreen","PaleGreen","PaleWhite","YellowWhite",
627 "LightBlue","Purple","MediumOrchid","Magenta","Magenta",
628 "RoyalBlue","SlateBlue","MediumOrchid","Orchid","Magenta",
629 "DeepSkyBlue","LightSteelBlue","LightSkyBlue","Violet","LightPink",
630 "Cyaan","DarkTurquoise","SkyBlue","Grey","Snow",
631 "Mint","Mint","Aquamarine","MintCream","Ivory",
632 "Blue","Blue","DarkMagenta","DarkOrchid","Magenta",
633 "SkyBlue","RoyalBlue","LightSlateBlue","MediumOrchid","Magenta",
634 "DodgerBlue","SteelBlue","MediumPurple","PalePurple","Plum",
635 "DeepSkyBlue","PaleBlue","LightSkyBlue","PalePurple","Thistle",
636 "Cyan","ColdBlue","PaleTurquoise","GhostWhite","White"
637 };
638
639 void automatname(Material *ma)
640 {
641         int nr, r, g, b;
642         float ref;
643         
644         if(ma==0) return;
645         if(ma->mode & MA_SHLESS) ref= 1.0;
646         else ref= ma->ref;
647         
648         r= (int)(4.99*(ref*ma->r));
649         g= (int)(4.99*(ref*ma->g));
650         b= (int)(4.99*(ref*ma->b));
651         nr= r + 5*g + 25*b;
652         if(nr>124) nr= 124;
653         new_id(&G.main->mat, (ID *)ma, colname_array[nr]);
654         
655 }
656
657
658 void delete_material_index()
659 {
660         Material *mao, ***matarar;
661         Object *ob, *obt;
662         Mesh *me;
663         Curve *cu;
664         Nurb *nu;
665         MFace *mface;
666         short *totcolp;
667         int a, actcol;
668         
669         if(G.obedit) {
670                 error("Unable to perform function in EditMode");
671                 return;
672         }
673         ob= ((G.scene->basact)? (G.scene->basact->object) : 0) ;
674         if(ob==0 || ob->totcol==0) return;
675         
676         /* neem als uitgangspunt de mesh/curve/mball, verwijder 1 index, EN bij alle ob's 
677          * die ook zelfde ob->data hebben
678          * 
679          * Daarna ook indexen in mesh/curve/mball wijzigen!!!
680          */
681         
682         totcolp= give_totcolp(ob);
683         matarar= give_matarar(ob);
684
685         /* we deleten de actcol */
686         if(ob->totcol) {
687                 mao= (*matarar)[ob->actcol-1];
688                 if(mao) mao->id.us--;
689         }
690         
691         for(a=ob->actcol; a<ob->totcol; a++) {
692                 (*matarar)[a-1]= (*matarar)[a];
693         }
694         (*totcolp)--;
695         
696         if(*totcolp==0) {
697                 MEM_freeN(*matarar);
698                 *matarar= 0;
699         }
700         
701         actcol= ob->actcol;
702         obt= G.main->object.first;
703         while(obt) {
704         
705                 if(obt->data==ob->data) {
706                         
707                         /* LET OP: actcol hier niet van ob of van obt pakken (kan nul worden) */
708                         mao= obt->mat[actcol-1];
709                         if(mao) mao->id.us--;
710                 
711                         for(a=actcol; a<obt->totcol; a++) obt->mat[a-1]= obt->mat[a];
712                         obt->totcol--;
713                         if(obt->actcol > obt->totcol) obt->actcol= obt->totcol;
714                         
715                         if(obt->totcol==0) {
716                                 MEM_freeN(obt->mat);
717                                 obt->mat= 0;
718                         }
719                 }
720                 obt= obt->id.next;
721         }
722         allqueue(REDRAWBUTSMAT, 0);
723         
724
725         /* indexen van mesh goedzetten */
726
727         if(ob->type==OB_MESH) {
728                 me= get_mesh(ob);
729                 mface= me->mface;
730                 a= me->totface;
731                 while(a--) {
732                         if(mface->mat_nr && mface->mat_nr>=actcol-1) mface->mat_nr--;
733                         mface++;
734                 }
735                 makeDispList(ob);
736         }
737         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
738                 cu= ob->data;
739                 nu= cu->nurb.first;
740                 
741                 while(nu) {
742                         if(nu->mat_nr && nu->mat_nr>=actcol-1) nu->mat_nr--;
743                         nu= nu->next;
744                 }
745                 makeDispList(ob);
746         }
747 }