Source

Coin / src / 3ds / 3dsLoader.cpp

Full commit
   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
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
/**************************************************************************\
 * Copyright (c) Kongsberg Oil & Gas Technologies AS
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\**************************************************************************/

//
//  3DS File Loader for Open Inventor
//
//  developed by PC John (peciva@fit.vutbr.cz)
//
//
//  Comments about 3ds files: Structure of the 3ds files is well known
//  (http://www.cyberloonies.com/3dsftk.html). However, it is often hard to
//  understand what is the informations in 3ds file about and how to
//  interpret them. For example, texture coordinates are not always
//  represented by OpenGL texture coordinates, and I can't find out
//  what is their meaning.
//
//
//  All loaded models are centered around [0,0,0] and normalized to
//  size 10 by default.
//
//  If loading fails during reading 3ds file, file pointer position is
//  undefined.
//
//  By default only error messages are generated. COIN_DEBUG_3DS environment
//  variable can be used to specify amount of debug messages:
//  0 .. only error messages (default)
//  1 .. warnings that usually concerns data parsed from the 3ds file
//  2 .. print basic 3ds file structure info
//  3 .. print everything
//
//
//  TODO list:
//
//  - incomplete texture implementation - in 3ds files there is possible to
//    make materials with about 20 textures (diffuse color texture, specular
//    texture, bump-map texture,...) There is not a support for them in the
//    Inventor yet.
//
//  - Texture coordinates are not always loaded right, because 3ds uses
//    many strange mapping modes. Deeper understanding of 3ds will be needed
//    to fix this.
//
//  - Backface culling functionality is disabled because it does not work
//    right on some models. It looks like some models are CLOCKWISE and other
//    COUNTERCLOCKWISE.
//
//  - unimplemented lights
//
//  - per-vertex normals generation
//
//  - investigate the color of the default material objects
//
//  - ?environment? (ambient light, fog,...)
//
//  - ?emissiveColor? (maybe MAT_SELF_ILLUM and MAT_SELF_ILPCT chunks)
//
//  - some animations?
//
//  - public API to control the loading
//
//


#include "SoStream.h"

#include "coindefs.h"

#include <Inventor/SbPlane.h>
#include <Inventor/SbRotation.h>
#include <Inventor/nodes/SoCoordinate3.h>
#include <Inventor/nodes/SoIndexedTriangleStripSet.h>
#include <Inventor/nodes/SoMaterial.h>
#include <Inventor/nodes/SoMaterialBinding.h>
#include <Inventor/nodes/SoMatrixTransform.h>
#include <Inventor/nodes/SoNormal.h>
#include <Inventor/nodes/SoNormalBinding.h>
#include <Inventor/nodes/SoShapeHints.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoTexture2.h>
#include <Inventor/nodes/SoTextureCoordinate2.h>
#include <Inventor/nodes/SoTextureCoordinateBinding.h>
#include <Inventor/nodes/SoTexture2Transform.h>
#include <Inventor/nodes/SoTriangleStripSet.h>
#include <Inventor/errors/SoDebugError.h>
#include <Inventor/SoInput.h>
#include <Inventor/C/tidbits.h>
#include <cstring>



#define DISABLE_BACKFACE_CULLING

#define MATNAME_LENGTH 17 // 16 + \0
#define OBJNAME_LENGTH 11 // 10 + \0


// File Header Chunks
#define M3DMAGIC    0x4D4D
#define M3D_VERSION 0x0002
#define MLIBMAGIC   0x3DAA
#define CMAGIC      0xC23D

// Major Section Chunks
#define MDATA  0x3D3D
#define KFDATA 0xB000

// Viewport Control Chunks
#define VIEWPORT_LAYOUT 0x7001
#define VIEWPORT_DATA   0x7011
#define VIEWPORT_DATA_3 0x7012
#define VIEWPORT_SIZE   0x7020


// MDATA Section Chunks
// Common Chunks
#define COLOR_F 0x0010
#define COLOR_24 0x0011
#define LIN_COLOR_24 0x0012
#define LIN_COLOR_F 0x0013
#define INT_PERCENTAGE 0x0030
#define FLOAT_PERCENTAGE 0x0031

// Section Settings Chunks
#define MESH_VERSION 0x3D3E
#define MASTER_SCALE 0x0100
#define LO_SHADOW_BIAS 0x1400
#define HI_SHADOW_BIAS 0x1410
#define SHADOW_MAP_SIZE 0x1420
#define SHADOW_SAMPLES 0x1430
#define SHADOW_RANGE 0x1440
#define SHADOW_FILTER 0x1450
#define RAY_BIAS 0x1460
#define O_CONSTS 0x1500
#define AMBIENT_LIGHT 0x2100

// Background Settings Chunks
#define BIT_MAP 0x1100
#define SOLID_BGND 0x1200
#define V_GRADIENT 0x1300
#define USE_BIT_MAP 0x1101
#define USE_SOLID_BGND 0x1201
#define USE_V_GRADIENT 0x1301

// Atmosphere Settings Chunks
#define FOG 0x2200
#define FOG_BGND 0x2210
#define LAYER_FOG 0x2302
#define DISTANCE_CUE 0x2300
#define DCUE_BGND 0x2310
#define USE_FOG 0x2201
#define USE_LAYER_FOG 0x2303
#define USE_DISTANCE_CUE 0x2301

// Viewport Chunks
#define DEFAULT_VIEW 0x3000
#define VIEW_TOP 0x3010
#define VIEW_BOTTOM 0x3020
#define VIEW_LEFT 0x3030
#define VIEW_RIGHT 0x3040
#define VIEW_FRONT 0x3050
#define VIEW_BACK 0x3060
#define VIEW_USER 0x3070
#define VIEW_CAMERA 0x3080

// Materials Chunks
#define MAT_ENTRY 0xAFFF
#define MAT_NAME 0xA000
#define MAT_AMBIENT 0xA010
#define MAT_DIFFUSE 0xA020
#define MAT_SPECULAR 0xA030
#define MAT_SHININESS 0xA040
#define MAT_SHIN2PCT 0xA041
#define MAT_TRANSPARENCY 0xA050
#define MAT_XPFALL 0xA052
#define MAT_USE_XPFALL 0xA240
#define MAT_REFBLUR 0xA053
#define MAT_SHADING 0xA100
#define MAT_USE_REFBLUR 0xA250
#define MAT_SELF_ILLUM 0xA080
#define MAT_TWO_SIDE 0xA081
#define MAT_DECAL 0xA082
#define MAT_ADDITIVE 0xA083
#define MAT_WIRE 0xA085
#define MAT_FACEMAP 0xA088
#define MAT_PHONGSOFT 0xA08C
#define MAT_WIREABS 0xA08E
#define MAT_WIRESIZE 0xA087
#define MAT_TEXMAP 0xA200
#define MAT_SXP_TEXT_DATA 0xA320
#define MAT_TEXMASK 0xA3EH
#define MAT_SXP_TEXTMASK_DATA 0xA32A
#define MAT_TEX2MAP 0xA33A
#define MAT_SXP_TEXT2_DATA 0xA321
#define MAT_TEX2MASK 0xA340H
#define MAT_SXP_TEXT2MASK_DATA 0xA32C
#define MAT_OPACMAP 0xA210
#define MAT_SXP_OPAC_DATA 0xA322
#define MAT_OPACMASK 0xA342
#define MAT_SXP_OPACMASK_DATA 0xA32E
#define MAT_BUMPMAP 0xA230
#define MAT_SXP_BUMP_DATA 0xA324
#define MAT_BUMPMASK 0xA344
#define MAT_SXP_BUMPMASK_DATA 0xA330
#define MAT_SPECMAP 0xA204
#define MAT_SXP_SPEC_DATA 0xA325
#define MAT_SPECMASK 0xA348
#define MAT_SXP_SPECMASK_DATA 0xA332
#define MAT_SHINMAP 0xA33C
#define MAT_SXP_SHIN_DATA 0xA326
#define MAT_SHINMASK 0xA346
#define MAT_SXP_SHINMASK_DATA 0xA334
#define MAT_SELFIMAP 0xA33D
#define MAT_SXP_SELFI_DATA 0xA328
#define MAT_SELFIMASK 0xA34A
#define MAT_SXP_SELFIMASK_DATA 0xA336
#define MAT_REFLMAP 0xA220
#define MAT_REFLMASK 0xA34C
#define MAT_SXP_REFLMASK_DATA 0xA338
#define MAT_ACUBIC 0xA310
#define MAT_MAPNAME 0xA300
#define MAT_MAP_TILING 0xA351
#define MAT_MAT_TEXBLUR 0xA353
#define MAT_MAP_USCALE 0xA354
#define MAT_MAP_VSCALE 0xA356
#define MAT_MAP_UOFFSET 0xA358
#define MAT_MAP_VOFFSET 0xA35A
#define MAT_MAP_ANG 0xA35C
#define MAT_MAP_COL1 0xA360
#define MAT_MAP_COL2 0xA362
#define MAT_MAP_RCOL 0xA364
#define MAT_MAP_GCOL 0xA366
#define MAT_MAP_BCOL 0xA368

