C:/jkyprian/devel/lib3ds/lib3ds/atmosphere.c

00001 /*
00002  * The 3D Studio File Format Library
00003  * Copyright (C) 1996-2007 by Jan Eric Kyprianidis <www.kyprianidis.com>
00004  * All rights reserved.
00005  *
00006  * This program is  free  software;  you can redistribute it and/or modify it
00007  * under the terms of the  GNU Lesser General Public License  as published by 
00008  * the  Free Software Foundation;  either version 2.1 of the License,  or (at 
00009  * your option) any later version.
00010  *
00011  * This  program  is  distributed in  the  hope that it will  be useful,  but
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00013  * or  FITNESS FOR A  PARTICULAR PURPOSE.  See the  GNU Lesser General Public  
00014  * License for more details.
00015  *
00016  * You should  have received  a copy of the GNU Lesser General Public License
00017  * along with  this program;  if not, write to the  Free Software Foundation,
00018  * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  *
00020  * $Id: atmosphere.c,v 1.12 2007/06/20 17:04:08 jeh Exp $
00021  */
00022 #include <lib3ds/atmosphere.h>
00023 #include <lib3ds/chunk.h>
00024 #include <lib3ds/io.h>
00025 
00026 
00032 static Lib3dsBool
00033 fog_read(Lib3dsFog *fog, Lib3dsIo *io)
00034 {
00035   Lib3dsChunk c;
00036   Lib3dsWord chunk;
00037 
00038   if (!lib3ds_chunk_read_start(&c, LIB3DS_FOG, io)) {
00039     return(LIB3DS_FALSE);
00040   }
00041   fog->near_plane = lib3ds_io_read_float(io);
00042   fog->near_density=lib3ds_io_read_float(io);
00043   fog->far_plane=lib3ds_io_read_float(io);
00044   fog->far_density=lib3ds_io_read_float(io);
00045   lib3ds_chunk_read_tell(&c, io);
00046   
00047   while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
00048     switch (chunk) {
00049       case LIB3DS_LIN_COLOR_F:
00050         {
00051           int i;
00052           for (i=0; i<3; ++i) {
00053             fog->col[i]=lib3ds_io_read_float(io);
00054           }
00055         }
00056         break;
00057       case LIB3DS_COLOR_F:
00058         break;
00059       case LIB3DS_FOG_BGND:
00060         {
00061           fog->fog_background=LIB3DS_TRUE;
00062         }
00063         break;
00064       default:
00065         lib3ds_chunk_unknown(chunk);
00066     }
00067   }
00068   
00069   lib3ds_chunk_read_end(&c, io);
00070   return(LIB3DS_TRUE);
00071 }
00072 
00073 
00074 static Lib3dsBool
00075 layer_fog_read(Lib3dsLayerFog *fog, Lib3dsIo *io)
00076 {
00077   Lib3dsChunk c;
00078   Lib3dsWord chunk;
00079   Lib3dsBool have_lin=LIB3DS_FALSE;
00080 
00081   if (!lib3ds_chunk_read_start(&c, LIB3DS_LAYER_FOG, io)) {
00082     return(LIB3DS_FALSE);
00083   }
00084   fog->near_y=lib3ds_io_read_float(io);
00085   fog->far_y=lib3ds_io_read_float(io);
00086   fog->density=lib3ds_io_read_float(io);
00087   fog->flags=lib3ds_io_read_dword(io);
00088   lib3ds_chunk_read_tell(&c, io);
00089   
00090   while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
00091     switch (chunk) {
00092       case LIB3DS_LIN_COLOR_F:
00093         lib3ds_io_read_rgb(io, fog->col);
00094         have_lin=LIB3DS_TRUE;
00095         break;
00096       case LIB3DS_COLOR_F:
00097         lib3ds_io_read_rgb(io, fog->col);
00098         break;
00099       default:
00100         lib3ds_chunk_unknown(chunk);
00101     }
00102   }
00103   
00104   lib3ds_chunk_read_end(&c, io);
00105   return(LIB3DS_TRUE);
00106 }
00107 
00108 
00109 static Lib3dsBool
00110 distance_cue_read(Lib3dsDistanceCue *cue, Lib3dsIo *io)
00111 {
00112   Lib3dsChunk c;
00113   Lib3dsWord chunk;
00114 
00115   if (!lib3ds_chunk_read_start(&c, LIB3DS_DISTANCE_CUE, io)) {
00116     return(LIB3DS_FALSE);
00117   }
00118   cue->near_plane=lib3ds_io_read_float(io);
00119   cue->near_dimming=lib3ds_io_read_float(io);
00120   cue->far_plane=lib3ds_io_read_float(io);
00121   cue->far_dimming=lib3ds_io_read_float(io);
00122   lib3ds_chunk_read_tell(&c, io);
00123   
00124   while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
00125     switch (chunk) {
00126       case LIB3DS_DCUE_BGND:
00127         {
00128           cue->cue_background=LIB3DS_TRUE;
00129         }
00130         break;
00131       default:
00132         lib3ds_chunk_unknown(chunk);
00133     }
00134   }
00135   
00136   lib3ds_chunk_read_end(&c, io);
00137   return(LIB3DS_TRUE);
00138 }
00139 
00140 
00144 Lib3dsBool
00145 lib3ds_atmosphere_read(Lib3dsAtmosphere *atmosphere, Lib3dsIo *io)
00146 {
00147   Lib3dsChunk c;
00148 
00149   if (!lib3ds_chunk_read(&c, io)) {
00150     return(LIB3DS_FALSE);
00151   }
00152   
00153   switch (c.chunk) {
00154       case LIB3DS_FOG:
00155         {
00156           lib3ds_chunk_read_reset(&c, io);
00157           if (!fog_read(&atmosphere->fog, io)) {
00158             return(LIB3DS_FALSE);
00159           }
00160         }
00161         break;
00162       case LIB3DS_LAYER_FOG:
00163         {
00164           lib3ds_chunk_read_reset(&c, io);
00165           if (!layer_fog_read(&atmosphere->layer_fog, io)) {
00166             return(LIB3DS_FALSE);
00167           }
00168         }
00169         break;
00170       case LIB3DS_DISTANCE_CUE:
00171         {
00172           lib3ds_chunk_read_reset(&c, io);
00173           if (!distance_cue_read(&atmosphere->dist_cue, io)) {
00174             return(LIB3DS_FALSE);
00175           }
00176         }
00177         break;
00178       case LIB3DS_USE_FOG:
00179         {
00180           atmosphere->fog.use=LIB3DS_TRUE;
00181         }
00182         break;
00183       case LIB3DS_USE_LAYER_FOG:
00184         {
00185           atmosphere->fog.use=LIB3DS_TRUE;
00186         }
00187         break;
00188       case LIB3DS_USE_DISTANCE_CUE:
00189         {
00190           atmosphere->dist_cue.use=LIB3DS_TRUE;
00191         }
00192         break;
00193   }
00194 
00195   return(LIB3DS_TRUE);
00196 }
00197 
00198 
00202 Lib3dsBool
00203 lib3ds_atmosphere_write(Lib3dsAtmosphere *atmosphere, Lib3dsIo *io)
00204 {
00205   if (atmosphere->fog.use) { /*---- LIB3DS_FOG ----*/
00206     Lib3dsChunk c;
00207     c.chunk=LIB3DS_FOG;
00208     if (!lib3ds_chunk_write_start(&c,io)) {
00209       return(LIB3DS_FALSE);
00210     }
00211     lib3ds_io_write_float(io, atmosphere->fog.near_plane);
00212     lib3ds_io_write_float(io, atmosphere->fog.near_density);
00213     lib3ds_io_write_float(io, atmosphere->fog.far_plane);
00214     lib3ds_io_write_float(io, atmosphere->fog.far_density);
00215     {
00216       Lib3dsChunk c;
00217       c.chunk=LIB3DS_COLOR_F;
00218       c.size=18;
00219       lib3ds_chunk_write(&c,io);
00220       lib3ds_io_write_rgb(io, atmosphere->fog.col);
00221     }
00222     if (atmosphere->fog.fog_background) {
00223       Lib3dsChunk c;
00224       c.chunk=LIB3DS_FOG_BGND;
00225       c.size=6;
00226       lib3ds_chunk_write(&c,io);
00227     }
00228     if (!lib3ds_chunk_write_end(&c,io)) {
00229       return(LIB3DS_FALSE);
00230     }
00231   }
00232 
00233   if (atmosphere->layer_fog.use) { /*---- LIB3DS_LAYER_FOG ----*/
00234     Lib3dsChunk c;
00235     c.chunk=LIB3DS_LAYER_FOG;
00236     c.size=40;
00237     lib3ds_chunk_write(&c,io);
00238     lib3ds_io_write_float(io, atmosphere->layer_fog.near_y);
00239     lib3ds_io_write_float(io, atmosphere->layer_fog.far_y);
00240     lib3ds_io_write_float(io, atmosphere->layer_fog.near_y);
00241     lib3ds_io_write_dword(io, atmosphere->layer_fog.flags);
00242     {
00243       Lib3dsChunk c;
00244       c.chunk=LIB3DS_COLOR_F;
00245       c.size=18;
00246       lib3ds_chunk_write(&c,io);
00247       lib3ds_io_write_rgb(io, atmosphere->fog.col);
00248     }
00249   }
00250 
00251   if (atmosphere->dist_cue.use) { /*---- LIB3DS_DISTANCE_CUE ----*/
00252     Lib3dsChunk c;
00253     c.chunk=LIB3DS_DISTANCE_CUE;
00254     if (!lib3ds_chunk_write_start(&c,io)) {
00255       return(LIB3DS_FALSE);
00256     }
00257     lib3ds_io_write_float(io, atmosphere->dist_cue.near_plane);
00258     lib3ds_io_write_float(io, atmosphere->dist_cue.near_dimming);
00259     lib3ds_io_write_float(io, atmosphere->dist_cue.far_plane);
00260     lib3ds_io_write_float(io, atmosphere->dist_cue.far_dimming);
00261     if (atmosphere->dist_cue.cue_background) {
00262       Lib3dsChunk c;
00263       c.chunk=LIB3DS_DCUE_BGND;
00264       c.size=6;
00265       lib3ds_chunk_write(&c,io);
00266     }
00267     if (!lib3ds_chunk_write_end(&c,io)) {
00268       return(LIB3DS_FALSE);
00269     }
00270   }
00271 
00272   if (atmosphere->fog.use) { /*---- LIB3DS_USE_FOG ----*/
00273     Lib3dsChunk c;
00274     c.chunk=LIB3DS_USE_FOG;
00275     c.size=6;
00276     lib3ds_chunk_write(&c,io);
00277   }
00278 
00279   if (atmosphere->layer_fog.use) { /*---- LIB3DS_USE_LAYER_FOG ----*/
00280     Lib3dsChunk c;
00281     c.chunk=LIB3DS_USE_LAYER_FOG;
00282     c.size=6;
00283     lib3ds_chunk_write(&c,io);
00284   }
00285 
00286   if (atmosphere->dist_cue.use) { /*---- LIB3DS_USE_DISTANCE_CUE ----*/
00287     Lib3dsChunk c;
00288     c.chunk=LIB3DS_USE_V_GRADIENT;
00289     c.size=6;
00290     lib3ds_chunk_write(&c,io);
00291   }
00292   
00293   return(LIB3DS_TRUE);
00294 }
00295 
00296 

Hosted by
SourceForge.net Logo
Generated at Wed Jun 20 18:51:36 2007 by Doxygen 1.5.2