root/OpenSceneGraph/trunk/src/osgPlugins/3ds/atmosphere.cpp @ 10076

Revision 10076, 7.4 kB (checked in by robert, 5 years ago)

From Neil Hughes, converted across to use istream for reading data from file to enable reading .3ds files over http (use OSG's libcurl plugin).

From Robert Osfield, ammendments of the above to better support reading of files from local directories.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[8]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
[151]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
[8]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
[151]13 * or  FITNESS FOR A  PARTICULAR PURPOSE.  See the  GNU Lesser General Public 
[8]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$
21 */
22#define LIB3DS_EXPORT
[1563]23#include "atmosphere.h"
24#include "chunk.h"
25#include "readwrite.h"
[8]26
[151]27
[8]28/*!
29 * \defgroup atmosphere Atmosphere Settings
30 *
31 * \author J.E. Hoffmann <je-h@gmx.net>
32 */
33
[151]34
[8]35static Lib3dsBool
[10076]36fog_read(Lib3dsFog *fog, iostream *strm)
[8]37{
[151]38  Lib3dsChunk c;
39  Lib3dsWord chunk;
[8]40
[10076]41  if (!lib3ds_chunk_read_start(&c, LIB3DS_FOG, strm)) {
[151]42    return(LIB3DS_FALSE);
43  }
[10076]44  fog->near_plane=lib3ds_float_read(strm);
45  fog->near_density=lib3ds_float_read(strm);
46  fog->far_plane=lib3ds_float_read(strm);
47  fog->far_density=lib3ds_float_read(strm);
48  lib3ds_chunk_read_tell(&c, strm);
[151]49 
[10076]50  while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) {
[151]51    switch (chunk) {
52      case LIB3DS_LIN_COLOR_F:
[8]53        {
[151]54          int i;
55          for (i=0; i<3; ++i) {
[10076]56            fog->col[i]=lib3ds_float_read(strm);
[151]57          }
[8]58        }
[151]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);
[8]69    }
[151]70  }
71 
[10076]72  lib3ds_chunk_read_end(&c, strm);
[151]73  return(LIB3DS_TRUE);
[8]74}
75
76
77static Lib3dsBool
[10076]78layer_fog_read(Lib3dsLayerFog *fog, iostream *strm)
[8]79{
[151]80  Lib3dsChunk c;
81  Lib3dsWord chunk;
[8]82
[10076]83  if (!lib3ds_chunk_read_start(&c, LIB3DS_LAYER_FOG, strm)) {
[151]84    return(LIB3DS_FALSE);
85  }
[10076]86  fog->near_y=lib3ds_float_read(strm);
87  fog->far_y=lib3ds_float_read(strm);
88  fog->density=lib3ds_float_read(strm);
89  fog->flags=lib3ds_dword_read(strm);
90  lib3ds_chunk_read_tell(&c, strm);
[151]91 
[10076]92  while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) {
[151]93    switch (chunk) {
94      case LIB3DS_LIN_COLOR_F:
[10076]95        lib3ds_rgb_read(fog->col,strm);
[151]96        break;
97      case LIB3DS_COLOR_F:
[10076]98        lib3ds_rgb_read(fog->col,strm);
[151]99        break;
100      default:
101        lib3ds_chunk_unknown(chunk);
[8]102    }
[151]103  }
104 
[10076]105  lib3ds_chunk_read_end(&c, strm);
[151]106  return(LIB3DS_TRUE);
[8]107}
108
109
110static Lib3dsBool
[10076]111distance_cue_read(Lib3dsDistanceCue *cue, iostream *strm)
[8]112{
[151]113  Lib3dsChunk c;
114  Lib3dsWord chunk;
[8]115
[10076]116  if (!lib3ds_chunk_read_start(&c, LIB3DS_DISTANCE_CUE, strm)) {
[151]117    return(LIB3DS_FALSE);
118  }
[10076]119  cue->near_plane=lib3ds_float_read(strm);
120  cue->near_dimming=lib3ds_float_read(strm);
121  cue->far_plane=lib3ds_float_read(strm);
122  cue->far_dimming=lib3ds_float_read(strm);
123  lib3ds_chunk_read_tell(&c, strm);
[151]124 
[10076]125  while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) {
[151]126    switch (chunk) {
127      case LIB3DS_DCUE_BGND:
[8]128        {
[151]129          cue->cue_background=LIB3DS_TRUE;
[8]130        }
[151]131        break;
132      default:
133        lib3ds_chunk_unknown(chunk);
[8]134    }
[151]135  }
136 
[10076]137  lib3ds_chunk_read_end(&c, strm);
[151]138  return(LIB3DS_TRUE);
[8]139}
140
141
142/*!
143 * \ingroup atmosphere
144 */
145Lib3dsBool
[10076]146lib3ds_atmosphere_read(Lib3dsAtmosphere *atmosphere, iostream *strm)
[8]147{
[151]148  Lib3dsChunk c;
[8]149
[10076]150  if (!lib3ds_chunk_read(&c, strm)) {
[151]151    return(LIB3DS_FALSE);
152  }
153 
154  switch (c.chunk) {
155      case LIB3DS_FOG:
[8]156        {
[10076]157          lib3ds_chunk_read_reset(&c, strm);
158          if (!fog_read(&atmosphere->fog, strm)) {
[151]159            return(LIB3DS_FALSE);
160          }
[8]161        }
162        break;
[151]163      case LIB3DS_LAYER_FOG:
[8]164        {
[10076]165          lib3ds_chunk_read_reset(&c, strm);
166          if (!layer_fog_read(&atmosphere->layer_fog, strm)) {
[151]167            return(LIB3DS_FALSE);
168          }
[8]169        }
170        break;
[151]171      case LIB3DS_DISTANCE_CUE:
[8]172        {
[10076]173          lib3ds_chunk_read_reset(&c, strm);
174          if (!distance_cue_read(&atmosphere->dist_cue, strm)) {
[151]175            return(LIB3DS_FALSE);
176          }
[8]177        }
178        break;
[151]179      case LIB3DS_USE_FOG:
[8]180        {
[151]181          atmosphere->fog.use=LIB3DS_TRUE;
[8]182        }
183        break;
[151]184      case LIB3DS_USE_LAYER_FOG:
[8]185        {
[151]186          atmosphere->fog.use=LIB3DS_TRUE;
[8]187        }
188        break;
[151]189      case LIB3DS_USE_DISTANCE_CUE:
[8]190        {
[151]191          atmosphere->dist_cue.use=LIB3DS_TRUE;
[8]192        }
193        break;
[151]194  }
[8]195
[151]196  return(LIB3DS_TRUE);
[8]197}
198
199
200/*!
201 * \ingroup atmosphere
202 */
203Lib3dsBool
[10076]204lib3ds_atmosphere_write(Lib3dsAtmosphere *atmosphere, iostream *strm)
[8]205{
[151]206  if (atmosphere->fog.use) { /*---- LIB3DS_FOG ----*/
207    Lib3dsChunk c;
208    c.chunk=LIB3DS_FOG;
[10076]209    if (!lib3ds_chunk_write_start(&c,strm)) {
[151]210      return(LIB3DS_FALSE);
[8]211    }
[10076]212    lib3ds_float_write(atmosphere->fog.near_plane,strm);
213    lib3ds_float_write(atmosphere->fog.near_density,strm);
214    lib3ds_float_write(atmosphere->fog.far_plane,strm);
215    lib3ds_float_write(atmosphere->fog.far_density,strm);
[151]216    {
217      Lib3dsChunk c;
218      c.chunk=LIB3DS_COLOR_F;
219      c.size=18;
[10076]220      lib3ds_chunk_write(&c,strm);
221      lib3ds_rgb_write(atmosphere->fog.col,strm);
[8]222    }
[151]223    if (atmosphere->fog.fog_background) {
224      Lib3dsChunk c;
225      c.chunk=LIB3DS_FOG_BGND;
226      c.size=6;
[10076]227      lib3ds_chunk_write(&c,strm);
[8]228    }
[10076]229    if (!lib3ds_chunk_write_end(&c,strm)) {
[151]230      return(LIB3DS_FALSE);
[8]231    }
[151]232  }
233
234  if (atmosphere->layer_fog.use) { /*---- LIB3DS_LAYER_FOG ----*/
235    Lib3dsChunk c;
236    c.chunk=LIB3DS_LAYER_FOG;
237    c.size=40;
[10076]238    lib3ds_chunk_write(&c,strm);
239    lib3ds_float_write(atmosphere->layer_fog.near_y,strm);
240    lib3ds_float_write(atmosphere->layer_fog.far_y,strm);
241    lib3ds_float_write(atmosphere->layer_fog.near_y,strm);
242    lib3ds_dword_write(atmosphere->layer_fog.flags,strm);
[8]243    {
[151]244      Lib3dsChunk c;
245      c.chunk=LIB3DS_COLOR_F;
246      c.size=18;
[10076]247      lib3ds_chunk_write(&c,strm);
248      lib3ds_rgb_write(atmosphere->fog.col,strm);
[8]249    }
[151]250  }
251
252  if (atmosphere->dist_cue.use) { /*---- LIB3DS_DISTANCE_CUE ----*/
253    Lib3dsChunk c;
254    c.chunk=LIB3DS_DISTANCE_CUE;
[10076]255    if (!lib3ds_chunk_write_start(&c,strm)) {
[151]256      return(LIB3DS_FALSE);
[8]257    }
[10076]258    lib3ds_float_write(atmosphere->dist_cue.near_plane,strm);
259    lib3ds_float_write(atmosphere->dist_cue.near_dimming,strm);
260    lib3ds_float_write(atmosphere->dist_cue.far_plane,strm);
261    lib3ds_float_write(atmosphere->dist_cue.far_dimming,strm);
[151]262    if (atmosphere->dist_cue.cue_background) {
263      Lib3dsChunk c;
264      c.chunk=LIB3DS_DCUE_BGND;
265      c.size=6;
[10076]266      lib3ds_chunk_write(&c,strm);
[151]267    }
[10076]268    if (!lib3ds_chunk_write_end(&c,strm)) {
[151]269      return(LIB3DS_FALSE);
270    }
271  }
[8]272
[151]273  if (atmosphere->fog.use) { /*---- LIB3DS_USE_FOG ----*/
274    Lib3dsChunk c;
275    c.chunk=LIB3DS_USE_FOG;
276    c.size=6;
[10076]277    lib3ds_chunk_write(&c,strm);
[151]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;
[10076]284    lib3ds_chunk_write(&c,strm);
[151]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;
[10076]291    lib3ds_chunk_write(&c,strm);
[151]292  }
293 
294  return(LIB3DS_TRUE);
[8]295}
296
297
298/*!
299
300\typedef Lib3dsAtmosphere
301  \ingroup atmosphere
302  \sa _Lib3dsAtmosphere
303
304*/
Note: See TracBrowser for help on using the browser.