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

Revision 10076, 6.0 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 "background.h"
24#include "chunk.h"
25#include "readwrite.h"
[8]26#include <string.h>
[151]27#include <math.h>
[8]28
[151]29
[8]30/*!
31 * \defgroup background Background Settings
32 *
33 * \author J.E. Hoffmann <je-h@gmx.net>
34 */
35
[151]36
[8]37static Lib3dsBool
[10076]38solid_bgnd_read(Lib3dsBackground *background, iostream *strm)
[8]39{
[151]40  Lib3dsChunk c;
41  Lib3dsWord chunk;
42         
[10076]43  if (!lib3ds_chunk_read_start(&c, LIB3DS_SOLID_BGND, strm)) {
[151]44    return(LIB3DS_FALSE);
45  }
[8]46
[10076]47  while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) {
[151]48    switch (chunk) {
49      case LIB3DS_LIN_COLOR_F:
[10076]50        lib3ds_rgb_read(background->solid.col, strm);
[151]51        break;
52      case LIB3DS_COLOR_F:
[10076]53        lib3ds_rgb_read(background->solid.col, strm);
[151]54        break;
55      default:
56        lib3ds_chunk_unknown(chunk);
[8]57    }
[151]58  }
59 
[10076]60  lib3ds_chunk_read_end(&c, strm);
[151]61  return(LIB3DS_TRUE);
[8]62}
63
64
65static Lib3dsBool
[10076]66v_gradient_read(Lib3dsBackground *background, iostream *strm)
[8]67{
[151]68  Lib3dsChunk c;
69  Lib3dsWord chunk;
70  int index[2];
71  Lib3dsRgb col[2][3];
72  int have_lin=0;
73 
[8]74
[10076]75  if (!lib3ds_chunk_read_start(&c, LIB3DS_V_GRADIENT, strm)) {
[151]76    return(LIB3DS_FALSE);
77  }
[10076]78  background->gradient.percent=lib3ds_float_read(strm);
79  lib3ds_chunk_read_tell(&c, strm);
[8]80
[151]81  index[0]=index[1]=0;
[10076]82  while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) {
[151]83    switch (chunk) {
84      case LIB3DS_COLOR_F:
[10076]85        lib3ds_rgb_read(col[0][index[0]],strm);
[151]86        index[0]++;
87        break;
88      case LIB3DS_LIN_COLOR_F:
[10076]89        lib3ds_rgb_read(col[1][index[1]],strm);
[151]90        index[1]++;
91        have_lin=1;
92        break;
93      default:
94        lib3ds_chunk_unknown(chunk);
[8]95    }
[151]96  }
97  {
98    int i;
99    for (i=0; i<3; ++i) {
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];
[8]103    }
[151]104  }
[10076]105  lib3ds_chunk_read_end(&c, strm);
[151]106  return(LIB3DS_TRUE);
[8]107}
108
109
110/*!
111 * \ingroup background
112 */
113Lib3dsBool
[10076]114lib3ds_background_read(Lib3dsBackground *background, iostream *strm)
[8]115{
[151]116  Lib3dsChunk c;
[8]117
[10076]118  if (!lib3ds_chunk_read(&c, strm)) {
[151]119    return(LIB3DS_FALSE);
120  }
121 
122  switch (c.chunk) {
123    case LIB3DS_BIT_MAP:
124      {
[10076]125        if (!lib3ds_string_read(background->bitmap.name, 64, strm)) {
[151]126            return(LIB3DS_FALSE);
[8]127        }
[151]128      }
[8]129        break;
[151]130      case LIB3DS_SOLID_BGND:
[8]131        {
[10076]132          lib3ds_chunk_read_reset(&c, strm);
133          if (!solid_bgnd_read(background, strm)) {
[151]134            return(LIB3DS_FALSE);
135          }
[8]136        }
137        break;
[151]138      case LIB3DS_V_GRADIENT:
[8]139        {
[10076]140          lib3ds_chunk_read_reset(&c, strm);
141          if (!v_gradient_read(background, strm)) {
[151]142            return(LIB3DS_FALSE);
143          }
[8]144        }
145        break;
[151]146      case LIB3DS_USE_BIT_MAP:
[8]147        {
[151]148          background->bitmap.use=LIB3DS_TRUE;
[8]149        }
150        break;
[151]151      case LIB3DS_USE_SOLID_BGND:
[8]152        {
[151]153          background->solid.use=LIB3DS_TRUE;
[8]154        }
155        break;
[151]156      case LIB3DS_USE_V_GRADIENT:
[8]157        {
[151]158          background->gradient.use=LIB3DS_TRUE;
[8]159        }
160        break;
[151]161  }
162 
163  return(LIB3DS_TRUE);
[8]164}
165
166
167static Lib3dsBool
[10076]168colorf_write(Lib3dsRgba rgb, iostream *strm)
[8]169{
[151]170  Lib3dsChunk c;
[8]171
[151]172  c.chunk=LIB3DS_COLOR_F;
173  c.size=18;
[10076]174  lib3ds_chunk_write(&c,strm);
175  lib3ds_rgb_write(rgb,strm);
[8]176
[151]177  c.chunk=LIB3DS_LIN_COLOR_F;
178  c.size=18;
[10076]179  lib3ds_chunk_write(&c,strm);
180  lib3ds_rgb_write(rgb,strm);
[151]181  return(LIB3DS_TRUE);
[8]182}
183
184
[151]185static Lib3dsBool
186colorf_defined(Lib3dsRgba rgb)
187{
188  int i;
189  for (i=0; i<3; ++i) {
190    if (fabs(rgb[i])>LIB3DS_EPSILON) {
191      break;
192    }
193  }
194  return(i<3);
195}
196
197
[8]198/*!
199 * \ingroup background
200 */
201Lib3dsBool
[10076]202lib3ds_background_write(Lib3dsBackground *background, iostream *strm)
[8]203{
[151]204  if (strlen(background->bitmap.name)) { /*---- LIB3DS_BIT_MAP ----*/
205    Lib3dsChunk c;
206    c.chunk=LIB3DS_BIT_MAP;
207    c.size=6+1+strlen(background->bitmap.name);
[10076]208    lib3ds_chunk_write(&c,strm);
209    lib3ds_string_write(background->bitmap.name, strm);
[151]210  }
[8]211
[151]212  if (colorf_defined(background->solid.col)) { /*---- LIB3DS_SOLID_BGND ----*/
213    Lib3dsChunk c;
214    c.chunk=LIB3DS_SOLID_BGND;
215    c.size=42;
[10076]216    lib3ds_chunk_write(&c,strm);
217    colorf_write(background->solid.col,strm);
[151]218  }
219
220  if (colorf_defined(background->gradient.top) ||
221    colorf_defined(background->gradient.middle) ||
222    colorf_defined(background->gradient.bottom)) { /*---- LIB3DS_V_GRADIENT ----*/
223    Lib3dsChunk c;
224    c.chunk=LIB3DS_V_GRADIENT;
225    c.size=118;
[10076]226    lib3ds_chunk_write(&c,strm);
227    lib3ds_float_write(background->gradient.percent,strm);
228    colorf_write(background->gradient.top,strm);
229    colorf_write(background->gradient.middle,strm);
230    colorf_write(background->gradient.bottom,strm);
[151]231  }
232
233  if (background->bitmap.use) { /*---- LIB3DS_USE_BIT_MAP ----*/
234    Lib3dsChunk c;
235    c.chunk=LIB3DS_USE_BIT_MAP;
236    c.size=6;
[10076]237    lib3ds_chunk_write(&c,strm);
[151]238  }
239
240  if (background->solid.use) { /*---- LIB3DS_USE_SOLID_BGND ----*/
241    Lib3dsChunk c;
242    c.chunk=LIB3DS_USE_SOLID_BGND;
243    c.size=6;
[10076]244    lib3ds_chunk_write(&c,strm);
[151]245  }
246
247  if (background->gradient.use) { /*---- LIB3DS_USE_V_GRADIENT ----*/
248    Lib3dsChunk c;
249    c.chunk=LIB3DS_USE_V_GRADIENT;
250    c.size=6;
[10076]251    lib3ds_chunk_write(&c,strm);
[151]252  }
253 
254  return(LIB3DS_TRUE);
[8]255}
256
257
258/*!
259
260\typedef Lib3dsBackground
261  \ingroup background
262  \sa _Lib3dsBackground
263
264*/
Note: See TracBrowser for help on using the browser.