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: background.c,v 1.8 2001/07/07 19:05:30 jeh Exp $
23 #include <lib3ds/background.h>
24 #include <lib3ds/chunk.h>
25 #include <lib3ds/io.h>
31 * \defgroup background Background Settings
33 * \author J.E. Hoffmann <je-h@gmx.net>
38 solid_bgnd_read(Lib3dsBackground *background, Lib3dsIo *io)
42 Lib3dsBool have_lin=LIB3DS_FALSE;
44 if (!lib3ds_chunk_read_start(&c, LIB3DS_SOLID_BGND, io)) {
48 while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
50 case LIB3DS_LIN_COLOR_F:
51 lib3ds_io_read_rgb(io, background->solid.col);
55 lib3ds_io_read_rgb(io, background->solid.col);
58 lib3ds_chunk_unknown(chunk);
62 lib3ds_chunk_read_end(&c, io);
68 v_gradient_read(Lib3dsBackground *background, Lib3dsIo *io)
77 if (!lib3ds_chunk_read_start(&c, LIB3DS_V_GRADIENT, io)) {
80 background->gradient.percent=lib3ds_io_read_float(io);
81 lib3ds_chunk_read_tell(&c, io);
84 while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
87 lib3ds_io_read_rgb(io, col[0][index[0]]);
90 case LIB3DS_LIN_COLOR_F:
91 lib3ds_io_read_rgb(io, col[1][index[1]]);
96 lib3ds_chunk_unknown(chunk);
101 for (i=0; i<3; ++i) {
102 background->gradient.top[i]=col[have_lin][0][i];
103 background->gradient.middle[i]=col[have_lin][1][i];
104 background->gradient.bottom[i]=col[have_lin][2][i];
107 lib3ds_chunk_read_end(&c, io);
113 * \ingroup background
116 lib3ds_background_read(Lib3dsBackground *background, Lib3dsIo *io)
120 if (!lib3ds_chunk_read(&c, io)) {
121 return(LIB3DS_FALSE);
127 if (!lib3ds_io_read_string(io, background->bitmap.name, 64)) {
128 return(LIB3DS_FALSE);
132 case LIB3DS_SOLID_BGND:
134 lib3ds_chunk_read_reset(&c, io);
135 if (!solid_bgnd_read(background, io)) {
136 return(LIB3DS_FALSE);
140 case LIB3DS_V_GRADIENT:
142 lib3ds_chunk_read_reset(&c, io);
143 if (!v_gradient_read(background, io)) {
144 return(LIB3DS_FALSE);
148 case LIB3DS_USE_BIT_MAP:
150 background->bitmap.use=LIB3DS_TRUE;
153 case LIB3DS_USE_SOLID_BGND:
155 background->solid.use=LIB3DS_TRUE;
158 case LIB3DS_USE_V_GRADIENT:
160 background->gradient.use=LIB3DS_TRUE;
170 colorf_write(Lib3dsRgba rgb, Lib3dsIo *io)
174 c.chunk=LIB3DS_COLOR_F;
176 lib3ds_chunk_write(&c,io);
177 lib3ds_io_write_rgb(io, rgb);
179 c.chunk=LIB3DS_LIN_COLOR_F;
181 lib3ds_chunk_write(&c,io);
182 lib3ds_io_write_rgb(io, rgb);
188 colorf_defined(Lib3dsRgba rgb)
191 for (i=0; i<3; ++i) {
192 if (fabs(rgb[i])>LIB3DS_EPSILON) {
201 * \ingroup background
204 lib3ds_background_write(Lib3dsBackground *background, Lib3dsIo *io)
206 if (strlen(background->bitmap.name)) { /*---- LIB3DS_BIT_MAP ----*/
208 c.chunk=LIB3DS_BIT_MAP;
209 c.size=6+1+strlen(background->bitmap.name);
210 lib3ds_chunk_write(&c,io);
211 lib3ds_io_write_string(io, background->bitmap.name);
214 if (colorf_defined(background->solid.col)) { /*---- LIB3DS_SOLID_BGND ----*/
216 c.chunk=LIB3DS_SOLID_BGND;
218 lib3ds_chunk_write(&c,io);
219 colorf_write(background->solid.col, io);
222 if (colorf_defined(background->gradient.top) ||
223 colorf_defined(background->gradient.middle) ||
224 colorf_defined(background->gradient.bottom)) { /*---- LIB3DS_V_GRADIENT ----*/
226 c.chunk=LIB3DS_V_GRADIENT;
228 lib3ds_chunk_write(&c,io);
229 lib3ds_io_write_float(io, background->gradient.percent);
230 colorf_write(background->gradient.top,io);
231 colorf_write(background->gradient.middle,io);
232 colorf_write(background->gradient.bottom,io);
235 if (background->bitmap.use) { /*---- LIB3DS_USE_BIT_MAP ----*/
237 c.chunk=LIB3DS_USE_BIT_MAP;
239 lib3ds_chunk_write(&c,io);
242 if (background->solid.use) { /*---- LIB3DS_USE_SOLID_BGND ----*/
244 c.chunk=LIB3DS_USE_SOLID_BGND;
246 lib3ds_chunk_write(&c,io);
249 if (background->gradient.use) { /*---- LIB3DS_USE_V_GRADIENT ----*/
251 c.chunk=LIB3DS_USE_V_GRADIENT;
253 lib3ds_chunk_write(&c,io);
262 \typedef Lib3dsBackground
264 \sa _Lib3dsBackground