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

Revision 10076, 11.0 kB (checked in by robert, 6 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 "light.h"
24#include "chunk.h"
25#include "readwrite.h"
[8]26#include <stdlib.h>
27#include <math.h>
28#include <string.h>
[1563]29#include "config.h"
[8]30#ifdef WITH_DMALLOC
31#include <dmalloc.h>
32#endif
33
[151]34
[8]35/*!
36 * \defgroup light Lights
37 *
38 * \author J.E. Hoffmann <je-h@gmx.net>
39 */
[151]40/*!
[8]41
[151]42\typedef Lib3dsLight
43  \ingroup light
44  \sa _Lib3dsLight
45
46*/
47
48
49
[8]50/*!
51 * \ingroup light
52 */
53Lib3dsLight*
54lib3ds_light_new(const char *name)
55{
[151]56  Lib3dsLight *light;
[8]57
[151]58  ASSERT(name);
59  ASSERT(strlen(name)<64);
60 
61  light=(Lib3dsLight*)calloc(sizeof(Lib3dsLight), 1);
62  if (!light) {
63    return(0);
64  }
65  strcpy(light->name, name);
66  return(light);
[8]67}
68
69
70/*!
71 * \ingroup light
72 */
73void
74lib3ds_light_free(Lib3dsLight *light)
75{
[151]76  memset(light, 0, sizeof(Lib3dsLight));
77  free(light);
[8]78}
79
80
81/*!
82 * \ingroup light
83 */
[151]84void
85lib3ds_light_dump(Lib3dsLight *light)
[8]86{
[151]87  ASSERT(light);
88  printf("  name:             %s\n", light->name);
89  printf("  spot_light:       %s\n", light->spot_light ? "yes" : "no");
90  printf("  see_cone:         %s\n", light->see_cone ? "yes" : "no");
91  printf("  color:            (%f, %f, %f)\n",
92    light->color[0], light->color[1], light->color[2]);
93  printf("  position          (%f, %f, %f)\n",
94    light->position[0], light->position[1], light->position[2]);
95  printf("  spot              (%f, %f, %f)\n",
96    light->spot[0], light->spot[1], light->spot[2]);
97  printf("  roll:             %f\n", light->roll);
98  printf("  off:              %s\n", light->off ? "yes" : "no");
99  printf("  outer_range:      %f\n", light->outer_range);
100  printf("  inner_range:      %f\n", light->inner_range);
101  printf("  multiplier:       %f\n", light->multiplier);
102  printf("  attenuation:      %f\n", light->attenuation);
103  printf("  rectangular_spot: %s\n", light->rectangular_spot ? "yes" : "no");
104  printf("  shadowed:         %s\n", light->shadowed ? "yes" : "no");
105  printf("  shadow_bias:      %f\n", light->shadow_bias);
106  printf("  shadow_filter:    %f\n", light->shadow_filter);
107  printf("  shadow_size:      %d\n", light->shadow_size);
108  printf("  spot_aspect:      %f\n", light->spot_aspect);
109  printf("  use_projector:    %s\n", light->use_projector ? "yes" : "no");
110  printf("  projector:        %s\n", light->projector);
[576]111  printf("  spot_overshoot:   %i\n", static_cast<int>(light->spot_overshoot));
[151]112  printf("  ray_shadows:      %s\n", light->ray_shadows ? "yes" : "no");
113  printf("  ray_bias:         %f\n", light->ray_bias);
114  printf("  hot_spot:         %f\n", light->hot_spot);
115  printf("  fall_off:         %f\n", light->fall_off);
116  printf("\n");
117}
[8]118
[151]119
120/*!
121 * \ingroup light
122 */
123static Lib3dsBool
[10076]124spotlight_read(Lib3dsLight *light, iostream *strm)
[151]125{
126  Lib3dsChunk c;
127  Lib3dsWord chunk;
128  int i;
129
[10076]130  if (!lib3ds_chunk_read_start(&c, LIB3DS_DL_SPOTLIGHT, strm)) {
[151]131    return(LIB3DS_FALSE);
132  }
133  light->spot_light=LIB3DS_TRUE;
134  for (i=0; i<3; ++i) {
[10076]135    light->spot[i]=lib3ds_float_read(strm);
[151]136  }
[10076]137  light->hot_spot = lib3ds_float_read(strm);
138  light->fall_off = lib3ds_float_read(strm);
139  lib3ds_chunk_read_tell(&c, strm);
[151]140 
[10076]141  while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) {
[151]142    switch (chunk) {
143      case LIB3DS_DL_SPOT_ROLL:
[8]144        {
[10076]145          light->roll=lib3ds_float_read(strm);
[8]146        }
[151]147        break;
148      case LIB3DS_DL_SHADOWED:
[8]149        {
[151]150          light->shadowed=LIB3DS_TRUE;
[8]151        }
[151]152        break;
153      case LIB3DS_DL_LOCAL_SHADOW2:
154        {
[10076]155          light->shadow_bias=lib3ds_float_read(strm);
156          light->shadow_filter=lib3ds_float_read(strm);
157          light->shadow_size=lib3ds_intw_read(strm);
[151]158        }
159        break;
160      case LIB3DS_DL_SEE_CONE:
161        {
162          light->see_cone=LIB3DS_TRUE;
163        }
164        break;
165      case LIB3DS_DL_SPOT_RECTANGULAR:
166        {
167          light->rectangular_spot=LIB3DS_TRUE;
168        }
169        break;
170      case LIB3DS_DL_SPOT_ASPECT:
171        {
[10076]172          light->spot_aspect=lib3ds_float_read(strm);
[151]173        }
174        break;
175      case LIB3DS_DL_SPOT_PROJECTOR:
176        {
177          light->use_projector=LIB3DS_TRUE;
[10076]178          if (!lib3ds_string_read(light->projector, 64, strm)) {
[151]179            return(LIB3DS_FALSE);
180          }
181        }
182      case LIB3DS_DL_SPOT_OVERSHOOT:
183        {
184          light->spot_overshoot=LIB3DS_TRUE;
185        }
186        break;
187      case LIB3DS_DL_RAY_BIAS:
188        {
[10076]189          light->ray_bias=lib3ds_float_read(strm);
[151]190        }
191        break;
192      case LIB3DS_DL_RAYSHAD:
193        {
194          light->ray_shadows=LIB3DS_TRUE;
195        }
196        break;
197      default:
198        lib3ds_chunk_unknown(chunk);
[8]199    }
[151]200  }
201 
[10076]202  lib3ds_chunk_read_end(&c, strm);
[151]203  return(LIB3DS_TRUE);
[8]204}
205
206
207/*!
208 * \ingroup light
209 */
210Lib3dsBool
[10076]211lib3ds_light_read(Lib3dsLight *light, iostream * strm)
[8]212{
[151]213  Lib3dsChunk c;
214  Lib3dsWord chunk;
[8]215
[10076]216  if (!lib3ds_chunk_read_start(&c, LIB3DS_N_DIRECT_LIGHT, strm)) {
[151]217    return(LIB3DS_FALSE);
218  }
219  {
220    int i;
221    for (i=0; i<3; ++i) {
[10076]222      light->position[i]=lib3ds_float_read(strm);
[8]223    }
[151]224  }
[10076]225  lib3ds_chunk_read_tell(&c, strm);
[151]226 
[10076]227  while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) {
[151]228    switch (chunk) {
229      case LIB3DS_COLOR_F:
[8]230        {
[151]231          int i;
232          for (i=0; i<3; ++i) {
[10076]233            light->color[i]=lib3ds_float_read(strm);
[151]234          }
[8]235        }
[151]236        break;
237      case LIB3DS_DL_OFF:
[8]238        {
[151]239          light->off=LIB3DS_TRUE;
[8]240        }
[151]241        break;
242      case LIB3DS_DL_OUTER_RANGE:
[8]243        {
[10076]244          light->outer_range=lib3ds_float_read(strm);
[8]245        }
[151]246        break;
247      case LIB3DS_DL_INNER_RANGE:
[8]248        {
[10076]249          light->inner_range=lib3ds_float_read(strm);
[8]250        }
[151]251        break;
252      case LIB3DS_DL_MULTIPLIER:
[8]253        {
[10076]254          light->multiplier=lib3ds_float_read(strm);
[8]255        }
[151]256        break;
257      case LIB3DS_DL_EXCLUDE:
[8]258        {
[151]259          /* FIXME: */
260          lib3ds_chunk_unknown(chunk);
[8]261        }
[151]262      case LIB3DS_DL_ATTENUATE:
[8]263        {
[10076]264          light->attenuation=lib3ds_float_read(strm);
[8]265        }
[151]266        break;
267      case LIB3DS_DL_SPOTLIGHT:
[8]268        {
[10076]269          lib3ds_chunk_read_reset(&c, strm);
270          if (!spotlight_read(light, strm)) {
[8]271            return(LIB3DS_FALSE);
[151]272          }
[8]273        }
[151]274        break;
275      default:
276        lib3ds_chunk_unknown(chunk);
[8]277    }
[151]278  }
279 
[10076]280  lib3ds_chunk_read_end(&c, strm);
[151]281  return(LIB3DS_TRUE);
[8]282}
283
284
285/*!
[151]286 * \ingroup light
287 */
288Lib3dsBool
[10076]289lib3ds_light_write(Lib3dsLight *light, iostream *strm)
[151]290{
291  Lib3dsChunk c;
[8]292
[151]293  c.chunk=LIB3DS_N_DIRECT_LIGHT;
[10076]294  if (!lib3ds_chunk_write_start(&c,strm)) {
[151]295    return(LIB3DS_FALSE);
296  }
[10076]297  lib3ds_vector_write(light->position, strm);
[151]298  { /*---- LIB3DS_COLOR_F ----*/
299    Lib3dsChunk c;
300    c.chunk=LIB3DS_COLOR_F;
301    c.size=18;
[10076]302    lib3ds_chunk_write(&c, strm);
303    lib3ds_rgb_write(light->color,strm);
[151]304  }
305  if (light->off) { /*---- LIB3DS_DL_OFF ----*/
306    Lib3dsChunk c;
307    c.chunk=LIB3DS_DL_OFF;
308    c.size=6;
[10076]309    lib3ds_chunk_write(&c, strm);
[151]310  }
311  { /*---- LIB3DS_DL_OUTER_RANGE ----*/
312    Lib3dsChunk c;
313    c.chunk=LIB3DS_DL_OUTER_RANGE;
314    c.size=10;
[10076]315    lib3ds_chunk_write(&c, strm);
316    lib3ds_float_write(light->outer_range,strm);
[151]317  }
318  { /*---- LIB3DS_DL_INNER_RANGE ----*/
319    Lib3dsChunk c;
320    c.chunk=LIB3DS_DL_INNER_RANGE;
321    c.size=10;
[10076]322    lib3ds_chunk_write(&c, strm);
323    lib3ds_float_write(light->inner_range,strm);
[151]324  }
325  { /*---- LIB3DS_DL_MULTIPLIER ----*/
326    Lib3dsChunk c;
327    c.chunk=LIB3DS_DL_MULTIPLIER;
328    c.size=10;
[10076]329    lib3ds_chunk_write(&c, strm);
330    lib3ds_float_write(light->multiplier, strm);
[151]331  }
332  if (light->attenuation) { /*---- LIB3DS_DL_ATTENUATE ----*/
333    Lib3dsChunk c;
334    c.chunk=LIB3DS_DL_ATTENUATE;
335    c.size=6;
[10076]336    lib3ds_chunk_write(&c, strm);
[151]337  }
[8]338
[151]339  if (light->spot_light) {
340    Lib3dsChunk c;
341
342    c.chunk=LIB3DS_DL_SPOTLIGHT;
[10076]343    if (!lib3ds_chunk_write_start(&c,strm)) {
[151]344      return(LIB3DS_FALSE);
345    }
[10076]346    lib3ds_vector_write(light->spot, strm);
347    lib3ds_float_write(light->hot_spot, strm);
348    lib3ds_float_write(light->fall_off, strm);
[151]349   
350    { /*---- LIB3DS_DL_SPOT_ROLL ----*/
351      Lib3dsChunk c;
352      c.chunk=LIB3DS_DL_SPOT_ROLL;
353      c.size=10;
[10076]354      lib3ds_chunk_write(&c, strm);
355      lib3ds_float_write(light->roll,strm);
[151]356    }
357    if (light->shadowed) { /*---- LIB3DS_DL_SHADOWED ----*/
358      Lib3dsChunk c;
359      c.chunk=LIB3DS_DL_SHADOWED;
360      c.size=6;
[10076]361      lib3ds_chunk_write(&c, strm);
[151]362    }
363    if ((fabs(light->shadow_bias)>LIB3DS_EPSILON) ||
364      (fabs(light->shadow_filter)>LIB3DS_EPSILON) ||
365      (light->shadow_size!=0)) { /*---- LIB3DS_DL_LOCAL_SHADOW2 ----*/
366      Lib3dsChunk c;
367      c.chunk=LIB3DS_DL_LOCAL_SHADOW2;
368      c.size=16;
[10076]369      lib3ds_chunk_write(&c, strm);
370      lib3ds_float_write(light->shadow_bias,strm);
371      lib3ds_float_write(light->shadow_filter,strm);
372      lib3ds_intw_write(light->shadow_size,strm);
[151]373    }
374    if (light->see_cone) { /*---- LIB3DS_DL_SEE_CONE ----*/
375      Lib3dsChunk c;
376      c.chunk=LIB3DS_DL_SEE_CONE;
377      c.size=6;
[10076]378      lib3ds_chunk_write(&c, strm);
[151]379    }
380    if (light->rectangular_spot) { /*---- LIB3DS_DL_SPOT_RECTANGULAR ----*/
381      Lib3dsChunk c;
382      c.chunk=LIB3DS_DL_SPOT_RECTANGULAR;
383      c.size=6;
[10076]384      lib3ds_chunk_write(&c, strm);
[151]385    }
386    if (fabs(light->spot_aspect)>LIB3DS_EPSILON) { /*---- LIB3DS_DL_SPOT_ASPECT ----*/
387      Lib3dsChunk c;
388      c.chunk=LIB3DS_DL_SPOT_ASPECT;
389      c.size=10;
[10076]390      lib3ds_chunk_write(&c, strm);
391      lib3ds_float_write(light->spot_aspect,strm);
[151]392    }
393    if (light->use_projector) { /*---- LIB3DS_DL_SPOT_PROJECTOR ----*/
394      Lib3dsChunk c;
395      c.chunk=LIB3DS_DL_SPOT_PROJECTOR;
396      c.size=10;
[10076]397      lib3ds_chunk_write(&c, strm);
398      lib3ds_string_write(light->projector,strm);
[151]399    }
400    if (light->spot_overshoot) { /*---- LIB3DS_DL_SPOT_OVERSHOOT ----*/
401      Lib3dsChunk c;
402      c.chunk=LIB3DS_DL_SPOT_OVERSHOOT;
403      c.size=6;
[10076]404      lib3ds_chunk_write(&c, strm);
[151]405    }
406    if (fabs(light->ray_bias)>LIB3DS_EPSILON) { /*---- LIB3DS_DL_RAY_BIAS ----*/
407      Lib3dsChunk c;
408      c.chunk=LIB3DS_DL_RAY_BIAS;
409      c.size=10;
[10076]410      lib3ds_chunk_write(&c, strm);
411      lib3ds_float_write(light->ray_bias,strm);
[151]412    }
413    if (light->ray_shadows) { /*---- LIB3DS_DL_RAYSHAD ----*/
414      Lib3dsChunk c;
415      c.chunk=LIB3DS_DL_RAYSHAD;
416      c.size=6;
[10076]417      lib3ds_chunk_write(&c, strm);
[151]418    }
[10076]419    if (!lib3ds_chunk_write_end(&c,strm)) {
[151]420      return(LIB3DS_FALSE);
421    }
422  }
[10076]423  if (!lib3ds_chunk_write_end(&c,strm)) {
[151]424    return(LIB3DS_FALSE);
425  }
426  return(LIB3DS_TRUE);
427}
428
Note: See TracBrowser for help on using the browser.