// Object Chunks
#define NAMED_OBJECT 0x4000
#define N_TRI_OBJECT 0x4100
#define POINT_ARRAY 0x4110
#define POINT_FLAG_ARRAY 0x4111
#define FACE_ARRAY 0x4120
#define MSH_MAT_GROUP 0x4130
#define SMOOTH_GROUP 0x4150
#define MSH_BOXMAP 0x4190
#define TEX_VERTS 0x4140
#define MESH_MATRIX 0x4160
#define MESH_COLOR 0x4165
#define MESH_TEXTURE_INFO 0x4170
#define PROC_NAME 0x4181
#define PROC_DATA 0x4182
#define N_DIRECT_LIGHT 0x4600
#define DL_OFF 0x4620
#define DL_OUTER_RANGE 0x465A
#define DL_INNER_RANGE 0x4659
#define DL_MULTIPLIER 0x465B
#define DL_EXCLUDE 0x4654
#define DL_ATTENUATE 0x4625
#define DL_SPOTLIGHT 0x4610
#define DL_SPOT_ROLL 0x4656
#define DL_SHADOWED 0x4630
#define DL_LOCAL_SHADOW2 0x4641
#define DL_SEE_CONE 0x4650
#define DL_SPOT_RECTANGULAR 0x4651
#define DL_SPOT_ASPECT 0x4657
#define DL_SPOT_PROJECTOR 0x4653
#define DL_SPOT_OVERSHOOT 0x4652
#define DL_RAY_BIAS 0x4658
#define DL_RAYSHAD 0x4627
#define N_CAMERA 0x4700
#define CAM_SEE_CONE 0x4710
#define CAM_RANGES 0x4720
#define OBJ_HIDDEN 0x4010
#define OBJ_VIS_LOFTER 0x4011
#define OBJ_DOESNT_CAST 0x4012
#define OBJ_DONT_RCVSHADOW 0x4017
#define OBJ_MATTE 0x4013
#define OBJ_FAST 0x4014
#define OBJ_PROCEDURAL 0x4015
#define OBJ_FROZEN 0x4016




struct tagFace;
struct tagFaceGroup;
struct tagMaterial;
struct tagContext;


typedef struct {
  SbVec3f point;
  SbVec2f texturePoint;
  SbList<tagFace*> faceList;

  SbVec3f getNormal(tagContext *con, uint16_t myIndex) const;
} Vertex;

typedef struct tagFace {
  uint16_t v1,v2,v3;
  uint16_t flags;
  tagFaceGroup *faceGroup;
  uint32_t e12,e23,e31;
  SbBool isDegenerated;

  SbVec3f getNormal(tagContext *con) const;
  float getAngle(tagContext *con, uint16_t vertexIndex) const;
  SbVec3f getWeightedNormal(tagContext *con, uint16_t vertexIndex) const;
  void init(tagContext *con, uint16_t a, uint16_t b, uint16_t c, uint16_t f);
} Face;

typedef struct tagFaceGroup {
  tagMaterial *mat;

  SbList<Face*> faceList;
  uint16_t numDegFaces;

  SbBool hasTexture2(tagContext *con);
  SoTexture2* getSoTexture2(tagContext *con);
  SbBool hasTexture2Transform(tagContext *con);
  SoTexture2Transform* getSoTexture2Transform(tagContext *con);
  SoMaterial* getSoMaterial(tagContext *con);
  SoNormal* createSoNormal(tagContext *con);

  SoCoordinate3* createSoCoordinate3_n(tagContext *con);
  SoTextureCoordinate2* createSoTextureCoordinate2_n(tagContext *con);
  SoTriangleStripSet* createSoTriStripSet_n(tagContext *con);

  SoIndexedTriangleStripSet* createSoIndexedTriStripSet_i(tagContext *con);
} FaceGroup;

// FIXME mortene: don't use "namespace"
namespace DefaultFaceGroup {
  static tagMaterial* getMaterial(tagContext *con);
  static SbBool isEmpty(tagContext *con);

  static SoMaterial* getSoMaterial(tagContext *con);
  static SoNormal* createSoNormal(tagContext *con);

  static SoCoordinate3* createSoCoordinate3_n(tagContext *con);
  static SoTriangleStripSet* createSoTriStripSet_n(tagContext *con);

  static SoIndexedTriangleStripSet* createSoIndexedTriStripSet_i(tagContext *con);
}

typedef struct {
  SbList<Face*> faceList;
} Edge;

typedef struct tagMaterial {
  SbString name;
  SbColor ambient;
  SbColor diffuse;
  SbColor specular;
  float shininess;
  float transparency;
  SbString textureName;
  float uscale;
  float vscale;
  float uoffset;
  float voffset;
  SbBool twoSided;

  SoMaterial *matCache;
  SoTexture2 *texture2Cache;
  SoTexture2Transform *texture2TransformCache;

  void updateSoMaterial(int index, SoMaterial *m);
  SoMaterial* getSoMaterial(tagContext *con);
  SbBool hasTexture2(tagContext *con);
  SoTexture2* getSoTexture2(tagContext *con);
  SbBool hasTexture2Transform(tagContext *con);
  SoTexture2Transform* getSoTexture2Transform(tagContext *con);

  tagMaterial() : matCache(NULL), texture2Cache(NULL),
                  texture2TransformCache(NULL)  {}
  ~tagMaterial()  {
    if (matCache) matCache->unref();
    if (texture2Cache) texture2Cache->unref();
    if (texture2TransformCache) texture2TransformCache->unref();
  }
} Material;



typedef struct tagContext {
  // flags "What to load"
  int appendNormals;
  SbBool loadMaterials;
  SbBool loadTextures;
  SbBool loadObjNames;
  SbBool useIndexedTriSet;
  SbBool centerModel;

  // basic loading stuff
  SoStream &s;
  size_t stopPos;
  SoSeparator *root;
  SoSeparator *cObj;
  SbBool minMaxValid;
  float minX,maxX;
  float minY,maxY;
  float minZ,maxZ;
  char objectName[OBJNAME_LENGTH];
  int totalVertices;
  int totalFaces;

  // material stuff
  SbList<FaceGroup*> faceGroupList;
  SbList<Material*> matList;
  Material defaultMat;
  Material *cMat;
  SbColor cColor;
  float cColorFloat;
  SbBool textureCoordsFound;

  // geometry stuff
  Vertex *vertexList;
  uint16_t numVertices;
  Face *faceList;
  uint16_t numFaces;
  uint16_t numDefaultDegFaces;
  Edge *edgeList;
  uint32_t numEdges;

  // scene graph generator stuff
  SoTexture2 *genCurrentTexture;
  SoMaterial *genCurrentMaterial;
  SoTexture2Transform *genCurrentTexTransform;
  int genTwoSided;
  // multiple-time used nodes
  SoTexture2 *genEmptyTexture;
  SoTexture2Transform *genEmptyTexTransform;
  SoShapeHints *genOneSidedHints;
  SoShapeHints *genTwoSidedHints;


  SoCoordinate3* createSoCoordinate3_i(tagContext *con) const;
  SoTextureCoordinate2* createSoTextureCoordinate2_i(tagContext *con) const;

  SoTexture2* genGetEmptyTexture();
  SoTexture2Transform* genGetEmptyTexTransform();
  SoShapeHints* genGetOneSidedHints();
  SoShapeHints* genGetTwoSidedHints();

  tagContext(SoStream &stream) : s(stream), root(NULL), cObj(NULL),
      totalVertices(0), totalFaces(0),
      vertexList(NULL), faceList(NULL),
      genEmptyTexture(NULL), genEmptyTexTransform(NULL),
      genOneSidedHints(NULL), genTwoSidedHints(NULL)  {}
  ~tagContext() {
      for (int i=matList.getLength()-1; i>=1; i--)
        delete matList[i];

      if (genEmptyTexture)  genEmptyTexture->unref();
      if (genEmptyTexTransform)  genEmptyTexTransform->unref();
      if (genOneSidedHints)  genOneSidedHints->unref();
      if (genTwoSidedHints)  genTwoSidedHints->unref();

      assert(!root && !cObj && !vertexList && !faceList &&
          "You forgot to free some memory.");
  }
} Context;



