VC project files
[summerhack] / libs / lib3ds / atmosphere.c
1 /*
2  * The 3D Studio File Format Library
3  * Copyright (C) 1996-2001 by J.E. Hoffmann <je-h@gmx.net>
4  * All rights reserved.
5  *
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.
10  *
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.
15  *
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.
19  *
20  * $Id: atmosphere.c,v 1.9 2001/07/07 19:05:30 jeh Exp $
21  */
22 #define LIB3DS_EXPORT
23 #include <lib3ds/atmosphere.h>
24 #include <lib3ds/chunk.h>
25 #include <lib3ds/io.h>
26
27
28 /*!
29  * \defgroup atmosphere Atmosphere Settings
30  *
31  * \author J.E. Hoffmann <je-h@gmx.net>
32  */
33
34
35 static Lib3dsBool
36 fog_read(Lib3dsFog *fog, Lib3dsIo *io)
37 {
38   Lib3dsChunk c;
39   Lib3dsWord chunk;
40
41   if (!lib3ds_chunk_read_start(&c, LIB3DS_FOG, io)) {
42     return(LIB3DS_FALSE);
43   }
44   fog->near_plane = lib3ds_io_read_float(io);
45   fog->near_density=lib3ds_io_read_float(io);
46   fog->far_plane=lib3ds_io_read_float(io);
47   fog->far_density=lib3ds_io_read_float(io);
48   lib3ds_chunk_read_tell(&c, io);
49   
50   while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
51     switch (chunk) {
52       case LIB3DS_LIN_COLOR_F:
53         {
54           int i;
55           for (i=0; i<3; ++i) {
56             fog->col[i]=lib3ds_io_read_float(io);
57           }
58         }
59         break;
60       case LIB3DS_COLOR_F:
61         break;
62       case LIB3DS_FOG_BGND:
63         {
64           fog->fog_background=LIB3DS_TRUE;
65         }
66         break;
67       default:
68         lib3ds_chunk_unknown(chunk);
69     }
70   }
71   
72   lib3ds_chunk_read_end(&c, io);
73   return(LIB3DS_TRUE);
74 }
75
76
77 static Lib3dsBool
78 layer_fog_read(Lib3dsLayerFog *fog, Lib3dsIo *io)
79 {
80   Lib3dsChunk c;
81   Lib3dsWord chunk;
82
83   if (!lib3ds_chunk_read_start(&c, LIB3DS_LAYER_FOG, io)) {
84     return(LIB3DS_FALSE);
85   }
86   fog->near_y=lib3ds_io_read_float(io);
87   fog->far_y=lib3ds_io_read_float(io);
88   fog->density=lib3ds_io_read_float(io);
89   fog->flags=lib3ds_io_read_dword(io);
90   lib3ds_chunk_read_tell(&c, io);
91   
92   while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
93     switch (chunk) {
94       case LIB3DS_LIN_COLOR_F:
95         lib3ds_io_read_rgb(io, fog->col);
96         break;
97       case LIB3DS_COLOR_F:
98         lib3ds_io_read_rgb(io, fog->col);
99         break;
100       default:
101         lib3ds_chunk_unknown(chunk);
102     }
103   }
104   
105   lib3ds_chunk_read_end(&c, io);
106   return(LIB3DS_TRUE);
107 }
108
109
110 static Lib3dsBool
111 distance_cue_read(Lib3dsDistanceCue *cue, Lib3dsIo *io)
112 {
113   Lib3dsChunk c;
114   Lib3dsWord chunk;
115
116   if (!lib3ds_chunk_read_start(&c, LIB3DS_DISTANCE_CUE, io)) {
117     return(LIB3DS_FALSE);
118   }
119   cue->near_plane=lib3ds_io_read_float(io);
120   cue->near_dimming=lib3ds_io_read_float(io);
121   cue->far_plane=lib3ds_io_read_float(io);
122   cue->far_dimming=lib3ds_io_read_float(io);
123   lib3ds_chunk_read_tell(&c, io);
124   
125   while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
126     switch (chunk) {
127       case LIB3DS_DCUE_BGND:
128         {
129           cue->cue_background=LIB3DS_TRUE;
130         }
131         break;
132       default:
133         lib3ds_chunk_unknown(chunk);
134     }
135   }
136   
137   lib3ds_chunk_read_end(&c, io);
138   return(LIB3DS_TRUE);
139 }
140
141
142 /*!
143  * \ingroup atmosphere
144  */
145 Lib3dsBool
146 lib3ds_atmosphere_read(Lib3dsAtmosphere *atmosphere, Lib3dsIo *io)
147 {
148   Lib3dsChunk c;
149
150   if (!lib3ds_chunk_read(&c, io)) {
151     return(LIB3DS_FALSE);
152   }
153   
154   switch (c.chunk) {
155       case LIB3DS_FOG:
156         {
157           lib3ds_chunk_read_reset(&c, io);
158           if (!fog_read(&atmosphere->fog, io)) {
159             return(LIB3DS_FALSE);
160           }
161         }
162         break;
163       case LIB3DS_LAYER_FOG:
164         {
165           lib3ds_chunk_read_reset(&c, io);
166           if (!layer_fog_read(&atmosphere->layer_fog, io)) {
167             return(LIB3DS_FALSE);
168           }
169         }
170         break;
171       case LIB3DS_DISTANCE_CUE:
172         {
173           lib3ds_chunk_read_reset(&c, io);
174           if (!distance_cue_read(&atmosphere->dist_cue, io)) {
175             return(LIB3DS_FALSE);
176           }
177         }
178         break;
179       case LIB3DS_USE_FOG:
180         {
181           atmosphere->fog.use=LIB3DS_TRUE;
182         }
183         break;
184       case LIB3DS_USE_LAYER_FOG:
185         {
186           atmosphere->fog.use=LIB3DS_TRUE;
187         }
188         break;
189       case LIB3DS_USE_DISTANCE_CUE:
190         {
191           atmosphere->dist_cue.use=LIB3DS_TRUE;
192         }
193         break;
194   }
195
196   return(LIB3DS_TRUE);
197 }
198
199
200 /*!
201  * \ingroup atmosphere
202  */
203 Lib3dsBool
204 lib3ds_atmosphere_write(Lib3dsAtmosphere *atmosphere, Lib3dsIo *io)
205 {
206   if (atmosphere->fog.use) { /*---- LIB3DS_FOG ----*/
207     Lib3dsChunk c;
208     c.chunk=LIB3DS_FOG;
209     if (!lib3ds_chunk_write_start(&c,io)) {
210       return(LIB3DS_FALSE);
211     }
212     lib3ds_io_write_float(io, atmosphere->fog.near_plane);
213     lib3ds_io_write_float(io, atmosphere->fog.near_density);
214     lib3ds_io_write_float(io, atmosphere->fog.far_plane);
215     lib3ds_io_write_float(io, atmosphere->fog.far_density);
216     {
217       Lib3dsChunk c;
218       c.chunk=LIB3DS_COLOR_F;
219       c.size=18;
220       lib3ds_chunk_write(&c,io);
221       lib3ds_io_write_rgb(io, atmosphere->fog.col);
222     }
223     if (atmosphere->fog.fog_background) {
224       Lib3dsChunk c;
225       c.chunk=LIB3DS_FOG_BGND;
226       c.size=6;
227       lib3ds_chunk_write(&c,io);
228     }
229     if (!lib3ds_chunk_write_end(&c,io)) {
230       return(LIB3DS_FALSE);
231     }
232   }
233
234   if (atmosphere->layer_fog.use) { /*---- LIB3DS_LAYER_FOG ----*/
235     Lib3dsChunk c;
236     c.chunk=LIB3DS_LAYER_FOG;
237     c.size=40;
238     lib3ds_chunk_write(&c,io);
239     lib3ds_io_write_float(io, atmosphere->layer_fog.near_y);
240     lib3ds_io_write_float(io, atmosphere->layer_fog.far_y);
241     lib3ds_io_write_float(io, atmosphere->layer_fog.near_y);
242     lib3ds_io_write_dword(io, atmosphere->layer_fog.flags);
243     {
244       Lib3dsChunk c;
245       c.chunk=LIB3DS_COLOR_F;
246       c.size=18;
247       lib3ds_chunk_write(&c,io);
248       lib3ds_io_write_rgb(io, atmosphere->fog.col);
249     }
250   }
251
252   if (atmosphere->dist_cue.use) { /*---- LIB3DS_DISTANCE_CUE ----*/
253     Lib3dsChunk c;
254     c.chunk=LIB3DS_DISTANCE_CUE;
255     if (!lib3ds_chunk_write_start(&c,io)) {
256       return(LIB3DS_FALSE);
257     }
258     lib3ds_io_write_float(io, atmosphere->dist_cue.near_plane);
259     lib3ds_io_write_float(io, atmosphere->dist_cue.near_dimming);
260     lib3ds_io_write_float(io, atmosphere->dist_cue.far_plane);
261     lib3ds_io_write_float(io, atmosphere->dist_cue.far_dimming);
262     if (atmosphere->dist_cue.cue_background) {
263       Lib3dsChunk c;
264       c.chunk=LIB3DS_DCUE_BGND;
265       c.size=6;
266       lib3ds_chunk_write(&c,io);
267     }
268     if (!lib3ds_chunk_write_end(&c,io)) {
269       return(LIB3DS_FALSE);
270     }
271   }
272
273   if (atmosphere->fog.use) { /*---- LIB3DS_USE_FOG ----*/
274     Lib3dsChunk c;
275     c.chunk=LIB3DS_USE_FOG;
276     c.size=6;
277     lib3ds_chunk_write(&c,io);
278   }
279
280   if (atmosphere->layer_fog.use) { /*---- LIB3DS_USE_LAYER_FOG ----*/
281     Lib3dsChunk c;
282     c.chunk=LIB3DS_USE_LAYER_FOG;
283     c.size=6;
284     lib3ds_chunk_write(&c,io);
285   }
286
287   if (atmosphere->dist_cue.use) { /*---- LIB3DS_USE_DISTANCE_CUE ----*/
288     Lib3dsChunk c;
289     c.chunk=LIB3DS_USE_V_GRADIENT;
290     c.size=6;
291     lib3ds_chunk_write(&c,io);
292   }
293   
294   return(LIB3DS_TRUE);
295 }
296
297
298 /*!
299
300 \typedef Lib3dsAtmosphere
301   \ingroup atmosphere
302   \sa _Lib3dsAtmosphere
303
304 */
305