1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 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 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916
|
/*
* The 3D Studio File Format Library
* Copyright (C) 1996-2001 by J.E. Hoffmann <je-h@gmx.net>
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id: file.c,v 1.23 2005/01/11 10:20:36 madmac Exp $
*/
#define LIB3DS_EXPORT
#include <lib3ds/file.h>
#include <lib3ds/chunk.h>
#include <lib3ds/io.h>
#include <lib3ds/material.h>
#include <lib3ds/mesh.h>
#include <lib3ds/camera.h>
#include <lib3ds/light.h>
#include <lib3ds/node.h>
#include <lib3ds/vector.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <config.h>
#ifdef WITH_DMALLOC
#include <dmalloc.h>
#endif
/*!
* \defgroup file Files
*
* \author J.E. Hoffmann <je-h@gmx.net>
*/
static Lib3dsBool
fileio_error_func(void *self)
{
FILE *f = (FILE*)self;
return(ferror(f)!=0);
}
static long
fileio_seek_func(void *self, long offset, Lib3dsIoSeek origin)
{
FILE *f = (FILE*)self;
int o;
switch (origin) {
case LIB3DS_SEEK_SET:
o = SEEK_SET;
break;
case LIB3DS_SEEK_CUR:
o = SEEK_CUR;
break;
case LIB3DS_SEEK_END:
o = SEEK_END;
break;
default:
ASSERT(0);
return(0);
}
return (fseek(f, offset, o));
}
static long
fileio_tell_func(void *self)
{
FILE *f = (FILE*)self;
return(ftell(f));
}
static int
fileio_read_func(void *self, Lib3dsByte *buffer, int size)
{
FILE *f = (FILE*)self;
return(fread(buffer, 1, size, f));
}
static int
fileio_write_func(void *self, const Lib3dsByte *buffer, int size)
{
FILE *f = (FILE*)self;
return(fwrite(buffer, 1, size, f));
}
/*!
* Loads a .3DS file from disk into memory.
*
* \param filename The filename of the .3DS file
*
* \return A pointer to the Lib3dsFile structure containing the
* data of the .3DS file.
* If the .3DS file can not be loaded NULL is returned.
*
* \note To free the returned structure use lib3ds_free.
*
* \see lib3ds_file_save
* \see lib3ds_file_new
* \see lib3ds_file_free
*
* \ingroup file
*/
Lib3dsFile*
lib3ds_file_load(const char *filename)
{
FILE *f;
Lib3dsFile *file;
Lib3dsIo *io;
f = fopen(filename, "rb");
if (!f) {
return(0);
}
file = lib3ds_file_new();
if (!file) {
fclose(f);
return(0);
}
io = lib3ds_io_new(
f,
fileio_error_func,
fileio_seek_func,
fileio_tell_func,
fileio_read_func,
fileio_write_func
);
if (!io) {
lib3ds_file_free(file);
fclose(f);
return(0);
}
if (!lib3ds_file_read(file, io)) {
free(file);
fclose(f);
return(0);
}
lib3ds_io_free(io);
fclose(f);
return(file);
}
/*!
* Saves a .3DS file from memory to disk.
*
* \param file A pointer to a Lib3dsFile structure containing the
* the data that should be stored.
* \param filename The filename of the .3DS file to store the data in.
*
* \return TRUE on success, FALSE otherwise.
*
* \see lib3ds_file_load
*
* \ingroup file
*/
Lib3dsBool
lib3ds_file_save(Lib3dsFile *file, const char *filename)
{
FILE *f;
Lib3dsIo *io;
Lib3dsBool result;
f = fopen(filename, "wb");
if (!f) {
return(LIB3DS_FALSE);
}
io = lib3ds_io_new(
f,
fileio_error_func,
fileio_seek_func,
fileio_tell_func,
fileio_read_func,
fileio_write_func
);
if (!io) {
fclose(f);
return LIB3DS_FALSE;
}
result = lib3ds_file_write(file, io);
fclose(f);
lib3ds_io_free(io);
return(result);
}
/*!
* Creates and returns a new, empty Lib3dsFile object.
*
* \return A pointer to the Lib3dsFile structure.
* If the structure cannot be allocated, NULL is returned.
*
* \ingroup file
*/
Lib3dsFile*
lib3ds_file_new()
{
Lib3dsFile *file;
file=(Lib3dsFile*)calloc(sizeof(Lib3dsFile),1);
if (!file) {
return(0);
}
file->mesh_version=3;
file->master_scale=1.0f;
file->keyf_revision=5;
strcpy(file->name, "LIB3DS");
file->frames=100;
file->segment_from=0;
file->segment_to=100;
file->current_frame=0;
return(file);
}
/*!
* Free a Lib3dsFile object and all of its resources.
*
* \param file The Lib3dsFile object to be freed.
*
* \ingroup file
*/
void
lib3ds_file_free(Lib3dsFile* file)
{
ASSERT(file);
lib3ds_viewport_set_views(&file->viewport,0);
{
Lib3dsMaterial *p,*q;
for (p=file->materials; p; p=q) {
q=p->next;
lib3ds_material_free(p);
}
file->materials=0;
}
{
Lib3dsCamera *p,*q;
for (p=file->cameras; p; p=q) {
q=p->next;
lib3ds_camera_free(p);
}
file->cameras=0;
}
{
Lib3dsLight *p,*q;
for (p=file->lights; p; p=q) {
q=p->next;
lib3ds_light_free(p);
}
file->lights=0;
}
{
Lib3dsMesh *p,*q;
for (p=file->meshes; p; p=q) {
q=p->next;
lib3ds_mesh_free(p);
}
file->meshes=0;
}
{
Lib3dsNode *p,*q;
for (p=file->nodes; p; p=q) {
q=p->next;
lib3ds_node_free(p);
}
}
free(file);
}
/*!
* Evaluate all of the nodes in this Lib3dsFile object.
*
* \param file The Lib3dsFile object to be evaluated.
* \param t time value, between 0. and file->frames
*
* \see lib3ds_node_eval
*
* \ingroup file
*/
void
lib3ds_file_eval(Lib3dsFile *file, Lib3dsFloat t)
{
Lib3dsNode *p;
for (p=file->nodes; p!=0; p=p->next) {
lib3ds_node_eval(p, t);
}
}
static Lib3dsBool
named_object_read(Lib3dsFile *file, Lib3dsIo *io)
{
Lib3dsChunk c;
char name[64];
Lib3dsWord chunk;
if (!lib3ds_chunk_read_start(&c, LIB3DS_NAMED_OBJECT, io)) {
return(LIB3DS_FALSE);
}
if (!lib3ds_io_read_string(io, name, 64)) {
return(LIB3DS_FALSE);
}
lib3ds_chunk_dump_info(" NAME=%s", name);
lib3ds_chunk_read_tell(&c, io);
while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
switch (chunk) {
case LIB3DS_N_TRI_OBJECT:
{
Lib3dsMesh *mesh;
mesh=lib3ds_mesh_new(name);
if (!mesh) {
return(LIB3DS_FALSE);
}
lib3ds_chunk_read_reset(&c, io);
if (!lib3ds_mesh_read(mesh, io)) {
return(LIB3DS_FALSE);
}
lib3ds_file_insert_mesh(file, mesh);
}
break;
case LIB3DS_N_CAMERA:
{
Lib3dsCamera *camera;
camera=lib3ds_camera_new(name);
if (!camera) {
return(LIB3DS_FALSE);
}
lib3ds_chunk_read_reset(&c, io);
if (!lib3ds_camera_read(camera, io)) {
return(LIB3DS_FALSE);
}
lib3ds_file_insert_camera(file, camera);
}
break;
case LIB3DS_N_DIRECT_LIGHT:
{
Lib3dsLight *light;
light=lib3ds_light_new(name);
if (!light) {
return(LIB3DS_FALSE);
}
lib3ds_chunk_read_reset(&c, io);
if (!lib3ds_light_read(light, io)) {
return(LIB3DS_FALSE);
}
lib3ds_file_insert_light(file, light);
}
break;
default:
lib3ds_chunk_unknown(chunk);
}
}
lib3ds_chunk_read_end(&c, io);
return(LIB3DS_TRUE);
}
static Lib3dsBool
ambient_read(Lib3dsFile *file, Lib3dsIo *io)
{
Lib3dsChunk c;
Lib3dsWord chunk;
Lib3dsBool have_lin=LIB3DS_FALSE;
if (!lib3ds_chunk_read_start(&c, LIB3DS_AMBIENT_LIGHT, io)) {
return(LIB3DS_FALSE);
}
while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
switch (chunk) {
case LIB3DS_LIN_COLOR_F:
{
int i;
for (i=0; i<3; ++i) {
file->ambient[i]=lib3ds_io_read_float(io);
}
}
have_lin=LIB3DS_TRUE;
break;
case LIB3DS_COLOR_F:
{
/* gamma corrected color chunk
replaced in 3ds R3 by LIN_COLOR_24 */
if (!have_lin) {
int i;
for (i=0; i<3; ++i) {
file->ambient[i]=lib3ds_io_read_float(io);
}
}
}
break;
default:
lib3ds_chunk_unknown(chunk);
}
}
lib3ds_chunk_read_end(&c, io);
return(LIB3DS_TRUE);
}
static Lib3dsBool
mdata_read(Lib3dsFile *file, Lib3dsIo *io)
{
Lib3dsChunk c;
Lib3dsWord chunk;
if (!lib3ds_chunk_read_start(&c, LIB3DS_MDATA, io)) {
return(LIB3DS_FALSE);
}
while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
switch (chunk) {
case LIB3DS_MESH_VERSION:
{
file->mesh_version=lib3ds_io_read_intd(io);
}
break;
case LIB3DS_MASTER_SCALE:
{
file->master_scale=lib3ds_io_read_float(io);
}
break;
case LIB3DS_SHADOW_MAP_SIZE:
case LIB3DS_LO_SHADOW_BIAS:
case LIB3DS_HI_SHADOW_BIAS:
case LIB3DS_SHADOW_SAMPLES:
case LIB3DS_SHADOW_RANGE:
case LIB3DS_SHADOW_FILTER:
case LIB3DS_RAY_BIAS:
{
lib3ds_chunk_read_reset(&c, io);
if (!lib3ds_shadow_read(&file->shadow, io)) {
return(LIB3DS_FALSE);
}
}
break;
case LIB3DS_VIEWPORT_LAYOUT:
case LIB3DS_DEFAULT_VIEW:
{
lib3ds_chunk_read_reset(&c, io);
if (!lib3ds_viewport_read(&file->viewport, io)) {
return(LIB3DS_FALSE);
}
}
break;
case LIB3DS_O_CONSTS:
{
int i;
for (i=0; i<3; ++i) {
file->construction_plane[i]=lib3ds_io_read_float(io);
}
}
break;
case LIB3DS_AMBIENT_LIGHT:
{
lib3ds_chunk_read_reset(&c, io);
if (!ambient_read(file, io)) {
return(LIB3DS_FALSE);
}
}
break;
case LIB3DS_BIT_MAP:
case LIB3DS_SOLID_BGND:
case LIB3DS_V_GRADIENT:
case LIB3DS_USE_BIT_MAP:
case LIB3DS_USE_SOLID_BGND:
case LIB3DS_USE_V_GRADIENT:
{
lib3ds_chunk_read_reset(&c, io);
if (!lib3ds_background_read(&file->background, io)) {
return(LIB3DS_FALSE);
}
}
break;
case LIB3DS_FOG:
case LIB3DS_LAYER_FOG:
case LIB3DS_DISTANCE_CUE:
case LIB3DS_USE_FOG:
case LIB3DS_USE_LAYER_FOG:
case LIB3DS_USE_DISTANCE_CUE:
{
lib3ds_chunk_read_reset(&c, io);
if (!lib3ds_atmosphere_read(&file->atmosphere, io)) {
return(LIB3DS_FALSE);
}
}
break;
case LIB3DS_MAT_ENTRY:
{
Lib3dsMaterial *material;
material=lib3ds_material_new();
if (!material) {
return(LIB3DS_FALSE);
}
lib3ds_chunk_read_reset(&c, io);
if (!lib3ds_material_read(material, io)) {
return(LIB3DS_FALSE);
}
lib3ds_file_insert_material(file, material);
}
break;
case LIB3DS_NAMED_OBJECT:
{
lib3ds_chunk_read_reset(&c, io);
if (!named_object_read(file, io)) {
return(LIB3DS_FALSE);
}
}
break;
default:
lib3ds_chunk_unknown(chunk);
}
}
lib3ds_chunk_read_end(&c, io);
return(LIB3DS_TRUE);
}
static Lib3dsBool
kfdata_read(Lib3dsFile *file, Lib3dsIo *io)
{
Lib3dsChunk c;
Lib3dsWord chunk;
if (!lib3ds_chunk_read_start(&c, LIB3DS_KFDATA, io)) {
return(LIB3DS_FALSE);
}
while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
switch (chunk) {
case LIB3DS_KFHDR:
{
file->keyf_revision=lib3ds_io_read_word(io);
if (!lib3ds_io_read_string(io, file->name, 12+1)) {
return(LIB3DS_FALSE);
}
file->frames=lib3ds_io_read_intd(io);
}
break;
case LIB3DS_KFSEG:
{
file->segment_from=lib3ds_io_read_intd(io);
file->segment_to=lib3ds_io_read_intd(io);
}
break;
case LIB3DS_KFCURTIME:
{
file->current_frame=lib3ds_io_read_intd(io);
}
break;
case LIB3DS_VIEWPORT_LAYOUT:
case LIB3DS_DEFAULT_VIEW:
{
lib3ds_chunk_read_reset(&c, io);
if (!lib3ds_viewport_read(&file->viewport_keyf, io)) {
return(LIB3DS_FALSE);
}
}
break;
case LIB3DS_AMBIENT_NODE_TAG:
{
Lib3dsNode *node;
node=lib3ds_node_new_ambient();
if (!node) {
return(LIB3DS_FALSE);
}
lib3ds_chunk_read_reset(&c, io);
if (!lib3ds_node_read(node, file, io)) {
return(LIB3DS_FALSE);
}
lib3ds_file_insert_node(file, node);
}
break;
case LIB3DS_OBJECT_NODE_TAG:
{
Lib3dsNode *node;
node=lib3ds_node_new_object();
if (!node) {
return(LIB3DS_FALSE);
}
lib3ds_chunk_read_reset(&c, io);
if (!lib3ds_node_read(node, file, io)) {
return(LIB3DS_FALSE);
}
lib3ds_file_insert_node(file, node);
}
break;
case LIB3DS_CAMERA_NODE_TAG:
{
Lib3dsNode *node;
node=lib3ds_node_new_camera();
if (!node) {
return(LIB3DS_FALSE);
}
lib3ds_chunk_read_reset(&c, io);
if (!lib3ds_node_read(node, file, io)) {
return(LIB3DS_FALSE);
}
lib3ds_file_insert_node(file, node);
}
break;
case LIB3DS_TARGET_NODE_TAG:
{
Lib3dsNode *node;
node=lib3ds_node_new_target();
if (!node) {
return(LIB3DS_FALSE);
}
lib3ds_chunk_read_reset(&c, io);
if (!lib3ds_node_read(node, file, io)) {
return(LIB3DS_FALSE);
}
lib3ds_file_insert_node(file, node);
}
break;
case LIB3DS_LIGHT_NODE_TAG:
case LIB3DS_SPOTLIGHT_NODE_TAG:
{
Lib3dsNode *node;
node=lib3ds_node_new_light();
if (!node) {
return(LIB3DS_FALSE);
}
lib3ds_chunk_read_reset(&c, io);
if (!lib3ds_node_read(node, file, io)) {
return(LIB3DS_FALSE);
}
lib3ds_file_insert_node(file, node);
}
break;
case LIB3DS_L_TARGET_NODE_TAG:
{
Lib3dsNode *node;
node=lib3ds_node_new_spot();
if (!node) {
return(LIB3DS_FALSE);
}
lib3ds_chunk_read_reset(&c, io);
if (!lib3ds_node_read(node, file, io)) {
return(LIB3DS_FALSE);
}
lib3ds_file_insert_node(file, node);
}
break;
default:
lib3ds_chunk_unknown(chunk);
}
}
lib3ds_chunk_read_end(&c, io);
return(LIB3DS_TRUE);
}
/*!
* Read 3ds file data into a Lib3dsFile object.
*
* \param file The Lib3dsFile object to be filled.
* \param io A Lib3dsIo object previously set up by the caller.
*
* \return LIB3DS_TRUE on success, LIB3DS_FALSE on failure.
*
* \ingroup file
*/
Lib3dsBool
lib3ds_file_read(Lib3dsFile *file, Lib3dsIo *io)
{
Lib3dsChunk c;
Lib3dsWord chunk;
if (!lib3ds_chunk_read_start(&c, 0, io)) {
return(LIB3DS_FALSE);
}
switch (c.chunk) {
case LIB3DS_MDATA:
{
lib3ds_chunk_read_reset(&c, io);
if (!mdata_read(file, io)) {
return(LIB3DS_FALSE);
}
}
break;
case LIB3DS_M3DMAGIC:
case LIB3DS_MLIBMAGIC:
case LIB3DS_CMAGIC:
{
while ((chunk=lib3ds_chunk_read_next(&c, io))!=0) {
switch (chunk) {
case LIB3DS_M3D_VERSION:
{
file->mesh_version=lib3ds_io_read_dword(io);
}
break;
case LIB3DS_MDATA:
{
lib3ds_chunk_read_reset(&c, io);
if (!mdata_read(file, io)) {
return(LIB3DS_FALSE);
}
}
break;
case LIB3DS_KFDATA:
{
lib3ds_chunk_read_reset(&c, io);
if (!kfdata_read(file, io)) {
return(LIB3DS_FALSE);
}
}
break;
default:
lib3ds_chunk_unknown(chunk);
}
}
}
break;
default:
lib3ds_chunk_unknown(c.chunk);
return(LIB3DS_FALSE);
}
lib3ds_chunk_read_end(&c, io);
return(LIB3DS_TRUE);
}
static Lib3dsBool
colorf_write(Lib3dsRgba rgb, Lib3dsIo *io)
{
Lib3dsChunk c;
c.chunk=LIB3DS_COLOR_F;
c.size=18;
lib3ds_chunk_write(&c,io);
lib3ds_io_write_rgb(io, rgb);
c.chunk=LIB3DS_LIN_COLOR_F;
c.size=18;
lib3ds_chunk_write(&c,io);
lib3ds_io_write_rgb(io, rgb);
return(LIB3DS_TRUE);
}
static Lib3dsBool
mdata_write(Lib3dsFile *file, Lib3dsIo *io)
{
Lib3dsChunk c;
c.chunk=LIB3DS_MDATA;
if (!lib3ds_chunk_write_start(&c,io)) {
return(LIB3DS_FALSE);
}
{ /*---- LIB3DS_MESH_VERSION ----*/
Lib3dsChunk c;
c.chunk=LIB3DS_MESH_VERSION;
c.size=10;
lib3ds_chunk_write(&c,io);
lib3ds_io_write_intd(io, file->mesh_version);
}
{ /*---- LIB3DS_MASTER_SCALE ----*/
Lib3dsChunk c;
c.chunk=LIB3DS_MASTER_SCALE;
c.size=10;
lib3ds_chunk_write(&c,io);
lib3ds_io_write_float(io, file->master_scale);
}
{ /*---- LIB3DS_O_CONSTS ----*/
int i;
for (i=0; i<3; ++i) {
if (fabs(file->construction_plane[i])>LIB3DS_EPSILON) {
break;
}
}
if (i<3) {
Lib3dsChunk c;
c.chunk=LIB3DS_O_CONSTS;
c.size=18;
lib3ds_chunk_write(&c,io);
lib3ds_io_write_vector(io, file->construction_plane);
}
}
{ /*---- LIB3DS_AMBIENT_LIGHT ----*/
int i;
for (i=0; i<3; ++i) {
if (fabs(file->ambient[i])>LIB3DS_EPSILON) {
break;
}
}
if (i<3) {
Lib3dsChunk c;
c.chunk=LIB3DS_AMBIENT_LIGHT;
c.size=42;
lib3ds_chunk_write(&c,io);
colorf_write(file->ambient,io);
}
}
lib3ds_background_write(&file->background, io);
lib3ds_atmosphere_write(&file->atmosphere, io);
lib3ds_shadow_write(&file->shadow, io);
lib3ds_viewport_write(&file->viewport, io);
{
Lib3dsMaterial *p;
for (p=file->materials; p!=0; p=p->next) {
if (!lib3ds_material_write(p,io)) {
return(LIB3DS_FALSE);
}
}
}
{
Lib3dsCamera *p;
Lib3dsChunk c;
for (p=file->cameras; p!=0; p=p->next) {
c.chunk=LIB3DS_NAMED_OBJECT;
if (!lib3ds_chunk_write_start(&c,io)) {
return(LIB3DS_FALSE);
}
lib3ds_io_write_string(io, p->name);
lib3ds_camera_write(p,io);
if (!lib3ds_chunk_write_end(&c,io)) {
return(LIB3DS_FALSE);
}
}
}
{
Lib3dsLight *p;
Lib3dsChunk c;
for (p=file->lights; p!=0; p=p->next) {
c.chunk=LIB3DS_NAMED_OBJECT;
if (!lib3ds_chunk_write_start(&c,io)) {
return(LIB3DS_FALSE);
}
lib3ds_io_write_string(io,p->name);
lib3ds_light_write(p,io);
if (!lib3ds_chunk_write_end(&c,io)) {
return(LIB3DS_FALSE);
}
}
}
{
Lib3dsMesh *p;
Lib3dsChunk c;
for (p=file->meshes; p!=0; p=p->next) {
c.chunk=LIB3DS_NAMED_OBJECT;
if (!lib3ds_chunk_write_start(&c,io)) {
return(LIB3DS_FALSE);
}
lib3ds_io_write_string(io, p->name);
lib3ds_mesh_write(p,io);
if (!lib3ds_chunk_write_end(&c,io)) {
return(LIB3DS_FALSE);
}
}
}
if (!lib3ds_chunk_write_end(&c,io)) {
return(LIB3DS_FALSE);
}
return(LIB3DS_TRUE);
}
static Lib3dsBool
nodes_write(Lib3dsNode *node, Lib3dsFile *file, Lib3dsIo *io)
{
{
Lib3dsNode *p;
for (p=node->childs; p!=0; p=p->next) {
if (!lib3ds_node_write(p, file, io)) {
return(LIB3DS_FALSE);
}
nodes_write(p, file, io);
}
}
return(LIB3DS_TRUE);
}
static Lib3dsBool
kfdata_write(Lib3dsFile *file, Lib3dsIo *io)
{
Lib3dsChunk c;
if (!file->nodes) {
return(LIB3DS_TRUE);
}
c.chunk=LIB3DS_KFDATA;
if (!lib3ds_chunk_write_start(&c,io)) {
return(LIB3DS_FALSE);
}
{ /*---- LIB3DS_KFHDR ----*/
Lib3dsChunk c;
c.chunk=LIB3DS_KFHDR;
c.size=6 + 2 + strlen(file->name)+1 +4;
lib3ds_chunk_write(&c,io);
lib3ds_io_write_intw(io, file->keyf_revision);
lib3ds_io_write_string(io, file->name);
lib3ds_io_write_intd(io, file->frames);
}
{ /*---- LIB3DS_KFSEG ----*/
Lib3dsChunk c;
c.chunk=LIB3DS_KFSEG;
c.size=14;
lib3ds_chunk_write(&c,io);
lib3ds_io_write_intd(io, file->segment_from);
lib3ds_io_write_intd(io, file->segment_to);
}
{ /*---- LIB3DS_KFCURTIME ----*/
Lib3dsChunk c;
c.chunk=LIB3DS_KFCURTIME;
c.size=10;
lib3ds_chunk_write(&c,io);
lib3ds_io_write_intd(io, file->current_frame);
}
lib3ds_viewport_write(&file->viewport_keyf, io);
{
Lib3dsNode *p;
for (p=file->nodes; p!=0; p=p->next) {
if (!lib3ds_node_write(p, file, io)) {
return(LIB3DS_FALSE);
}
if (!nodes_write(p, file, io)) {
return(LIB3DS_FALSE);
}
}
}
if (!lib3ds_chunk_write_end(&c,io)) {
return(LIB3DS_FALSE);
}
return(LIB3DS_TRUE);
}
/*!
* Write 3ds file data from a Lib3dsFile object to a file.
*
* \param file The Lib3dsFile object to be written.
* \param io A Lib3dsIo object previously set up by the caller.
*
* \return LIB3DS_TRUE on success, LIB3DS_FALSE on failure.
*
* \ingroup file
*/
Lib3dsBool
lib3ds_file_write(Lib3dsFile *file, Lib3dsIo *io)
{
Lib3dsChunk c;
c.chunk=LIB3DS_M3DMAGIC;
if (!lib3ds_chunk_write_start(&c,io)) {
LIB3DS_ERROR_LOG;
return(LIB3DS_FALSE);
}
{ /*---- LIB3DS_M3D_VERSION ----*/
Lib3dsChunk c;
c.chunk=LIB3DS_M3D_VERSION;
c.size=10;
lib3ds_chunk_write(&c,io);
lib3ds_io_write_dword(io, file->mesh_version);
}
if (!mdata_write(file, io)) {
return(LIB3DS_FALSE);
}
if (!kfdata_write(file, io)) {
return(LIB3DS_FALSE);
}
if (!lib3ds_chunk_write_end(&c,io)) {
return(LIB3DS_FALSE);
}
return(LIB3DS_TRUE);
}
/*!
* Insert a new Lib3dsMaterial object into the materials list of
* a Lib3dsFile object.
*
* The new Lib3dsMaterial object is inserted into the materials list
* in alphabetic order by name.
*
* \param file The Lib3dsFile object to be modified.
* \param material The Lib3dsMaterial object to be inserted into file->materials
*
* \ingroup file
*/
void
lib3ds_file_insert_material(Lib3dsFile *file, Lib3dsMaterial *material)
{
Lib3dsMaterial *p,*q;
ASSERT(file);
ASSERT(material);
ASSERT(!material->next);
q=0;
for (p=file->materials; p!=0; p=p->next) {
if (strcmp(material->name, p->name)<0) {
break;
}
q=p;
}
if (!q) {
material->next=file->materials;
file->materials=material;
}
else {
material->next=q->next;
q->next=material;
}
}
/*!
* Remove a Lib3dsMaterial object from the materials list of
* a Lib3dsFile object.
*
* If the Lib3dsMaterial is not found in the materials list, nothing is
* done (except that an error log message may be generated.)
*
* \param file The Lib3dsFile object to be modified.
* \param material The Lib3dsMaterial object to be removed from file->materials
*
* \ingroup file
*/
void
lib3ds_file_remove_material(Lib3dsFile *file, Lib3dsMaterial *material)
{
Lib3dsMaterial *p,*q;
ASSERT(file);
ASSERT(material);
ASSERT(file->materials);
for (p=0,q=file->materials; q; p=q,q=q->next) {
if (q==material) {
break;
}
}
if (!q) {
ASSERT(LIB3DS_FALSE);
return;
}
if (!p) {
file->materials=material->next;
}
else {
p->next=q->next;
}
material->next=0;
}
/*!
* Return a Lib3dsMaterial object by name.
*
* \param file Lib3dsFile object to be searched.
* \param name Name of the Lib3dsMaterial object to be searched for.
*
* \return A pointer to the named Lib3dsMaterial, or NULL if not found.
*
* \ingroup file
*/
Lib3dsMaterial*
lib3ds_file_material_by_name(Lib3dsFile *file, const char *name)
{
Lib3dsMaterial *p;
ASSERT(file);
for (p=file->materials; p!=0; p=p->next) {
if (strcmp(p->name,name)==0) {
return(p);
}
}
return(0);
}
/*!
* Dump all Lib3dsMaterial objects found in a Lib3dsFile object.
*
* \param file Lib3dsFile object to be dumped.
*
* \see lib3ds_material_dump
*
* \ingroup file
*/
void
lib3ds_file_dump_materials(Lib3dsFile *file)
{
Lib3dsMaterial *p;
ASSERT(file);
for (p=file->materials; p!=0; p=p->next) {
lib3ds_material_dump(p);
}
}
/*!
* Insert a new Lib3dsMesh object into the meshes list of
* a Lib3dsFile object.
*
* The new Lib3dsMesh object is inserted into the meshes list
* in alphabetic order by name.
*
* \param file The Lib3dsFile object to be modified.
* \param material The Lib3dsMesh object to be inserted into file->meshes
*
* \ingroup file
*/
void
lib3ds_file_insert_mesh(Lib3dsFile *file, Lib3dsMesh *mesh)
{
Lib3dsMesh *p,*q;
ASSERT(file);
ASSERT(mesh);
ASSERT(!mesh->next);
q=0;
for (p=file->meshes; p!=0; p=p->next) {
if (strcmp(mesh->name, p->name)<0) {
break;
}
q=p;
}
if (!q) {
mesh->next=file->meshes;
file->meshes=mesh;
}
else {
mesh->next=q->next;
q->next=mesh;
}
}
/*!
* Remove a Lib3dsMesh object from the meshes list of
* a Lib3dsFile object.
*
* If the Lib3dsMesh is not found in the meshes list, nothing is done
* (except that an error log message may be generated.)
*
* \param file The Lib3dsFile object to be modified.
* \param material The Lib3dsMesh object to be removed from file->meshes
*
* \ingroup file
*/
void
lib3ds_file_remove_mesh(Lib3dsFile *file, Lib3dsMesh *mesh)
{
Lib3dsMesh *p,*q;
ASSERT(file);
ASSERT(mesh);
ASSERT(file->meshes);
for (p=0,q=file->meshes; q; p=q,q=q->next) {
if (q==mesh) {
break;
}
}
if (!q) {
ASSERT(LIB3DS_FALSE);
return;
}
if (!p) {
file->meshes=mesh->next;
}
else {
p->next=q->next;
}
mesh->next=0;
}
/*!
* Return a Lib3dsMesh object from a Lib3dsFile by name.
*
* \param file Lib3dsFile object to be searched.
* \param name Name of the Lib3dsMesh object to be searched for.
*
* \return A pointer to the named Lib3dsMesh, or NULL if not found.
*
* \ingroup file
*/
Lib3dsMesh*
lib3ds_file_mesh_by_name(Lib3dsFile *file, const char *name)
{
Lib3dsMesh *p;
ASSERT(file);
for (p=file->meshes; p!=0; p=p->next) {
if (strcmp(p->name,name)==0) {
return(p);
}
}
return(0);
}
/*!
* Dump all Lib3dsMesh objects found in a Lib3dsFile object.
*
* \param file Lib3dsFile object to be dumped.
*
* \see lib3ds_mesh_dump
*
* \ingroup file
*/
void
lib3ds_file_dump_meshes(Lib3dsFile *file)
{
Lib3dsMesh *p;
ASSERT(file);
for (p=file->meshes; p!=0; p=p->next) {
lib3ds_mesh_dump(p);
}
}
static void
dump_instances(Lib3dsNode *node, const char* parent)
{
Lib3dsNode *p;
char name[255];
ASSERT(node);
ASSERT(parent);
strcpy(name, parent);
strcat(name, ".");
strcat(name, node->name);
if (node->type==LIB3DS_OBJECT_NODE) {
printf(" %s : %s\n", name, node->data.object.instance);
}
for (p=node->childs; p!=0; p=p->next) {
dump_instances(p, parent);
}
}
/*!
* Dump all Lib3dsNode object names found in a Lib3dsFile object.
*
* For each node of type OBJECT_NODE, its name and data.object.instance
* fields are printed to stdout. Consider using lib3ds_file_dump_nodes()
* instead, as that function dumps more information.
*
* Nodes are dumped recursively.
*
* \param file Lib3dsFile object to be dumped.
*
* \see lib3ds_file_dump_nodes
*
* \ingroup file
*/
void
lib3ds_file_dump_instances(Lib3dsFile *file)
{
Lib3dsNode *p;
ASSERT(file);
for (p=file->nodes; p!=0; p=p->next) {
dump_instances(p,"");
}
}
/*!
* Insert a new Lib3dsCamera object into the cameras list of
* a Lib3dsFile object.
*
* The new Lib3dsCamera object is inserted into the cameras list
* in alphabetic order by name.
*
* \param file The Lib3dsFile object to be modified.
* \param material The Lib3dsCamera object to be inserted into file->cameras
*
* \ingroup file
*/
void
lib3ds_file_insert_camera(Lib3dsFile *file, Lib3dsCamera *camera)
{
Lib3dsCamera *p,*q;
ASSERT(file);
ASSERT(camera);
ASSERT(!camera->next);
q=0;
for (p=file->cameras; p!=0; p=p->next) {
if (strcmp(camera->name, p->name)<0) {
break;
}
q=p;
}
if (!q) {
camera->next=file->cameras;
file->cameras=camera;
}
else {
camera->next=q->next;
q->next=camera;
}
}
/*!
* Remove a Lib3dsCamera object from the cameras list of
* a Lib3dsFile object.
*
* If the Lib3dsCamera is not found in the cameras list, nothing is done
* (except that an error log message may be generated.)
*
* \param file The Lib3dsFile object to be modified.
* \param material The Lib3dsCamera object to be removed from file->cameras
*
* \ingroup file
*/
void
lib3ds_file_remove_camera(Lib3dsFile *file, Lib3dsCamera *camera)
{
Lib3dsCamera *p,*q;
ASSERT(file);
ASSERT(camera);
ASSERT(file->cameras);
for (p=0,q=file->cameras; q; p=q,q=q->next) {
if (q==camera) {
break;
}
}
if (!q) {
ASSERT(LIB3DS_FALSE);
return;
}
if (!p) {
file->cameras=camera->next;
}
else {
p->next=q->next;
}
camera->next=0;
}
/*!
* Return a Lib3dsCamera object from a Lib3dsFile by name.
*
* \param file Lib3dsFile object to be searched.
* \param name Name of the Lib3dsCamera object to be searched for.
*
* \return A pointer to the named Lib3dsCamera, or NULL if not found.
*
* \ingroup file
*/
Lib3dsCamera*
lib3ds_file_camera_by_name(Lib3dsFile *file, const char *name)
{
Lib3dsCamera *p;
ASSERT(file);
for (p=file->cameras; p!=0; p=p->next) {
if (strcmp(p->name,name)==0) {
return(p);
}
}
return(0);
}
/*!
* Dump all Lib3dsCamera objects found in a Lib3dsFile object.
*
* \param file Lib3dsFile object to be dumped.
*
* \see lib3ds_camera_dump
*
* \ingroup file
*/
void
lib3ds_file_dump_cameras(Lib3dsFile *file)
{
Lib3dsCamera *p;
ASSERT(file);
for (p=file->cameras; p!=0; p=p->next) {
lib3ds_camera_dump(p);
}
}
/*!
* Insert a new Lib3dsLight object into the lights list of
* a Lib3dsFile object.
*
* The new Lib3dsLight object is inserted into the lights list
* in alphabetic order by name.
*
* \param file The Lib3dsFile object to be modified.
* \param material The Lib3dsLight object to be inserted into file->lights
*
* \ingroup file
*/
void
lib3ds_file_insert_light(Lib3dsFile *file, Lib3dsLight *light)
{
Lib3dsLight *p,*q;
ASSERT(file);
ASSERT(light);
ASSERT(!light->next);
q=0;
for (p=file->lights; p!=0; p=p->next) {
if (strcmp(light->name, p->name)<0) {
break;
}
q=p;
}
if (!q) {
light->next=file->lights;
file->lights=light;
}
else {
light->next=q->next;
q->next=light;
}
}
/*!
* Remove a Lib3dsLight object from the lights list of
* a Lib3dsFile object.
*
* If the Lib3dsLight is not found in the lights list, nothing is done
* (except that an error log message may be generated.)
*
* \param file The Lib3dsFile object to be modified.
* \param material The Lib3dsLight object to be removed from file->lights
*
* \ingroup file
*/
void
lib3ds_file_remove_light(Lib3dsFile *file, Lib3dsLight *light)
{
Lib3dsLight *p,*q;
ASSERT(file);
ASSERT(light);
ASSERT(file->lights);
for (p=0,q=file->lights; q; p=q,q=q->next) {
if (q==light) {
break;
}
}
if (!q) {
ASSERT(LIB3DS_FALSE);
return;
}
if (!p) {
file->lights=light->next;
}
else {
p->next=q->next;
}
light->next=0;
}
/*!
* Return a Lib3dsLight object from a Lib3dsFile by name.
*
* \param file Lib3dsFile object to be searched.
* \param name Name of the Lib3dsLight object to be searched for.
*
* \return A pointer to the named Lib3dsLight, or NULL if not found.
*
* \ingroup file
*/
Lib3dsLight*
lib3ds_file_light_by_name(Lib3dsFile *file, const char *name)
{
Lib3dsLight *p;
ASSERT(file);
for (p=file->lights; p!=0; p=p->next) {
if (strcmp(p->name,name)==0) {
return(p);
}
}
return(0);
}
/*!
* Dump all Lib3dsLight objects found in a Lib3dsFile object.
*
* \param file Lib3dsFile object to be dumped.
*
* \see lib3ds_light_dump
*
* \ingroup file
*/
void
lib3ds_file_dump_lights(Lib3dsFile *file)
{
Lib3dsLight *p;
ASSERT(file);
for (p=file->lights; p!=0; p=p->next) {
lib3ds_light_dump(p);
}
}
/*!
* Compute the bounding box for Lib3dsFile objects.
*
* This function computes the bounding box for all meshes
* in the Lib3dsFile object. Cameras and lights are not included.
*
* \param file The Lib3dsFile object to be examined.
* \param min Returned minimum x,y,z values.
* \param max Returned maximum x,y,z values.
*
* \ingroup file
*/
void
lib3ds_object_bounding_box(Lib3dsFile *file, Lib3dsVector min, Lib3dsVector max)
{
{
Lib3dsVector lmin, lmax;
Lib3dsMesh *p=file->meshes;
if (p) {
lib3ds_mesh_bounding_box(p, min, max);
p = p->next;
}
while (p) {
lib3ds_mesh_bounding_box(p, lmin, lmax);
lib3ds_vector_min(min, lmin);
lib3ds_vector_max(max, lmax);
p=p->next;
}
}
}
/*!
* Compute the bounding box for a Lib3dsFile.
*
* This function computes the bounding box for all meshes, cameras,
* and lights in the Lib3dsFile object.
*
* \param file The Lib3dsFile object to be examined.
* \param min Returned minimum x,y,z values.
* \param max Returned maximum x,y,z values.
*
* \ingroup file
*/
void
lib3ds_file_bounding_box(Lib3dsFile *file, Lib3dsVector min, Lib3dsVector max)
{
Lib3dsBool init=LIB3DS_FALSE;
{
Lib3dsVector lmin, lmax;
Lib3dsMesh *p=file->meshes;
if (!init && p) {
init = LIB3DS_TRUE;
lib3ds_mesh_bounding_box(p, min, max);
p = p->next;
}
while (p) {
lib3ds_mesh_bounding_box(p, lmin, lmax);
lib3ds_vector_min(min, lmin);
lib3ds_vector_max(max, lmax);
p=p->next;
}
}
{
Lib3dsCamera *p=file->cameras;
if (!init && p) {
init = LIB3DS_TRUE;
lib3ds_vector_copy(min, p->position);
lib3ds_vector_copy(max, p->position);
}
while (p) {
lib3ds_vector_min(min, p->position);
lib3ds_vector_max(max, p->position);
lib3ds_vector_min(min, p->target);
lib3ds_vector_max(max, p->target);
p=p->next;
}
}
{
Lib3dsLight *p=file->lights;
if (!init && p) {
init = LIB3DS_TRUE;
lib3ds_vector_copy(min, p->position);
lib3ds_vector_copy(max, p->position);
}
while (p) {
lib3ds_vector_min(min, p->position);
lib3ds_vector_max(max, p->position);
if (p->spot_light) {
lib3ds_vector_min(min, p->spot);
lib3ds_vector_max(max, p->spot);
}
p=p->next;
}
}
}
/*!
* Return a node object by name and type.
*
* This function performs a recursive search for the specified node.
* Both name and type must match.
*
* \param file The Lib3dsFile to be searched.
* \param name The target node name.
* \param type The target node type
*
* \return A pointer to the first matching node, or NULL if not found.
*
* \see lib3ds_node_by_name
*
* \ingroup file
*/
Lib3dsNode*
lib3ds_file_node_by_name(Lib3dsFile *file, const char* name, Lib3dsNodeTypes type)
{
Lib3dsNode *p,*q;
ASSERT(file);
for (p=file->nodes; p!=0; p=p->next) {
if ((p->type==type) && (strcmp(p->name, name)==0)) {
return(p);
}
q=lib3ds_node_by_name(p, name, type);
if (q) {
return(q);
}
}
return(0);
}
/*!
* Return a node object by id.
*
* This function performs a recursive search for the specified node.
*
* \param file The Lib3dsFile to be searched.
* \param node_id The target node id.
*
* \return A pointer to the first matching node, or NULL if not found.
*
* \see lib3ds_node_by_id
*
* \ingroup file
*/
Lib3dsNode*
lib3ds_file_node_by_id(Lib3dsFile *file, Lib3dsWord node_id)
{
Lib3dsNode *p,*q;
ASSERT(file);
for (p=file->nodes; p!=0; p=p->next) {
if (p->node_id==node_id) {
return(p);
}
q=lib3ds_node_by_id(p, node_id);
if (q) {
return(q);
}
}
return(0);
}
/*!
* Insert a new node into a Lib3dsFile object.
*
* If the node's parent_id structure is not LIB3DS_NO_PARENT and the
* specified parent is found inside the Lib3dsFile object, then the
* node is inserted as a child of that parent. If the parent_id
* structure is LIB3DS_NO_PARENT or the specified parent is not found,
* then the node is inserted at the top level.
*
* Node is inserted in alphabetic order by name.
*
* Finally, if any other top-level nodes in file specify this node as
* their parent, they are relocated as a child of this node.
*
* \param file The Lib3dsFile object to be modified.
* \param node The node to be inserted into file
*
* \ingroup file
*/
void
lib3ds_file_insert_node(Lib3dsFile *file, Lib3dsNode *node)
{
Lib3dsNode *parent,*p,*n;
ASSERT(node);
ASSERT(!node->next);
ASSERT(!node->parent);
parent=0;
if (node->parent_id!=LIB3DS_NO_PARENT) {
parent=lib3ds_file_node_by_id(file, node->parent_id);
}
node->parent=parent;
if (!parent) {
for (p=0,n=file->nodes; n!=0; p=n,n=n->next) {
if (strcmp(n->name, node->name)>0) {
break;
}
}
if (!p) {
node->next=file->nodes;
file->nodes=node;
}
else {
node->next=p->next;
p->next=node;
}
}
else {
for (p=0,n=parent->childs; n!=0; p=n,n=n->next) {
if (strcmp(n->name, node->name)>0) {
break;
}
}
if (!p) {
node->next=parent->childs;
parent->childs=node;
}
else {
node->next=p->next;
p->next=node;
}
}
if (node->node_id!=LIB3DS_NO_PARENT) {
for (n=file->nodes; n!=0; n=p) {
p=n->next;
if (n->parent_id==node->node_id) {
lib3ds_file_remove_node(file, n);
lib3ds_file_insert_node(file, n);
}
}
}
}
/*!
* Remove a node from the a Lib3dsFile object.
*
* \param file The Lib3dsFile object to be modified.
* \param node The Lib3dsNode object to be removed from file
*
* \return LIB3DS_TRUE on success, LIB3DS_FALSE if node is not found in file
*
* \ingroup file
*/
Lib3dsBool
lib3ds_file_remove_node(Lib3dsFile *file, Lib3dsNode *node)
{
Lib3dsNode *p,*n;
if (node->parent) {
for (p=0,n=node->parent->childs; n; p=n,n=n->next) {
if (n==node) {
break;
}
}
if (!n) {
return(LIB3DS_FALSE);
}
if (!p) {
node->parent->childs=n->next;
}
else {
p->next=n->next;
}
}
else {
for (p=0,n=file->nodes; n; p=n,n=n->next) {
if (n==node) {
break;
}
}
if (!n) {
return(LIB3DS_FALSE);
}
if (!p) {
file->nodes=n->next;
}
else {
p->next=n->next;
}
}
return(LIB3DS_TRUE);
}
/*!
* Dump all node objects found in a Lib3dsFile object.
*
* Nodes are dumped recursively.
*
* \param file Lib3dsFile object to be dumped.
*
* \see lib3ds_node_dump
*
* \ingroup file
*/
void
lib3ds_file_dump_nodes(Lib3dsFile *file)
{
Lib3dsNode *p;
ASSERT(file);
for (p=file->nodes; p!=0; p=p->next) {
lib3ds_node_dump(p,1);
}
}
/*!
\typedef Lib3dsFile
\ingroup file
\sa _Lib3dsFile
*/
/* Programming trick to force users to compile their source code against the
* correct headers. The symbol lib3ds_version1_3 will be defined iff the users
* compile with the current version of <lib3ds/types.h>
*/
//extern int lib3ds_version1_3;
//static const int *lib3ds_version = &lib3ds_version1_3;
|