#define CHUNK_DECL(_name_)  static void _name_(Context *con)

CHUNK_DECL(SkipChunk);
CHUNK_DECL(LoadNamedObject);
CHUNK_DECL(LoadNTriObject);
CHUNK_DECL(LoadPointArray);
CHUNK_DECL(LoadFaceArray);
CHUNK_DECL(LoadMshMatGroup);
CHUNK_DECL(LoadTexVerts);
CHUNK_DECL(LoadMatEntry);
CHUNK_DECL(LoadMatName);
CHUNK_DECL(LoadMatAmbient);
CHUNK_DECL(LoadMatDiffuse);
CHUNK_DECL(LoadMatSpecular);
CHUNK_DECL(LoadShininess);
CHUNK_DECL(LoadTransparency);
CHUNK_DECL(LoadMatTwoSide);
CHUNK_DECL(LoadColor24);
CHUNK_DECL(LoadLinColor24);
CHUNK_DECL(LoadIntPercentage);
CHUNK_DECL(LoadFloatPercentage);
CHUNK_DECL(LoadM3DMagic);
CHUNK_DECL(LoadM3DVersion);
CHUNK_DECL(LoadMData);
CHUNK_DECL(LoadMeshVersion);
CHUNK_DECL(LoadTexMap);
CHUNK_DECL(LoadMapName);
CHUNK_DECL(LoadMapUScale);
CHUNK_DECL(LoadMapVScale);
CHUNK_DECL(LoadMapUOffset);
CHUNK_DECL(LoadMapVOffset);
static int coin_debug_3ds();

static SbBool
read3dsFile(SoStream *in, SoSeparator *&root,
            int appendNormals, float COIN_UNUSED_ARG(creaseAngle),
            SbBool loadMaterials, SbBool loadTextures,
            SbBool loadObjNames, SbBool indexedTriSet,
            SbBool centerModel, float modelSize)
{
  // read the stream header
  uint16_t header;
  *in >> header;
  if (header != M3DMAGIC) {
    SoDebugError::post("read3dsFile",
                       "Bad 3ds stream: invalid header.");
    return FALSE;
  }

  // prepare Context structure
  Context con(*in);
  con.stopPos = 0;
  con.root = new SoSeparator;
  con.root->ref();
  con.minMaxValid = FALSE;

  // customize loader
  if (appendNormals >= 2)  appendNormals = 1; // per-vertex normals are
                                              // not currently supported
  con.appendNormals = appendNormals;
  con.loadMaterials = loadMaterials;
  con.loadTextures = loadTextures;
  con.loadObjNames = loadObjNames;
  con.useIndexedTriSet = indexedTriSet;
  con.centerModel = centerModel;

  // initialize materials and prepare default one
  con.matList.append(&con.defaultMat);
  // FIXME: the values for the default material are guessed one (maybe completely wrong)
  con.defaultMat.ambient = SbColor(0.6f, 0.6f, 0.6f);
  con.defaultMat.diffuse = SbColor(0.8f, 0.8f, 0.8f);
  con.defaultMat.specular = SbColor(0.f, 0.f, 0.f);
  con.defaultMat.shininess = 0.f;
  con.defaultMat.transparency = 0.f;
  con.defaultMat.twoSided = TRUE;  // FIXME: is default material double sided?
  con.defaultMat.matCache = new SoMaterial;
  con.defaultMat.matCache->ref();
  con.defaultMat.updateSoMaterial(0, con.defaultMat.matCache);
  con.cMat = 0;

  // build base scene graph

#ifdef DISABLE_BACKFACE_CULLING // put default shape hints into the scene;
          // Backface culling is set to off and two sided lighting is enabled.
  SoShapeHints *sh = new SoShapeHints;
  sh->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE;
  sh->shapeType = SoShapeHints::UNKNOWN_SHAPE_TYPE;
  con.root->addChild(sh);
#endif

  // center model and scale it
  SoMatrixTransform *matrix = NULL;
  if (con.centerModel || modelSize != 0.f) {
    matrix = new SoMatrixTransform;
    con.root->addChild(matrix);
  }

  if (con.loadMaterials) {
    // material binding
    SoMaterialBinding *mb = new SoMaterialBinding;
    mb->value.setValue(SoMaterialBinding::OVERALL);
    con.root->addChild(mb);
  }

  if (con.appendNormals) {
    // normal binding
    SoNormalBinding *nb = new SoNormalBinding;
    if (con.appendNormals == 1)
      nb->value.setValue(SoNormalBinding::PER_FACE);
    else
      nb->value.setValue(SoNormalBinding::PER_VERTEX);
    con.root->addChild(nb);
  }

  if (con.loadTextures) {
    SoTextureCoordinateBinding *tb = new SoTextureCoordinateBinding;
    if (con.useIndexedTriSet)
      tb->value.setValue(SoTextureCoordinateBinding::PER_VERTEX_INDEXED);
    else
      tb->value.setValue(SoTextureCoordinateBinding::PER_VERTEX);
    con.root->addChild(tb);
  }


  // read the stream
  LoadM3DMagic(&con);

  // handle errors
  if (con.s.isBad()) {
    con.root->unref();
    con.root = NULL;

    SoDebugError::post("read3dsFile",
                       "3ds loading failed.");
    return FALSE;
  }

  if (con.centerModel || modelSize != 0.f) {
    SbMatrix m;

    // center model
    if (con.centerModel)
      m.setTranslate(SbVec3f(-(con.maxX+con.minX)/2.f, -(con.maxY+con.minY)/2.f,
                             -(con.maxZ+con.minZ)/2.f));
    else
      m.makeIdentity();

    // set model size
    if (modelSize != 0.f) {
      SbMatrix m2;
      float max = SbMax(SbMax(con.maxX-con.minX, con.maxY-con.minY), con.maxZ-con.minZ);
      m2.setScale(modelSize/max);
      m.multRight(m2);
    }

    matrix->matrix.setValue(m);
  }

  // return root
  con.root->unrefNoDelete();
  root = con.root;
  con.root = NULL;

  // debug info
  if (coin_debug_3ds() >= 2)
    SoDebugError::postInfo("read3dsFile",
                           "File loading ok. Loaded %i vertices and %i faces.",
                           con.totalVertices, con.totalFaces);

  return TRUE;
}




#define CHUNK(_name_) CHUNK_DECL(_name_)

#define HEADER \
  if (con->s.isBad()) \
    return; \
 \
  uint32_t size; \
  con->s >> size;

#define FULLHEADER \
  HEADER \
  uint32_t cpos = static_cast<uint32_t>(con->s.getPos()); \
  uint32_t stopPos = cpos + size - 6;




#define READ_SUBCHUNKS(_subChunkSwitch_) \
while (con->s.getPos() < stopPos) { \
  uint16_t chid; \
  con->s >> chid; \
  if (con->s.isBad()) \
    break; \
 \
  switch (chid) { \
    _subChunkSwitch_; \
  default: \
    SkipChunk(con); \
  }; \
}





CHUNK(SkipChunk)
{
  FULLHEADER;

  // move on the position of the next chunk
  con->s.setPos(stopPos);
}



CHUNK(LoadM3DMagic)
{
  FULLHEADER;
  con->stopPos = con->s.getPos() + size - 6;

  if (coin_debug_3ds() >= 2)
    SoDebugError::postInfo("LoadM3DMagic",
                           "Loading 3ds stream (stream size: %i).", size);

  READ_SUBCHUNKS(
    case M3D_VERSION: LoadM3DVersion(con); break;
    case MDATA:       LoadMData(con); break;
    case KFDATA:      SkipChunk(con); break;
  )
}



