2 * The 3D Studio File Format Library
3 * Copyright (C) 1996-2001 by J.E. Hoffmann <je-h@gmx.net>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 * License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * $Id: material.c,v 1.18 2004/11/20 08:31:31 efalk Exp $
23 #include <lib3ds/material.h>
24 #include <lib3ds/chunk.h>
25 #include <lib3ds/io.h>
35 * \defgroup material Materials
37 * \author J.E. Hoffmann <je-h@gmx.net>
42 initialize_texture_map(Lib3dsTextureMap *map)
52 * Creates and returns a new, empty Lib3dsMaterial object.
54 * Initial value of the material is a shiny grey.
56 * \return A pointer to the Lib3dsMaterial structure.
57 * If the structure cannot be allocated, NULL is returned.
66 mat = (Lib3dsMaterial*)calloc(sizeof(Lib3dsMaterial), 1);
71 mat->ambient[0] = mat->ambient[1] = mat->ambient[2] = 0.588235f;
72 mat->diffuse[0] = mat->diffuse[1] = mat->diffuse[2] = 0.588235f;
73 mat->specular[0] = mat->specular[1] = mat->specular[2] = 0.898039f;
74 mat->shininess = 0.1f;
75 mat->wire_size = 1.0f;
78 initialize_texture_map(&mat->texture1_map);
79 initialize_texture_map(&mat->texture1_mask);
80 initialize_texture_map(&mat->texture2_map);
81 initialize_texture_map(&mat->texture2_mask);
82 initialize_texture_map(&mat->opacity_map);
83 initialize_texture_map(&mat->opacity_mask);
84 initialize_texture_map(&mat->bump_map);
85 initialize_texture_map(&mat->bump_mask);
86 initialize_texture_map(&mat->specular_map);
87 initialize_texture_map(&mat->specular_mask);
88 initialize_texture_map(&mat->shininess_map);
89 initialize_texture_map(&mat->shininess_mask);
90 initialize_texture_map(&mat->self_illum_map);
91 initialize_texture_map(&mat->self_illum_mask);
92 initialize_texture_map(&mat->reflection_map);
93 initialize_texture_map(&mat->reflection_mask);
103 lib3ds_material_free(Lib3dsMaterial *material)
105 memset(material, 0, sizeof(Lib3dsMaterial));
111 color_read(Lib3dsRgba rgb, Lib3dsIo *io)
115 Lib3dsBool have_lin=LIB3DS_FALSE;
117 if (!lib3ds_chunk_read_start(&c, 0, io)) {
118 return(LIB3DS_FALSE);
121 while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
123 case LIB3DS_LIN_COLOR_24:
126 for (i=0; i<3; ++i) {
127 rgb[i]=1.0f*lib3ds_io_read_byte(io)/255.0f;
131 have_lin=LIB3DS_TRUE;
133 case LIB3DS_COLOR_24:
134 /* gamma corrected color chunk
135 replaced in 3ds R3 by LIN_COLOR_24 */
138 for (i=0; i<3; ++i) {
139 rgb[i]=1.0f*lib3ds_io_read_byte(io)/255.0f;
145 lib3ds_chunk_unknown(chunk);
149 lib3ds_chunk_read_end(&c, io);
155 int_percentage_read(Lib3dsFloat *p, Lib3dsIo *io)
160 if (!lib3ds_chunk_read_start(&c, 0, io)) {
161 return(LIB3DS_FALSE);
164 while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
166 case LIB3DS_INT_PERCENTAGE:
168 Lib3dsIntw i=lib3ds_io_read_intw(io);
169 *p=(Lib3dsFloat)(1.0*i/100.0);
173 lib3ds_chunk_unknown(chunk);
177 lib3ds_chunk_read_end(&c, io);
183 texture_map_read(Lib3dsTextureMap *map, Lib3dsIo *io)
188 if (!lib3ds_chunk_read_start(&c, 0, io)) {
189 return(LIB3DS_FALSE);
192 while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
194 case LIB3DS_INT_PERCENTAGE:
196 map->percent=1.0f*lib3ds_io_read_intw(io)/100.0f;
199 case LIB3DS_MAT_MAPNAME:
201 if (!lib3ds_io_read_string(io, map->name, 64)) {
202 return(LIB3DS_FALSE);
204 lib3ds_chunk_dump_info(" NAME=%s", map->name);
207 case LIB3DS_MAT_MAP_TILING:
209 map->flags=lib3ds_io_read_word(io);
212 case LIB3DS_MAT_MAP_TEXBLUR:
214 map->blur=lib3ds_io_read_float(io);
217 case LIB3DS_MAT_MAP_USCALE:
219 map->scale[0]=lib3ds_io_read_float(io);
222 case LIB3DS_MAT_MAP_VSCALE:
224 map->scale[1]=lib3ds_io_read_float(io);
227 case LIB3DS_MAT_MAP_UOFFSET:
229 map->offset[0]=lib3ds_io_read_float(io);
232 case LIB3DS_MAT_MAP_VOFFSET:
234 map->offset[1]=lib3ds_io_read_float(io);
237 case LIB3DS_MAT_MAP_ANG:
239 map->rotation=lib3ds_io_read_float(io);
242 case LIB3DS_MAT_MAP_COL1:
244 map->tint_1[0]=1.0f*lib3ds_io_read_byte(io)/255.0f;
245 map->tint_1[1]=1.0f*lib3ds_io_read_byte(io)/255.0f;
246 map->tint_1[2]=1.0f*lib3ds_io_read_byte(io)/255.0f;
249 case LIB3DS_MAT_MAP_COL2:
251 map->tint_2[0]=1.0f*lib3ds_io_read_byte(io)/255.0f;
252 map->tint_2[1]=1.0f*lib3ds_io_read_byte(io)/255.0f;
253 map->tint_2[2]=1.0f*lib3ds_io_read_byte(io)/255.0f;
256 case LIB3DS_MAT_MAP_RCOL:
258 map->tint_r[0]=1.0f*lib3ds_io_read_byte(io)/255.0f;
259 map->tint_r[1]=1.0f*lib3ds_io_read_byte(io)/255.0f;
260 map->tint_r[2]=1.0f*lib3ds_io_read_byte(io)/255.0f;
263 case LIB3DS_MAT_MAP_GCOL:
265 map->tint_g[0]=1.0f*lib3ds_io_read_byte(io)/255.0f;
266 map->tint_g[1]=1.0f*lib3ds_io_read_byte(io)/255.0f;
267 map->tint_g[2]=1.0f*lib3ds_io_read_byte(io)/255.0f;
270 case LIB3DS_MAT_MAP_BCOL:
272 map->tint_b[0]=1.0f*lib3ds_io_read_byte(io)/255.0f;
273 map->tint_b[1]=1.0f*lib3ds_io_read_byte(io)/255.0f;
274 map->tint_b[2]=1.0f*lib3ds_io_read_byte(io)/255.0f;
278 lib3ds_chunk_unknown(chunk);
282 lib3ds_chunk_read_end(&c, io);
291 texture_dump(const char *maptype, Lib3dsTextureMap *texture)
294 if (strlen(texture->name)==0) {
297 printf(" %s:\n", maptype);
298 printf(" name: %s\n", texture->name);
299 printf(" flags: %X\n", (unsigned)texture->flags);
300 printf(" percent: %f\n", texture->percent);
301 printf(" blur: %f\n", texture->blur);
302 printf(" scale: (%f, %f)\n", texture->scale[0], texture->scale[1]);
303 printf(" offset: (%f, %f)\n", texture->offset[0], texture->offset[1]);
304 printf(" rotation: %f\n", texture->rotation);
305 printf(" tint_1: (%f, %f, %f)\n",
306 texture->tint_1[0], texture->tint_1[1], texture->tint_1[2]);
307 printf(" tint_2: (%f, %f, %f)\n",
308 texture->tint_2[0], texture->tint_2[1], texture->tint_2[2]);
309 printf(" tint_r: (%f, %f, %f)\n",
310 texture->tint_r[0], texture->tint_r[1], texture->tint_r[2]);
311 printf(" tint_g: (%f, %f, %f)\n",
312 texture->tint_g[0], texture->tint_g[1], texture->tint_g[2]);
313 printf(" tint_b: (%f, %f, %f)\n",
314 texture->tint_b[0], texture->tint_b[1], texture->tint_b[2]);
322 lib3ds_material_dump(Lib3dsMaterial *material)
325 printf(" name: %s\n", material->name);
326 printf(" ambient: (%f, %f, %f)\n",
327 material->ambient[0], material->ambient[1], material->ambient[2]);
328 printf(" diffuse: (%f, %f, %f)\n",
329 material->diffuse[0], material->diffuse[1], material->diffuse[2]);
330 printf(" specular: (%f, %f, %f)\n",
331 material->specular[0], material->specular[1], material->specular[2]);
332 printf(" shininess: %f\n", material->shininess);
333 printf(" shin_strength: %f\n", material->shin_strength);
334 printf(" use_blur: %s\n", material->use_blur ? "yes" : "no");
335 printf(" blur: %f\n", material->blur);
336 printf(" falloff: %f\n", material->falloff);
337 printf(" additive: %s\n", material->additive ? "yes" : "no");
338 printf(" use_falloff: %s\n", material->use_falloff ? "yes" : "no");
339 printf(" self_illum: %s\n", material->self_illum ? "yes" : "no");
340 printf(" self_ilpct: %f\n", material->self_ilpct);
341 printf(" shading: %d\n", material->shading);
342 printf(" soften: %s\n", material->soften ? "yes" : "no");
343 printf(" face_map: %s\n", material->face_map ? "yes" : "no");
344 printf(" two_sided: %s\n", material->two_sided ? "yes" : "no");
345 printf(" map_decal: %s\n", material->map_decal ? "yes" : "no");
346 printf(" use_wire: %s\n", material->use_wire ? "yes" : "no");
347 printf(" use_wire_abs: %s\n", material->use_wire_abs ? "yes" : "no");
348 printf(" wire_size: %f\n", material->wire_size);
349 texture_dump("texture1_map", &material->texture1_map);
350 texture_dump("texture1_mask", &material->texture1_mask);
351 texture_dump("texture2_map", &material->texture2_map);
352 texture_dump("texture2_mask", &material->texture2_mask);
353 texture_dump("opacity_map", &material->opacity_map);
354 texture_dump("opacity_mask", &material->opacity_mask);
355 texture_dump("bump_map", &material->bump_map);
356 texture_dump("bump_mask", &material->bump_mask);
357 texture_dump("specular_map", &material->specular_map);
358 texture_dump("specular_mask", &material->specular_mask);
359 texture_dump("shininess_map", &material->shininess_map);
360 texture_dump("shininess_mask", &material->shininess_mask);
361 texture_dump("self_illum_map", &material->self_illum_map);
362 texture_dump("self_illum_mask", &material->self_illum_mask);
363 texture_dump("reflection_map", &material->reflection_map);
364 texture_dump("reflection_mask", &material->reflection_mask);
365 printf(" autorefl_map:\n");
366 printf(" flags %X\n", (unsigned)material->autorefl_map.flags);
367 printf(" level %d\n", (int)material->autorefl_map.level);
368 printf(" size %d\n", (int)material->autorefl_map.size);
369 printf(" frame_step %d\n", (int)material->autorefl_map.frame_step);
378 lib3ds_material_read(Lib3dsMaterial *material, Lib3dsIo *io)
384 if (!lib3ds_chunk_read_start(&c, LIB3DS_MAT_ENTRY, io)) {
385 return(LIB3DS_FALSE);
388 while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
390 case LIB3DS_MAT_NAME:
392 if (!lib3ds_io_read_string(io, material->name, 64)) {
393 return(LIB3DS_FALSE);
395 lib3ds_chunk_dump_info(" NAME=%s", material->name);
398 case LIB3DS_MAT_AMBIENT:
400 lib3ds_chunk_read_reset(&c, io);
401 if (!color_read(material->ambient, io)) {
402 return(LIB3DS_FALSE);
406 case LIB3DS_MAT_DIFFUSE:
408 lib3ds_chunk_read_reset(&c, io);
409 if (!color_read(material->diffuse, io)) {
410 return(LIB3DS_FALSE);
414 case LIB3DS_MAT_SPECULAR:
416 lib3ds_chunk_read_reset(&c, io);
417 if (!color_read(material->specular, io)) {
418 return(LIB3DS_FALSE);
422 case LIB3DS_MAT_SHININESS:
424 lib3ds_chunk_read_reset(&c, io);
425 if (!int_percentage_read(&material->shininess, io)) {
426 return(LIB3DS_FALSE);
430 case LIB3DS_MAT_SHIN2PCT:
432 lib3ds_chunk_read_reset(&c, io);
433 if (!int_percentage_read(&material->shin_strength, io)) {
434 return(LIB3DS_FALSE);
438 case LIB3DS_MAT_TRANSPARENCY:
440 lib3ds_chunk_read_reset(&c, io);
441 if (!int_percentage_read(&material->transparency, io)) {
442 return(LIB3DS_FALSE);
446 case LIB3DS_MAT_XPFALL:
448 lib3ds_chunk_read_reset(&c, io);
449 if (!int_percentage_read(&material->falloff, io)) {
450 return(LIB3DS_FALSE);
454 case LIB3DS_MAT_SELF_ILPCT:
456 lib3ds_chunk_read_reset(&c, io);
457 if (!int_percentage_read(&material->self_ilpct, io)) {
458 return(LIB3DS_FALSE);
462 case LIB3DS_MAT_USE_XPFALL:
464 material->use_falloff=LIB3DS_TRUE;
467 case LIB3DS_MAT_REFBLUR:
469 lib3ds_chunk_read_reset(&c, io);
470 if (!int_percentage_read(&material->blur, io)) {
471 return(LIB3DS_FALSE);
475 case LIB3DS_MAT_USE_REFBLUR:
477 material->use_blur=LIB3DS_TRUE;
480 case LIB3DS_MAT_SHADING:
482 material->shading=lib3ds_io_read_intw(io);
485 case LIB3DS_MAT_SELF_ILLUM:
487 material->self_illum=LIB3DS_TRUE;
490 case LIB3DS_MAT_TWO_SIDE:
492 material->two_sided=LIB3DS_TRUE;
495 case LIB3DS_MAT_DECAL:
497 material->map_decal=LIB3DS_TRUE;
500 case LIB3DS_MAT_ADDITIVE:
502 material->additive=LIB3DS_TRUE;
505 case LIB3DS_MAT_FACEMAP:
507 material->face_map=LIB3DS_TRUE;
510 case LIB3DS_MAT_PHONGSOFT:
512 material->soften=LIB3DS_TRUE;
515 case LIB3DS_MAT_WIRE:
517 material->use_wire=LIB3DS_TRUE;
520 case LIB3DS_MAT_WIREABS:
522 material->use_wire_abs=LIB3DS_TRUE;
525 case LIB3DS_MAT_WIRE_SIZE:
527 material->wire_size=lib3ds_io_read_float(io);
530 case LIB3DS_MAT_TEXMAP:
532 lib3ds_chunk_read_reset(&c, io);
533 if (!texture_map_read(&material->texture1_map, io)) {
534 return(LIB3DS_FALSE);
538 case LIB3DS_MAT_TEXMASK:
540 lib3ds_chunk_read_reset(&c, io);
541 if (!texture_map_read(&material->texture1_mask, io)) {
542 return(LIB3DS_FALSE);
546 case LIB3DS_MAT_TEX2MAP:
548 lib3ds_chunk_read_reset(&c, io);
549 if (!texture_map_read(&material->texture2_map, io)) {
550 return(LIB3DS_FALSE);
554 case LIB3DS_MAT_TEX2MASK:
556 lib3ds_chunk_read_reset(&c, io);
557 if (!texture_map_read(&material->texture2_mask, io)) {
558 return(LIB3DS_FALSE);
562 case LIB3DS_MAT_OPACMAP:
564 lib3ds_chunk_read_reset(&c, io);
565 if (!texture_map_read(&material->opacity_map, io)) {
566 return(LIB3DS_FALSE);
570 case LIB3DS_MAT_OPACMASK:
572 lib3ds_chunk_read_reset(&c, io);
573 if (!texture_map_read(&material->opacity_mask, io)) {
574 return(LIB3DS_FALSE);
578 case LIB3DS_MAT_BUMPMAP:
580 lib3ds_chunk_read_reset(&c, io);
581 if (!texture_map_read(&material->bump_map, io)) {
582 return(LIB3DS_FALSE);
586 case LIB3DS_MAT_BUMPMASK:
588 lib3ds_chunk_read_reset(&c, io);
589 if (!texture_map_read(&material->bump_mask, io)) {
590 return(LIB3DS_FALSE);
594 case LIB3DS_MAT_SPECMAP:
596 lib3ds_chunk_read_reset(&c, io);
597 if (!texture_map_read(&material->specular_map, io)) {
598 return(LIB3DS_FALSE);
602 case LIB3DS_MAT_SPECMASK:
604 lib3ds_chunk_read_reset(&c, io);
605 if (!texture_map_read(&material->specular_mask, io)) {
606 return(LIB3DS_FALSE);
610 case LIB3DS_MAT_SHINMAP:
612 lib3ds_chunk_read_reset(&c, io);
613 if (!texture_map_read(&material->shininess_map, io)) {
614 return(LIB3DS_FALSE);
618 case LIB3DS_MAT_SHINMASK:
620 lib3ds_chunk_read_reset(&c, io);
621 if (!texture_map_read(&material->shininess_mask, io)) {
622 return(LIB3DS_FALSE);
626 case LIB3DS_MAT_SELFIMAP:
628 lib3ds_chunk_read_reset(&c, io);
629 if (!texture_map_read(&material->self_illum_map, io)) {
630 return(LIB3DS_FALSE);
634 case LIB3DS_MAT_SELFIMASK:
636 lib3ds_chunk_read_reset(&c, io);
637 if (!texture_map_read(&material->self_illum_mask, io)) {
638 return(LIB3DS_FALSE);
642 case LIB3DS_MAT_REFLMAP:
644 lib3ds_chunk_read_reset(&c, io);
645 if (!texture_map_read(&material->reflection_map, io)) {
646 return(LIB3DS_FALSE);
650 case LIB3DS_MAT_REFLMASK:
652 lib3ds_chunk_read_reset(&c, io);
653 if (!texture_map_read(&material->reflection_mask, io)) {
654 return(LIB3DS_FALSE);
658 case LIB3DS_MAT_ACUBIC:
660 lib3ds_io_read_intb(io);
661 material->autorefl_map.level=lib3ds_io_read_intb(io);
662 material->autorefl_map.flags=lib3ds_io_read_intw(io);
663 material->autorefl_map.size=lib3ds_io_read_intd(io);
664 material->autorefl_map.frame_step=lib3ds_io_read_intd(io);
668 lib3ds_chunk_unknown(chunk);
672 lib3ds_chunk_read_end(&c, io);
678 color_write(Lib3dsRgba rgb, Lib3dsIo *io)
682 c.chunk=LIB3DS_COLOR_24;
684 lib3ds_chunk_write(&c,io);
685 lib3ds_io_write_byte(io, (Lib3dsByte)floor(255.0*rgb[0]+0.5));
686 lib3ds_io_write_byte(io, (Lib3dsByte)floor(255.0*rgb[1]+0.5));
687 lib3ds_io_write_byte(io, (Lib3dsByte)floor(255.0*rgb[2]+0.5));
689 c.chunk=LIB3DS_LIN_COLOR_24;
691 lib3ds_chunk_write(&c,io);
692 lib3ds_io_write_byte(io, (Lib3dsByte)floor(255.0*rgb[0]+0.5));
693 lib3ds_io_write_byte(io, (Lib3dsByte)floor(255.0*rgb[1]+0.5));
694 lib3ds_io_write_byte(io, (Lib3dsByte)floor(255.0*rgb[2]+0.5));
701 int_percentage_write(Lib3dsFloat p, Lib3dsIo *io)
705 c.chunk=LIB3DS_INT_PERCENTAGE;
707 lib3ds_chunk_write(&c,io);
708 lib3ds_io_write_intw(io, (Lib3dsByte)floor(100.0*p+0.5));
715 texture_map_write(Lib3dsWord chunk, Lib3dsTextureMap *map, Lib3dsIo *io)
719 if (strlen(map->name)==0) {
723 if (!lib3ds_chunk_write_start(&c,io)) {
724 return(LIB3DS_FALSE);
727 int_percentage_write(map->percent,io);
729 { /*---- LIB3DS_MAT_MAPNAME ----*/
731 c.chunk=LIB3DS_MAT_MAPNAME;
732 c.size=6+strlen(map->name)+1;
733 lib3ds_chunk_write(&c,io);
734 lib3ds_io_write_string(io, map->name);
737 { /*---- LIB3DS_MAT_MAP_TILING ----*/
739 c.chunk=LIB3DS_MAT_MAP_TILING;
741 lib3ds_chunk_write(&c,io);
742 lib3ds_io_write_word(io, (Lib3dsWord)map->flags);
745 { /*---- LIB3DS_MAT_MAP_TEXBLUR ----*/
747 c.chunk=LIB3DS_MAT_MAP_TEXBLUR;
749 lib3ds_chunk_write(&c,io);
750 lib3ds_io_write_float(io, map->blur);
753 { /*---- LIB3DS_MAT_MAP_USCALE ----*/
755 c.chunk=LIB3DS_MAT_MAP_USCALE;
757 lib3ds_chunk_write(&c,io);
758 lib3ds_io_write_float(io, map->scale[0]);
761 { /*---- LIB3DS_MAT_MAP_VSCALE ----*/
763 c.chunk=LIB3DS_MAT_MAP_VSCALE;
765 lib3ds_chunk_write(&c,io);
766 lib3ds_io_write_float(io, map->scale[1]);
769 { /*---- LIB3DS_MAT_MAP_UOFFSET ----*/
771 c.chunk=LIB3DS_MAT_MAP_UOFFSET;
773 lib3ds_chunk_write(&c,io);
774 lib3ds_io_write_float(io, map->offset[0]);
777 { /*---- LIB3DS_MAT_MAP_VOFFSET ----*/
779 c.chunk=LIB3DS_MAT_MAP_VOFFSET;
781 lib3ds_chunk_write(&c,io);
782 lib3ds_io_write_float(io, map->offset[1]);
785 { /*---- LIB3DS_MAT_MAP_ANG ----*/
787 c.chunk=LIB3DS_MAT_MAP_ANG;
789 lib3ds_chunk_write(&c,io);
790 lib3ds_io_write_float(io, map->rotation);
793 { /*---- LIB3DS_MAT_MAP_COL1 ----*/
795 c.chunk=LIB3DS_MAT_MAP_COL1;
797 lib3ds_chunk_write(&c,io);
798 lib3ds_io_write_byte(io, (Lib3dsByte)floor(255.0*map->tint_1[0]+0.5));
799 lib3ds_io_write_byte(io, (Lib3dsByte)floor(255.0*map->tint_1[1]+0.5));
800 lib3ds_io_write_byte(io, (Lib3dsByte)floor(255.0*map->tint_1[2]+0.5));
803 { /*---- LIB3DS_MAT_MAP_COL2 ----*/
805 c.chunk=LIB3DS_MAT_MAP_COL2;
807 lib3ds_chunk_write(&c,io);
808 lib3ds_io_write_byte(io, (Lib3dsByte)floor(255.0*map->tint_2[0]+0.5));
809 lib3ds_io_write_byte(io, (Lib3dsByte)floor(255.0*map->tint_2[1]+0.5));
810 lib3ds_io_write_byte(io, (Lib3dsByte)floor(255.0*map->tint_2[2]+0.5));
813 { /*---- LIB3DS_MAT_MAP_RCOL ----*/
815 c.chunk=LIB3DS_MAT_MAP_RCOL;
817 lib3ds_chunk_write(&c,io);
818 lib3ds_io_write_byte(io, (Lib3dsByte)floor(255.0*map->tint_r[0]+0.5));
819 lib3ds_io_write_byte(io, (Lib3dsByte)floor(255.0*map->tint_r[1]+0.5));
820 lib3ds_io_write_byte(io, (Lib3dsByte)floor(255.0*map->tint_r[2]+0.5));
823 { /*---- LIB3DS_MAT_MAP_GCOL ----*/
825 c.chunk=LIB3DS_MAT_MAP_GCOL;
827 lib3ds_chunk_write(&c,io);
828 lib3ds_io_write_byte(io, (Lib3dsByte)floor(255.0*map->tint_g[0]+0.5));
829 lib3ds_io_write_byte(io, (Lib3dsByte)floor(255.0*map->tint_g[1]+0.5));
830 lib3ds_io_write_byte(io, (Lib3dsByte)floor(255.0*map->tint_g[2]+0.5));
833 { /*---- LIB3DS_MAT_MAP_BCOL ----*/
835 c.chunk=LIB3DS_MAT_MAP_BCOL;
837 lib3ds_chunk_write(&c,io);
838 lib3ds_io_write_byte(io, (Lib3dsByte)floor(255.0*map->tint_b[0]+0.5));
839 lib3ds_io_write_byte(io, (Lib3dsByte)floor(255.0*map->tint_b[1]+0.5));
840 lib3ds_io_write_byte(io, (Lib3dsByte)floor(255.0*map->tint_b[2]+0.5));
843 if (!lib3ds_chunk_write_end(&c,io)) {
844 return(LIB3DS_FALSE);
854 lib3ds_material_write(Lib3dsMaterial *material, Lib3dsIo *io)
858 c.chunk=LIB3DS_MAT_ENTRY;
859 if (!lib3ds_chunk_write_start(&c,io)) {
860 return(LIB3DS_FALSE);
863 { /*---- LIB3DS_MAT_NAME ----*/
865 c.chunk=LIB3DS_MAT_NAME;
866 c.size=6+strlen(material->name)+1;
867 lib3ds_chunk_write(&c,io);
868 lib3ds_io_write_string(io, material->name);
871 { /*---- LIB3DS_MAT_AMBIENT ----*/
873 c.chunk=LIB3DS_MAT_AMBIENT;
875 lib3ds_chunk_write(&c,io);
876 color_write(material->ambient,io);
879 { /*---- LIB3DS_MAT_DIFFUSE ----*/
881 c.chunk=LIB3DS_MAT_DIFFUSE;
883 lib3ds_chunk_write(&c,io);
884 color_write(material->diffuse,io);
887 { /*---- LIB3DS_MAT_SPECULAR ----*/
889 c.chunk=LIB3DS_MAT_SPECULAR;
891 lib3ds_chunk_write(&c,io);
892 color_write(material->specular,io);
895 { /*---- LIB3DS_MAT_SHININESS ----*/
897 c.chunk=LIB3DS_MAT_SHININESS;
899 lib3ds_chunk_write(&c,io);
900 int_percentage_write(material->shininess,io);
903 { /*---- LIB3DS_MAT_SHIN2PCT ----*/
905 c.chunk=LIB3DS_MAT_SHIN2PCT;
907 lib3ds_chunk_write(&c,io);
908 int_percentage_write(material->shin_strength,io);
911 { /*---- LIB3DS_MAT_TRANSPARENCY ----*/
913 c.chunk=LIB3DS_MAT_TRANSPARENCY;
915 lib3ds_chunk_write(&c,io);
916 int_percentage_write(material->transparency,io);
919 { /*---- LIB3DS_MAT_XPFALL ----*/
921 c.chunk=LIB3DS_MAT_XPFALL;
923 lib3ds_chunk_write(&c,io);
924 int_percentage_write(material->falloff,io);
927 if (material->use_falloff) { /*---- LIB3DS_MAT_USE_XPFALL ----*/
929 c.chunk=LIB3DS_MAT_USE_XPFALL;
931 lib3ds_chunk_write(&c,io);
934 { /*---- LIB3DS_MAT_SHADING ----*/
936 c.chunk=LIB3DS_MAT_SHADING;
938 lib3ds_chunk_write(&c,io);
939 lib3ds_io_write_intw(io, material->shading);
942 { /*---- LIB3DS_MAT_REFBLUR ----*/
944 c.chunk=LIB3DS_MAT_REFBLUR;
946 lib3ds_chunk_write(&c,io);
947 int_percentage_write(material->blur,io);
950 if (material->use_blur) { /*---- LIB3DS_MAT_USE_REFBLUR ----*/
952 c.chunk=LIB3DS_MAT_USE_REFBLUR;
954 lib3ds_chunk_write(&c,io);
957 if (material->self_illum) { /*---- LIB3DS_MAT_SELF_ILLUM ----*/
959 c.chunk=LIB3DS_MAT_SELF_ILLUM;
961 lib3ds_chunk_write(&c,io);
964 if (material->two_sided) { /*---- LIB3DS_MAT_TWO_SIDE ----*/
966 c.chunk=LIB3DS_MAT_TWO_SIDE;
968 lib3ds_chunk_write(&c,io);
971 if (material->map_decal) { /*---- LIB3DS_MAT_DECAL ----*/
973 c.chunk=LIB3DS_MAT_DECAL;
975 lib3ds_chunk_write(&c,io);
978 if (material->additive) { /*---- LIB3DS_MAT_ADDITIVE ----*/
980 c.chunk=LIB3DS_MAT_ADDITIVE;
982 lib3ds_chunk_write(&c,io);
985 if (material->use_wire) { /*---- LIB3DS_MAT_WIRE ----*/
987 c.chunk=LIB3DS_MAT_WIRE;
989 lib3ds_chunk_write(&c,io);
992 if (material->use_wire_abs) { /*---- LIB3DS_MAT_WIREABS ----*/
994 c.chunk=LIB3DS_MAT_WIREABS;
996 lib3ds_chunk_write(&c,io);
999 { /*---- LIB3DS_MAT_WIRE_SIZE ----*/
1001 c.chunk=LIB3DS_MAT_WIRE_SIZE;
1003 lib3ds_chunk_write(&c,io);
1004 lib3ds_io_write_float(io, material->wire_size);
1007 if (material->face_map) { /*---- LIB3DS_MAT_FACEMAP ----*/
1009 c.chunk=LIB3DS_MAT_FACEMAP;
1011 lib3ds_chunk_write(&c,io);
1014 if (material->soften) { /*---- LIB3DS_MAT_PHONGSOFT ----*/
1016 c.chunk=LIB3DS_MAT_PHONGSOFT;
1018 lib3ds_chunk_write(&c,io);
1021 if (!texture_map_write(LIB3DS_MAT_TEXMAP, &material->texture1_map, io)) {
1022 return(LIB3DS_FALSE);
1024 if (!texture_map_write(LIB3DS_MAT_TEXMASK, &material->texture1_mask, io)) {
1025 return(LIB3DS_FALSE);
1027 if (!texture_map_write(LIB3DS_MAT_TEX2MAP, &material->texture2_map, io)) {
1028 return(LIB3DS_FALSE);
1030 if (!texture_map_write(LIB3DS_MAT_TEX2MASK, &material->texture2_mask, io)) {
1031 return(LIB3DS_FALSE);
1033 if (!texture_map_write(LIB3DS_MAT_OPACMAP, &material->opacity_map, io)) {
1034 return(LIB3DS_FALSE);
1036 if (!texture_map_write(LIB3DS_MAT_OPACMASK, &material->opacity_mask, io)) {
1037 return(LIB3DS_FALSE);
1039 if (!texture_map_write(LIB3DS_MAT_BUMPMAP, &material->bump_map, io)) {
1040 return(LIB3DS_FALSE);
1042 if (!texture_map_write(LIB3DS_MAT_BUMPMASK, &material->bump_mask, io)) {
1043 return(LIB3DS_FALSE);
1045 if (!texture_map_write(LIB3DS_MAT_SPECMAP, &material->specular_map, io)) {
1046 return(LIB3DS_FALSE);
1048 if (!texture_map_write(LIB3DS_MAT_SPECMASK, &material->specular_mask, io)) {
1049 return(LIB3DS_FALSE);
1051 if (!texture_map_write(LIB3DS_MAT_SHINMAP, &material->shininess_map, io)) {
1052 return(LIB3DS_FALSE);
1054 if (!texture_map_write(LIB3DS_MAT_SHINMASK, &material->shininess_mask, io)) {
1055 return(LIB3DS_FALSE);
1057 if (!texture_map_write(LIB3DS_MAT_SELFIMAP, &material->self_illum_map, io)) {
1058 return(LIB3DS_FALSE);
1060 if (!texture_map_write(LIB3DS_MAT_SELFIMASK, &material->self_illum_mask, io)) {
1061 return(LIB3DS_FALSE);
1063 if (!texture_map_write(LIB3DS_MAT_REFLMAP, &material->reflection_map, io)) {
1064 return(LIB3DS_FALSE);
1066 if (!texture_map_write(LIB3DS_MAT_REFLMASK, &material->reflection_mask, io)) {
1067 return(LIB3DS_FALSE);
1070 if (!lib3ds_chunk_write_end(&c,io)) {
1071 return(LIB3DS_FALSE);
1073 return(LIB3DS_TRUE);
1079 \typedef Lib3dsMaterial