| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | #define LIB3DS_EXPORT |
|---|
| 23 | #include "light.h" |
|---|
| 24 | #include "chunk.h" |
|---|
| 25 | #include "readwrite.h" |
|---|
| 26 | #include <stdlib.h> |
|---|
| 27 | #include <math.h> |
|---|
| 28 | #include <string.h> |
|---|
| 29 | #include "config.h" |
|---|
| 30 | #ifdef WITH_DMALLOC |
|---|
| 31 | #include <dmalloc.h> |
|---|
| 32 | #endif |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | Lib3dsLight* |
|---|
| 54 | lib3ds_light_new(const char *name) |
|---|
| 55 | { |
|---|
| 56 | Lib3dsLight *light; |
|---|
| 57 | |
|---|
| 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); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | void |
|---|
| 74 | lib3ds_light_free(Lib3dsLight *light) |
|---|
| 75 | { |
|---|
| 76 | memset(light, 0, sizeof(Lib3dsLight)); |
|---|
| 77 | free(light); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | void |
|---|
| 85 | lib3ds_light_dump(Lib3dsLight *light) |
|---|
| 86 | { |
|---|
| 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); |
|---|
| 111 | printf(" spot_overshoot: %i\n", static_cast<int>(light->spot_overshoot)); |
|---|
| 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 | } |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | static Lib3dsBool |
|---|
| 124 | spotlight_read(Lib3dsLight *light, iostream *strm) |
|---|
| 125 | { |
|---|
| 126 | Lib3dsChunk c; |
|---|
| 127 | Lib3dsWord chunk; |
|---|
| 128 | int i; |
|---|
| 129 | |
|---|
| 130 | if (!lib3ds_chunk_read_start(&c, LIB3DS_DL_SPOTLIGHT, strm)) { |
|---|
| 131 | return(LIB3DS_FALSE); |
|---|
| 132 | } |
|---|
| 133 | light->spot_light=LIB3DS_TRUE; |
|---|
| 134 | for (i=0; i<3; ++i) { |
|---|
| 135 | light->spot[i]=lib3ds_float_read(strm); |
|---|
| 136 | } |
|---|
| 137 | light->hot_spot = lib3ds_float_read(strm); |
|---|
| 138 | light->fall_off = lib3ds_float_read(strm); |
|---|
| 139 | lib3ds_chunk_read_tell(&c, strm); |
|---|
| 140 | |
|---|
| 141 | while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { |
|---|
| 142 | switch (chunk) { |
|---|
| 143 | case LIB3DS_DL_SPOT_ROLL: |
|---|
| 144 | { |
|---|
| 145 | light->roll=lib3ds_float_read(strm); |
|---|
| 146 | } |
|---|
| 147 | break; |
|---|
| 148 | case LIB3DS_DL_SHADOWED: |
|---|
| 149 | { |
|---|
| 150 | light->shadowed=LIB3DS_TRUE; |
|---|
| 151 | } |
|---|
| 152 | break; |
|---|
| 153 | case LIB3DS_DL_LOCAL_SHADOW2: |
|---|
| 154 | { |
|---|
| 155 | light->shadow_bias=lib3ds_float_read(strm); |
|---|
| 156 | light->shadow_filter=lib3ds_float_read(strm); |
|---|
| 157 | light->shadow_size=lib3ds_intw_read(strm); |
|---|
| 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 | { |
|---|
| 172 | light->spot_aspect=lib3ds_float_read(strm); |
|---|
| 173 | } |
|---|
| 174 | break; |
|---|
| 175 | case LIB3DS_DL_SPOT_PROJECTOR: |
|---|
| 176 | { |
|---|
| 177 | light->use_projector=LIB3DS_TRUE; |
|---|
| 178 | if (!lib3ds_string_read(light->projector, 64, strm)) { |
|---|
| 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 | { |
|---|
| 189 | light->ray_bias=lib3ds_float_read(strm); |
|---|
| 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); |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | lib3ds_chunk_read_end(&c, strm); |
|---|
| 203 | return(LIB3DS_TRUE); |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | Lib3dsBool |
|---|
| 211 | lib3ds_light_read(Lib3dsLight *light, iostream * strm) |
|---|
| 212 | { |
|---|
| 213 | Lib3dsChunk c; |
|---|
| 214 | Lib3dsWord chunk; |
|---|
| 215 | |
|---|
| 216 | if (!lib3ds_chunk_read_start(&c, LIB3DS_N_DIRECT_LIGHT, strm)) { |
|---|
| 217 | return(LIB3DS_FALSE); |
|---|
| 218 | } |
|---|
| 219 | { |
|---|
| 220 | int i; |
|---|
| 221 | for (i=0; i<3; ++i) { |
|---|
| 222 | light->position[i]=lib3ds_float_read(strm); |
|---|
| 223 | } |
|---|
| 224 | } |
|---|
| 225 | lib3ds_chunk_read_tell(&c, strm); |
|---|
| 226 | |
|---|
| 227 | while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { |
|---|
| 228 | switch (chunk) { |
|---|
| 229 | case LIB3DS_COLOR_F: |
|---|
| 230 | { |
|---|
| 231 | int i; |
|---|
| 232 | for (i=0; i<3; ++i) { |
|---|
| 233 | light->color[i]=lib3ds_float_read(strm); |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | break; |
|---|
| 237 | case LIB3DS_DL_OFF: |
|---|
| 238 | { |
|---|
| 239 | light->off=LIB3DS_TRUE; |
|---|
| 240 | } |
|---|
| 241 | break; |
|---|
| 242 | case LIB3DS_DL_OUTER_RANGE: |
|---|
| 243 | { |
|---|
| 244 | light->outer_range=lib3ds_float_read(strm); |
|---|
| 245 | } |
|---|
| 246 | break; |
|---|
| 247 | case LIB3DS_DL_INNER_RANGE: |
|---|
| 248 | { |
|---|
| 249 | light->inner_range=lib3ds_float_read(strm); |
|---|
| 250 | } |
|---|
| 251 | break; |
|---|
| 252 | case LIB3DS_DL_MULTIPLIER: |
|---|
| 253 | { |
|---|
| 254 | light->multiplier=lib3ds_float_read(strm); |
|---|
| 255 | } |
|---|
| 256 | break; |
|---|
| 257 | case LIB3DS_DL_EXCLUDE: |
|---|
| 258 | { |
|---|
| 259 | |
|---|
| 260 | lib3ds_chunk_unknown(chunk); |
|---|
| 261 | } |
|---|
| 262 | case LIB3DS_DL_ATTENUATE: |
|---|
| 263 | { |
|---|
| 264 | light->attenuation=lib3ds_float_read(strm); |
|---|
| 265 | } |
|---|
| 266 | break; |
|---|
| 267 | case LIB3DS_DL_SPOTLIGHT: |
|---|
| 268 | { |
|---|
| 269 | lib3ds_chunk_read_reset(&c, strm); |
|---|
| 270 | if (!spotlight_read(light, strm)) { |
|---|
| 271 | return(LIB3DS_FALSE); |
|---|
| 272 | } |
|---|
| 273 | } |
|---|
| 274 | break; |
|---|
| 275 | default: |
|---|
| 276 | lib3ds_chunk_unknown(chunk); |
|---|
| 277 | } |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | lib3ds_chunk_read_end(&c, strm); |
|---|
| 281 | return(LIB3DS_TRUE); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | Lib3dsBool |
|---|
| 289 | lib3ds_light_write(Lib3dsLight *light, iostream *strm) |
|---|
| 290 | { |
|---|
| 291 | Lib3dsChunk c; |
|---|
| 292 | |
|---|
| 293 | c.chunk=LIB3DS_N_DIRECT_LIGHT; |
|---|
| 294 | if (!lib3ds_chunk_write_start(&c,strm)) { |
|---|
| 295 | return(LIB3DS_FALSE); |
|---|
| 296 | } |
|---|
| 297 | lib3ds_vector_write(light->position, strm); |
|---|
| 298 | { |
|---|
| 299 | Lib3dsChunk c; |
|---|
| 300 | c.chunk=LIB3DS_COLOR_F; |
|---|
| 301 | c.size=18; |
|---|
| 302 | lib3ds_chunk_write(&c, strm); |
|---|
| 303 | lib3ds_rgb_write(light->color,strm); |
|---|
| 304 | } |
|---|
| 305 | if (light->off) { |
|---|
| 306 | Lib3dsChunk c; |
|---|
| 307 | c.chunk=LIB3DS_DL_OFF; |
|---|
| 308 | c.size=6; |
|---|
| 309 | lib3ds_chunk_write(&c, strm); |
|---|
| 310 | } |
|---|
| 311 | { |
|---|
| 312 | Lib3dsChunk c; |
|---|
| 313 | c.chunk=LIB3DS_DL_OUTER_RANGE; |
|---|
| 314 | c.size=10; |
|---|
| 315 | lib3ds_chunk_write(&c, strm); |
|---|
| 316 | lib3ds_float_write(light->outer_range,strm); |
|---|
| 317 | } |
|---|
| 318 | { |
|---|
| 319 | Lib3dsChunk c; |
|---|
| 320 | c.chunk=LIB3DS_DL_INNER_RANGE; |
|---|
| 321 | c.size=10; |
|---|
| 322 | lib3ds_chunk_write(&c, strm); |
|---|
| 323 | lib3ds_float_write(light->inner_range,strm); |
|---|
| 324 | } |
|---|
| 325 | { |
|---|
| 326 | Lib3dsChunk c; |
|---|
| 327 | c.chunk=LIB3DS_DL_MULTIPLIER; |
|---|
| 328 | c.size=10; |
|---|
| 329 | lib3ds_chunk_write(&c, strm); |
|---|
| 330 | lib3ds_float_write(light->multiplier, strm); |
|---|
| 331 | } |
|---|
| 332 | if (light->attenuation) { |
|---|
| 333 | Lib3dsChunk c; |
|---|
| 334 | c.chunk=LIB3DS_DL_ATTENUATE; |
|---|
| 335 | c.size=6; |
|---|
| 336 | lib3ds_chunk_write(&c, strm); |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | if (light->spot_light) { |
|---|
| 340 | Lib3dsChunk c; |
|---|
| 341 | |
|---|
| 342 | c.chunk=LIB3DS_DL_SPOTLIGHT; |
|---|
| 343 | if (!lib3ds_chunk_write_start(&c,strm)) { |
|---|
| 344 | return(LIB3DS_FALSE); |
|---|
| 345 | } |
|---|
| 346 | lib3ds_vector_write(light->spot, strm); |
|---|
| 347 | lib3ds_float_write(light->hot_spot, strm); |
|---|
| 348 | lib3ds_float_write(light->fall_off, strm); |
|---|
| 349 | |
|---|
| 350 | { |
|---|
| 351 | Lib3dsChunk c; |
|---|
| 352 | c.chunk=LIB3DS_DL_SPOT_ROLL; |
|---|
| 353 | c.size=10; |
|---|
| 354 | lib3ds_chunk_write(&c, strm); |
|---|
| 355 | lib3ds_float_write(light->roll,strm); |
|---|
| 356 | } |
|---|
| 357 | if (light->shadowed) { |
|---|
| 358 | Lib3dsChunk c; |
|---|
| 359 | c.chunk=LIB3DS_DL_SHADOWED; |
|---|
| 360 | c.size=6; |
|---|
| 361 | lib3ds_chunk_write(&c, strm); |
|---|
| 362 | } |
|---|
| 363 | if ((fabs(light->shadow_bias)>LIB3DS_EPSILON) || |
|---|
| 364 | (fabs(light->shadow_filter)>LIB3DS_EPSILON) || |
|---|
| 365 | (light->shadow_size!=0)) { |
|---|
| 366 | Lib3dsChunk c; |
|---|
| 367 | c.chunk=LIB3DS_DL_LOCAL_SHADOW2; |
|---|
| 368 | c.size=16; |
|---|
| 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); |
|---|
| 373 | } |
|---|
| 374 | if (light->see_cone) { |
|---|
| 375 | Lib3dsChunk c; |
|---|
| 376 | c.chunk=LIB3DS_DL_SEE_CONE; |
|---|
| 377 | c.size=6; |
|---|
| 378 | lib3ds_chunk_write(&c, strm); |
|---|
| 379 | } |
|---|
| 380 | if (light->rectangular_spot) { |
|---|
| 381 | Lib3dsChunk c; |
|---|
| 382 | c.chunk=LIB3DS_DL_SPOT_RECTANGULAR; |
|---|
| 383 | c.size=6; |
|---|
| 384 | lib3ds_chunk_write(&c, strm); |
|---|
| 385 | } |
|---|
| 386 | if (fabs(light->spot_aspect)>LIB3DS_EPSILON) { |
|---|
| 387 | Lib3dsChunk c; |
|---|
| 388 | c.chunk=LIB3DS_DL_SPOT_ASPECT; |
|---|
| 389 | c.size=10; |
|---|
| 390 | lib3ds_chunk_write(&c, strm); |
|---|
| 391 | lib3ds_float_write(light->spot_aspect,strm); |
|---|
| 392 | } |
|---|
| 393 | if (light->use_projector) { |
|---|
| 394 | Lib3dsChunk c; |
|---|
| 395 | c.chunk=LIB3DS_DL_SPOT_PROJECTOR; |
|---|
| 396 | c.size=10; |
|---|
| 397 | lib3ds_chunk_write(&c, strm); |
|---|
| 398 | lib3ds_string_write(light->projector,strm); |
|---|
| 399 | } |
|---|
| 400 | if (light->spot_overshoot) { |
|---|
| 401 | Lib3dsChunk c; |
|---|
| 402 | c.chunk=LIB3DS_DL_SPOT_OVERSHOOT; |
|---|
| 403 | c.size=6; |
|---|
| 404 | lib3ds_chunk_write(&c, strm); |
|---|
| 405 | } |
|---|
| 406 | if (fabs(light->ray_bias)>LIB3DS_EPSILON) { |
|---|
| 407 | Lib3dsChunk c; |
|---|
| 408 | c.chunk=LIB3DS_DL_RAY_BIAS; |
|---|
| 409 | c.size=10; |
|---|
| 410 | lib3ds_chunk_write(&c, strm); |
|---|
| 411 | lib3ds_float_write(light->ray_bias,strm); |
|---|
| 412 | } |
|---|
| 413 | if (light->ray_shadows) { |
|---|
| 414 | Lib3dsChunk c; |
|---|
| 415 | c.chunk=LIB3DS_DL_RAYSHAD; |
|---|
| 416 | c.size=6; |
|---|
| 417 | lib3ds_chunk_write(&c, strm); |
|---|
| 418 | } |
|---|
| 419 | if (!lib3ds_chunk_write_end(&c,strm)) { |
|---|
| 420 | return(LIB3DS_FALSE); |
|---|
| 421 | } |
|---|
| 422 | } |
|---|
| 423 | if (!lib3ds_chunk_write_end(&c,strm)) { |
|---|
| 424 | return(LIB3DS_FALSE); |
|---|
| 425 | } |
|---|
| 426 | return(LIB3DS_TRUE); |
|---|
| 427 | } |
|---|
| 428 | |
|---|