CHUNK(LoadM3DVersion)
{
  HEADER;

  int32_t version;
  con->s >> version;

  if (version != 3 && coin_debug_3ds() >= 1)
    SoDebugError::postWarning("LoadM3DVersion",
                              "Non-standard 3ds format version: %i.", version);
}



CHUNK(LoadMData)
{
  FULLHEADER;

  READ_SUBCHUNKS(
    case MESH_VERSION: LoadMeshVersion(con); break;
    case MAT_ENTRY:    LoadMatEntry(con); break;
    case NAMED_OBJECT: LoadNamedObject(con); break;
  )
}



CHUNK(LoadMeshVersion)
{
  HEADER;

  int32_t version;
  con->s >> version;

  if (coin_debug_3ds() >= 3)
    SoDebugError::postInfo("LoadMeshVersion",
                           "The 3ds file version: %i.", version);
}



CHUNK(LoadNamedObject)
{
  FULLHEADER;

  assert(!con->cObj && "Forgot to free the current object.");

  // read object name
  con->s.readZString(con->objectName, OBJNAME_LENGTH);

  if (coin_debug_3ds() >= 3)
    SoDebugError::postInfo("LoadNamesObject",
                           "Name: %s.", con->objectName);

  READ_SUBCHUNKS(
    case N_TRI_OBJECT:  LoadNTriObject(con); break;
  )

  if (con->cObj) {
    // set object name
    if (con->loadObjNames && strlen(con->objectName) > 0)
      con->cObj->setName(con->objectName);

    // add cObj to the main scene graph
    if (con->cObj->getNumChildren() > 0)
      con->root->addChild(con->cObj);
    con->cObj->unref();
    con->cObj = NULL;
  }
}



CHUNK(LoadNTriObject)
{
  FULLHEADER;

  assert(con->faceGroupList.getLength() == 0);
  con->numVertices = 0;
  con->numFaces = 0;
  con->numDefaultDegFaces = 0;
  con->textureCoordsFound = FALSE;

  if (coin_debug_3ds() >= 3)
    SoDebugError::postInfo("LoadNTriObject",
                           "About to load");

  READ_SUBCHUNKS(
    case POINT_ARRAY:       LoadPointArray(con); break;
    case POINT_FLAG_ARRAY:  SkipChunk(con); break;
    case FACE_ARRAY:        LoadFaceArray(con); break;
    case MSH_MAT_GROUP:     LoadMshMatGroup(con); break;
    case TEX_VERTS:         LoadTexVerts(con); break;
    case MESH_MATRIX:       SkipChunk(con); break;
    case MESH_COLOR:        SkipChunk(con); break;
    case MESH_TEXTURE_INFO: SkipChunk(con); break;
    case PROC_NAME:         SkipChunk(con); break;
    case PROC_DATA:         SkipChunk(con); break;
  )

  // debug info
  if (coin_debug_3ds() >= 3)
    SoDebugError::postInfo("LoadNTriObject",
                           "Object %s parsed - vertices: %i, faces %i.", &con->objectName, con->numVertices, con->numFaces);
  con->totalVertices += con->numVertices;
  con->totalFaces += con->numFaces;

  // create object separator
  con->cObj = new SoSeparator;
  con->cObj->ref();

  con->genCurrentTexture = NULL;
  con->genCurrentTexTransform = NULL;
  con->genCurrentMaterial = NULL;
  con->genTwoSided = -1;

  // create coordinates (in indexed mode)
  if (con->useIndexedTriSet) {
    // coordinates
    con->cObj->addChild(con->createSoCoordinate3_i(con));
    // texture coordinates
    if (con->textureCoordsFound && con->loadTextures)
      con->cObj->addChild(con->createSoTextureCoordinate2_i(con));
  }

  // create "default material" scene
  if (!DefaultFaceGroup::isEmpty(con)) {

    // default material has not a texture => switch it off
#if 0 // non-optimized version
    if (con->loadTextures)
      con->cObj->addChild(new SoTexture2);
#else
    if (con->loadTextures) {
      SoTexture2 *t = con->genGetEmptyTexture();
      if (t != con->genCurrentTexture) {
        con->genCurrentTexture = t;
        con->cObj->addChild(t);
      }
      SoTexture2Transform *tt = con->genGetEmptyTexTransform();
      if (tt != con->genCurrentTexTransform) {
        con->genCurrentTexTransform = tt;
        con->cObj->addChild(tt);
      }
    }
#endif

    // materials
#if 0 // non-optimized version
    if (con->loadMaterials)
      con->cObj->addChild(DefaultFaceGroup::getSoMaterial(con));
#else
    if (con->loadMaterials) {
      SoMaterial *m = DefaultFaceGroup::getSoMaterial(con);
      if (m != con->genCurrentMaterial) {
        con->genCurrentMaterial = m;
        con->cObj->addChild(con->genCurrentMaterial);
      }
    }
#endif

    // normals
    if (con->appendNormals)
      con->cObj->addChild(DefaultFaceGroup::createSoNormal(con));

#ifndef DISABLE_BACKFACE_CULLING
    // single x double faces
    SbBool matTwoSided = DefaultFaceGroup::getMaterial(con)->twoSided;
    if (con->genTwoSided == -1 || (matTwoSided && (con->genTwoSided == 0)) ||
       (!matTwoSided && (con->genTwoSided == 1))) {
      con->genTwoSided = (DefaultFaceGroup::getMaterial(con)->twoSided ? 1 : 0);
      con->cObj->addChild((con->genTwoSided == 1) ?
          con->genGetTwoSidedHints() : con->genGetOneSidedHints());
    }
#endif

    // load default material geometry
    if (con->useIndexedTriSet) {
      // indexed triStripSet
      con->cObj->addChild(DefaultFaceGroup::createSoIndexedTriStripSet_i(con));
    } else {
      // coordinates
      con->cObj->addChild(DefaultFaceGroup::createSoCoordinate3_n(con));
      // triStripSet
      con->cObj->addChild(DefaultFaceGroup::createSoTriStripSet_n(con));
    }
  }

  // create non-default materials scene
  for (int i=0; i<con->faceGroupList.getLength(); i++) {
    FaceGroup *fg = con->faceGroupList[i];

    // materials
#if 0 // non-optimized version
    if (con->loadMaterials)
      con->cObj->addChild(fg->getSoMaterial(con));
#else
    if (con->loadMaterials) {
      SoMaterial *m = fg->getSoMaterial(con);
      if (m != con->genCurrentMaterial) {
        con->genCurrentMaterial = m;
        con->cObj->addChild(m);
      }
    }
#endif

    // normals
    if (con->appendNormals)
      con->cObj->addChild(fg->createSoNormal(con));

    // textures - optimized code
    if (con->loadTextures) {
      if (fg->hasTexture2(con)) {
        SoTexture2 *t = fg->getSoTexture2(con);
        if (t != con->genCurrentTexture) {
          con->genCurrentTexture = t;
          con->cObj->addChild(t);
        }
      } else {
        SoTexture2 *t = con->genGetEmptyTexture();
        if (t != con->genCurrentTexture) {
          con->genCurrentTexture = t;
          con->cObj->addChild(t);
        }
      }
      // texture transform - optimized code
      if (fg->hasTexture2Transform(con)) {
        SoTexture2Transform *tt = fg->getSoTexture2Transform(con);
        if (tt != con->genCurrentTexTransform) {
          con->genCurrentTexTransform = tt;
          con->cObj->addChild(tt);
        }
      } else {
        SoTexture2Transform *tt = con->genGetEmptyTexTransform();
        if (tt != con->genCurrentTexTransform) {
          con->genCurrentTexTransform = tt;
          con->cObj->addChild(tt);
        }
      }
    }

#ifndef DISABLE_BACKFACE_CULLING
    // single x double faces
    if (con->genTwoSided == -1 || (fg->mat->twoSided && (con->genTwoSided == 0)) ||
       (!fg->mat->twoSided && (con->genTwoSided == 1))) {
      con->genTwoSided = (fg->mat->twoSided ? 1 : 0);
      con->cObj->addChild((con->genTwoSided == 1) ?
          con->genGetTwoSidedHints() : con->genGetOneSidedHints());
    }
#endif

    if (con->useIndexedTriSet) {
      // indexed triStripSet
      con->cObj->addChild(fg->createSoIndexedTriStripSet_i(con));
    } else {
      // coordinates
      con->cObj->addChild(fg->createSoCoordinate3_n(con));
      // textures
      if (con->loadTextures && fg->hasTexture2(con))
        con->cObj->addChild(fg->createSoTextureCoordinate2_n(con));
      // triStripSet
      con->cObj->addChild(fg->createSoTriStripSet_n(con));
    }
  }

  // clean up memory
  delete[] con->vertexList;
  con->vertexList = NULL;
  delete[] con->faceList;
  con->faceList = NULL;
  for (int j=con->faceGroupList.getLength()-1; j>=0; j--) {
    delete con->faceGroupList[j];
    con->faceGroupList.removeFast(j);
  }
}



