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)
43 if (!lib3ds_chunk_read_start(&c, LIB3DS_SOLID_BGND, io)) {
47 while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
49 case LIB3DS_LIN_COLOR_F:
50 lib3ds_io_read_rgb(io, background->solid.col);
53 lib3ds_io_read_rgb(io, background->solid.col);
56 lib3ds_chunk_unknown(chunk);
60 lib3ds_chunk_read_end(&c, io);
66 v_gradient_read(Lib3dsBackground *background, Lib3dsIo *io)
75 if (!lib3ds_chunk_read_start(&c, LIB3DS_V_GRADIENT, io)) {
78 background->gradient.percent=lib3ds_io_read_float(io);
79 lib3ds_chunk_read_tell(&c, io);
82 while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
85 lib3ds_io_read_rgb(io, col[0][index[0]]);
88 case LIB3DS_LIN_COLOR_F:
89 lib3ds_io_read_rgb(io, col[1][index[1]]);
94 lib3ds_chunk_unknown(chunk);
100 background->gradient.top[i]=col[have_lin][0][i];
101 background->gradient.middle[i]=col[have_lin][1][i];
102 background->gradient.bottom[i]=col[have_lin][2][i];
105 lib3ds_chunk_read_end(&c, io);
111 * \ingroup background
114 lib3ds_background_read(Lib3dsBackground *background, Lib3dsIo *io)
118 if (!lib3ds_chunk_read(&c, io)) {
119 return(LIB3DS_FALSE);
125 if (!lib3ds_io_read_string(io, background->bitmap.name, 64)) {
126 return(LIB3DS_FALSE);
130 case LIB3DS_SOLID_BGND:
132 lib3ds_chunk_read_reset(&c, io);
133 if (!solid_bgnd_read(background, io)) {
134 return(LIB3DS_FALSE);
138 case LIB3DS_V_GRADIENT:
140 lib3ds_chunk_read_reset(&c, io);
141 if (!v_gradient_read(background, io)) {
142 return(LIB3DS_FALSE);
146 case LIB3DS_USE_BIT_MAP:
148 background->bitmap.use=LIB3DS_TRUE;
151 case LIB3DS_USE_SOLID_BGND:
153 background->solid.use=LIB3DS_TRUE;
156 case LIB3DS_USE_V_GRADIENT:
158 background->gradient.use=LIB3DS_TRUE;
168 colorf_write(Lib3dsRgba rgb, Lib3dsIo *io)
172 c.chunk=LIB3DS_COLOR_F;
174 lib3ds_chunk_write(&c,io);
175 lib3ds_io_write_rgb(io, rgb);
177 c.chunk=LIB3DS_LIN_COLOR_F;
179 lib3ds_chunk_write(&c,io);
180 lib3ds_io_write_rgb(io, rgb);
186 colorf_defined(Lib3dsRgba rgb)
189 for (i=0; i<3; ++i) {
190 if (fabs(rgb[i])>LIB3DS_EPSILON) {
199 * \ingroup background
202 lib3ds_background_write(Lib3dsBackground *background, Lib3dsIo *io)
204 if (strlen(background->bitmap.name)) { /*---- LIB3DS_BIT_MAP ----*/
206 c.chunk=LIB3DS_BIT_MAP;
207 c.size=6+1+strlen(background->bitmap.name);
208 lib3ds_chunk_write(&c,io);
209 lib3ds_io_write_string(io, background->bitmap.name);
212 if (colorf_defined(background->solid.col)) { /*---- LIB3DS_SOLID_BGND ----*/
214 c.chunk=LIB3DS_SOLID_BGND;
216 lib3ds_chunk_write(&c,io);
217 colorf_write(background->solid.col, io);
220 if (colorf_defined(background->gradient.top) ||
221 colorf_defined(background->gradient.middle) ||
222 colorf_defined(background->gradient.bottom)) { /*---- LIB3DS_V_GRADIENT ----*/
224 c.chunk=LIB3DS_V_GRADIENT;
226 lib3ds_chunk_write(&c,io);
227 lib3ds_io_write_float(io, background->gradient.percent);
228 colorf_write(background->gradient.top,io);
229 colorf_write(background->gradient.middle,io);
230 colorf_write(background->gradient.bottom,io);
233 if (background->bitmap.use) { /*---- LIB3DS_USE_BIT_MAP ----*/
235 c.chunk=LIB3DS_USE_BIT_MAP;
237 lib3ds_chunk_write(&c,io);
240 if (background->solid.use) { /*---- LIB3DS_USE_SOLID_BGND ----*/
242 c.chunk=LIB3DS_USE_SOLID_BGND;
244 lib3ds_chunk_write(&c,io);
247 if (background->gradient.use) { /*---- LIB3DS_USE_V_GRADIENT ----*/
249 c.chunk=LIB3DS_USE_V_GRADIENT;
251 lib3ds_chunk_write(&c,io);
260 \typedef Lib3dsBackground
262 \sa _Lib3dsBackground