2
Copyright (C) 1996-2008 by Jan Eric Kyprianidis <www.kyprianidis.com>
5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU Lesser General Public License as published
7
by the Free Software Foundation, either version 2.1 of the License, or
8
(at your option) any later version.
10
Thisprogram is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU Lesser General Public License for more details.
15
You should have received a copy of the GNU Lesser General Public License
16
along with this program; If not, see <http://www.gnu.org/licenses/>.
18
#include "lib3ds_impl.h"
22
lib3ds_light_new(const char *name) {
26
assert(strlen(name) < 64);
28
light = (Lib3dsLight*)calloc(sizeof(Lib3dsLight), 1);
32
strcpy(light->name, name);
38
lib3ds_light_free(Lib3dsLight *light) {
39
memset(light, 0, sizeof(Lib3dsLight));
45
spotlight_read(Lib3dsLight *light, Lib3dsIo *io) {
50
lib3ds_chunk_read_start(&c, CHK_DL_SPOTLIGHT, io);
52
light->spot_light = TRUE;
53
for (i = 0; i < 3; ++i) {
54
light->target[i] = lib3ds_io_read_float(io);
56
light->hotspot = lib3ds_io_read_float(io);
57
light->falloff = lib3ds_io_read_float(io);
58
lib3ds_chunk_read_tell(&c, io);
60
while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) {
62
case CHK_DL_SPOT_ROLL:
63
light->roll = lib3ds_io_read_float(io);
66
case CHK_DL_SHADOWED: {
67
light->shadowed = TRUE;
71
case CHK_DL_LOCAL_SHADOW2: {
72
light->shadow_bias = lib3ds_io_read_float(io);
73
light->shadow_filter = lib3ds_io_read_float(io);
74
light->shadow_size = lib3ds_io_read_intw(io);
78
case CHK_DL_SEE_CONE: {
79
light->see_cone = TRUE;
83
case CHK_DL_SPOT_RECTANGULAR: {
84
light->rectangular_spot = TRUE;
88
case CHK_DL_SPOT_ASPECT: {
89
light->spot_aspect = lib3ds_io_read_float(io);
93
case CHK_DL_SPOT_PROJECTOR: {
94
light->use_projector = TRUE;
95
lib3ds_io_read_string(io, light->projector, 64);
99
case CHK_DL_SPOT_OVERSHOOT: {
100
light->spot_overshoot = TRUE;
104
case CHK_DL_RAY_BIAS: {
105
light->ray_bias = lib3ds_io_read_float(io);
109
case CHK_DL_RAYSHAD: {
110
light->ray_shadows = TRUE;
115
lib3ds_chunk_unknown(chunk, io);
119
lib3ds_chunk_read_end(&c, io);
124
lib3ds_light_read(Lib3dsLight *light, Lib3dsIo *io) {
128
lib3ds_chunk_read_start(&c, CHK_N_DIRECT_LIGHT, io);
132
for (i = 0; i < 3; ++i) {
133
light->position[i] = lib3ds_io_read_float(io);
136
lib3ds_chunk_read_tell(&c, io);
138
while ((chunk = lib3ds_chunk_read_next(&c, io)) != 0) {
142
for (i = 0; i < 3; ++i) {
143
light->color[i] = lib3ds_io_read_float(io);
152
case CHK_DL_OUTER_RANGE:
153
light->outer_range = lib3ds_io_read_float(io);
156
case CHK_DL_INNER_RANGE:
157
light->inner_range = lib3ds_io_read_float(io);
160
case CHK_DL_MULTIPLIER:
161
light->multiplier = lib3ds_io_read_float(io);
164
case CHK_DL_EXCLUDE: {
166
lib3ds_chunk_unknown(chunk, io);
170
case CHK_DL_ATTENUATE:
171
light->attenuation = lib3ds_io_read_float(io);
174
case CHK_DL_SPOTLIGHT: {
175
lib3ds_chunk_read_reset(&c, io);
176
spotlight_read(light, io);
181
lib3ds_chunk_unknown(chunk, io);
185
lib3ds_chunk_read_end(&c, io);
190
lib3ds_light_write(Lib3dsLight *light, Lib3dsIo *io) {
193
c.chunk = CHK_N_DIRECT_LIGHT;
194
lib3ds_chunk_write_start(&c, io);
196
lib3ds_io_write_vector(io, light->position);
197
{ /*---- LIB3DS_COLOR_F ----*/
199
c.chunk = CHK_COLOR_F;
201
lib3ds_chunk_write(&c, io);
202
lib3ds_io_write_rgb(io, light->color);
204
if (light->off) { /*---- LIB3DS_DL_OFF ----*/
206
c.chunk = CHK_DL_OFF;
208
lib3ds_chunk_write(&c, io);
210
{ /*---- LIB3DS_DL_OUTER_RANGE ----*/
212
c.chunk = CHK_DL_OUTER_RANGE;
214
lib3ds_chunk_write(&c, io);
215
lib3ds_io_write_float(io, light->outer_range);
217
{ /*---- LIB3DS_DL_INNER_RANGE ----*/
219
c.chunk = CHK_DL_INNER_RANGE;
221
lib3ds_chunk_write(&c, io);
222
lib3ds_io_write_float(io, light->inner_range);
224
{ /*---- LIB3DS_DL_MULTIPLIER ----*/
226
c.chunk = CHK_DL_MULTIPLIER;
228
lib3ds_chunk_write(&c, io);
229
lib3ds_io_write_float(io, light->multiplier);
231
if (light->attenuation) { /*---- LIB3DS_DL_ATTENUATE ----*/
233
c.chunk = CHK_DL_ATTENUATE;
235
lib3ds_chunk_write(&c, io);
238
if (light->spot_light) {
241
c.chunk = CHK_DL_SPOTLIGHT;
242
lib3ds_chunk_write_start(&c, io);
244
lib3ds_io_write_vector(io, light->target);
245
lib3ds_io_write_float(io, light->hotspot);
246
lib3ds_io_write_float(io, light->falloff);
248
{ /*---- LIB3DS_DL_SPOT_ROLL ----*/
250
c.chunk = CHK_DL_SPOT_ROLL;
252
lib3ds_chunk_write(&c, io);
253
lib3ds_io_write_float(io, light->roll);
255
if (light->shadowed) { /*---- LIB3DS_DL_SHADOWED ----*/
257
c.chunk = CHK_DL_SHADOWED;
259
lib3ds_chunk_write(&c, io);
261
if ((fabs(light->shadow_bias) > LIB3DS_EPSILON) ||
262
(fabs(light->shadow_filter) > LIB3DS_EPSILON) ||
263
(light->shadow_size != 0)) { /*---- LIB3DS_DL_LOCAL_SHADOW2 ----*/
265
c.chunk = CHK_DL_LOCAL_SHADOW2;
267
lib3ds_chunk_write(&c, io);
268
lib3ds_io_write_float(io, light->shadow_bias);
269
lib3ds_io_write_float(io, light->shadow_filter);
270
lib3ds_io_write_intw(io, (int16_t)light->shadow_size);
272
if (light->see_cone) { /*---- LIB3DS_DL_SEE_CONE ----*/
274
c.chunk = CHK_DL_SEE_CONE;
276
lib3ds_chunk_write(&c, io);
278
if (light->rectangular_spot) { /*---- LIB3DS_DL_SPOT_RECTANGULAR ----*/
280
c.chunk = CHK_DL_SPOT_RECTANGULAR;
282
lib3ds_chunk_write(&c, io);
284
if (fabs(light->spot_aspect) > LIB3DS_EPSILON) { /*---- LIB3DS_DL_SPOT_ASPECT ----*/
286
c.chunk = CHK_DL_SPOT_ASPECT;
288
lib3ds_chunk_write(&c, io);
289
lib3ds_io_write_float(io, light->spot_aspect);
291
if (light->use_projector) { /*---- LIB3DS_DL_SPOT_PROJECTOR ----*/
293
c.chunk = CHK_DL_SPOT_PROJECTOR;
295
lib3ds_chunk_write(&c, io);
296
lib3ds_io_write_string(io, light->projector);
298
if (light->spot_overshoot) { /*---- LIB3DS_DL_SPOT_OVERSHOOT ----*/
300
c.chunk = CHK_DL_SPOT_OVERSHOOT;
302
lib3ds_chunk_write(&c, io);
304
if (fabs(light->ray_bias) > LIB3DS_EPSILON) { /*---- LIB3DS_DL_RAY_BIAS ----*/
306
c.chunk = CHK_DL_RAY_BIAS;
308
lib3ds_chunk_write(&c, io);
309
lib3ds_io_write_float(io, light->ray_bias);
311
if (light->ray_shadows) { /*---- LIB3DS_DL_RAYSHAD ----*/
313
c.chunk = CHK_DL_RAYSHAD;
315
lib3ds_chunk_write(&c, io);
317
lib3ds_chunk_write_end(&c, io);
320
lib3ds_chunk_write_end(&c, io);