CHUNK(LoadPointArray)
{
  HEADER;

  if (coin_debug_3ds() >= 3)
    SoDebugError::postInfo("LoadPointArray",
                           "Begin");


  // number of vertices
  uint16_t num;
  con->s >> num;

  // alloc memory for Vertices
  assert(con->vertexList == NULL && "Forgot to free memory.");
  con->vertexList = new Vertex[num];
  con->numVertices = num;

  // read points
  float x,y,z;
  for (int i=0; i<num; i++) {
    con->s >> x;
    con->s >> y;
    con->s >> z;
    con->vertexList[i].point = SbVec3f(x,z,-y); // 3ds has different
        //coordinate system. Z is up, and Y goes into the scene.
  }
}



CHUNK(LoadFaceArray)
{
  FULLHEADER;

  if (coin_debug_3ds() >= 3)
    SoDebugError::postInfo("LoadFaceArray",
                           "Begin");

  // number of faces
  uint16_t num;
  con->s >> num;

  // alloc memory for Faces
  assert(con->faceList == NULL && "Forgot to free memory.");
  con->faceList = new Face[num];
  con->numFaces = num;

  // alloc memory for edges
  if (con->appendNormals == 2) {
    con->numEdges = 0;
    con->edgeList = new Edge[num*3];
  }

  // make sure vertices are present yet
  if (num > 0) {
    if (con->vertexList == NULL) {
      assert(FALSE && "Vertex list not present.");
      con->s.setBadBit();
      return;
    }
  }

  // read Faces
  uint16_t a,b,c;
  uint16_t flags;
  for (int i=0; i<num; i++) {
    con->s >> a;
    con->s >> b;
    con->s >> c;
    con->s >> flags; // STUB: decode flags (edge visibility and texture
                     // wrapping, but first get idea what's the flags meaning)
    if (flags != 7 && coin_debug_3ds() >= 2)
      SoDebugError::postWarning("LoadFaceArray",
                                "Non-standard face flags: %x, investigate it.\n", flags);
    con->faceList[i].init(con, a,c,b,flags); // we have to swap two
                     // indices (b<=>c); It looks like 3ds uses clockwise
                     // vertex ordering and we need it to be counter-clockwise.

    if (!con->minMaxValid) {
      con->minMaxValid = TRUE;
      con->minX = con->maxX = con->vertexList[a].point[0];
      con->minY = con->maxY = con->vertexList[a].point[1];
      con->minZ = con->maxZ = con->vertexList[a].point[2];
    }
    #define PROCESS_VERTEX(_abc_, _xyz_, _index_) \
      if (con->min##_xyz_ > con->vertexList[_abc_].point[_index_]) \
        con->min##_xyz_ = con->vertexList[_abc_].point[_index_]; \
      else \
        if (con->max##_xyz_ < con->vertexList[_abc_].point[_index_]) \
          con->max##_xyz_ = con->vertexList[_abc_].point[_index_]

    PROCESS_VERTEX(a, X, 0);
    PROCESS_VERTEX(a, Y, 1);
    PROCESS_VERTEX(a, Z, 2);
    PROCESS_VERTEX(b, X, 0);
    PROCESS_VERTEX(b, Y, 1);
    PROCESS_VERTEX(b, Z, 2);
    PROCESS_VERTEX(c, X, 0);
    PROCESS_VERTEX(c, Y, 1);
    PROCESS_VERTEX(c, Z, 2);
    #undef PROCESS_VERTEX
  }

  // report degenerated faces
  if (con->numDefaultDegFaces > 0 && coin_debug_3ds() >= 1)
    SoDebugError::postWarning("LoadFaceArray",
                              "There are %i degenerated faces in the object named \"%s\" - removing them.",
                              con->numDefaultDegFaces, &con->objectName);

  READ_SUBCHUNKS(
    case MSH_MAT_GROUP: LoadMshMatGroup(con); break;
    case SMOOTH_GROUP:  SkipChunk(con); break;
    case MSH_BOXMAP:    SkipChunk(con); break;
  )
}



CHUNK(LoadMshMatGroup)
{
  FULLHEADER;

  if (coin_debug_3ds() >= 4)
    SoDebugError::postInfo("LoadMatGroup",
                           "Begin");

  if (!con->loadMaterials && !con->loadTextures) {
    // move on the position of the next chunk
    con->s.setPos(stopPos);
    return;
  }

  // material name
  char materialName[MATNAME_LENGTH];
  con->s.readZString(materialName, MATNAME_LENGTH);
  int matIndex;
  for (matIndex=1; matIndex<con->matList.getLength(); matIndex++) {
    if (strcmp(con->matList[matIndex]->name.getString(), materialName) == 0)
      break;
  }
  if (matIndex == con->matList.getLength()) {
    assert(FALSE && "Wrong material name in the file.");
    con->s.setBadBit();
    return;
  }

  // create FaceGroup
  FaceGroup *mm = new FaceGroup;
  mm->mat = con->matList[matIndex];
  con->faceGroupList.append(mm);
  mm->numDegFaces = 0;

  // number of faces
  uint16_t num;
  con->s >> num;

  // make sure faces are present yet
  if (num > 0) {
    if (con->faceList == NULL) {
      assert(FALSE && "Face list not present.");
      con->s.setBadBit();
      return;
    }
  }

  // face indexes
  uint16_t faceMatIndex;
  for (int i=0; i<num; i++) {
    con->s >> faceMatIndex;
    if (faceMatIndex < con->numFaces) {
      assert(con->faceList[faceMatIndex].faceGroup == NULL &&
             "3ds file error: Two materials on one face.");
      con->faceList[faceMatIndex].faceGroup = mm;
      mm->faceList.append(&con->faceList[faceMatIndex]);
      if (con->faceList[faceMatIndex].isDegenerated) {
        mm->numDegFaces++;
        con->numDefaultDegFaces--;
      }
    } else {
      assert(FALSE && "Wrong face material index.");
      con->s.setBadBit();
      return;
    }
  }
}



CHUNK(LoadTexVerts)
{
  HEADER;

  if (coin_debug_3ds() >= 4)
    SoDebugError::postInfo("LoadTexVerts",
                           "Begin");

  con->textureCoordsFound = TRUE;

  // number of faces
  uint16_t num;
  con->s >> num;

  // make sure vertices are present yet
  if (num > 0) {
    if (con->vertexList == NULL) {
      assert(FALSE && "Vertex list not present.");
      con->s.setBadBit();
      return;
    }
  }

  // texture coordinates
  float u;
  float v;
  for (int i=0; i<num; i++) {
    con->s >> u;
    con->s >> v;
    con->vertexList[i].texturePoint = SbVec2f(u,v);
  }
}



CHUNK(LoadMatEntry)
{
  FULLHEADER;

  if (coin_debug_3ds() >= 4)
    SoDebugError::postInfo("LoadMatEntry",
                           "Begin");

  if (!con->loadMaterials && !con->loadTextures) {
    // move on the position of the next chunk
    con->s.setPos(stopPos);
    return;
  }

  assert(con->cMat == NULL);
  con->cMat = new Material;
  con->matList.append(con->cMat);

  // default values
  con->cMat->name = "";
  con->cMat->ambient = SbColor(0.f, 0.f, 0.f);
  con->cMat->diffuse = SbColor(0.f, 0.f, 0.f);
  con->cMat->specular = SbColor(0.f, 0.f, 0.f);
  con->cMat->shininess = 0.f;
  con->cMat->transparency = 0.f;
  con->cMat->twoSided = FALSE;

  READ_SUBCHUNKS(
    case MAT_NAME:     LoadMatName(con); break;
    case MAT_AMBIENT:  LoadMatAmbient(con); break;
    case MAT_DIFFUSE:  LoadMatDiffuse(con); break;
    case MAT_SPECULAR: LoadMatSpecular(con); break;
    case MAT_SHININESS: LoadShininess(con); break;
    case MAT_TRANSPARENCY: LoadTransparency(con); break;
    case MAT_TWO_SIDE: LoadMatTwoSide(con); break;
    case MAT_TEXMAP:   LoadTexMap(con); break;
  )

  // create new SoMaterial
  con->cMat->matCache = new SoMaterial;
  con->cMat->matCache->ref();
  con->cMat->updateSoMaterial(0, con->cMat->matCache);

  con->cMat = NULL;
}



CHUNK(LoadMatName)
{
  HEADER;

  if (coin_debug_3ds() >= 4)
    SoDebugError::postInfo("LoadMatName",
                           "Begin");

  char materialName[MATNAME_LENGTH];
  con->s.readZString(materialName, MATNAME_LENGTH);

  con->cMat->name = materialName;
}



CHUNK(LoadMatAmbient)
{
  FULLHEADER;

  if (coin_debug_3ds() >= 4)
    SoDebugError::postInfo("LoadMatAmbient",
                           "Begin");

  READ_SUBCHUNKS(
    case COLOR_24:     LoadColor24(con); break;
    case LIN_COLOR_24: LoadLinColor24(con); break;
  )
  con->cMat->ambient = con->cColor;
}



CHUNK(LoadMatDiffuse)
{
  FULLHEADER;

  if (coin_debug_3ds() >= 4)
    SoDebugError::postInfo("LoadMatDiffuse",
                           "Begin");

  READ_SUBCHUNKS(
    case COLOR_24:     LoadColor24(con); break;
    case LIN_COLOR_24: LoadLinColor24(con); break;
  )
  con->cMat->diffuse = con->cColor;
}



CHUNK(LoadMatSpecular)
{
  FULLHEADER;

  if (coin_debug_3ds() >= 4)
    SoDebugError::postInfo("LoadMatSpecular",
                           "Begin");

  READ_SUBCHUNKS(
    case COLOR_24:     LoadColor24(con); break;
    case LIN_COLOR_24: LoadLinColor24(con); break;
  )
  con->cMat->specular = con->cColor;
}



CHUNK(LoadShininess)
{
  FULLHEADER;

  if (coin_debug_3ds() >= 4)
    SoDebugError::postInfo("LoadMatShininesst",
                           "Begin");

  con->cColorFloat = 0.f;

  READ_SUBCHUNKS(
    case INT_PERCENTAGE: LoadIntPercentage(con); break;
    case FLOAT_PERCENTAGE: LoadFloatPercentage(con); break;
  )
  con->cMat->shininess = con->cColorFloat;
}



CHUNK(LoadMatTwoSide)
{
  HEADER;

  if (coin_debug_3ds() >= 4)
    SoDebugError::postInfo("LoadMatTwoSide",
                           "Begin");

  con->cMat->twoSided = TRUE;
}



CHUNK(LoadTransparency)
{
  FULLHEADER;

  if (coin_debug_3ds() >= 4)
    SoDebugError::postInfo("LoadTransparency",
                           "Begin");

  con->cColorFloat = 0.f;

  READ_SUBCHUNKS(
    case INT_PERCENTAGE: LoadIntPercentage(con); break;
    case FLOAT_PERCENTAGE: LoadFloatPercentage(con); break;
  )
  con->cMat->transparency = con->cColorFloat;
}



CHUNK(LoadTexMap)
{
  FULLHEADER;

  if (coin_debug_3ds() >= 4)
    SoDebugError::postInfo("LoadTexMap",
                           "Begin");

  if (!con->loadTextures) {
    // move on the position of the next chunk
    con->s.setPos(stopPos);
    return;
  }

  con->cMat->uscale = con->cMat->vscale = 1.f;
  con->cMat->uoffset = con->cMat->voffset = 0.f;

  READ_SUBCHUNKS(
    case INT_PERCENTAGE: LoadIntPercentage(con); break;
    case FLOAT_PERCENTAGE: LoadFloatPercentage(con); break;
    case MAT_MAPNAME:    LoadMapName(con); break;
    case MAT_MAP_USCALE:  LoadMapUScale(con); break;
    case MAT_MAP_VSCALE:  LoadMapVScale(con); break;
    case MAT_MAP_UOFFSET: LoadMapUOffset(con); break;
    case MAT_MAP_VOFFSET: LoadMapVOffset(con); break;
  )

  if (con->cMat->textureName.getLength() > 0) {
    SoTexture2 *t = new SoTexture2;
    t->ref();
    t->filename.setValue(con->cMat->textureName);
    t->model.setValue(SoTexture2::DECAL);
    con->cMat->texture2Cache = t;
  }

  if (con->cMat->uscale  != 1.f || con->cMat->vscale  != 1.f ||
      con->cMat->uoffset != 0.f || con->cMat->voffset != 0.f) {
    SoTexture2Transform *tt = new SoTexture2Transform;
    tt->ref();
    tt->scaleFactor.setValue(SbVec2f(con->cMat->uscale, con->cMat->vscale));
    tt->translation.setValue(SbVec2f(con->cMat->uoffset, con->cMat->voffset));
    con->cMat->texture2TransformCache = tt;
  }
}



CHUNK(LoadMapName)
{
  HEADER;

  if (coin_debug_3ds() >= 4)
    SoDebugError::postInfo("LoadMapName",
                           "Begin");

  char textureName[13];
  con->s.readZString(textureName, 13);

  con->cMat->textureName = textureName;
}



CHUNK(LoadMapUScale)
{
  HEADER;

  if (coin_debug_3ds() >= 4)
    SoDebugError::postInfo("LoadMatUScale",
                           "Begin");

  con->s >> con->cMat->uscale;
}



CHUNK(LoadMapVScale)
{
  HEADER;

  if (coin_debug_3ds() >= 4)
    SoDebugError::postInfo("LoadMapVScale",
                           "Begin");

  con->s >> con->cMat->vscale;
}



CHUNK(LoadMapUOffset)
{
  HEADER;

  if (coin_debug_3ds() >= 4)
    SoDebugError::postInfo("LoadMapUOffset",
                           "Begin");

  con->s >> con->cMat->uoffset;
}



CHUNK(LoadMapVOffset)
{
  HEADER;
  if (coin_debug_3ds() >= 4)
    SoDebugError::postInfo("LoadMapVOffset",
                           "Begin");

  con->s >> con->cMat->voffset;
}



CHUNK(LoadColor24)
{
  HEADER;

  if (coin_debug_3ds() >= 4)
    SoDebugError::postInfo("LoadColor24",
                           "Begin");

  uint8_t r,g,b;
  con->s >> r;
  con->s >> g;
  con->s >> b;

  con->cColor = SbColor(float(r)/255.f, float(g)/255.f, float(b)/255.f);
}



CHUNK(LoadLinColor24)
{
  HEADER;

  if (coin_debug_3ds() >= 4)
    SoDebugError::postInfo("LoadLinColor24",
                           "Begin");

  uint8_t r,g,b;
  con->s >> r;
  con->s >> g;
  con->s >> b;

  con->cColor = SbColor(float(r)/255.f, float(g)/255.f, float(b)/255.f);
}



CHUNK(LoadIntPercentage)
{
  HEADER;

  if (coin_debug_3ds() >= 4)
    SoDebugError::postInfo("LoadIntPerscentage",
                           "Begin");

  int16_t i;
  con->s >> i;

  con->cColorFloat = float(i)/100.f;
}



CHUNK(LoadFloatPercentage)
{
  HEADER;

  if (coin_debug_3ds() >= 4)
    SoDebugError::postInfo("LoadFloatPersentage",
                           "Begin");

  con->s >> (con->cColorFloat);
}





SbVec3f Vertex::getNormal(tagContext *con, uint16_t myIndex) const
{
  int c = this->faceList.getLength();
  SbVec3f normal(0.f,0.f,0.f);
  for (int i=0; i<c; i++)
    normal += this->faceList[i]->getWeightedNormal(con, myIndex);
  // ok to have a null vector here, it probably just means that we
  // have an empty triangle.
  (void) normal.normalize();
  return normal;
}



SbVec3f Face::getNormal(tagContext *con) const
{
  SbPlane plane(con->vertexList[v1].point, con->vertexList[v2].point,
      con->vertexList[v3].point);
  return plane.getNormal();
}



float Face::getAngle(tagContext *con, uint16_t vertexIndex) const
{
  int i1, i2;
  if (v1 == vertexIndex) {
    i1 = v2; i2 = v3;
  } else {
    i1 = v1;
    if (v2 == vertexIndex) i2 = v3;
    else {
      assert(v3 == vertexIndex);
      i2 = v2;
    }
  }

  SbVec3f vec1(con->vertexList[i1].point - con->vertexList[vertexIndex].point);
  SbVec3f vec2(con->vertexList[i2].point - con->vertexList[vertexIndex].point);

  SbRotation rot(vec1, vec2);
  float value;
  rot.getValue(vec1, value);
  return value;
}



SbVec3f Face::getWeightedNormal(tagContext *con, uint16_t vertexIndex) const
{
  return getNormal(con) * getAngle(con, vertexIndex);
}



void Face::init(tagContext *con, uint16_t a, uint16_t b, uint16_t c, uint16_t f)
{
  v1=a; v2=b; v3=c; flags=f;
  faceGroup = NULL;

  isDegenerated = (con->vertexList[v2].point-con->vertexList[v1].point).cross(
      con->vertexList[v3].point - con->vertexList[v1].point).sqrLength() == 0.f;
  if (isDegenerated)  con->numDefaultDegFaces++;

  int n,i;
  Face *face;
  if (con->appendNormals == 2) {
    e12 = e23 = e31 = 0xffffffff;

#define VERT_CODE(_nc_, _ns1_, _ns2_) \
    n = con->vertexList[v##_nc_].faceList.getLength(); \
    for (i=0; i<n; i++) { \
      face = con->vertexList[v##_nc_].faceList[i]; \
      if (v##_ns1_ == face->v##_ns1_) { \
        e##_nc_##_ns1_ = face->e##_nc_##_ns1_; \
        con->edgeList[face->e##_nc_##_ns1_].faceList.append(this); \
      } \
      if (v##_ns2_ == face->v##_ns1_) { \
        e##_ns2_##_nc_ = face->e##_nc_##_ns1_; \
        con->edgeList[face->e##_nc_##_ns1_].faceList.append(this); \
      } \
      if (v##_ns1_ == face->v##_ns2_) { \
        e##_nc_##_ns1_ = face->e##_ns2_##_nc_; \
        con->edgeList[face->e##_ns2_##_nc_].faceList.append(this); \
      } \
      if (v##_ns2_ == face->v##_ns2_) { \
        e##_ns2_##_nc_ = face->e##_ns2_##_nc_; \
        con->edgeList[face->e##_ns2_##_nc_].faceList.append(this); \
      } \
    } \
    con->vertexList[v##_nc_].faceList.append(this);

    VERT_CODE(1,2,3);
    VERT_CODE(2,3,1);
    VERT_CODE(3,1,2);

#undef VERT_CODE

    if (e12 == 0xffffffff)
      e12 = con->numEdges++;
    if (e23 == 0xffffffff)
      e23 = con->numEdges++;
    if (e31 == 0xffffffff)
      e31 = con->numEdges++;
  }
}



SbBool FaceGroup::hasTexture2(tagContext *con)
{ return mat->hasTexture2(con); }

SoTexture2* FaceGroup::getSoTexture2(tagContext *con)
{ return mat->getSoTexture2(con); }

SbBool FaceGroup::hasTexture2Transform(tagContext *con)
{ return mat->hasTexture2Transform(con); }

SoTexture2Transform* FaceGroup::getSoTexture2Transform(tagContext *con)
{ return mat->getSoTexture2Transform(con); }

SoMaterial* FaceGroup::getSoMaterial(tagContext *con)
{ return mat->getSoMaterial(con); }



SoNormal* FaceGroup::createSoNormal(tagContext *con)
{
  SoNormal *normals = new SoNormal;
  int num = faceList.getLength();
  if (con->appendNormals == 1) {
    normals->vector.setNum(num-numDegFaces);
    SbVec3f *v = normals->vector.startEditing();
    for (int i=0; i<num; i++) {
      Face *f = faceList[i];
      if (!f->isDegenerated)
        *(v++) = f->getNormal(con);
    }
    normals->vector.finishEditing();
  } else {
    normals->vector.setNum(0);
/*  FIXME: This is incomplete implementation of per-vertex normal generator.

    normals->vector.setNum(num*3);
    SbVec3f *v = normals->vector.startEditing();
    SbPlane p1, p2;
    for (int i=0; i<num; i++) {
      Face *f = faceList[i];
      Edge *e = &con->edgeList[f->e12];
      int fnum = e->faceList.getLength();
      int j;
      for (j=0; i<fnum; j++) {
        if (currface != facenum) { // check all but this face
      const SbVec3f &normal = facenormals[currface];
      if ((normal.dot(*facenormal)) > threshold) {
        // smooth towards this face
        vertnormal += normal;
      }
    }
  }
      *(v++) = con->vertexList[f->v1].getNormal(con, f->v1);
      *(v++) = con->vertexList[f->v2].getNormal(con, f->v2);
      *(v++) = con->vertexList[f->v3].getNormal(con, f->v3);
    }
    normals->vector.finishEditing();*/
  }
  return normals;
}



SoCoordinate3* FaceGroup::createSoCoordinate3_n(tagContext *con)
{
  assert(!con->useIndexedTriSet && "Improper use.");

  SoCoordinate3 *coords = new SoCoordinate3;
  int num = faceList.getLength();
  coords->point.setNum((num-numDegFaces)*3);
  SbVec3f *c = coords->point.startEditing();
  for (int i=0; i<num; i++) {
    Face *f = faceList[i];
    if (!f->isDegenerated) {
      *(c++) = con->vertexList[f->v1].point;
      *(c++) = con->vertexList[f->v2].point;
      *(c++) = con->vertexList[f->v3].point;
    }
  }
  coords->point.finishEditing();
  return coords;
}



SoTextureCoordinate2* FaceGroup::createSoTextureCoordinate2_n(tagContext *con)
{
  assert(!con->useIndexedTriSet && "Improper use.");

  SoTextureCoordinate2 *tCoords = new SoTextureCoordinate2;
  int num = faceList.getLength();
  tCoords->point.setNum((num-numDegFaces)*3);
  SbVec2f *c = tCoords->point.startEditing();
  for (int i=0; i<num; i++) {
    Face *f = faceList[i];
    if (!f->isDegenerated) {
      *(c++) = con->vertexList[f->v1].texturePoint;
      *(c++) = con->vertexList[f->v2].texturePoint;
      *(c++) = con->vertexList[f->v3].texturePoint;
    }
  }
  tCoords->point.finishEditing();
  return tCoords;
}



SoTriangleStripSet* FaceGroup::createSoTriStripSet_n(tagContext *con)
{
  assert(!con->useIndexedTriSet && "Improper use.");

  SoTriangleStripSet *triSet = new SoTriangleStripSet;
  int num = faceList.getLength() - numDegFaces;
  triSet->numVertices.setNum(num);
  int32_t *n = triSet->numVertices.startEditing();
  for (int i=0; i<num; i++) {
    *(n++) = 3;
  }
  triSet->numVertices.finishEditing();
  return triSet;
}



SoIndexedTriangleStripSet* FaceGroup::createSoIndexedTriStripSet_i(tagContext *con)
{
  assert(con->useIndexedTriSet && "Improper use.");

  SoIndexedTriangleStripSet *triSet = new SoIndexedTriangleStripSet;
  int num = faceList.getLength();
  int i;

  // coords
  triSet->coordIndex.setNum((num-numDegFaces)*4);
  int32_t *c = triSet->coordIndex.startEditing();
  for (i=0; i<num; i++) {
    Face *f = faceList[i];
    if (!f->isDegenerated) {
      *(c++) = f->v1;
      *(c++) = f->v2;
      *(c++) = f->v3;
      *(c++) = SO_END_STRIP_INDEX;
    }
  }
  triSet->coordIndex.finishEditing();

  // texture
  if (mat->hasTexture2(con) && con->loadTextures) {
    triSet->textureCoordIndex.setNum((num-numDegFaces)*4);
    int32_t *tc = triSet->textureCoordIndex.startEditing();
    for (i=0; i<num; i++) {
      Face *f = faceList[i];
      if (!f->isDegenerated) {
        *(tc++) = f->v1;
        *(tc++) = f->v2;
        *(tc++) = f->v3;
        *(tc++) = SO_END_STRIP_INDEX;
      }
    }
    triSet->textureCoordIndex.finishEditing();
  }

  return triSet;
}



Material* DefaultFaceGroup::getMaterial(tagContext *con)
{ return &con->defaultMat; }



SbBool DefaultFaceGroup::isEmpty(Context *con)
{
  int num = con->numFaces;
  for (int i=0; i<num; i++)
    if (con->faceList[i].faceGroup == NULL) return FALSE;
  return TRUE;
}



SoMaterial* DefaultFaceGroup::getSoMaterial(tagContext *con)
{
  return getMaterial(con)->getSoMaterial(con);
}



SoNormal* DefaultFaceGroup::createSoNormal(tagContext *con)
{
  SoNormal *normals = new SoNormal;
  int num = con->numFaces;
  int j = 0;
  if (con->appendNormals == 1) {
    normals->vector.setNum(num - con->numDefaultDegFaces);
    SbVec3f *v = normals->vector.startEditing();
    for (int i=0; i<num; i++) {
      Face *f = &con->faceList[i];
      if (f->faceGroup == NULL && !f->isDegenerated) {
        *(v++) = f->getNormal(con);
        j++;
      }
    }
    normals->vector.finishEditing();
    normals->vector.setNum(j);
  } else {
    assert(FALSE);
  }
  return normals;
}



SoCoordinate3* DefaultFaceGroup::createSoCoordinate3_n(tagContext *con)
{
  assert(!con->useIndexedTriSet && "Improper use.");

  SoCoordinate3 *coords = new SoCoordinate3;
  int num = con->numFaces;
  coords->point.setNum((num - con->numDefaultDegFaces) * 3);
  SbVec3f *c = coords->point.startEditing();
  int j = 0;
  for (int i=0; i<num; i++) {
    Face *f = &con->faceList[i];
    if (f->faceGroup == NULL && !f->isDegenerated) {
      *(c++) = con->vertexList[f->v1].point;
      *(c++) = con->vertexList[f->v2].point;
      *(c++) = con->vertexList[f->v3].point;
      j += 3;
    }
  }
  coords->point.finishEditing();
  coords->point.setNum(j);
  return coords;
}



SoTriangleStripSet* DefaultFaceGroup::createSoTriStripSet_n(tagContext *con)
{
  assert(!con->useIndexedTriSet && "Improper use.");

  SoTriangleStripSet *triSet = new SoTriangleStripSet;
  int num = con->numFaces - con->numDefaultDegFaces;
  int i;
  int j = 0;
  for (i=0; i<num; i++) {
    Face *f = &con->faceList[i];
    if (f->faceGroup == NULL)  j++;
  }
  j -= con->numDefaultDegFaces;

  triSet->numVertices.setNum(j);
  int32_t *n = triSet->numVertices.startEditing();
  for (i=0; i<j; i++) {
    *(n++) = 3;
  }
  triSet->numVertices.finishEditing();

  return triSet;
}



SoIndexedTriangleStripSet* DefaultFaceGroup::createSoIndexedTriStripSet_i(tagContext *con)
{
  assert(con->useIndexedTriSet && "Improper use.");

  SoIndexedTriangleStripSet *triSet = new SoIndexedTriangleStripSet;
  int num = con->numFaces;
  int i;
  int j = 0;
  for (i=0; i<num; i++) {
    Face *f = &con->faceList[i];
    if (f->faceGroup == NULL)  j++;
  }
  j -= con->numDefaultDegFaces;

  // coords
  triSet->coordIndex.setNum(j*4);
  int32_t *c = triSet->coordIndex.startEditing();
  for (i=0; i<num; i++) {
    Face *f = &con->faceList[i];
    if (f->faceGroup == NULL && !f->isDegenerated) {
      *(c++) = f->v1;
      *(c++) = f->v2;
      *(c++) = f->v3;
      *(c++) = SO_END_STRIP_INDEX;
    }
  }
  triSet->coordIndex.finishEditing();

  return triSet;
}



void Material::updateSoMaterial(int index, SoMaterial *m)
{
  m->ambientColor.set1Value(index, ambient);
  m->diffuseColor.set1Value(index, diffuse);
  m->specularColor.set1Value(index, specular);
  m->emissiveColor.set1Value(index, SbColor(0.f,0.f,0.f));
  m->shininess.set1Value(index, shininess);
  m->transparency.set1Value(index, transparency);
}



SoMaterial* Material::getSoMaterial(Context COIN_UNUSED_ARG(*con))
{ return matCache; }



SbBool Material::hasTexture2(tagContext COIN_UNUSED_ARG(*con))
{ return (texture2Cache != NULL); }



SoTexture2* Material::getSoTexture2(tagContext COIN_UNUSED_ARG(*con))
{ return texture2Cache; }



SbBool Material::hasTexture2Transform(tagContext COIN_UNUSED_ARG(*con))
{ return (texture2TransformCache != NULL); }



SoTexture2Transform* Material::getSoTexture2Transform(tagContext COIN_UNUSED_ARG(*con))
{ return texture2TransformCache; }



SoCoordinate3* Context::createSoCoordinate3_i(Context *con) const
{
  assert(con->useIndexedTriSet && "Improper use.");

  SoCoordinate3 *coords = new SoCoordinate3;
  coords->point.setNum(con->numVertices);
  SbVec3f *c = coords->point.startEditing();
  for (int i=0; i<con->numVertices; i++)
    c[i] = con->vertexList[i].point;
  coords->point.finishEditing();
  return coords;
}



SoTextureCoordinate2* Context::createSoTextureCoordinate2_i(tagContext *con) const
{
  assert(con->useIndexedTriSet && "Improper use.");

  SoTextureCoordinate2 *tCoords = new SoTextureCoordinate2;
  tCoords->point.setNum(con->numVertices);
  SbVec2f *c = tCoords->point.startEditing();
  for (int i=0; i<con->numVertices; i++)
    c[i] = con->vertexList[i].texturePoint;
  tCoords->point.finishEditing();
  return tCoords;
}



SoTexture2* Context::genGetEmptyTexture()
{
  if (!genEmptyTexture) {
    genEmptyTexture = new SoTexture2;
    genEmptyTexture->ref();
  }
  return genEmptyTexture;
}



SoTexture2Transform* Context::genGetEmptyTexTransform()
{
  if (!genEmptyTexTransform) {
    genEmptyTexTransform = new SoTexture2Transform;
    genEmptyTexTransform->ref();
  }
  return genEmptyTexTransform;
}



SoShapeHints* Context::genGetOneSidedHints()
{
  if (!genOneSidedHints) {
    genOneSidedHints = new SoShapeHints;
    genOneSidedHints->ref();
    // backface culling on, one-sided lighting
    genOneSidedHints->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE;
    genOneSidedHints->shapeType = SoShapeHints::SOLID;
  }
  return genOneSidedHints;
}



SoShapeHints* Context::genGetTwoSidedHints()
{
  if (!genTwoSidedHints) {
    genTwoSidedHints = new SoShapeHints;
    genTwoSidedHints->ref();
    // backface culling off, two-sided lighting
    genTwoSidedHints->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE;
    genTwoSidedHints->shapeType = SoShapeHints::UNKNOWN_SHAPE_TYPE;
  }
  return genTwoSidedHints;
}



/* Return value of COIN_DEBUG_3DS environment variable. */
static int coin_debug_3ds()
{
  static int d = -1;
  if (d == -1) {
    const char * val = coin_getenv("COIN_DEBUG_3DS");
    d = val ? atoi(val) : 0;
  }
  return d;
}

// *************************************************************************

// This is the only interface exposed to code outside this file.

SbBool
coin_3ds_read_file(SoInput *in, SoSeparator *&root,
                   int appendNormals, float creaseAngle,
                   SbBool loadMaterials, SbBool loadTextures,
                   SbBool loadObjNames, SbBool indexedTriSet,
                   SbBool centerModel, float modelSize)
{
  SoStream s;
  s.setBinary(TRUE);
  s.setEndianOrdering(SoStream::LITTLE_ENDIAN_STREAM);
  s.wrapSoInput(in);

  return read3dsFile(&s, root, appendNormals, creaseAngle, loadMaterials,
                     loadTextures, loadObjNames, indexedTriSet,
                     centerModel, modelSize);
}

// *************************************************************************