2 * The 3D Studio File Format Library
3 * Copyright (C) 1996-2001 by J.E. Hoffmann <je-h@gmx.net>
6 * This program is free software; you can redistribute it and/or modify it
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
9 * your option) any later version.
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
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 * License for more details.
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.
20 * $Id: light.c,v 1.10 2001/07/11 13:47:35 jeh Exp $
23 #include <lib3ds/light.h>
24 #include <lib3ds/chunk.h>
25 #include <lib3ds/io.h>
35 * \defgroup light Lights
37 * \author J.E. Hoffmann <je-h@gmx.net>
53 lib3ds_light_new(const char *name)
58 ASSERT(strlen(name)<64);
60 light=(Lib3dsLight*)calloc(sizeof(Lib3dsLight), 1);
64 strcpy(light->name, name);
73 lib3ds_light_free(Lib3dsLight *light)
75 memset(light, 0, sizeof(Lib3dsLight));
84 lib3ds_light_dump(Lib3dsLight *light)
87 printf(" name: %s\n", light->name);
88 printf(" spot_light: %s\n", light->spot_light ? "yes" : "no");
89 printf(" see_cone: %s\n", light->see_cone ? "yes" : "no");
90 printf(" color: (%f, %f, %f)\n",
91 light->color[0], light->color[1], light->color[2]);
92 printf(" position (%f, %f, %f)\n",
93 light->position[0], light->position[1], light->position[2]);
94 printf(" spot (%f, %f, %f)\n",
95 light->spot[0], light->spot[1], light->spot[2]);
96 printf(" roll: %f\n", light->roll);
97 printf(" off: %s\n", light->off ? "yes" : "no");
98 printf(" outer_range: %f\n", light->outer_range);
99 printf(" inner_range: %f\n", light->inner_range);
100 printf(" multiplier: %f\n", light->multiplier);
101 printf(" attenuation: %f\n", light->attenuation);
102 printf(" rectangular_spot: %s\n", light->rectangular_spot ? "yes" : "no");
103 printf(" shadowed: %s\n", light->shadowed ? "yes" : "no");
104 printf(" shadow_bias: %f\n", light->shadow_bias);
105 printf(" shadow_filter: %f\n", light->shadow_filter);
106 printf(" shadow_size: %d\n", light->shadow_size);
107 printf(" spot_aspect: %f\n", light->spot_aspect);
108 printf(" use_projector: %s\n", light->use_projector ? "yes" : "no");
109 printf(" projector: %s\n", light->projector);
110 printf(" spot_overshoot: %d\n", (int)light->spot_overshoot);
111 printf(" ray_shadows: %s\n", light->ray_shadows ? "yes" : "no");
112 printf(" ray_bias: %f\n", light->ray_bias);
113 printf(" hot_spot: %f\n", light->hot_spot);
114 printf(" fall_off: %f\n", light->fall_off);
123 spotlight_read(Lib3dsLight *light, Lib3dsIo *io)
129 if (!lib3ds_chunk_read_start(&c, LIB3DS_DL_SPOTLIGHT, io)) {
130 return(LIB3DS_FALSE);
132 light->spot_light=LIB3DS_TRUE;
133 for (i=0; i<3; ++i) {
134 light->spot[i]=lib3ds_io_read_float(io);
136 light->hot_spot = lib3ds_io_read_float(io);
137 light->fall_off = lib3ds_io_read_float(io);
138 lib3ds_chunk_read_tell(&c, io);
140 while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
142 case LIB3DS_DL_SPOT_ROLL:
144 light->roll=lib3ds_io_read_float(io);
147 case LIB3DS_DL_SHADOWED:
149 light->shadowed=LIB3DS_TRUE;
152 case LIB3DS_DL_LOCAL_SHADOW2:
154 light->shadow_bias=lib3ds_io_read_float(io);
155 light->shadow_filter=lib3ds_io_read_float(io);
156 light->shadow_size=lib3ds_io_read_intw(io);
159 case LIB3DS_DL_SEE_CONE:
161 light->see_cone=LIB3DS_TRUE;
164 case LIB3DS_DL_SPOT_RECTANGULAR:
166 light->rectangular_spot=LIB3DS_TRUE;
169 case LIB3DS_DL_SPOT_ASPECT:
171 light->spot_aspect=lib3ds_io_read_float(io);
174 case LIB3DS_DL_SPOT_PROJECTOR:
176 light->use_projector=LIB3DS_TRUE;
177 if (!lib3ds_io_read_string(io, light->projector, 64)) {
178 return(LIB3DS_FALSE);
181 case LIB3DS_DL_SPOT_OVERSHOOT:
183 light->spot_overshoot=LIB3DS_TRUE;
186 case LIB3DS_DL_RAY_BIAS:
188 light->ray_bias=lib3ds_io_read_float(io);
191 case LIB3DS_DL_RAYSHAD:
193 light->ray_shadows=LIB3DS_TRUE;
197 lib3ds_chunk_unknown(chunk);
201 lib3ds_chunk_read_end(&c, io);
210 lib3ds_light_read(Lib3dsLight *light, Lib3dsIo *io)
215 if (!lib3ds_chunk_read_start(&c, LIB3DS_N_DIRECT_LIGHT, io)) {
216 return(LIB3DS_FALSE);
220 for (i=0; i<3; ++i) {
221 light->position[i]=lib3ds_io_read_float(io);
224 lib3ds_chunk_read_tell(&c, io);
226 while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
231 for (i=0; i<3; ++i) {
232 light->color[i]=lib3ds_io_read_float(io);
238 light->off=LIB3DS_TRUE;
241 case LIB3DS_DL_OUTER_RANGE:
243 light->outer_range=lib3ds_io_read_float(io);
246 case LIB3DS_DL_INNER_RANGE:
248 light->inner_range=lib3ds_io_read_float(io);
251 case LIB3DS_DL_MULTIPLIER:
253 light->multiplier=lib3ds_io_read_float(io);
256 case LIB3DS_DL_EXCLUDE:
259 lib3ds_chunk_unknown(chunk);
261 case LIB3DS_DL_ATTENUATE:
263 light->attenuation=lib3ds_io_read_float(io);
266 case LIB3DS_DL_SPOTLIGHT:
268 lib3ds_chunk_read_reset(&c, io);
269 if (!spotlight_read(light, io)) {
270 return(LIB3DS_FALSE);
275 lib3ds_chunk_unknown(chunk);
279 lib3ds_chunk_read_end(&c, io);
288 lib3ds_light_write(Lib3dsLight *light, Lib3dsIo *io)
292 c.chunk=LIB3DS_N_DIRECT_LIGHT;
293 if (!lib3ds_chunk_write_start(&c,io)) {
294 return(LIB3DS_FALSE);
296 lib3ds_io_write_vector(io, light->position);
297 { /*---- LIB3DS_COLOR_F ----*/
299 c.chunk=LIB3DS_COLOR_F;
301 lib3ds_chunk_write(&c, io);
302 lib3ds_io_write_rgb(io, light->color);
304 if (light->off) { /*---- LIB3DS_DL_OFF ----*/
306 c.chunk=LIB3DS_DL_OFF;
308 lib3ds_chunk_write(&c, io);
310 { /*---- LIB3DS_DL_OUTER_RANGE ----*/
312 c.chunk=LIB3DS_DL_OUTER_RANGE;
314 lib3ds_chunk_write(&c, io);
315 lib3ds_io_write_float(io, light->outer_range);
317 { /*---- LIB3DS_DL_INNER_RANGE ----*/
319 c.chunk=LIB3DS_DL_INNER_RANGE;
321 lib3ds_chunk_write(&c, io);
322 lib3ds_io_write_float(io, light->inner_range);
324 { /*---- LIB3DS_DL_MULTIPLIER ----*/
326 c.chunk=LIB3DS_DL_MULTIPLIER;
328 lib3ds_chunk_write(&c, io);
329 lib3ds_io_write_float(io, light->multiplier);
331 if (light->attenuation) { /*---- LIB3DS_DL_ATTENUATE ----*/
333 c.chunk=LIB3DS_DL_ATTENUATE;
335 lib3ds_chunk_write(&c, io);
338 if (light->spot_light) {
341 c.chunk=LIB3DS_DL_SPOTLIGHT;
342 if (!lib3ds_chunk_write_start(&c,io)) {
343 return(LIB3DS_FALSE);
345 lib3ds_io_write_vector(io, light->spot);
346 lib3ds_io_write_float(io, light->hot_spot);
347 lib3ds_io_write_float(io, light->fall_off);
349 { /*---- LIB3DS_DL_SPOT_ROLL ----*/
351 c.chunk=LIB3DS_DL_SPOT_ROLL;
353 lib3ds_chunk_write(&c, io);
354 lib3ds_io_write_float(io, light->roll);
356 if (light->shadowed) { /*---- LIB3DS_DL_SHADOWED ----*/
358 c.chunk=LIB3DS_DL_SHADOWED;
360 lib3ds_chunk_write(&c, io);
362 if ((fabs(light->shadow_bias)>LIB3DS_EPSILON) ||
363 (fabs(light->shadow_filter)>LIB3DS_EPSILON) ||
364 (light->shadow_size!=0)) { /*---- LIB3DS_DL_LOCAL_SHADOW2 ----*/
366 c.chunk=LIB3DS_DL_LOCAL_SHADOW2;
368 lib3ds_chunk_write(&c, io);
369 lib3ds_io_write_float(io, light->shadow_bias);
370 lib3ds_io_write_float(io, light->shadow_filter);
371 lib3ds_io_write_intw(io, light->shadow_size);
373 if (light->see_cone) { /*---- LIB3DS_DL_SEE_CONE ----*/
375 c.chunk=LIB3DS_DL_SEE_CONE;
377 lib3ds_chunk_write(&c, io);
379 if (light->rectangular_spot) { /*---- LIB3DS_DL_SPOT_RECTANGULAR ----*/
381 c.chunk=LIB3DS_DL_SPOT_RECTANGULAR;
383 lib3ds_chunk_write(&c, io);
385 if (fabs(light->spot_aspect)>LIB3DS_EPSILON) { /*---- LIB3DS_DL_SPOT_ASPECT ----*/
387 c.chunk=LIB3DS_DL_SPOT_ASPECT;
389 lib3ds_chunk_write(&c, io);
390 lib3ds_io_write_float(io, light->spot_aspect);
392 if (light->use_projector) { /*---- LIB3DS_DL_SPOT_PROJECTOR ----*/
394 c.chunk=LIB3DS_DL_SPOT_PROJECTOR;
396 lib3ds_chunk_write(&c, io);
397 lib3ds_io_write_string(io, light->projector);
399 if (light->spot_overshoot) { /*---- LIB3DS_DL_SPOT_OVERSHOOT ----*/
401 c.chunk=LIB3DS_DL_SPOT_OVERSHOOT;
403 lib3ds_chunk_write(&c, io);
405 if (fabs(light->ray_bias)>LIB3DS_EPSILON) { /*---- LIB3DS_DL_RAY_BIAS ----*/
407 c.chunk=LIB3DS_DL_RAY_BIAS;
409 lib3ds_chunk_write(&c, io);
410 lib3ds_io_write_float(io, light->ray_bias);
412 if (light->ray_shadows) { /*---- LIB3DS_DL_RAYSHAD ----*/
414 c.chunk=LIB3DS_DL_RAYSHAD;
416 lib3ds_chunk_write(&c, io);
418 if (!lib3ds_chunk_write_end(&c,io)) {
419 return(LIB3DS_FALSE);
422 if (!lib3ds_chunk_write_end(&c,io)) {
423 return(LIB3DS_FALSE);