Home | API | File List | Examples | Download

lib3ds_atmosphere.c

00001 /*
00002     Copyright (C) 1996-2008 by Jan Eric Kyprianidis <www.kyprianidis.com>
00003     All rights reserved.
00004     
00005     This program is free  software: you can redistribute it and/or modify 
00006     it under the terms of the GNU Lesser General Public License as published 
00007     by the Free Software Foundation, either version 2.1 of the License, or 
00008     (at your option) any later version.
00009 
00010     Thisprogram  is  distributed in the hope that it will be useful, 
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of 
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
00013     GNU Lesser General Public License for more details.
00014     
00015     You should  have received a copy of the GNU Lesser General Public License
00016     along with  this program; If not, see <http://www.gnu.org/licenses/>. 
00017 */
00018 #include "lib3ds_impl.h"
00019 
00020 
00021 static void
00022 fog_read(Lib3dsAtmosphere *at, Lib3dsIo *io) {
00023     Lib3dsChunk c;
00024     uint16_t chunk;
00025 
00026     lib3ds_chunk_read_start(&c, CHK_FOG, io);
00027 
00028     at->fog_near_plane = lib3ds_io_read_float(io);
00029     at->fog_near_density = lib3ds_io_read_float(io);
00030     at->fog_far_plane = lib3ds_io_read_float(io);
00031     at->fog_far_density = lib3ds_io_read_float(io);
00032     lib3ds_chunk_read_tell(&c, io);
00033 
00034     while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) {
00035         switch (chunk) {
00036             case CHK_LIN_COLOR_F: {
00037                 int i;
00038                 for (i = 0; i < 3; ++i) {
00039                     at->fog_color[i] = lib3ds_io_read_float(io);
00040                 }
00041             }
00042             break;
00043 
00044             case CHK_COLOR_F:
00045                 break;
00046 
00047             case CHK_FOG_BGND: {
00048                 at->fog_background = TRUE;
00049             }
00050             break;
00051 
00052             default:
00053                 lib3ds_chunk_unknown(chunk, io);
00054         }
00055     }
00056 
00057     lib3ds_chunk_read_end(&c, io);
00058 }
00059 
00060 
00061 static void
00062 layer_fog_read(Lib3dsAtmosphere *at, Lib3dsIo *io) {
00063     Lib3dsChunk c;
00064     uint16_t chunk;
00065     int have_lin = FALSE;
00066 
00067     lib3ds_chunk_read_start(&c, CHK_LAYER_FOG, io);
00068 
00069     at->layer_fog_near_y = lib3ds_io_read_float(io);
00070     at->layer_fog_far_y = lib3ds_io_read_float(io);
00071     at->layer_fog_density = lib3ds_io_read_float(io);
00072     at->layer_fog_flags = lib3ds_io_read_dword(io);
00073     lib3ds_chunk_read_tell(&c, io);
00074 
00075     while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) {
00076         switch (chunk) {
00077             case CHK_LIN_COLOR_F:
00078                 lib3ds_io_read_rgb(io, at->layer_fog_color);
00079                 have_lin = TRUE;
00080                 break;
00081 
00082             case CHK_COLOR_F:
00083                 lib3ds_io_read_rgb(io, at->layer_fog_color);
00084                 break;
00085 
00086             default:
00087                 lib3ds_chunk_unknown(chunk, io);
00088         }
00089     }
00090 
00091     lib3ds_chunk_read_end(&c, io);
00092 }
00093 
00094 
00095 static void
00096 distance_cue_read(Lib3dsAtmosphere *at, Lib3dsIo *io) {
00097     Lib3dsChunk c;
00098     uint16_t chunk;
00099 
00100     lib3ds_chunk_read_start(&c, CHK_DISTANCE_CUE, io);
00101 
00102     at->dist_cue_near_plane = lib3ds_io_read_float(io);
00103     at->dist_cue_near_dimming = lib3ds_io_read_float(io);
00104     at->dist_cue_far_plane = lib3ds_io_read_float(io);
00105     at->dist_cue_far_dimming = lib3ds_io_read_float(io);
00106     lib3ds_chunk_read_tell(&c, io);
00107 
00108     while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) {
00109         switch (chunk) {
00110             case CHK_DCUE_BGND: {
00111                 at->dist_cue_background = TRUE;
00112             }
00113             break;
00114 
00115             default:
00116                 lib3ds_chunk_unknown(chunk, io);
00117         }
00118     }
00119 
00120     lib3ds_chunk_read_end(&c, io);
00121 }
00122 
00123 
00124 void
00125 lib3ds_atmosphere_read(Lib3dsAtmosphere *atmosphere, Lib3dsIo *io) {
00126     Lib3dsChunk c;
00127 
00128     lib3ds_chunk_read(&c, io);
00129     switch (c.chunk) {
00130         case CHK_FOG: {
00131             lib3ds_chunk_read_reset(&c, io);
00132             fog_read(atmosphere, io);
00133             break;
00134         }
00135 
00136         case CHK_LAYER_FOG: {
00137             lib3ds_chunk_read_reset(&c, io);
00138             layer_fog_read(atmosphere, io);
00139             break;
00140         }
00141 
00142         case CHK_DISTANCE_CUE: {
00143             lib3ds_chunk_read_reset(&c, io);
00144             distance_cue_read(atmosphere, io);
00145             break;
00146         }
00147 
00148         case CHK_USE_FOG: {
00149             atmosphere->use_fog = TRUE;
00150             break;
00151         }
00152 
00153         case CHK_USE_LAYER_FOG: {
00154             atmosphere->use_layer_fog = TRUE;
00155             break;
00156         }
00157 
00158         case CHK_USE_DISTANCE_CUE: {
00159             atmosphere->use_dist_cue = TRUE;
00160             break;
00161         }
00162     }
00163 }
00164 
00165 
00166 void
00167 lib3ds_atmosphere_write(Lib3dsAtmosphere *atmosphere, Lib3dsIo *io) {
00168     if (atmosphere->use_fog) { /*---- LIB3DS_FOG ----*/
00169         Lib3dsChunk c;
00170         c.chunk = CHK_FOG;
00171         lib3ds_chunk_write_start(&c, io);
00172 
00173         lib3ds_io_write_float(io, atmosphere->fog_near_plane);
00174         lib3ds_io_write_float(io, atmosphere->fog_near_density);
00175         lib3ds_io_write_float(io, atmosphere->fog_far_plane);
00176         lib3ds_io_write_float(io, atmosphere->fog_far_density);
00177         {
00178             Lib3dsChunk c;
00179             c.chunk = CHK_COLOR_F;
00180             c.size = 18;
00181             lib3ds_chunk_write(&c, io);
00182             lib3ds_io_write_rgb(io, atmosphere->fog_color);
00183         }
00184         if (atmosphere->fog_background) {
00185             Lib3dsChunk c;
00186             c.chunk = CHK_FOG_BGND;
00187             c.size = 6;
00188             lib3ds_chunk_write(&c, io);
00189         }
00190 
00191         lib3ds_chunk_write_end(&c, io);
00192     }
00193 
00194     if (atmosphere->use_layer_fog) { /*---- LIB3DS_LAYER_FOG ----*/
00195         Lib3dsChunk c;
00196         c.chunk = CHK_LAYER_FOG;
00197         c.size = 40;
00198         lib3ds_chunk_write(&c, io);
00199         lib3ds_io_write_float(io, atmosphere->layer_fog_near_y);
00200         lib3ds_io_write_float(io, atmosphere->layer_fog_far_y);
00201         lib3ds_io_write_float(io, atmosphere->layer_fog_near_y);
00202         lib3ds_io_write_dword(io, atmosphere->layer_fog_flags);
00203         {
00204             Lib3dsChunk c;
00205             c.chunk = CHK_COLOR_F;
00206             c.size = 18;
00207             lib3ds_chunk_write(&c, io);
00208             lib3ds_io_write_rgb(io, atmosphere->fog_color);
00209         }
00210     }
00211 
00212     if (atmosphere->use_dist_cue) { /*---- LIB3DS_DISTANCE_CUE ----*/
00213         Lib3dsChunk c;
00214         c.chunk = CHK_DISTANCE_CUE;
00215         lib3ds_chunk_write_start(&c, io);
00216 
00217         lib3ds_io_write_float(io, atmosphere->dist_cue_near_plane);
00218         lib3ds_io_write_float(io, atmosphere->dist_cue_near_dimming);
00219         lib3ds_io_write_float(io, atmosphere->dist_cue_far_plane);
00220         lib3ds_io_write_float(io, atmosphere->dist_cue_far_dimming);
00221         if (atmosphere->dist_cue_background) {
00222             Lib3dsChunk c;
00223             c.chunk = CHK_DCUE_BGND;
00224             c.size = 6;
00225             lib3ds_chunk_write(&c, io);
00226         }
00227 
00228         lib3ds_chunk_write_end(&c, io);
00229     }
00230 
00231     if (atmosphere->use_fog) { /*---- LIB3DS_USE_FOG ----*/
00232         Lib3dsChunk c;
00233         c.chunk = CHK_USE_FOG;
00234         c.size = 6;
00235         lib3ds_chunk_write(&c, io);
00236     }
00237 
00238     if (atmosphere->use_layer_fog) { /*---- LIB3DS_USE_LAYER_FOG ----*/
00239         Lib3dsChunk c;
00240         c.chunk = CHK_USE_LAYER_FOG;
00241         c.size = 6;
00242         lib3ds_chunk_write(&c, io);
00243     }
00244 
00245     if (atmosphere->use_dist_cue) { /*---- LIB3DS_USE_DISTANCE_CUE ----*/
00246         Lib3dsChunk c;
00247         c.chunk = CHK_USE_V_GRADIENT;
00248         c.size = 6;
00249         lib3ds_chunk_write(&c, io);
00250     }
00251 }
00252 
00253