| 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 "mesh.h" |
|---|
| 24 | #include "readwrite.h" |
|---|
| 25 | #include "chunk.h" |
|---|
| 26 | #include "vector.h" |
|---|
| 27 | #include "matrix.h" |
|---|
| 28 | #include <stdlib.h> |
|---|
| 29 | #include <string.h> |
|---|
| 30 | #include <math.h> |
|---|
| 31 | #include "config.h" |
|---|
| 32 | #ifdef WITH_DMALLOC |
|---|
| 33 | #include <dmalloc.h> |
|---|
| 34 | #endif |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | static Lib3dsBool |
|---|
| 45 | face_array_read(Lib3dsMesh *mesh, iostream *strm) |
|---|
| 46 | { |
|---|
| 47 | Lib3dsChunk c; |
|---|
| 48 | Lib3dsWord chunk; |
|---|
| 49 | int i; |
|---|
| 50 | int faces; |
|---|
| 51 | |
|---|
| 52 | if (!lib3ds_chunk_read_start(&c, LIB3DS_FACE_ARRAY, strm)) { |
|---|
| 53 | return(LIB3DS_FALSE); |
|---|
| 54 | } |
|---|
| 55 | lib3ds_mesh_free_face_list(mesh); |
|---|
| 56 | |
|---|
| 57 | faces=lib3ds_word_read(strm); |
|---|
| 58 | if (faces) { |
|---|
| 59 | if (!lib3ds_mesh_new_face_list(mesh, faces)) { |
|---|
| 60 | LIB3DS_ERROR_LOG; |
|---|
| 61 | return(LIB3DS_FALSE); |
|---|
| 62 | } |
|---|
| 63 | for (i=0; i<faces; ++i) { |
|---|
| 64 | strcpy(mesh->faceL[i].material, ""); |
|---|
| 65 | mesh->faceL[i].points[0]=lib3ds_word_read(strm); |
|---|
| 66 | mesh->faceL[i].points[1]=lib3ds_word_read(strm); |
|---|
| 67 | mesh->faceL[i].points[2]=lib3ds_word_read(strm); |
|---|
| 68 | mesh->faceL[i].flags=lib3ds_word_read(strm); |
|---|
| 69 | } |
|---|
| 70 | lib3ds_chunk_read_tell(&c, strm); |
|---|
| 71 | |
|---|
| 72 | while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { |
|---|
| 73 | switch (chunk) { |
|---|
| 74 | case LIB3DS_SMOOTH_GROUP: |
|---|
| 75 | { |
|---|
| 76 | unsigned i; |
|---|
| 77 | |
|---|
| 78 | for (i=0; i<mesh->faces; ++i) { |
|---|
| 79 | mesh->faceL[i].smoothing=lib3ds_dword_read(strm); |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | break; |
|---|
| 83 | case LIB3DS_MSH_MAT_GROUP: |
|---|
| 84 | { |
|---|
| 85 | char name[64]; |
|---|
| 86 | unsigned faces; |
|---|
| 87 | unsigned i; |
|---|
| 88 | unsigned index; |
|---|
| 89 | |
|---|
| 90 | if (!lib3ds_string_read(name, 64, strm)) { |
|---|
| 91 | return(LIB3DS_FALSE); |
|---|
| 92 | } |
|---|
| 93 | faces=lib3ds_word_read(strm); |
|---|
| 94 | for (i=0; i<faces; ++i) { |
|---|
| 95 | index=lib3ds_word_read(strm); |
|---|
| 96 | ASSERT(index<mesh->faces); |
|---|
| 97 | strcpy(mesh->faceL[index].material, name); |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | break; |
|---|
| 101 | case LIB3DS_MSH_BOXMAP: |
|---|
| 102 | { |
|---|
| 103 | char name[64]; |
|---|
| 104 | |
|---|
| 105 | if (!lib3ds_string_read(name, 64, strm)) { |
|---|
| 106 | return(LIB3DS_FALSE); |
|---|
| 107 | } |
|---|
| 108 | strcpy(mesh->box_map.front, name); |
|---|
| 109 | if (!lib3ds_string_read(name, 64, strm)) { |
|---|
| 110 | return(LIB3DS_FALSE); |
|---|
| 111 | } |
|---|
| 112 | strcpy(mesh->box_map.back, name); |
|---|
| 113 | if (!lib3ds_string_read(name, 64, strm)) { |
|---|
| 114 | return(LIB3DS_FALSE); |
|---|
| 115 | } |
|---|
| 116 | strcpy(mesh->box_map.left, name); |
|---|
| 117 | if (!lib3ds_string_read(name, 64, strm)) { |
|---|
| 118 | return(LIB3DS_FALSE); |
|---|
| 119 | } |
|---|
| 120 | strcpy(mesh->box_map.right, name); |
|---|
| 121 | if (!lib3ds_string_read(name, 64, strm)) { |
|---|
| 122 | return(LIB3DS_FALSE); |
|---|
| 123 | } |
|---|
| 124 | strcpy(mesh->box_map.top, name); |
|---|
| 125 | if (!lib3ds_string_read(name, 64, strm)) { |
|---|
| 126 | return(LIB3DS_FALSE); |
|---|
| 127 | } |
|---|
| 128 | strcpy(mesh->box_map.bottom, name); |
|---|
| 129 | } |
|---|
| 130 | break; |
|---|
| 131 | default: |
|---|
| 132 | lib3ds_chunk_unknown(chunk); |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | } |
|---|
| 137 | lib3ds_chunk_read_end(&c, strm); |
|---|
| 138 | return(LIB3DS_TRUE); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | Lib3dsMesh* |
|---|
| 146 | lib3ds_mesh_new(const char *name) |
|---|
| 147 | { |
|---|
| 148 | Lib3dsMesh *mesh; |
|---|
| 149 | |
|---|
| 150 | ASSERT(name); |
|---|
| 151 | ASSERT(strlen(name)<64); |
|---|
| 152 | |
|---|
| 153 | mesh=(Lib3dsMesh*)calloc(sizeof(Lib3dsMesh), 1); |
|---|
| 154 | if (!mesh) { |
|---|
| 155 | return(0); |
|---|
| 156 | } |
|---|
| 157 | strcpy(mesh->name, name); |
|---|
| 158 | lib3ds_matrix_identity(mesh->matrix); |
|---|
| 159 | mesh->map_data.maptype=LIB3DS_MAP_NONE; |
|---|
| 160 | return(mesh); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | void |
|---|
| 168 | lib3ds_mesh_free(Lib3dsMesh *mesh) |
|---|
| 169 | { |
|---|
| 170 | lib3ds_mesh_free_point_list(mesh); |
|---|
| 171 | lib3ds_mesh_free_flag_list(mesh); |
|---|
| 172 | lib3ds_mesh_free_texel_list(mesh); |
|---|
| 173 | lib3ds_mesh_free_face_list(mesh); |
|---|
| 174 | memset(mesh, 0, sizeof(Lib3dsMesh)); |
|---|
| 175 | free(mesh); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | Lib3dsBool |
|---|
| 183 | lib3ds_mesh_new_point_list(Lib3dsMesh *mesh, Lib3dsDword points) |
|---|
| 184 | { |
|---|
| 185 | ASSERT(mesh); |
|---|
| 186 | if (mesh->pointL) { |
|---|
| 187 | ASSERT(mesh->points); |
|---|
| 188 | lib3ds_mesh_free_point_list(mesh); |
|---|
| 189 | } |
|---|
| 190 | ASSERT(!mesh->pointL && !mesh->points); |
|---|
| 191 | mesh->points=0; |
|---|
| 192 | mesh->pointL=(Lib3dsPoint*)calloc(sizeof(Lib3dsPoint)*points,1); |
|---|
| 193 | if (!mesh->pointL) { |
|---|
| 194 | LIB3DS_ERROR_LOG; |
|---|
| 195 | return(LIB3DS_FALSE); |
|---|
| 196 | } |
|---|
| 197 | mesh->points=points; |
|---|
| 198 | return(LIB3DS_TRUE); |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | void |
|---|
| 206 | lib3ds_mesh_free_point_list(Lib3dsMesh *mesh) |
|---|
| 207 | { |
|---|
| 208 | ASSERT(mesh); |
|---|
| 209 | if (mesh->pointL) { |
|---|
| 210 | ASSERT(mesh->points); |
|---|
| 211 | free(mesh->pointL); |
|---|
| 212 | mesh->pointL=0; |
|---|
| 213 | mesh->points=0; |
|---|
| 214 | } |
|---|
| 215 | else { |
|---|
| 216 | ASSERT(!mesh->points); |
|---|
| 217 | } |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | Lib3dsBool |
|---|
| 225 | lib3ds_mesh_new_flag_list(Lib3dsMesh *mesh, Lib3dsDword flags) |
|---|
| 226 | { |
|---|
| 227 | ASSERT(mesh); |
|---|
| 228 | if (mesh->flagL) { |
|---|
| 229 | ASSERT(mesh->flags); |
|---|
| 230 | lib3ds_mesh_free_flag_list(mesh); |
|---|
| 231 | } |
|---|
| 232 | ASSERT(!mesh->flagL && !mesh->flags); |
|---|
| 233 | mesh->flags=0; |
|---|
| 234 | mesh->flagL=(Lib3dsWord*)calloc(sizeof(Lib3dsWord)*flags,1); |
|---|
| 235 | if (!mesh->flagL) { |
|---|
| 236 | LIB3DS_ERROR_LOG; |
|---|
| 237 | return(LIB3DS_FALSE); |
|---|
| 238 | } |
|---|
| 239 | mesh->flags=flags; |
|---|
| 240 | return(LIB3DS_TRUE); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | void |
|---|
| 248 | lib3ds_mesh_free_flag_list(Lib3dsMesh *mesh) |
|---|
| 249 | { |
|---|
| 250 | ASSERT(mesh); |
|---|
| 251 | if (mesh->flagL) { |
|---|
| 252 | ASSERT(mesh->flags); |
|---|
| 253 | free(mesh->flagL); |
|---|
| 254 | mesh->flagL=0; |
|---|
| 255 | mesh->flags=0; |
|---|
| 256 | } |
|---|
| 257 | else { |
|---|
| 258 | ASSERT(!mesh->flags); |
|---|
| 259 | } |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | Lib3dsBool |
|---|
| 267 | lib3ds_mesh_new_texel_list(Lib3dsMesh *mesh, Lib3dsDword texels) |
|---|
| 268 | { |
|---|
| 269 | ASSERT(mesh); |
|---|
| 270 | if (mesh->texelL) { |
|---|
| 271 | ASSERT(mesh->texels); |
|---|
| 272 | lib3ds_mesh_free_texel_list(mesh); |
|---|
| 273 | } |
|---|
| 274 | ASSERT(!mesh->texelL && !mesh->texels); |
|---|
| 275 | mesh->texels=0; |
|---|
| 276 | mesh->texelL=(Lib3dsTexel*) calloc(sizeof(Lib3dsTexel)*texels,1); |
|---|
| 277 | if (!mesh->texelL) { |
|---|
| 278 | LIB3DS_ERROR_LOG; |
|---|
| 279 | return(LIB3DS_FALSE); |
|---|
| 280 | } |
|---|
| 281 | mesh->texels=texels; |
|---|
| 282 | return(LIB3DS_TRUE); |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | void |
|---|
| 290 | lib3ds_mesh_free_texel_list(Lib3dsMesh *mesh) |
|---|
| 291 | { |
|---|
| 292 | ASSERT(mesh); |
|---|
| 293 | if (mesh->texelL) { |
|---|
| 294 | ASSERT(mesh->texels); |
|---|
| 295 | free(mesh->texelL); |
|---|
| 296 | mesh->texelL=0; |
|---|
| 297 | mesh->texels=0; |
|---|
| 298 | } |
|---|
| 299 | else { |
|---|
| 300 | ASSERT(!mesh->texels); |
|---|
| 301 | } |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | Lib3dsBool |
|---|
| 309 | lib3ds_mesh_new_face_list(Lib3dsMesh *mesh, Lib3dsDword faces) |
|---|
| 310 | { |
|---|
| 311 | ASSERT(mesh); |
|---|
| 312 | if (mesh->faceL) { |
|---|
| 313 | ASSERT(mesh->faces); |
|---|
| 314 | lib3ds_mesh_free_face_list(mesh); |
|---|
| 315 | } |
|---|
| 316 | ASSERT(!mesh->faceL && !mesh->faces); |
|---|
| 317 | mesh->faces=0; |
|---|
| 318 | mesh->faceL=(Lib3dsFace*)calloc(sizeof(Lib3dsFace)*faces,1); |
|---|
| 319 | if (!mesh->faceL) { |
|---|
| 320 | LIB3DS_ERROR_LOG; |
|---|
| 321 | return(LIB3DS_FALSE); |
|---|
| 322 | } |
|---|
| 323 | mesh->faces=faces; |
|---|
| 324 | return(LIB3DS_TRUE); |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | void |
|---|
| 332 | lib3ds_mesh_free_face_list(Lib3dsMesh *mesh) |
|---|
| 333 | { |
|---|
| 334 | ASSERT(mesh); |
|---|
| 335 | if (mesh->faceL) { |
|---|
| 336 | ASSERT(mesh->faces); |
|---|
| 337 | free(mesh->faceL); |
|---|
| 338 | mesh->faceL=0; |
|---|
| 339 | mesh->faces=0; |
|---|
| 340 | } |
|---|
| 341 | else { |
|---|
| 342 | ASSERT(!mesh->faces); |
|---|
| 343 | } |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | typedef struct _Lib3dsFaces Lib3dsFaces; |
|---|
| 348 | |
|---|
| 349 | struct _Lib3dsFaces { |
|---|
| 350 | Lib3dsFaces *next; |
|---|
| 351 | Lib3dsFace *face; |
|---|
| 352 | }; |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | |
|---|
| 360 | void |
|---|
| 361 | lib3ds_mesh_bounding_box(Lib3dsMesh *mesh, Lib3dsVector min, Lib3dsVector max) |
|---|
| 362 | { |
|---|
| 363 | unsigned i,j; |
|---|
| 364 | Lib3dsFloat v; |
|---|
| 365 | |
|---|
| 366 | if (!mesh->points) { |
|---|
| 367 | lib3ds_vector_zero(min); |
|---|
| 368 | lib3ds_vector_zero(max); |
|---|
| 369 | return; |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | lib3ds_vector_copy(min, mesh->pointL[0].pos); |
|---|
| 373 | lib3ds_vector_copy(max, mesh->pointL[0].pos); |
|---|
| 374 | for (i=1; i<mesh->points; ++i) { |
|---|
| 375 | for (j=0; j<3; ++j) { |
|---|
| 376 | v=mesh->pointL[i].pos[j]; |
|---|
| 377 | if (v<min[j]) { |
|---|
| 378 | min[j]=v; |
|---|
| 379 | } |
|---|
| 380 | if (v>max[j]) { |
|---|
| 381 | max[j]=v; |
|---|
| 382 | } |
|---|
| 383 | } |
|---|
| 384 | }; |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | |
|---|
| 389 | |
|---|
| 390 | |
|---|
| 391 | |
|---|
| 392 | |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | |
|---|
| 396 | |
|---|
| 397 | |
|---|
| 398 | |
|---|
| 399 | |
|---|
| 400 | |
|---|
| 401 | |
|---|
| 402 | |
|---|
| 403 | |
|---|
| 404 | |
|---|
| 405 | |
|---|
| 406 | |
|---|
| 407 | |
|---|
| 408 | void |
|---|
| 409 | lib3ds_mesh_calculate_normals(Lib3dsMesh *mesh, Lib3dsVector *normalL) |
|---|
| 410 | { |
|---|
| 411 | Lib3dsFaces **fl; |
|---|
| 412 | Lib3dsFaces *fa; |
|---|
| 413 | unsigned i,j,k; |
|---|
| 414 | |
|---|
| 415 | if (!mesh->faces) { |
|---|
| 416 | return; |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | fl=(Lib3dsFaces**)calloc(sizeof(Lib3dsFaces*),mesh->points); |
|---|
| 420 | ASSERT(fl); |
|---|
| 421 | fa=(Lib3dsFaces*)calloc(sizeof(Lib3dsFaces),3*mesh->faces); |
|---|
| 422 | ASSERT(fa); |
|---|
| 423 | k=0; |
|---|
| 424 | for (i=0; i<mesh->faces; ++i) { |
|---|
| 425 | Lib3dsFace *f=&mesh->faceL[i]; |
|---|
| 426 | for (j=0; j<3; ++j) { |
|---|
| 427 | Lib3dsFaces* l=&fa[k++]; |
|---|
| 428 | ASSERT(f->points[j]<mesh->points); |
|---|
| 429 | l->face=f; |
|---|
| 430 | l->next=fl[f->points[j]]; |
|---|
| 431 | fl[f->points[j]]=l; |
|---|
| 432 | } |
|---|
| 433 | } |
|---|
| 434 | |
|---|
| 435 | for (i=0; i<mesh->faces; ++i) { |
|---|
| 436 | Lib3dsFace *f=&mesh->faceL[i]; |
|---|
| 437 | for (j=0; j<3; ++j) { |
|---|
| 438 | Lib3dsVector n,N[32]; |
|---|
| 439 | Lib3dsFaces *p; |
|---|
| 440 | int k,l; |
|---|
| 441 | int found; |
|---|
| 442 | |
|---|
| 443 | ASSERT(f->points[j]<mesh->points); |
|---|
| 444 | |
|---|
| 445 | if (f->smoothing) { |
|---|
| 446 | lib3ds_vector_zero(n); |
|---|
| 447 | k=0; |
|---|
| 448 | for (p=fl[f->points[j]]; p; p=p->next) { |
|---|
| 449 | found=0; |
|---|
| 450 | for (l=0; l<k; ++l) { |
|---|
| 451 | if (fabs(lib3ds_vector_dot(N[l], p->face->normal)-1.0)<1e-5) { |
|---|
| 452 | found=1; |
|---|
| 453 | break; |
|---|
| 454 | } |
|---|
| 455 | } |
|---|
| 456 | if (!found) { |
|---|
| 457 | if (f->smoothing & p->face->smoothing) { |
|---|
| 458 | lib3ds_vector_add(n,n, p->face->normal); |
|---|
| 459 | lib3ds_vector_copy(N[k], p->face->normal); |
|---|
| 460 | ++k; |
|---|
| 461 | } |
|---|
| 462 | } |
|---|
| 463 | } |
|---|
| 464 | } |
|---|
| 465 | else { |
|---|
| 466 | lib3ds_vector_copy(n, f->normal); |
|---|
| 467 | } |
|---|
| 468 | lib3ds_vector_normalize(n); |
|---|
| 469 | |
|---|
| 470 | lib3ds_vector_copy(normalL[3*i+j], n); |
|---|
| 471 | } |
|---|
| 472 | } |
|---|
| 473 | |
|---|
| 474 | free(fa); |
|---|
| 475 | free(fl); |
|---|
| 476 | } |
|---|
| 477 | |
|---|
| 478 | |
|---|
| 479 | |
|---|
| 480 | |
|---|
| 481 | |
|---|
| 482 | |
|---|
| 483 | |
|---|
| 484 | |
|---|
| 485 | |
|---|
| 486 | |
|---|
| 487 | |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | |
|---|
| 491 | void |
|---|
| 492 | lib3ds_mesh_dump(Lib3dsMesh *mesh) |
|---|
| 493 | { |
|---|
| 494 | unsigned i; |
|---|
| 495 | Lib3dsVector p; |
|---|
| 496 | |
|---|
| 497 | ASSERT(mesh); |
|---|
| 498 | printf(" %s vertices=%d faces=%d\n", |
|---|
| 499 | mesh->name, |
|---|
| 500 | mesh->points, |
|---|
| 501 | mesh->faces |
|---|
| 502 | ); |
|---|
| 503 | printf(" matrix:\n"); |
|---|
| 504 | lib3ds_matrix_dump(mesh->matrix); |
|---|
| 505 | printf(" point list:\n"); |
|---|
| 506 | for (i=0; i<mesh->points; ++i) { |
|---|
| 507 | lib3ds_vector_copy(p, mesh->pointL[i].pos); |
|---|
| 508 | printf (" %8f %8f %8f\n", p[0], p[1], p[2]); |
|---|
| 509 | } |
|---|
| 510 | printf(" facelist:\n"); |
|---|
| 511 | for (i=0; i<mesh->points; ++i) { |
|---|
| 512 | printf (" %4d %4d %4d smoothing:%X\n", |
|---|
| 513 | mesh->faceL[i].points[0], |
|---|
| 514 | mesh->faceL[i].points[1], |
|---|
| 515 | mesh->faceL[i].points[2], |
|---|
| 516 | static_cast<int>(mesh->faceL[i].smoothing) |
|---|
| 517 | ); |
|---|
| 518 | } |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | |
|---|
| 523 | |
|---|
| 524 | |
|---|
| 525 | Lib3dsBool |
|---|
| 526 | lib3ds_mesh_read(Lib3dsMesh *mesh, iostream *strm) |
|---|
| 527 | { |
|---|
| 528 | Lib3dsChunk c; |
|---|
| 529 | Lib3dsWord chunk; |
|---|
| 530 | |
|---|
| 531 | if (!lib3ds_chunk_read_start(&c, LIB3DS_N_TRI_OBJECT, strm)) { |
|---|
| 532 | return(LIB3DS_FALSE); |
|---|
| 533 | } |
|---|
| 534 | |
|---|
| 535 | while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { |
|---|
| 536 | switch (chunk) { |
|---|
| 537 | case LIB3DS_MESH_MATRIX: |
|---|
| 538 | { |
|---|
| 539 | int i,j; |
|---|
| 540 | |
|---|
| 541 | lib3ds_matrix_identity(mesh->matrix); |
|---|
| 542 | for (i=0; i<4; i++) { |
|---|
| 543 | for (j=0; j<3; j++) { |
|---|
| 544 | mesh->matrix[i][j]=lib3ds_float_read(strm); |
|---|
| 545 | } |
|---|
| 546 | } |
|---|
| 547 | } |
|---|
| 548 | break; |
|---|
| 549 | case LIB3DS_MESH_COLOR: |
|---|
| 550 | { |
|---|
| 551 | mesh->color=lib3ds_byte_read(strm); |
|---|
| 552 | } |
|---|
| 553 | break; |
|---|
| 554 | case LIB3DS_POINT_ARRAY: |
|---|
| 555 | { |
|---|
| 556 | unsigned i,j; |
|---|
| 557 | unsigned points; |
|---|
| 558 | |
|---|
| 559 | lib3ds_mesh_free_point_list(mesh); |
|---|
| 560 | points=lib3ds_word_read(strm); |
|---|
| 561 | if (points) { |
|---|
| 562 | if (!lib3ds_mesh_new_point_list(mesh, points)) { |
|---|
| 563 | LIB3DS_ERROR_LOG; |
|---|
| 564 | return(LIB3DS_FALSE); |
|---|
| 565 | } |
|---|
| 566 | for (i=0; i<mesh->points; ++i) { |
|---|
| 567 | for (j=0; j<3; ++j) { |
|---|
| 568 | mesh->pointL[i].pos[j]=lib3ds_float_read(strm); |
|---|
| 569 | } |
|---|
| 570 | } |
|---|
| 571 | ASSERT((!mesh->flags) || (mesh->points==mesh->flags)); |
|---|
| 572 | ASSERT((!mesh->texels) || (mesh->points==mesh->texels)); |
|---|
| 573 | } |
|---|
| 574 | } |
|---|
| 575 | break; |
|---|
| 576 | case LIB3DS_POINT_FLAG_ARRAY: |
|---|
| 577 | { |
|---|
| 578 | unsigned i; |
|---|
| 579 | unsigned flags; |
|---|
| 580 | |
|---|
| 581 | lib3ds_mesh_free_flag_list(mesh); |
|---|
| 582 | flags=lib3ds_word_read(strm); |
|---|
| 583 | if (flags) { |
|---|
| 584 | if (!lib3ds_mesh_new_flag_list(mesh, flags)) { |
|---|
| 585 | LIB3DS_ERROR_LOG; |
|---|
| 586 | return(LIB3DS_FALSE); |
|---|
| 587 | } |
|---|
| 588 | for (i=0; i<mesh->flags; ++i) { |
|---|
| 589 | mesh->flagL[i]=lib3ds_word_read(strm); |
|---|
| 590 | } |
|---|
| 591 | ASSERT((!mesh->points) || (mesh->flags==mesh->points)); |
|---|
| 592 | ASSERT((!mesh->texels) || (mesh->flags==mesh->texels)); |
|---|
| 593 | } |
|---|
| 594 | } |
|---|
| 595 | break; |
|---|
| 596 | case LIB3DS_FACE_ARRAY: |
|---|
| 597 | { |
|---|
| 598 | lib3ds_chunk_read_reset(&c, strm); |
|---|
| 599 | if (!face_array_read(mesh, strm)) { |
|---|
| 600 | return(LIB3DS_FALSE); |
|---|
| 601 | } |
|---|
| 602 | } |
|---|
| 603 | break; |
|---|
| 604 | case LIB3DS_MESH_TEXTURE_INFO: |
|---|
| 605 | { |
|---|
| 606 | int i,j; |
|---|
| 607 | |
|---|
| 608 | for (i=0; i<2; ++i) { |
|---|
| 609 | mesh->map_data.tile[i]=lib3ds_float_read(strm); |
|---|
| 610 | } |
|---|
| 611 | for (i=0; i<3; ++i) { |
|---|
| 612 | mesh->map_data.pos[i]=lib3ds_float_read(strm); |
|---|
| 613 | } |
|---|
| 614 | mesh->map_data.scale=lib3ds_float_read(strm); |
|---|
| 615 | |
|---|
| 616 | lib3ds_matrix_identity(mesh->map_data.matrix); |
|---|
| 617 | for (i=0; i<4; i++) { |
|---|
| 618 | for (j=0; j<3; j++) { |
|---|
| 619 | mesh->map_data.matrix[i][j]=lib3ds_float_read(strm); |
|---|
| 620 | } |
|---|
| 621 | } |
|---|
| 622 | for (i=0; i<2; ++i) { |
|---|
| 623 | mesh->map_data.planar_size[i]=lib3ds_float_read(strm); |
|---|
| 624 | } |
|---|
| 625 | mesh->map_data.cylinder_height=lib3ds_float_read(strm); |
|---|
| 626 | } |
|---|
| 627 | break; |
|---|
| 628 | case LIB3DS_TEX_VERTS: |
|---|
| 629 | { |
|---|
| 630 | unsigned i; |
|---|
| 631 | unsigned texels; |
|---|
| 632 | |
|---|
| 633 | lib3ds_mesh_free_texel_list(mesh); |
|---|
| 634 | texels=lib3ds_word_read(strm); |
|---|
| 635 | if (texels) { |
|---|
| 636 | if (!lib3ds_mesh_new_texel_list(mesh, texels)) { |
|---|
| 637 | LIB3DS_ERROR_LOG; |
|---|
| 638 | return(LIB3DS_FALSE); |
|---|
| 639 | } |
|---|
| 640 | for (i=0; i<mesh->texels; ++i) { |
|---|
| 641 | mesh->texelL[i][0]=lib3ds_float_read(strm); |
|---|
| 642 | mesh->texelL[i][1]=lib3ds_float_read(strm); |
|---|
| 643 | } |
|---|
| 644 | ASSERT((!mesh->points) || (mesh->texels==mesh->points)); |
|---|
| 645 | ASSERT((!mesh->flags) || (mesh->texels==mesh->flags)); |
|---|
| 646 | } |
|---|
| 647 | } |
|---|
| 648 | break; |
|---|
| 649 | default: |
|---|
| 650 | lib3ds_chunk_unknown(chunk); |
|---|
| 651 | } |
|---|
| 652 | } |
|---|
| 653 | { |
|---|
| 654 | unsigned j; |
|---|
| 655 | |
|---|
| 656 | for (j=0; j<mesh->faces; ++j) { |
|---|
| 657 | ASSERT(mesh->faceL[j].points[0]<mesh->points); |
|---|
| 658 | ASSERT(mesh->faceL[j].points[1]<mesh->points); |
|---|
| 659 | ASSERT(mesh->faceL[j].points[2]<mesh->points); |
|---|
| 660 | lib3ds_vector_normal( |
|---|
| 661 | mesh->faceL[j].normal, |
|---|
| 662 | mesh->pointL[mesh->faceL[j].points[0]].pos, |
|---|
| 663 | mesh->pointL[mesh->faceL[j].points[1]].pos, |
|---|
| 664 | mesh->pointL[mesh->faceL[j].points[2]].pos |
|---|
| 665 | ); |
|---|
| 666 | } |
|---|
| 667 | } |
|---|
| 668 | |
|---|
| 669 | lib3ds_chunk_read_end(&c, strm); |
|---|
| 670 | return(LIB3DS_TRUE); |
|---|
| 671 | } |
|---|
| 672 | |
|---|
| 673 | |
|---|
| 674 | static Lib3dsBool |
|---|
| 675 | point_array_write(Lib3dsMesh *mesh, iostream *strm) |
|---|
| 676 | { |
|---|
| 677 | Lib3dsChunk c; |
|---|
| 678 | unsigned i; |
|---|
| 679 | |
|---|
| 680 | if (!mesh->points || !mesh->pointL) { |
|---|
| 681 | return(LIB3DS_TRUE); |
|---|
| 682 | } |
|---|
| 683 | ASSERT(mesh->points<0x10000); |
|---|
| 684 | c.chunk=LIB3DS_POINT_ARRAY; |
|---|
| 685 | c.size=8+12*mesh->points; |
|---|
| 686 | lib3ds_chunk_write(&c, strm); |
|---|
| 687 | |
|---|
| 688 | lib3ds_word_write((Lib3dsWord)mesh->points, strm); |
|---|
| 689 | for (i=0; i<mesh->points; ++i) { |
|---|
| 690 | lib3ds_vector_write(mesh->pointL[i].pos, strm); |
|---|
| 691 | } |
|---|
| 692 | return(LIB3DS_TRUE); |
|---|
| 693 | } |
|---|
| 694 | |
|---|
| 695 | |
|---|
| 696 | static Lib3dsBool |
|---|
| 697 | flag_array_write(Lib3dsMesh *mesh, iostream *strm) |
|---|
| 698 | { |
|---|
| 699 | Lib3dsChunk c; |
|---|
| 700 | unsigned i; |
|---|
| 701 | |
|---|
| 702 | if (!mesh->flags || !mesh->flagL) { |
|---|
| 703 | return(LIB3DS_TRUE); |
|---|
| 704 | } |
|---|
| 705 | ASSERT(mesh->flags<0x10000); |
|---|
| 706 | c.chunk=LIB3DS_POINT_FLAG_ARRAY; |
|---|
| 707 | c.size=8+2*mesh->flags; |
|---|
| 708 | lib3ds_chunk_write(&c, strm); |
|---|
| 709 | |
|---|
| 710 | lib3ds_word_write((Lib3dsWord)mesh->flags, strm); |
|---|
| 711 | for (i=0; i<mesh->flags; ++i) { |
|---|
| 712 | lib3ds_word_write(mesh->flagL[i], strm); |
|---|
| 713 | } |
|---|
| 714 | return(LIB3DS_TRUE); |
|---|
| 715 | } |
|---|
| 716 | |
|---|
| 717 | |
|---|
| 718 | static Lib3dsBool |
|---|
| 719 | face_array_write(Lib3dsMesh *mesh, iostream *strm) |
|---|
| 720 | { |
|---|
| 721 | Lib3dsChunk c; |
|---|
| 722 | |
|---|
| 723 | if (!mesh->faces || !mesh->faceL) { |
|---|
| 724 | return(LIB3DS_TRUE); |
|---|
| 725 | } |
|---|
| 726 | ASSERT(mesh->faces<0x10000); |
|---|
| 727 | c.chunk=LIB3DS_FACE_ARRAY; |
|---|
| 728 | if (!lib3ds_chunk_write_start(&c, strm)) { |
|---|
| 729 | return(LIB3DS_FALSE); |
|---|
| 730 | } |
|---|
| 731 | { |
|---|
| 732 | unsigned i; |
|---|
| 733 | |
|---|
| 734 | lib3ds_word_write((Lib3dsWord)mesh->faces, strm); |
|---|
| 735 | for (i=0; i<mesh->faces; ++i) { |
|---|
| 736 | lib3ds_word_write(mesh->faceL[i].points[0], strm); |
|---|
| 737 | lib3ds_word_write(mesh->faceL[i].points[1], strm); |
|---|
| 738 | lib3ds_word_write(mesh->faceL[i].points[2], strm); |
|---|
| 739 | lib3ds_word_write(mesh->faceL[i].flags, strm); |
|---|
| 740 | } |
|---|
| 741 | } |
|---|
| 742 | |
|---|
| 743 | { |
|---|
| 744 | Lib3dsChunk c; |
|---|
| 745 | unsigned i,j; |
|---|
| 746 | Lib3dsWord num; |
|---|
| 747 | char *matf=(char*)calloc(sizeof(char), mesh->faces); |
|---|
| 748 | if (!matf) { |
|---|
| 749 | return(LIB3DS_FALSE); |
|---|
| 750 | } |
|---|
| 751 | |
|---|
| 752 | for (i=0; i<mesh->faces; ++i) { |
|---|
| 753 | if (!matf[i] && strlen(mesh->faceL[i].material)) { |
|---|
| 754 | matf[i]=1; |
|---|
| 755 | num=1; |
|---|
| 756 | |
|---|
| 757 | for (j=i+1; j<mesh->faces; ++j) { |
|---|
| 758 | if (strcmp(mesh->faceL[i].material, mesh->faceL[j].material)==0) ++num; |
|---|
| 759 | } |
|---|
| 760 | |
|---|
| 761 | c.chunk=LIB3DS_MSH_MAT_GROUP; |
|---|
| 762 | c.size=6+ strlen(mesh->faceL[i].material)+1 +2+2*num; |
|---|
| 763 | lib3ds_chunk_write(&c, strm); |
|---|
| 764 | lib3ds_string_write(mesh->faceL[i].material, strm); |
|---|
| 765 | lib3ds_word_write(num, strm); |
|---|
| 766 | lib3ds_word_write((Lib3dsWord)i, strm); |
|---|
| 767 | |
|---|
| 768 | for (j=i+1; j<mesh->faces; ++j) { |
|---|
| 769 | if (strcmp(mesh->faceL[i].material, mesh->faceL[j].material)==0) { |
|---|
| 770 | lib3ds_word_write((Lib3dsWord)j, strm); |
|---|
| 771 | matf[j]=1; |
|---|
| 772 | } |
|---|
| 773 | } |
|---|
| 774 | } |
|---|
| 775 | } |
|---|
| 776 | free(matf); |
|---|
| 777 | } |
|---|
| 778 | |
|---|
| 779 | { |
|---|
| 780 | Lib3dsChunk c; |
|---|
| 781 | unsigned i; |
|---|
| 782 | |
|---|
| 783 | c.chunk=LIB3DS_SMOOTH_GROUP; |
|---|
| 784 | c.size=6+4*mesh->faces; |
|---|
| 785 | lib3ds_chunk_write(&c, strm); |
|---|
| 786 | |
|---|
| 787 | for (i=0; i<mesh->faces; ++i) { |
|---|
| 788 | lib3ds_dword_write(mesh->faceL[i].smoothing, strm); |
|---|
| 789 | } |
|---|
| 790 | } |
|---|
| 791 | |
|---|
| 792 | { |
|---|
| 793 | Lib3dsChunk c; |
|---|
| 794 | |
|---|
| 795 | if (strlen(mesh->box_map.front) || |
|---|
| 796 | strlen(mesh->box_map.back) || |
|---|
| 797 | strlen(mesh->box_map.left) || |
|---|
| 798 | strlen(mesh->box_map.right) || |
|---|
| 799 | strlen(mesh->box_map.top) || |
|---|
| 800 | strlen(mesh->box_map.bottom)) { |
|---|
| 801 | |
|---|
| 802 | c.chunk=LIB3DS_MSH_BOXMAP; |
|---|
| 803 | if (!lib3ds_chunk_write_start(&c, strm)) { |
|---|
| 804 | return(LIB3DS_FALSE); |
|---|
| 805 | } |
|---|
| 806 | |
|---|
| 807 | lib3ds_string_write(mesh->box_map.front, strm); |
|---|
| 808 | lib3ds_string_write(mesh->box_map.back, strm); |
|---|
| 809 | lib3ds_string_write(mesh->box_map.left, strm); |
|---|
| 810 | lib3ds_string_write(mesh->box_map.right, strm); |
|---|
| 811 | lib3ds_string_write(mesh->box_map.top, strm); |
|---|
| 812 | lib3ds_string_write(mesh->box_map.bottom, strm); |
|---|
| 813 | |
|---|
| 814 | if (!lib3ds_chunk_write_end(&c, strm)) { |
|---|
| 815 | return(LIB3DS_FALSE); |
|---|
| 816 | } |
|---|
| 817 | } |
|---|
| 818 | } |
|---|
| 819 | |
|---|
| 820 | if (!lib3ds_chunk_write_end(&c, strm)) { |
|---|
| 821 | return(LIB3DS_FALSE); |
|---|
| 822 | } |
|---|
| 823 | return(LIB3DS_TRUE); |
|---|
| 824 | } |
|---|
| 825 | |
|---|
| 826 | |
|---|
| 827 | static Lib3dsBool |
|---|
| 828 | texel_array_write(Lib3dsMesh *mesh, iostream *strm) |
|---|
| 829 | { |
|---|
| 830 | Lib3dsChunk c; |
|---|
| 831 | unsigned i; |
|---|
| 832 | |
|---|
| 833 | if (!mesh->texels || !mesh->texelL) { |
|---|
| 834 | return(LIB3DS_TRUE); |
|---|
| 835 | } |
|---|
| 836 | ASSERT(mesh->texels<0x10000); |
|---|
| 837 | c.chunk=LIB3DS_TEX_VERTS; |
|---|
| 838 | c.size=8+8*mesh->texels; |
|---|
| 839 | lib3ds_chunk_write(&c, strm); |
|---|
| 840 | |
|---|
| 841 | lib3ds_word_write((Lib3dsWord)mesh->texels, strm); |
|---|
| 842 | for (i=0; i<mesh->texels; ++i) { |
|---|
| 843 | lib3ds_float_write(mesh->texelL[i][0], strm); |
|---|
| 844 | lib3ds_float_write(mesh->texelL[i][1], strm); |
|---|
| 845 | } |
|---|
| 846 | return(LIB3DS_TRUE); |
|---|
| 847 | } |
|---|
| 848 | |
|---|
| 849 | |
|---|
| 850 | |
|---|
| 851 | |
|---|
| 852 | |
|---|
| 853 | Lib3dsBool |
|---|
| 854 | lib3ds_mesh_write(Lib3dsMesh *mesh, iostream *strm) |
|---|
| 855 | { |
|---|
| 856 | Lib3dsChunk c; |
|---|
| 857 | |
|---|
| 858 | c.chunk=LIB3DS_N_TRI_OBJECT; |
|---|
| 859 | if (!lib3ds_chunk_write_start(&c,strm)) { |
|---|
| 860 | return(LIB3DS_FALSE); |
|---|
| 861 | } |
|---|
| 862 | if (!point_array_write(mesh, strm)) { |
|---|
| 863 | return(LIB3DS_FALSE); |
|---|
| 864 | } |
|---|
| 865 | if (!texel_array_write(mesh, strm)) { |
|---|
| 866 | return(LIB3DS_FALSE); |
|---|
| 867 | } |
|---|
| 868 | |
|---|
| 869 | if (mesh->map_data.maptype!=LIB3DS_MAP_NONE) { |
|---|
| 870 | Lib3dsChunk c; |
|---|
| 871 | int i,j; |
|---|
| 872 | |
|---|
| 873 | c.chunk=LIB3DS_MESH_TEXTURE_INFO; |
|---|
| 874 | c.size=92; |
|---|
| 875 | if (!lib3ds_chunk_write(&c,strm)) { |
|---|
| 876 | return(LIB3DS_FALSE); |
|---|
| 877 | } |
|---|
| 878 | |
|---|
| 879 | lib3ds_word_write(mesh->map_data.maptype, strm); |
|---|
| 880 | |
|---|
| 881 | for (i=0; i<2; ++i) { |
|---|
| 882 | lib3ds_float_write(mesh->map_data.tile[i], strm); |
|---|
| 883 | } |
|---|
| 884 | for (i=0; i<3; ++i) { |
|---|
| 885 | lib3ds_float_write(mesh->map_data.pos[i], strm); |
|---|
| 886 | } |
|---|
| 887 | lib3ds_float_write(mesh->map_data.scale, strm); |
|---|
| 888 | |
|---|
| 889 | for (i=0; i<4; i++) { |
|---|
| 890 | for (j=0; j<3; j++) { |
|---|
| 891 | lib3ds_float_write(mesh->map_data.matrix[i][j], strm); |
|---|
| 892 | } |
|---|
| 893 | } |
|---|
| 894 | for (i=0; i<2; ++i) { |
|---|
| 895 | lib3ds_float_write(mesh->map_data.planar_size[i], strm); |
|---|
| 896 | } |
|---|
| 897 | lib3ds_float_write(mesh->map_data.cylinder_height, strm); |
|---|
| 898 | } |
|---|
| 899 | |
|---|
| 900 | if (!flag_array_write(mesh, strm)) { |
|---|
| 901 | return(LIB3DS_FALSE); |
|---|
| 902 | } |
|---|
| 903 | { |
|---|
| 904 | Lib3dsChunk c; |
|---|
| 905 | int i,j; |
|---|
| 906 | |
|---|
| 907 | c.chunk=LIB3DS_MESH_MATRIX; |
|---|
| 908 | c.size=54; |
|---|
| 909 | if (!lib3ds_chunk_write(&c,strm)) { |
|---|
| 910 | return(LIB3DS_FALSE); |
|---|
| 911 | } |
|---|
| 912 | for (i=0; i<4; i++) { |
|---|
| 913 | for (j=0; j<3; j++) { |
|---|
| 914 | lib3ds_float_write(mesh->matrix[i][j], strm); |
|---|
| 915 | } |
|---|
| 916 | } |
|---|
| 917 | } |
|---|
| 918 | |
|---|
| 919 | if (mesh->color) { |
|---|
| 920 | Lib3dsChunk c; |
|---|
| 921 | |
|---|
| 922 | c.chunk=LIB3DS_MESH_COLOR; |
|---|
| 923 | c.size=7; |
|---|
| 924 | if (!lib3ds_chunk_write(&c,strm)) { |
|---|
| 925 | return(LIB3DS_FALSE); |
|---|
| 926 | } |
|---|
| 927 | lib3ds_byte_write(mesh->color, strm); |
|---|
| 928 | } |
|---|
| 929 | if (!face_array_write(mesh, strm)) { |
|---|
| 930 | return(LIB3DS_FALSE); |
|---|
| 931 | } |
|---|
| 932 | |
|---|
| 933 | if (!lib3ds_chunk_write_end(&c,strm)) { |
|---|
| 934 | return(LIB3DS_FALSE); |
|---|
| 935 | } |
|---|
| 936 | return(LIB3DS_TRUE); |
|---|
| 937 | } |
|---|
| 938 | |
|---|
| 939 | |
|---|
| 940 | |
|---|
| 941 | |
|---|
| 942 | |
|---|
| 943 | |
|---|
| 944 | |
|---|
| 945 | |
|---|
| 946 | |
|---|
| 947 | |
|---|
| 948 | |
|---|
| 949 | |
|---|
| 950 | |
|---|
| 951 | |
|---|
| 952 | |
|---|
| 953 | |
|---|
| 954 | |
|---|
| 955 | |
|---|
| 956 | |
|---|
| 957 | |
|---|
| 958 | |
|---|
| 959 | |
|---|
| 960 | |
|---|
| 961 | |
|---|
| 962 | |
|---|
| 963 | |
|---|
| 964 | |
|---|
| 965 | |
|---|
| 966 | |
|---|
| 967 | |
|---|