summaryrefslogtreecommitdiffstats
path: root/tools/qgltf/qgltf.cpp
blob: a86dc2b7d2b61a96a4bc94fa19ca4faddb4cbd52 (plain)
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
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <assimp/Importer.hpp>
#include <assimp/IOStream.hpp>
#include <assimp/IOSystem.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>

#include <qiodevice.h>
#include <qfile.h>
#include <qfileinfo.h>
#include <qdir.h>
#include <qhash.h>
#include <qdebug.h>
#include <qcoreapplication.h>
#include <qcommandlineparser.h>
#include <qjsondocument.h>
#include <qjsonobject.h>
#include <qjsonarray.h>
#include <qmath.h>

#define GLT_UNSIGNED_SHORT 0x1403
#define GLT_UNSIGNED_INT 0x1405
#define GLT_FLOAT 0x1406

#define GLT_FLOAT_VEC2 0x8B50
#define GLT_FLOAT_VEC3 0x8B51
#define GLT_FLOAT_VEC4 0x8B52
#define GLT_FLOAT_MAT3 0x8B5B
#define GLT_FLOAT_MAT4 0x8B5C
#define GLT_SAMPLER_2D 0x8B5E

#define GLT_ARRAY_BUFFER 0x8892
#define GLT_ELEMENT_ARRAY_BUFFER 0x8893

#define GLT_DEPTH_TEST 0x0B71
#define GLT_CULL_FACE 0x0B44
#define GLT_BLEND 0x0BE2

class AssimpIOStream : public Assimp::IOStream
{
public:
    AssimpIOStream(QIODevice *device);
    ~AssimpIOStream();

    size_t Read(void *pvBuffer, size_t pSize, size_t pCount) override;
    size_t Write(const void *pvBuffer, size_t pSize, size_t pCount) override;
    aiReturn Seek(size_t pOffset, aiOrigin pOrigin) override;
    size_t Tell() const override;
    size_t FileSize() const override;
    void Flush() override;

private:
    QIODevice *m_device;
};

class AssimpIOSystem : public Assimp::IOSystem
{
public:
    bool Exists(const char *pFile) const override;
    char getOsSeparator() const override;
    Assimp::IOStream *Open(const char *pFile, const char *pMode) override;
    void Close(Assimp::IOStream *pFile) override;
};

AssimpIOStream::AssimpIOStream(QIODevice *device) :
    m_device(device)
{
    Q_ASSERT(m_device);
}

AssimpIOStream::~AssimpIOStream()
{
    delete m_device;
}

size_t AssimpIOStream::Read(void *pvBuffer, size_t pSize, size_t pCount)
{
    qint64 readBytes = m_device->read((char *)pvBuffer, pSize * pCount);
    if (readBytes < 0)
        qWarning() << Q_FUNC_INFO << " read failed";
    return readBytes;
}

size_t AssimpIOStream::Write(const void *pvBuffer, size_t pSize, size_t pCount)
{
    qint64 writtenBytes = m_device->write((char *)pvBuffer, pSize * pCount);
    if (writtenBytes < 0)
        qWarning() << Q_FUNC_INFO << " write failed";
    return writtenBytes;
}

aiReturn AssimpIOStream::Seek(size_t pOffset, aiOrigin pOrigin)
{
    qint64 seekPos = pOffset;

    if (pOrigin == aiOrigin_CUR)
        seekPos += m_device->pos();
    else if (pOrigin == aiOrigin_END)
        seekPos += m_device->size();

    if (!m_device->seek(seekPos)) {
        qWarning() << Q_FUNC_INFO << " seek failed";
        return aiReturn_FAILURE;
    }
    return aiReturn_SUCCESS;
}

size_t AssimpIOStream::Tell() const
{
    return m_device->pos();
}

size_t AssimpIOStream::FileSize() const
{
    return m_device->size();
}

void AssimpIOStream::Flush()
{
    // we don't write via assimp
}

static QIODevice::OpenMode openModeFromText(const char *name) noexcept
{
    static const struct OpenModeMapping {
        char name[2];
        int mode;
    } openModeMapping[] = {
        { { 'r',   0 },  QIODevice::ReadOnly  },
        { { 'r', '+' },  QIODevice::ReadWrite },
        { { 'w',   0 },  QIODevice::WriteOnly | QIODevice::Truncate },
        { { 'w', '+' },  QIODevice::ReadWrite | QIODevice::Truncate },
        { { 'a',   0 },  QIODevice::WriteOnly | QIODevice::Append },
        { { 'a', '+' },  QIODevice::ReadWrite | QIODevice::Append },
        { { 'w', 'b' },  QIODevice::WriteOnly },
        { { 'w', 't' },  QIODevice::WriteOnly | QIODevice::Text },
        { { 'r', 'b' },  QIODevice::ReadOnly  },
        { { 'r', 't' },  QIODevice::ReadOnly  | QIODevice::Text },
    };

    for (auto e : openModeMapping) {
        if (qstrncmp(e.name, name, sizeof(OpenModeMapping::name)) == 0)
            return static_cast<QIODevice::OpenMode>(e.mode);
    }
    return QIODevice::NotOpen;
}

bool AssimpIOSystem::Exists(const char *pFile) const
{
    return QFileInfo::exists(QString::fromUtf8(pFile));
}

char AssimpIOSystem::getOsSeparator() const
{
    return QDir::separator().toLatin1();
}

Assimp::IOStream *AssimpIOSystem::Open(const char *pFile, const char *pMode)
{
    const QString fileName(QString::fromUtf8(pFile));
    const QLatin1String cleanedMode = QLatin1String{pMode}.trimmed();

    if (const QIODevice::OpenMode openMode = openModeFromText(cleanedMode.data())) {
        QScopedPointer<QFile> file(new QFile(fileName));
        if (file->open(openMode))
            return new AssimpIOStream(file.take());
    }

    return nullptr;
}

void AssimpIOSystem::Close(Assimp::IOStream *pFile)
{
    delete pFile;
}

static inline QString ai2qt(const aiString &str)
{
    return QString::fromUtf8(str.data, int(str.length));
}

static inline QVector<float> ai2qt(const aiMatrix4x4 &matrix)
{
    return QVector<float>() << matrix.a1 << matrix.b1 << matrix.c1 << matrix.d1
                            << matrix.a2 << matrix.b2 << matrix.c2 << matrix.d2
                            << matrix.a3 << matrix.b3 << matrix.c3 << matrix.d3
                            << matrix.a4 << matrix.b4 << matrix.c4 << matrix.d4;
}

struct Options {
    QString outDir;
    bool genBin;
    bool compact;
    bool compress;
    bool genTangents;
    bool interleave;
    float scale;
    bool genCore;
    enum TextureCompression {
        NoTextureCompression,
        ETC1
    };
    TextureCompression texComp;
    bool commonMat;
    bool shaders;
    bool showLog;
} opts;

class Importer
{
public:
    Importer();
    virtual ~Importer();

    virtual bool load(const QString &filename) = 0;

    struct BufferInfo {
        QString name;
        QByteArray data;
    };
    QVector<BufferInfo> buffers() const;

    struct MeshInfo {
        struct BufferView {
            BufferView() : bufIndex(0), offset(0), length(0), componentType(0), target(0) { }
            QString name;
            uint bufIndex;
            uint offset;
            uint length;
            uint componentType;
            uint target;
        };
        QVector<BufferView> views;
        struct Accessor {
            Accessor() : offset(0), stride(0), count(0), componentType(0) { }
            QString name;
            QString usage;
            QString bufferView;
            uint offset;
            uint stride;
            uint count;
            uint componentType;
            QString type;
            QVector<float> minVal;
            QVector<float> maxVal;
        };
        QVector<Accessor> accessors;
        QString name; // generated
        QString originalName; // may be empty
        uint materialIndex;
    };

    QVector<MeshInfo::BufferView> bufferViews() const;
    QVector<MeshInfo::Accessor> accessors() const;
    uint meshCount() const;
    MeshInfo meshInfo(uint meshIndex) const;

    struct MaterialInfo {
        QString name;
        QString originalName;
        QHash<QByteArray, QVector<float> > m_colors;
        QHash<QByteArray, float> m_values;
        QHash<QByteArray, QString> m_textures;
    };
    uint materialCount() const;
    MaterialInfo materialInfo(uint materialIndex) const;

    QSet<QString> externalTextures() const;

    struct CameraInfo {
        QString name; // suffixed
        float aspectRatio;
        float yfov;
        float zfar;
        float znear;
    };
    QHash<QString, CameraInfo> cameraInfo() const;

    struct EmbeddedTextureInfo {
        EmbeddedTextureInfo() { }
        QString name;
#ifdef HAS_QIMAGE
        EmbeddedTextureInfo(const QString &name, const QImage &image) : name(name), image(image) { }
        QImage image;
#endif
    };
    QHash<QString, EmbeddedTextureInfo> embeddedTextures() const;

    struct Node {
        QString name;
        QString uniqueName; // generated
        QVector<float> transformation;
        QVector<Node *> children;
        QVector<uint> meshes;
    };
    const Node *rootNode() const;

    struct KeyFrame {
        KeyFrame() : t(0), transValid(false), rotValid(false), scaleValid(false) { }
        float t;
        bool transValid;
        QVector<float> trans;
        bool rotValid;
        QVector<float> rot;
        bool scaleValid;
        QVector<float> scale;
    };
    struct AnimationInfo {
        AnimationInfo() : hasTranslation(false), hasRotation(false), hasScale(false) { }
        QString name;
        QString targetNode;
        bool hasTranslation;
        bool hasRotation;
        bool hasScale;
        QVector<KeyFrame> keyFrames;
    };
    QVector<AnimationInfo> animations() const;

    bool allMeshesForMaterialHaveTangents(uint materialIndex) const;

    const Node *findNode(const Node *root, const QString &originalName) const;

protected:
    void delNode(Importer::Node *n);

    QByteArray m_buffer;
    QHash<uint, MeshInfo> m_meshInfo;
    QHash<uint, MaterialInfo> m_materialInfo;
    QHash<QString, EmbeddedTextureInfo> m_embeddedTextures;
    QSet<QString> m_externalTextures;
    QHash<QString, CameraInfo> m_cameraInfo;
    Node *m_rootNode;
    QVector<AnimationInfo> m_animations;
};
QT_BEGIN_NAMESPACE
Q_DECLARE_TYPEINFO(Importer::BufferInfo,           Q_MOVABLE_TYPE);
Q_DECLARE_TYPEINFO(Importer::MeshInfo::BufferView, Q_MOVABLE_TYPE);
Q_DECLARE_TYPEINFO(Importer::MeshInfo::Accessor,   Q_MOVABLE_TYPE);
Q_DECLARE_TYPEINFO(Importer::MaterialInfo,         Q_MOVABLE_TYPE);
Q_DECLARE_TYPEINFO(Importer::CameraInfo,           Q_MOVABLE_TYPE);
Q_DECLARE_TYPEINFO(Importer::EmbeddedTextureInfo,  Q_MOVABLE_TYPE);
Q_DECLARE_TYPEINFO(Importer::Node,                 Q_COMPLEX_TYPE); // uses address as identity
Q_DECLARE_TYPEINFO(Importer::KeyFrame,             Q_MOVABLE_TYPE);
Q_DECLARE_TYPEINFO(Importer::AnimationInfo,        Q_MOVABLE_TYPE);
QT_END_NAMESPACE

Importer::Importer()
    : m_rootNode(nullptr)
{
}

void Importer::delNode(Importer::Node *n)
{
    if (!n)
        return;
    for (Importer::Node *c : qAsConst(n->children))
        delNode(c);
    delete n;
}

Importer::~Importer()
{
    delNode(m_rootNode);
}

QVector<Importer::BufferInfo> Importer::buffers() const
{
    BufferInfo b;
    b.name = QStringLiteral("buf");
    b.data = m_buffer;
    return QVector<BufferInfo>() << b;
}

const Importer::Node *Importer::rootNode() const
{
    return m_rootNode;
}

bool Importer::allMeshesForMaterialHaveTangents(uint materialIndex) const
{
    for (const MeshInfo &mi : m_meshInfo) {
        if (mi.materialIndex == materialIndex) {
            bool hasTangents = false;
            for (const MeshInfo::Accessor &acc : mi.accessors) {
                if (acc.usage == QStringLiteral("TANGENT")) {
                    hasTangents = true;
                    break;
                }
            }
            if (!hasTangents)
                return false;
        }
    }
    return true;
}

QVector<Importer::MeshInfo::BufferView> Importer::bufferViews() const
{
    QVector<Importer::MeshInfo::BufferView> bv;
    for (const MeshInfo &mi : m_meshInfo) {
        for (const MeshInfo::BufferView &v : mi.views)
            bv << v;
    }
    return bv;
}

QVector<Importer::MeshInfo::Accessor> Importer::accessors() const
{
    QVector<Importer::MeshInfo::Accessor> acc;
    for (const MeshInfo &mi : m_meshInfo) {
        for (const MeshInfo::Accessor &a : mi.accessors)
            acc << a;
    }
    return acc;
}

uint Importer::meshCount() const
{
    return m_meshInfo.count();
}

Importer::MeshInfo Importer::meshInfo(uint meshIndex) const
{
    return m_meshInfo[meshIndex];
}

uint Importer::materialCount() const
{
    return m_materialInfo.count();
}

Importer::MaterialInfo Importer::materialInfo(uint materialIndex) const
{
    return m_materialInfo[materialIndex];
}

QHash<QString, Importer::CameraInfo> Importer::cameraInfo() const
{
    return m_cameraInfo;
}

QSet<QString> Importer::externalTextures() const
{
    return m_externalTextures;
}

QHash<QString, Importer::EmbeddedTextureInfo> Importer::embeddedTextures() const
{
    return m_embeddedTextures;
}

QVector<Importer::AnimationInfo> Importer::animations() const
{
    return m_animations;
}

const Importer::Node *Importer::findNode(const Node *root, const QString &originalName) const
{
    for (const Node *c : root->children) {
        if (c->name == originalName)
            return c;
        const Node *cn = findNode(c, originalName);
        if (cn)
            return cn;
    }
    return nullptr;
}

class AssimpImporter : public Importer
{
public:
    AssimpImporter();

    bool load(const QString &filename) override;

private:
    const aiScene *scene() const;
    void printNodes(const aiNode *node, int level = 1);
    void buildBuffer();
    void parseEmbeddedTextures();
    void parseMaterials();
    void parseCameras();
    void parseNode(Importer::Node *dst, const aiNode *src);
    void parseScene();
    void parseAnimations();
    void addKeyFrame(QVector<KeyFrame> &keyFrames, float t, aiVector3D *vt, aiQuaternion *vr, aiVector3D *vs);

    QScopedPointer<Assimp::Importer> m_importer;
};

AssimpImporter::AssimpImporter() :
    m_importer(new Assimp::Importer)
{
    m_importer->SetIOHandler(new AssimpIOSystem);
    m_importer->SetPropertyInteger(AI_CONFIG_PP_SBP_REMOVE, aiPrimitiveType_LINE | aiPrimitiveType_POINT);
}

bool AssimpImporter::load(const QString &filename)
{
    uint flags = aiProcess_Triangulate | aiProcess_SortByPType
            | aiProcess_JoinIdenticalVertices
            | aiProcess_GenSmoothNormals
            | aiProcess_GenUVCoords
            | aiProcess_FlipUVs
            | aiProcess_FindDegenerates;

    if (opts.genTangents)
        flags |= aiProcess_CalcTangentSpace;

    const aiScene *scene = m_importer->ReadFile(filename.toUtf8().constData(), flags);
    if (!scene)
        return false;

    if (opts.showLog) {
        qDebug().noquote() << filename
                           << scene->mNumMeshes << "meshes,"
                           << scene->mNumMaterials << "materials,"
                           << scene->mNumTextures << "embedded textures,"
                           << scene->mNumCameras << "cameras,"
                           << scene->mNumLights << "lights,"
                           << scene->mNumAnimations << "animations";
        qDebug() << "Scene:";
        printNodes(scene->mRootNode);
    }

    buildBuffer();
    parseEmbeddedTextures();
    parseMaterials();
    parseCameras();
    parseScene();
    parseAnimations();

    return true;
}

void AssimpImporter::printNodes(const aiNode *node, int level)
{
    qDebug().noquote() << QString().fill('-', level * 4) << ai2qt(node->mName) << node->mNumMeshes << "mesh refs";
    for (uint i = 0; i < node->mNumChildren; ++i)
        printNodes(node->mChildren[i], level + 1);
}

template<class T> void copyIndexBuf(T *dst, const aiMesh *src)
{
    for (uint j = 0; j < src->mNumFaces; ++j) {
        const aiFace *f = &src->mFaces[j];
        if (f->mNumIndices != 3)
            qFatal("Face %d is not a triangle (index count %d instead of 3)", j, f->mNumIndices);
        *dst++ = f->mIndices[0];
        *dst++ = f->mIndices[1];
        *dst++ = f->mIndices[2];
    }
}

static QString newBufferViewName()
{
    static int cnt = 0;
    return QString(QStringLiteral("bufferView_%1")).arg(++cnt);
}

static QString newAccessorName()
{
    static int cnt = 0;
    return QString(QStringLiteral("accessor_%1")).arg(++cnt);
}

static QString newMeshName()
{
    static int cnt = 0;
    return QString(QStringLiteral("mesh_%1")).arg(++cnt);
}

static QString newMaterialName()
{
    static int cnt = 0;
    return QString(QStringLiteral("material_%1")).arg(++cnt);
}

static QString newTechniqueName()
{
    static int cnt = 0;
    return QString(QStringLiteral("technique_%1")).arg(++cnt);
}

static QString newTextureName()
{
    static int cnt = 0;
    return QString(QStringLiteral("texture_%1")).arg(++cnt);
}

static QString newImageName()
{
    static int cnt = 0;
    return QString(QStringLiteral("image_%1")).arg(++cnt);
}

static QString newShaderName()
{
    static int cnt = 0;
    return QString(QStringLiteral("shader_%1")).arg(++cnt);
}

static QString newProgramName()
{
    static int cnt = 0;
    return QString(QStringLiteral("program_%1")).arg(++cnt);
}

static QString newNodeName()
{
    static int cnt = 0;
    return QString(QStringLiteral("node_%1")).arg(++cnt);
}

static QString newAnimationName()
{
    static int cnt = 0;
    return QString(QStringLiteral("animation_%1")).arg(++cnt);
}

template<class T> void calcBB(QVector<float> &minVal, QVector<float> &maxVal, T *data, int vertexCount, int compCount)
{
    minVal.resize(compCount);
    maxVal.resize(compCount);
    for (int i = 0; i < vertexCount; ++i) {
        for (int j = 0; j < compCount; ++j) {
            if (i == 0) {
                minVal[j] = maxVal[j] = data[i][j];
            } else {
                if (data[i][j] < minVal[j])
                    minVal[j] = data[i][j];
                if (data[i][j] > maxVal[j])
                    maxVal[j] = data[i][j];
            }
        }
    }
}

// One buffer per importer (scene).
// Two buffer views (array, index) + three or more accessors per mesh.

void AssimpImporter::buildBuffer()
{
    m_buffer.clear();
    m_meshInfo.clear();

    if (opts.showLog)
        qDebug() << "Meshes:";

    const aiScene *sc = scene();
    for (uint i = 0; i < sc->mNumMeshes; ++i) {
        aiMesh *m = sc->mMeshes[i];
        MeshInfo meshInfo;
        meshInfo.originalName = ai2qt(m->mName);
        meshInfo.name = newMeshName();
        meshInfo.materialIndex = m->mMaterialIndex;

        aiVector3D *vertices = m->mVertices;
        aiVector3D *normals = m->mNormals;
        aiVector3D *textureCoords = m->mTextureCoords[0];
        aiColor4D *colors = m->mColors[0];
        aiVector3D *tangents = m->mTangents;

        if (opts.scale != 1) {
            for (uint j = 0; j < m->mNumVertices; ++j) {
                vertices[j].x *= opts.scale;
                vertices[j].y *= opts.scale;
                vertices[j].z *= opts.scale;
            }
        }

        // Vertex (3), Normal (3), Coord? (2), Color? (4), Tangent? (3)
        uint stride = 3 + 3 + (textureCoords ? 2 : 0) + (colors ? 4 : 0) + (tangents ? 3 : 0);
        QByteArray vertexBuf;
        vertexBuf.resize(stride * m->mNumVertices * sizeof(float));
        float *p = reinterpret_cast<float *>(vertexBuf.data());

        if (opts.interleave) {
            for (uint j = 0; j < m->mNumVertices; ++j) {
                // Vertex
                *p++ = vertices[j].x;
                *p++ = vertices[j].y;
                *p++ = vertices[j].z;

                // Normal
                *p++ = normals[j].x;
                *p++ = normals[j].y;
                *p++ = normals[j].z;

                // Coord
                if (textureCoords) {
                    *p++ = textureCoords[j].x;
                    *p++ = textureCoords[j].y;
                }

                // Color
                if (colors) {
                    *p++ = colors[j].r;
                    *p++ = colors[j].g;
                    *p++ = colors[j].b;
                    *p++ = colors[j].a;
                }

                // Tangent
                if (tangents) {
                    *p++ = tangents[j].x;
                    *p++ = tangents[j].y;
                    *p++ = tangents[j].z;
                }
            }
        } else {
            // Vertex
            for (uint j = 0; j < m->mNumVertices; ++j) {
                *p++ = vertices[j].x;
                *p++ = vertices[j].y;
                *p++ = vertices[j].z;
            }

            // Normal
            for (uint j = 0; j < m->mNumVertices; ++j) {
                *p++ = normals[j].x;
                *p++ = normals[j].y;
                *p++ = normals[j].z;
            }

            // Coord
            if (textureCoords) {
                for (uint j = 0; j < m->mNumVertices; ++j) {
                    *p++ = textureCoords[j].x;
                    *p++ = textureCoords[j].y;
                }
            }

            // Color
            if (colors) {
                for (uint j = 0; j < m->mNumVertices; ++j) {
                    *p++ = colors[j].r;
                    *p++ = colors[j].g;
                    *p++ = colors[j].b;
                    *p++ = colors[j].a;
                }
            }

            // Tangent
            if (tangents) {
                for (uint j = 0; j < m->mNumVertices; ++j) {
                    *p++ = tangents[j].x;
                    *p++ = tangents[j].y;
                    *p++ = tangents[j].z;
                }
            }
        }

        MeshInfo::BufferView vertexBufView;
        vertexBufView.name = newBufferViewName();
        vertexBufView.length = vertexBuf.size();
        vertexBufView.offset = m_buffer.size();
        vertexBufView.componentType = GLT_FLOAT;
        vertexBufView.target = GLT_ARRAY_BUFFER;
        meshInfo.views.append(vertexBufView);

        QByteArray indexBuf;
        uint indexCount = m->mNumFaces * 3;
        if (indexCount >= USHRT_MAX) {
            indexBuf.resize(indexCount * sizeof(quint32));
            quint32 *p = reinterpret_cast<quint32 *>(indexBuf.data());
            copyIndexBuf(p, m);
        } else {
            indexBuf.resize(indexCount * sizeof(quint16));
            quint16 *p = reinterpret_cast<quint16 *>(indexBuf.data());
            copyIndexBuf(p, m);
        }

        MeshInfo::BufferView indexBufView;
        indexBufView.name = newBufferViewName();
        indexBufView.length = indexBuf.size();
        indexBufView.offset = vertexBufView.offset + vertexBufView.length;
        indexBufView.componentType = indexCount >= USHRT_MAX ? GLT_UNSIGNED_INT : GLT_UNSIGNED_SHORT;
        indexBufView.target = GLT_ELEMENT_ARRAY_BUFFER;
        meshInfo.views.append(indexBufView);

        MeshInfo::Accessor acc;
        uint startOffset = 0;
        // Vertex
        acc.name = newAccessorName();
        acc.usage = QStringLiteral("POSITION");
        acc.bufferView = vertexBufView.name;
        acc.offset = 0;
        acc.stride = opts.interleave ? stride * sizeof(float) : 3 * sizeof(float);
        acc.count = m->mNumVertices;
        acc.componentType = vertexBufView.componentType;
        acc.type = QStringLiteral("VEC3");
        calcBB(acc.minVal, acc.maxVal, vertices, m->mNumVertices, 3);
        meshInfo.accessors.append(acc);
        startOffset += opts.interleave ? 3 : 3 * m->mNumVertices;
        // Normal
        acc.name = newAccessorName();
        acc.usage = QStringLiteral("NORMAL");
        acc.offset = startOffset * sizeof(float);
        if (!opts.interleave)
            acc.stride = 3 * sizeof(float);
        calcBB(acc.minVal, acc.maxVal, normals, m->mNumVertices, 3);
        meshInfo.accessors.append(acc);
        startOffset += opts.interleave ? 3 : 3 * m->mNumVertices;
        // Coord
        if (textureCoords) {
            acc.name = newAccessorName();
            acc.usage = QStringLiteral("TEXCOORD_0");
            acc.offset = startOffset * sizeof(float);
            if (!opts.interleave)
                acc.stride = 2 * sizeof(float);
            acc.type = QStringLiteral("VEC2");
            calcBB(acc.minVal, acc.maxVal, textureCoords, m->mNumVertices, 2);
            meshInfo.accessors.append(acc);
            startOffset += opts.interleave ? 2 : 2 * m->mNumVertices;
        }
        // Color
        if (colors) {
            acc.name = newAccessorName();
            acc.usage = QStringLiteral("COLOR");
            acc.offset = startOffset * sizeof(float);
            if (!opts.interleave)
                acc.stride = 4 * sizeof(float);
            acc.type = QStringLiteral("VEC4");
            calcBB(acc.minVal, acc.maxVal, colors, m->mNumVertices, 4);
            meshInfo.accessors.append(acc);
            startOffset += opts.interleave ? 4 : 4 * m->mNumVertices;
        }
        // Tangent
        if (tangents) {
            acc.name = newAccessorName();
            acc.usage = QStringLiteral("TANGENT");
            acc.offset = startOffset * sizeof(float);
            if (!opts.interleave)
                acc.stride = 3 * sizeof(float);
            acc.type = QStringLiteral("VEC3");
            calcBB(acc.minVal, acc.maxVal, tangents, m->mNumVertices, 3);
            meshInfo.accessors.append(acc);
            startOffset += opts.interleave ? 3 : 3 * m->mNumVertices;
        }

        // Index
        acc.name = newAccessorName();
        acc.usage = QStringLiteral("INDEX");
        acc.bufferView = indexBufView.name;
        acc.offset = 0;
        acc.stride = 0;
        acc.count = indexCount;
        acc.componentType = indexBufView.componentType;
        acc.type = QStringLiteral("SCALAR");
        acc.minVal = acc.maxVal = QVector<float>();
        meshInfo.accessors.append(acc);

        if (opts.showLog) {
            qDebug().noquote() << "#" << i << "(" << meshInfo.name << "/" << meshInfo.originalName << ")"
                               << m->mNumVertices << "vertices,"
                               << m->mNumFaces << "faces," << stride << "bytes per vertex,"
                               << vertexBuf.size() << "vertex bytes," << indexBuf.size() << "index bytes";
            if (opts.scale != 1)
                qDebug() << "  scaled by" << opts.scale;
            if (!opts.interleave)
                qDebug() << "  non-interleaved layout";
            QStringList sl;
            for (const MeshInfo::BufferView &bv : qAsConst(meshInfo.views)) sl << bv.name;
            qDebug() << "  buffer views:" << sl;
            sl.clear();
            for (const MeshInfo::Accessor &acc : qAsConst(meshInfo.accessors)) sl << acc.name;
            qDebug() << "  accessors:" << sl;
            qDebug() << "  material: #" << meshInfo.materialIndex;
        }

        m_buffer.append(vertexBuf);
        m_buffer.append(indexBuf);

        m_meshInfo.insert(i, meshInfo);
    }

    if (opts.showLog)
        qDebug().noquote() << "Total buffer size" << m_buffer.size();
}

void AssimpImporter::parseEmbeddedTextures()
{
#ifdef HAS_QIMAGE
    m_embeddedTextures.clear();

    const aiScene *sc = scene();
    if (opts.showLog && sc->mNumTextures)
        qDebug() << "Embedded textures:";

    for (uint i = 0; i < sc->mNumTextures; ++i) {
        aiTexture *t = sc->mTextures[i];
        QImage img;
        if (t->mHeight == 0) {
            img = QImage::fromData(reinterpret_cast<uchar *>(t->pcData), t->mWidth);
        } else {
            uint sz = t->mWidth * t->mHeight;
            QByteArray data;
            data.resize(sz * 4);
            uchar *p = reinterpret_cast<uchar *>(data.data());
            for (uint j = 0; j < sz; ++j) {
                *p++ = t->pcData[j].r;
                *p++ = t->pcData[j].g;
                *p++ = t->pcData[j].b;
                *p++ = t->pcData[j].a;
            }
            img = QImage(reinterpret_cast<const uchar *>(data.constData()), t->mWidth, t->mHeight, QImage::Format_RGBA8888);
            img.detach();
        }
        QString name;
        static int imgCnt = 0;
        name = QString(QStringLiteral("texture_%1.png")).arg(++imgCnt);
        QString embeddedTextureRef = QStringLiteral("*") + QString::number(i); // see AI_MAKE_EMBEDDED_TEXNAME
        m_embeddedTextures.insert(embeddedTextureRef, EmbeddedTextureInfo(name, img));
        if (opts.showLog)
            qDebug().noquote() << "#" << i << name << img;
    }
#else
    if (scene()->mNumTextures)
        qWarning() << "WARNING: No image support, ignoring" << scene()->mNumTextures << "embedded textures";
#endif
}

void AssimpImporter::parseMaterials()
{
    m_materialInfo.clear();
    m_externalTextures.clear();

    if (opts.showLog)
        qDebug() << "Materials:";

    const aiScene *sc = scene();
    for (uint i = 0; i < sc->mNumMaterials; ++i) {
        const aiMaterial *mat = sc->mMaterials[i];
        MaterialInfo matInfo;
        matInfo.name = newMaterialName();

        aiString s;
        if (mat->Get(AI_MATKEY_NAME, s) == aiReturn_SUCCESS)
            matInfo.originalName = ai2qt(s);

        aiColor4D color;
        if (mat->Get(AI_MATKEY_COLOR_DIFFUSE, color) == aiReturn_SUCCESS)
            matInfo.m_colors.insert("diffuse", QVector<float>() << color.r << color.g << color.b << color.a);
        if (mat->Get(AI_MATKEY_COLOR_SPECULAR, color) == aiReturn_SUCCESS)
            matInfo.m_colors.insert("specular", QVector<float>() << color.r << color.g << color.b);
        if (mat->Get(AI_MATKEY_COLOR_AMBIENT, color) == aiReturn_SUCCESS)
            matInfo.m_colors.insert("ambient", QVector<float>() << color.r << color.g << color.b);

        float f;
        if (mat->Get(AI_MATKEY_SHININESS, f) == aiReturn_SUCCESS)
            matInfo.m_values.insert("shininess", f);

        if (mat->GetTexture(aiTextureType_DIFFUSE, 0, &s) == aiReturn_SUCCESS)
            matInfo.m_textures.insert("diffuse", ai2qt(s));
        if (mat->GetTexture(aiTextureType_SPECULAR, 0, &s) == aiReturn_SUCCESS)
            matInfo.m_textures.insert("specular", ai2qt(s));
        if (mat->GetTexture(aiTextureType_NORMALS, 0, &s) == aiReturn_SUCCESS)
            matInfo.m_textures.insert("normal", ai2qt(s));

        QHash<QByteArray, QString>::iterator texIt = matInfo.m_textures.begin();
        while (texIt != matInfo.m_textures.end()) {
            // Map embedded texture references to real files.
            if (texIt->startsWith('*'))
                *texIt = m_embeddedTextures[*texIt].name;
            else
                m_externalTextures.insert(*texIt);
            ++texIt;
        }

        m_materialInfo.insert(i, matInfo);

        if (opts.showLog) {
            qDebug().noquote() << "#" << i << "(" << matInfo.name << "/" << matInfo.originalName << ")";
            qDebug() << "  colors:" << matInfo.m_colors;
            qDebug() << "  values:" << matInfo.m_values;
            qDebug() << "  textures:" << matInfo.m_textures;
        }
    }
}

void AssimpImporter::parseCameras()
{
    m_cameraInfo.clear();

    if (opts.showLog)
        qDebug() << "Cameras:";

    const aiScene *sc = scene();
    for (uint i = 0; i < sc->mNumCameras; ++i) {
        const aiCamera *cam = sc->mCameras[i];
        QString name = ai2qt(cam->mName);
        CameraInfo c;

        c.name = name + QStringLiteral("_cam");
        c.aspectRatio = qFuzzyIsNull(cam->mAspect) ? 1.5f : cam->mAspect;
        c.yfov = cam->mHorizontalFOV;
        if (c.yfov < (M_PI / 10.0)) // this can't be right (probably orthographic source camera)
            c.yfov = float(M_PI / 4.0);
        c.znear = cam->mClipPlaneNear;
        c.zfar = cam->mClipPlaneFar;

        // Collada / glTF cameras point in -Z by default, the rest is in the
        // node matrix, no separate look-at params given here.

        m_cameraInfo.insert(name, c);

        if (opts.showLog)
            qDebug().noquote() << "#" << i << "(" << name << ")" << c.aspectRatio << c.yfov << c.znear << c.zfar;
    }
}

void AssimpImporter::parseNode(Importer::Node *dst, const aiNode *src)
{
    dst->name = ai2qt(src->mName);
    dst->uniqueName = newNodeName();
    for (uint j = 0; j < src->mNumChildren; ++j) {
        Node *c = new Node;
        parseNode(c, src->mChildren[j]);
        dst->children << c;
    }
    dst->transformation = ai2qt(src->mTransformation);
    for (uint j = 0; j < src->mNumMeshes; ++j)
        dst->meshes << src->mMeshes[j];
}

void AssimpImporter::parseScene()
{
    delNode(m_rootNode);
    const aiScene *sc = scene();
    m_rootNode = new Node;
    parseNode(m_rootNode, sc->mRootNode);
}

void AssimpImporter::addKeyFrame(QVector<KeyFrame> &keyFrames, float t, aiVector3D *vt, aiQuaternion *vr, aiVector3D *vs)
{
    KeyFrame kf;
    int idx = -1;
    for (int i = 0; i < keyFrames.count(); ++i) {
        if (qFuzzyCompare(keyFrames[i].t, t)) {
            kf = keyFrames[i];
            idx = i;
            break;
        }
    }

    kf.t = t;
    if (vt) {
        kf.transValid = true;
        kf.trans = QVector<float>() << vt->x << vt->y << vt->z;
    }
    if (vr) {
        kf.rotValid = true;
        kf.rot = QVector<float>() << vr->w << vr->x << vr->y << vr->z;
    }
    if (vs) {
        kf.scaleValid = true;
        kf.scale = QVector<float>() << vs->x << vs->y << vs->z;
    }

    if (idx >= 0)
        keyFrames[idx] = kf;
    else
        keyFrames.append(kf);
}

void AssimpImporter::parseAnimations()
{
    const aiScene *sc = scene();
    if (opts.showLog && sc->mNumAnimations)
        qDebug() << "Animations:";

    for (uint i = 0; i < sc->mNumAnimations; ++i) {
        const aiAnimation *anim = sc->mAnimations[i];

        // Only care about node animations.
        for (uint j = 0; j < anim->mNumChannels; ++j) {
            const aiNodeAnim *a = anim->mChannels[j];
            AnimationInfo animInfo;
            QVector<KeyFrame> keyFrames;

            if (opts.showLog)
                qDebug().noquote() << ai2qt(anim->mName) << "->" << ai2qt(a->mNodeName);

            // Target values in the keyframes are local absolute (relative to parent, like node.matrix).
            for (uint kf = 0; kf < a->mNumPositionKeys; ++kf) {
                float t = float(a->mPositionKeys[kf].mTime);
                aiVector3D v = a->mPositionKeys[kf].mValue;
                animInfo.hasTranslation = true;
                addKeyFrame(keyFrames, t, &v, nullptr, nullptr);
            }
            for (uint kf = 0; kf < a->mNumRotationKeys; ++kf) {
                float t = float(a->mRotationKeys[kf].mTime);
                aiQuaternion v = a->mRotationKeys[kf].mValue;
                animInfo.hasRotation = true;
                addKeyFrame(keyFrames, t, nullptr, &v, nullptr);
            }
            for (uint kf = 0; kf < a->mNumScalingKeys; ++kf) {
                float t = float(a->mScalingKeys[kf].mTime);
                aiVector3D v = a->mScalingKeys[kf].mValue;
                animInfo.hasScale = true;
                addKeyFrame(keyFrames, t, nullptr, nullptr, &v);
            }

            // Here we should ideally get rid of non-animated properties (that
            // just set the t-r-s value from node.matrix in every frame) but
            // let's leave that as a future exercise.

            if (!keyFrames.isEmpty()) {
                animInfo.name = ai2qt(anim->mName);
                QString nodeName = ai2qt(a->mNodeName); // have to map to our generated, unique node names
                const Node *targetNode = findNode(m_rootNode, nodeName);
                if (targetNode)
                    animInfo.targetNode = targetNode->uniqueName;
                else
                    qWarning().noquote() << "ERROR: Cannot find target node" << nodeName << "for animation" << animInfo.name;
                animInfo.keyFrames = keyFrames;
                m_animations << animInfo;

                if (opts.showLog) {
                    for (const KeyFrame &kf : qAsConst(keyFrames)) {
                        QString msg;
                        QTextStream s(&msg);
                        s << "  @ " << kf.t;
                        if (kf.transValid)
                            s << " T=(" << kf.trans[0] << ", " << kf.trans[1] << ", " << kf.trans[2] << ")";
                        if (kf.rotValid)
                            s << " R=(w=" << kf.rot[0] << ", " << kf.rot[1] << ", " << kf.rot[2] << ", " << kf.rot[3] << ")";
                        if (kf.scaleValid)
                            s << " S=(" << kf.scale[0] << ", " << kf.scale[1] << ", " << kf.scale[2] << ")";
                        qDebug().noquote() << msg;
                    }
                }
            }
        }
    }
}

const aiScene *AssimpImporter::scene() const
{
    return m_importer->GetScene();
}

class Exporter
{
public:
    Exporter(Importer *importer) : m_importer(importer) { }
    virtual ~Exporter() { }

    virtual void save(const QString &inputFilename) = 0;

protected:
    bool nodeIsUseful(const Importer::Node *n) const;
    void copyExternalTextures(const QString &inputFilename);
    void exportEmbeddedTextures();
    void compressTextures();

    Importer *m_importer;
    QSet<QString> m_files;
    QHash<QString, QString> m_compressedTextures;
};

bool Exporter::nodeIsUseful(const Importer::Node *n) const
{
    if (!n->meshes.isEmpty() || m_importer->cameraInfo().contains(n->name))
        return true;

    for (const Importer::Node *c : n->children) {
        if (nodeIsUseful(c))
            return true;
    }

    return false;
}

void Exporter::copyExternalTextures(const QString &inputFilename)
{
    const auto textureFilenames = m_importer->externalTextures();
    for (const QString &textureFilename : textureFilenames) {
        const QString dst = opts.outDir + textureFilename;
        m_files.insert(QFileInfo(dst).fileName());
        // External textures need copying only when output dir was specified.
        if (!opts.outDir.isEmpty()) {
            const QString src = QFileInfo(inputFilename).path() + QStringLiteral("/") + textureFilename;
            if (QFileInfo(src).absolutePath() != QFileInfo(dst).absolutePath()) {
                if (opts.showLog)
                    qDebug().noquote() << "Copying" << src << "to" << dst;
                QFile(src).copy(dst);
            }
        }
    }
}

void Exporter::exportEmbeddedTextures()
{
#ifdef HAS_QIMAGE
    const auto embeddedTextures = m_importer->embeddedTextures();
    for (const Importer::EmbeddedTextureInfo &embTex : embeddedTextures) {
        QString fn = opts.outDir + embTex.name;
        m_files.insert(QFileInfo(fn).fileName());
        if (opts.showLog)
            qDebug().noquote() << "Writing" << fn;
        embTex.image.save(fn);
    }
#endif
}

void Exporter::compressTextures()
{
    if (opts.texComp != Options::ETC1)
        return;

    const auto textureFilenames = m_importer->externalTextures();
    const auto embeddedTextures = m_importer->embeddedTextures();
    QStringList imageList;
    imageList.reserve(textureFilenames.size() + embeddedTextures.size());
    for (const QString &textureFilename : textureFilenames)
        imageList << opts.outDir + textureFilename;
    for (const Importer::EmbeddedTextureInfo &embTex : embeddedTextures)
        imageList << opts.outDir + embTex.name;

    for (const QString &filename : qAsConst(imageList)) {
        if (QFileInfo(filename).suffix().toLower() != QStringLiteral("png"))
            continue;
        QByteArray cmd = QByteArrayLiteral("etc1tool ");
        cmd += filename.toUtf8();
        qDebug().noquote() << "Invoking" << cmd;
        // No QProcess in bootstrap
        if (system(cmd.constData()) == -1) {
            qWarning() << "ERROR: Failed to launch etc1tool";
        } else {
            QString src = QFileInfo(filename).fileName();
            QString dst = QFileInfo(src).baseName() + QStringLiteral(".pkm");
            m_compressedTextures.insert(src, dst);
            m_files.remove(src);
            m_files.insert(dst);
        }
    }
}

class GltfExporter : public Exporter
{
public:
    GltfExporter(Importer *importer);
    void save(const QString &inputFilename) override;

private:
    struct ProgramInfo {
        struct Param {
            Param() : type(0) { }
            Param(QString name, QString nameInShader, QString semantic, uint type)
                : name(name), nameInShader(nameInShader), semantic(semantic), type(type) { }
            QString name;
            QString nameInShader;
            QString semantic;
            uint type;
        };
        QString commonTechniqueName;
        QString vertShader;
        QString fragShader;
        QVector<Param> attributes;
        QVector<Param> uniforms;
    };
    friend class QTypeInfo<ProgramInfo>;
    friend class QTypeInfo<ProgramInfo::Param>;

    void writeShader(const QString &src, const QString &dst, const QVector<QPair<QByteArray, QByteArray> > &substTab);
    QString exportNode(const Importer::Node *n, QJsonObject &nodes);
    void exportMaterials(QJsonObject &materials, QHash<QString, QString> *textureNameMap);
    void exportParameter(QJsonObject &dst, const QVector<ProgramInfo::Param> &params);
    void exportTechniques(QJsonObject &obj, const QString &basename);
    void exportAnimations(QJsonObject &obj, QVector<Importer::BufferInfo> &bufList,
                          QVector<Importer::MeshInfo::BufferView> &bvList,
                          QVector<Importer::MeshInfo::Accessor> &accList);
    void initShaderInfo();
    ProgramInfo *chooseProgram(uint materialIndex);

    QJsonObject m_obj;
    QJsonDocument m_doc;
    QVector<ProgramInfo> m_progs;

    struct TechniqueInfo {
        TechniqueInfo() : opaque(true), prog(nullptr) { }
        TechniqueInfo(const QString &name, bool opaque, ProgramInfo *prog)
            : name(name)
            , opaque(opaque)
            , prog(prog)
        {
            coreName = name + QStringLiteral("_core");
            gl2Name = name + QStringLiteral("_gl2");
        }
        QString name;
        QString coreName;
        QString gl2Name;
        bool opaque;
        ProgramInfo *prog;
    };
    friend class QTypeInfo<TechniqueInfo>;
    QVector<TechniqueInfo> m_techniques;
    QSet<ProgramInfo *> m_usedPrograms;

    QVector<QPair<QByteArray, QByteArray> > m_subst_es2;
    QVector<QPair<QByteArray, QByteArray> > m_subst_core;

    QHash<QString, bool> m_imageHasAlpha;
};
QT_BEGIN_NAMESPACE
Q_DECLARE_TYPEINFO(GltfExporter::ProgramInfo,        Q_MOVABLE_TYPE);
Q_DECLARE_TYPEINFO(GltfExporter::ProgramInfo::Param, Q_MOVABLE_TYPE);
Q_DECLARE_TYPEINFO(GltfExporter::TechniqueInfo,      Q_MOVABLE_TYPE);
QT_END_NAMESPACE

GltfExporter::GltfExporter(Importer *importer)
    : Exporter(importer)
{
    initShaderInfo();
}

struct Shader {
    const char *name;
    const char *text;
} shaders[] = {
    {
        "color.vert",
"$VERSION\n"
"$ATTRIBUTE vec3 vertexPosition;\n"
"$ATTRIBUTE vec3 vertexNormal;\n"
"$VVARYING vec3 vPosition;\n"
"$VVARYING vec3 vNormal;\n"
"uniform mat4 projection;\n"
"uniform mat4 modelView;\n"
"uniform mat3 modelViewNormal;\n"
"void main()\n"
"{\n"
"    vNormal = normalize( modelViewNormal * vertexNormal );\n"
"    vPosition = vec3( modelView * vec4( vertexPosition, 1.0 ) );\n"
"    gl_Position = projection * modelView * vec4( vertexPosition, 1.0 );\n"
"}\n"
    },
    {
        "color.frag",
"$VERSION\n"
"uniform $HIGHP vec4 lightPosition;\n"
"uniform $HIGHP vec3 lightIntensity;\n"
"uniform $HIGHP vec3 ka;\n"
"uniform $HIGHP vec4 kd;\n"
"uniform $HIGHP vec3 ks;\n"
"uniform $HIGHP float shininess;\n"
"$FVARYING $HIGHP vec3 vPosition;\n"
"$FVARYING $HIGHP vec3 vNormal;\n"
"$DECL_FRAGCOLOR\n"
"$HIGHP vec3 adsModel( const $HIGHP vec3 pos, const $HIGHP vec3 n )\n"
"{\n"
"    $HIGHP vec3 s = normalize( vec3( lightPosition ) - pos );\n"
"    $HIGHP vec3 v = normalize( -pos );\n"
"    $HIGHP vec3 r = reflect( -s, n );\n"
"    $HIGHP float diffuse = max( dot( s, n ), 0.0 );\n"
"    $HIGHP float specular = 0.0;\n"
"    if ( dot( s, n ) > 0.0 )\n"
"        specular = pow( max( dot( r, v ), 0.0 ), shininess );\n"
"    return lightIntensity * ( ka + kd.rgb * diffuse + ks * specular );\n"
"}\n"
"void main()\n"
"{\n"
"    $FRAGCOLOR = vec4( adsModel( vPosition, normalize( vNormal ) ) * kd.a, kd.a );\n"
"}\n"
    },
    {
        "diffusemap.vert",
"$VERSION\n"
"$ATTRIBUTE vec3 vertexPosition;\n"
"$ATTRIBUTE vec3 vertexNormal;\n"
"$ATTRIBUTE vec2 vertexTexCoord;\n"
"$VVARYING vec3 vPosition;\n"
"$VVARYING vec3 vNormal;\n"
"$VVARYING vec2 vTexCoord;\n"
"uniform mat4 projection;\n"
"uniform mat4 modelView;\n"
"uniform mat3 modelViewNormal;\n"
"void main()\n"
"{\n"
"    vTexCoord = vertexTexCoord;\n"
"    vNormal = normalize( modelViewNormal * vertexNormal );\n"
"    vPosition = vec3( modelView * vec4( vertexPosition, 1.0 ) );\n"
"    gl_Position = projection * modelView * vec4( vertexPosition, 1.0 );\n"
"}\n"
    },
    {
        "diffusemap.frag",
"$VERSION\n"
"uniform $HIGHP vec4 lightPosition;\n"
"uniform $HIGHP vec3 lightIntensity;\n"
"uniform $HIGHP vec3 ka;\n"
"uniform $HIGHP vec3 ks;\n"
"uniform $HIGHP float shininess;\n"
"uniform sampler2D diffuseTexture;\n"
"$FVARYING $HIGHP vec3 vPosition;\n"
"$FVARYING $HIGHP vec3 vNormal;\n"
"$FVARYING $HIGHP vec2 vTexCoord;\n"
"$DECL_FRAGCOLOR\n"
"$HIGHP vec4 adsModel( const $HIGHP vec3 pos, const $HIGHP vec3 n )\n"
"{\n"
"    $HIGHP vec3 s = normalize( vec3( lightPosition ) - pos );\n"
"    $HIGHP vec3 v = normalize( -pos );\n"
"    $HIGHP vec3 r = reflect( -s, n );\n"
"    $HIGHP float diffuse = max( dot( s, n ), 0.0 );\n"
"    $HIGHP float specular = 0.0;\n"
"    if ( dot( s, n ) > 0.0 )\n"
"        specular = pow( max( dot( r, v ), 0.0 ), shininess );\n"
"    $HIGHP vec4 kd = $TEXTURE2D( diffuseTexture, vTexCoord );\n"
"    return vec4( lightIntensity * ( ka + kd.rgb * diffuse + ks * specular ) * kd.a, kd.a );\n"
"}\n"
"void main()\n"
"{\n"
"    $FRAGCOLOR = adsModel( vPosition, normalize( vNormal ) );\n"
"}\n"
    },
    {
        "diffusespecularmap.frag",
"$VERSION\n"
"uniform $HIGHP vec4 lightPosition;\n"
"uniform $HIGHP vec3 lightIntensity;\n"
"uniform $HIGHP vec3 ka;\n"
"uniform $HIGHP float shininess;\n"
"uniform sampler2D diffuseTexture;\n"
"uniform sampler2D specularTexture;\n"
"$FVARYING $HIGHP vec3 vPosition;\n"
"$FVARYING $HIGHP vec3 vNormal;\n"
"$FVARYING $HIGHP vec2 vTexCoord;\n"
"$DECL_FRAGCOLOR\n"
"$HIGHP vec4 adsModel( const in $HIGHP vec3 pos, const in $HIGHP vec3 n )\n"
"{\n"
"    $HIGHP vec3 s = normalize( vec3( lightPosition ) - pos );\n"
"    $HIGHP vec3 v = normalize( -pos );\n"
"    $HIGHP vec3 r = reflect( -s, n );\n"
"    $HIGHP float diffuse = max( dot( s, n ), 0.0 );\n"
"    $HIGHP float specular = 0.0;\n"
"    if ( dot( s, n ) > 0.0 )\n"
"        specular = ( shininess / ( 8.0 * 3.14 ) ) * pow( max( dot( r, v ), 0.0 ), shininess );\n"
"    $HIGHP vec4 kd = $TEXTURE2D( diffuseTexture, vTexCoord );\n"
"    $HIGHP vec3 ks = $TEXTURE2D( specularTexture, vTexCoord );\n"
"    return vec4( lightIntensity * ( ka + kd.rgb * diffuse + ks * specular ) * kd.a, kd.a );\n"
"}\n"
"void main()\n"
"{\n"
"    $FRAGCOLOR = vec4( adsModel( vPosition, normalize( vNormal ) ), 1.0 );\n"
"}\n"
    },
    {
        "normaldiffusemap.vert",
"$VERSION\n"
"$ATTRIBUTE vec3 vertexPosition;\n"
"$ATTRIBUTE vec3 vertexNormal;\n"
"$ATTRIBUTE vec2 vertexTexCoord;\n"
"$ATTRIBUTE vec4 vertexTangent;\n"
"$VVARYING vec3 lightDir;\n"
"$VVARYING vec3 viewDir;\n"
"$VVARYING vec2 texCoord;\n"
"uniform mat4 projection;\n"
"uniform mat4 modelView;\n"
"uniform mat3 modelViewNormal;\n"
"uniform vec4 lightPosition;\n"
"void main()\n"
"{\n"
"    texCoord = vertexTexCoord;\n"
"    vec3 normal = normalize( modelViewNormal * vertexNormal );\n"
"    vec3 tangent = normalize( modelViewNormal * vertexTangent.xyz );\n"
"    vec3 position = vec3( modelView * vec4( vertexPosition, 1.0 ) );\n"
"    vec3 binormal = normalize( cross( normal, tangent ) );\n"
"    mat3 tangentMatrix = mat3 (\n"
"        tangent.x, binormal.x, normal.x,\n"
"        tangent.y, binormal.y, normal.y,\n"
"        tangent.z, binormal.z, normal.z );\n"
"    vec3 s = vec3( lightPosition ) - position;\n"
"    lightDir = normalize( tangentMatrix * s );\n"
"    vec3 v = -position;\n"
"    viewDir = normalize( tangentMatrix * v );\n"
"    gl_Position = projection * modelView * vec4( vertexPosition, 1.0 );\n"
"}\n"
    },
    {
        "normaldiffusemap.frag",
"$VERSION\n"
"uniform $HIGHP vec3 lightIntensity;\n"
"uniform $HIGHP vec3 ka;\n"
"uniform $HIGHP vec3 ks;\n"
"uniform $HIGHP float shininess;\n"
"uniform sampler2D diffuseTexture;\n"
"uniform sampler2D normalTexture;\n"
"$FVARYING $HIGHP vec3 lightDir;\n"
"$FVARYING $HIGHP vec3 viewDir;\n"
"$FVARYING $HIGHP vec2 texCoord;\n"
"$DECL_FRAGCOLOR\n"
"$HIGHP vec3 adsModel( const $HIGHP vec3 norm, const $HIGHP vec3 diffuseReflect)\n"
"{\n"
"    $HIGHP vec3 r = reflect( -lightDir, norm );\n"
"    $HIGHP vec3 ambient = lightIntensity * ka;\n"
"    $HIGHP float sDotN = max( dot( lightDir, norm ), 0.0 );\n"
"    $HIGHP vec3 diffuse = lightIntensity * diffuseReflect * sDotN;\n"
"    $HIGHP vec3 ambientAndDiff = ambient + diffuse;\n"
"    $HIGHP vec3 spec = vec3( 0.0 );\n"
"    if ( sDotN > 0.0 )\n"
"        spec = lightIntensity * ks * pow( max( dot( r, viewDir ), 0.0 ), shininess );\n"
"    return ambientAndDiff + spec;\n"
"}\n"
"void main()\n"
"{\n"
"    $HIGHP vec4 kd = $TEXTURE2D( diffuseTexture, texCoord );\n"
"    $HIGHP vec4 normal = 2.0 * $TEXTURE2D( normalTexture, texCoord ) - vec4( 1.0 );\n"
"    $FRAGCOLOR = vec4( adsModel( normalize( normal.xyz ), kd.rgb) * kd.a, kd.a );\n"
"}\n"
    },
    {
        "normaldiffusespecularmap.frag",
"$VERSION\n"
"uniform $HIGHP vec3 lightIntensity;\n"
"uniform $HIGHP vec3 ka;\n"
"uniform $HIGHP float shininess;\n"
"uniform sampler2D diffuseTexture;\n"
"uniform sampler2D specularTexture;\n"
"uniform sampler2D normalTexture;\n"
"$FVARYING $HIGHP vec3 lightDir;\n"
"$FVARYING $HIGHP vec3 viewDir;\n"
"$FVARYING $HIGHP vec2 texCoord;\n"
"$DECL_FRAGCOLOR\n"
"$HIGHP vec3 adsModel( const $HIGHP vec3 norm, const $HIGHP vec3 diffuseReflect, const $HIGHP vec3 specular )\n"
"{\n"
"    $HIGHP vec3 r = reflect( -lightDir, norm );\n"
"    $HIGHP vec3 ambient = lightIntensity * ka;\n"
"    $HIGHP float sDotN = max( dot( lightDir, norm ), 0.0 );\n"
"    $HIGHP vec3 diffuse = lightIntensity * diffuseReflect * sDotN;\n"
"    $HIGHP vec3 ambientAndDiff = ambient + diffuse;\n"
"    $HIGHP vec3 spec = vec3( 0.0 );\n"
"    if ( sDotN > 0.0 )\n"
"        spec = lightIntensity * ( shininess / ( 8.0 * 3.14 ) ) * pow( max( dot( r, viewDir ), 0.0 ), shininess );\n"
"    return (ambientAndDiff + spec * specular.rgb);\n"
"}\n"
"void main()\n"
"{\n"
"    $HIGHP vec4 kd = $TEXTURE2D( diffuseTexture, texCoord );\n"
"    $HIGHP vec3 ks = $TEXTURE2D( specularTexture, texCoord );\n"
"    $HIGHP vec4 normal = 2.0 * $TEXTURE2D( normalTexture, texCoord ) - vec4( 1.0 );\n"
"    $FRAGCOLOR = vec4( adsModel( normalize( normal.xyz ), kd.rgb, ks ) * kd.a, kd.a );\n"
"}\n"
    }
};

void GltfExporter::initShaderInfo()
{
    ProgramInfo p;

    p = ProgramInfo();
    p.commonTechniqueName = "PHONG"; // diffuse RGBA, specular RGBA
    p.vertShader = "color.vert";
    p.fragShader = "color.frag";
    p.attributes << ProgramInfo::Param("position", "vertexPosition", "POSITION", GLT_FLOAT_VEC3);
    p.attributes << ProgramInfo::Param("normal", "vertexNormal", "NORMAL", GLT_FLOAT_VEC3);
    p.uniforms << ProgramInfo::Param("projection", "projection", "PROJECTION", GLT_FLOAT_MAT4);
    p.uniforms << ProgramInfo::Param("modelView", "modelView", "MODELVIEW", GLT_FLOAT_MAT4);
    p.uniforms << ProgramInfo::Param("normalMatrix", "modelViewNormal", "MODELVIEWINVERSETRANSPOSE", GLT_FLOAT_MAT3);
    p.uniforms << ProgramInfo::Param("lightPosition", "lightPosition", QString(), GLT_FLOAT_VEC4);
    p.uniforms << ProgramInfo::Param("lightIntensity", "lightIntensity", QString(), GLT_FLOAT_VEC3);
    p.uniforms << ProgramInfo::Param("ambient", "ka", QString(), GLT_FLOAT_VEC3);
    p.uniforms << ProgramInfo::Param("diffuse", "kd", QString(), GLT_FLOAT_VEC4);
    p.uniforms << ProgramInfo::Param("specular", "ks", QString(), GLT_FLOAT_VEC3);
    p.uniforms << ProgramInfo::Param("shininess", "shininess", QString(), GLT_FLOAT);
    m_progs << p;

    p = ProgramInfo();
    p.commonTechniqueName = "PHONG"; //  diffuse texture, specular RGBA
    p.vertShader = "diffusemap.vert";
    p.fragShader = "diffusemap.frag";
    p.attributes << ProgramInfo::Param("position", "vertexPosition", "POSITION", GLT_FLOAT_VEC3);
    p.attributes << ProgramInfo::Param("normal", "vertexNormal", "NORMAL", GLT_FLOAT_VEC3);
    p.attributes << ProgramInfo::Param("texcoord0", "vertexTexCoord", "TEXCOORD_0", GLT_FLOAT_VEC2);
    p.uniforms << ProgramInfo::Param("projection", "projection", "PROJECTION", GLT_FLOAT_MAT4);
    p.uniforms << ProgramInfo::Param("modelView", "modelView", "MODELVIEW", GLT_FLOAT_MAT4);
    p.uniforms << ProgramInfo::Param("normalMatrix", "modelViewNormal", "MODELVIEWINVERSETRANSPOSE", GLT_FLOAT_MAT3);
    p.uniforms << ProgramInfo::Param("lightPosition", "lightPosition", QString(), GLT_FLOAT_VEC4);
    p.uniforms << ProgramInfo::Param("lightIntensity", "lightIntensity", QString(), GLT_FLOAT_VEC3);
    p.uniforms << ProgramInfo::Param("ambient", "ka", QString(), GLT_FLOAT_VEC3);
    p.uniforms << ProgramInfo::Param("specular", "ks", QString(), GLT_FLOAT_VEC3);
    p.uniforms << ProgramInfo::Param("shininess", "shininess", QString(), GLT_FLOAT);
    p.uniforms << ProgramInfo::Param("diffuse", "diffuseTexture", QString(), GLT_SAMPLER_2D);
    m_progs << p;

    p = ProgramInfo();
    p.commonTechniqueName = "PHONG"; // diffuse texture, specular texture
    p.vertShader = "diffusemap.vert";
    p.fragShader = "diffusespecularmap.frag";
    p.attributes << ProgramInfo::Param("position", "vertexPosition", "POSITION", GLT_FLOAT_VEC3);
    p.attributes << ProgramInfo::Param("normal", "vertexNormal", "NORMAL", GLT_FLOAT_VEC3);
    p.attributes << ProgramInfo::Param("texcoord0", "vertexTexCoord", "TEXCOORD_0", GLT_FLOAT_VEC2);
    p.uniforms << ProgramInfo::Param("projection", "projection", "PROJECTION", GLT_FLOAT_MAT4);
    p.uniforms << ProgramInfo::Param("modelView", "modelView", "MODELVIEW", GLT_FLOAT_MAT4);
    p.uniforms << ProgramInfo::Param("normalMatrix", "modelViewNormal", "MODELVIEWINVERSETRANSPOSE", GLT_FLOAT_MAT3);
    p.uniforms << ProgramInfo::Param("lightPosition", "lightPosition", QString(), GLT_FLOAT_VEC4);
    p.uniforms << ProgramInfo::Param("lightIntensity", "lightIntensity", QString(), GLT_FLOAT_VEC3);
    p.uniforms << ProgramInfo::Param("ambient", "ka", QString(), GLT_FLOAT_VEC3);
    p.uniforms << ProgramInfo::Param("shininess", "shininess", QString(), GLT_FLOAT);
    p.uniforms << ProgramInfo::Param("diffuse", "diffuseTexture", QString(), GLT_SAMPLER_2D);
    p.uniforms << ProgramInfo::Param("specular", "specularTexture", QString(), GLT_SAMPLER_2D);
    m_progs << p;

    p = ProgramInfo();
    p.commonTechniqueName = "PHONG"; // diffuse texture, specular RGBA, normalmap texture
    p.vertShader = "normaldiffusemap.vert";
    p.fragShader = "normaldiffusemap.frag";
    p.attributes << ProgramInfo::Param("position", "vertexPosition", "POSITION", GLT_FLOAT_VEC3);
    p.attributes << ProgramInfo::Param("normal", "vertexNormal", "NORMAL", GLT_FLOAT_VEC3);
    p.attributes << ProgramInfo::Param("texcoord0", "vertexTexCoord", "TEXCOORD_0", GLT_FLOAT_VEC2);
    p.attributes << ProgramInfo::Param("tangent", "vertexTangent", "TANGENT", GLT_FLOAT_VEC3);
    p.uniforms << ProgramInfo::Param("projection", "projection", "PROJECTION", GLT_FLOAT_MAT4);
    p.uniforms << ProgramInfo::Param("modelView", "modelView", "MODELVIEW", GLT_FLOAT_MAT4);
    p.uniforms << ProgramInfo::Param("normalMatrix", "modelViewNormal", "MODELVIEWINVERSETRANSPOSE", GLT_FLOAT_MAT3);
    p.uniforms << ProgramInfo::Param("lightPosition", "lightPosition", QString(), GLT_FLOAT_VEC4);
    p.uniforms << ProgramInfo::Param("lightIntensity", "lightIntensity", QString(), GLT_FLOAT_VEC3);
    p.uniforms << ProgramInfo::Param("ambient", "ka", QString(), GLT_FLOAT_VEC3);
    p.uniforms << ProgramInfo::Param("specular", "ks", QString(), GLT_FLOAT_VEC3);
    p.uniforms << ProgramInfo::Param("shininess", "shininess", QString(), GLT_FLOAT);
    p.uniforms << ProgramInfo::Param("diffuse", "diffuseTexture", QString(), GLT_SAMPLER_2D);
    p.uniforms << ProgramInfo::Param("normalmap", "normalTexture", QString(), GLT_SAMPLER_2D);
    m_progs << p;

    p = ProgramInfo();
    p.commonTechniqueName = "PHONG"; // diffuse texture, specular texture, normalmap texture
    p.vertShader = "normaldiffusemap.vert";
    p.fragShader = "normaldiffusespecularmap.frag";
    p.attributes << ProgramInfo::Param("position", "vertexPosition", "POSITION", GLT_FLOAT_VEC3);
    p.attributes << ProgramInfo::Param("normal", "vertexNormal", "NORMAL", GLT_FLOAT_VEC3);
    p.attributes << ProgramInfo::Param("texcoord0", "vertexTexCoord", "TEXCOORD_0", GLT_FLOAT_VEC2);
    p.attributes << ProgramInfo::Param("tangent", "vertexTangent", "TANGENT", GLT_FLOAT_VEC3);
    p.uniforms << ProgramInfo::Param("projection", "projection", "PROJECTION", GLT_FLOAT_MAT4);
    p.uniforms << ProgramInfo::Param("modelView", "modelView", "MODELVIEW", GLT_FLOAT_MAT4);
    p.uniforms << ProgramInfo::Param("normalMatrix", "modelViewNormal", "MODELVIEWINVERSETRANSPOSE", GLT_FLOAT_MAT3);
    p.uniforms << ProgramInfo::Param("lightPosition", "lightPosition", QString(), GLT_FLOAT_VEC4);
    p.uniforms << ProgramInfo::Param("lightIntensity", "lightIntensity", QString(), GLT_FLOAT_VEC3);
    p.uniforms << ProgramInfo::Param("ambient", "ka", QString(), GLT_FLOAT_VEC3);
    p.uniforms << ProgramInfo::Param("shininess", "shininess", QString(), GLT_FLOAT);
    p.uniforms << ProgramInfo::Param("diffuse", "diffuseTexture", QString(), GLT_SAMPLER_2D);
    p.uniforms << ProgramInfo::Param("specular", "specularTexture", QString(), GLT_SAMPLER_2D);
    p.uniforms << ProgramInfo::Param("normalmap", "normalTexture", QString(), GLT_SAMPLER_2D);
    m_progs << p;

    m_subst_es2 << qMakePair(QByteArrayLiteral("$VERSION"), QByteArray());
    m_subst_es2 << qMakePair(QByteArrayLiteral("$ATTRIBUTE"), QByteArrayLiteral("attribute"));
    m_subst_es2 << qMakePair(QByteArrayLiteral("$VVARYING"), QByteArrayLiteral("varying"));
    m_subst_es2 << qMakePair(QByteArrayLiteral("$FVARYING"), QByteArrayLiteral("varying"));
    m_subst_es2 << qMakePair(QByteArrayLiteral("$TEXTURE2D"), QByteArrayLiteral("texture2D"));
    m_subst_es2 << qMakePair(QByteArrayLiteral("$DECL_FRAGCOLOR"), QByteArray());
    m_subst_es2 << qMakePair(QByteArrayLiteral("$FRAGCOLOR"), QByteArrayLiteral("gl_FragColor"));
    m_subst_es2 << qMakePair(QByteArrayLiteral("$HIGHP"), QByteArrayLiteral("highp"));

    m_subst_core << qMakePair(QByteArrayLiteral("$VERSION"), QByteArrayLiteral("#version 150 core"));
    m_subst_core << qMakePair(QByteArrayLiteral("$ATTRIBUTE"), QByteArrayLiteral("in"));
    m_subst_core << qMakePair(QByteArrayLiteral("$VVARYING"), QByteArrayLiteral("out"));
    m_subst_core << qMakePair(QByteArrayLiteral("$FVARYING"), QByteArrayLiteral("in"));
    m_subst_core << qMakePair(QByteArrayLiteral("$TEXTURE2D"), QByteArrayLiteral("texture"));
    m_subst_core << qMakePair(QByteArrayLiteral("$DECL_FRAGCOLOR"), QByteArrayLiteral("out vec4 fragColor;"));
    m_subst_core << qMakePair(QByteArrayLiteral("$FRAGCOLOR"), QByteArrayLiteral("fragColor"));
    m_subst_core << qMakePair(QByteArrayLiteral("$HIGHP "), QByteArray());
}

GltfExporter::ProgramInfo *GltfExporter::chooseProgram(uint materialIndex)
{
    Importer::MaterialInfo matInfo = m_importer->materialInfo(materialIndex);
    const bool hasNormalTexture = matInfo.m_textures.contains("normal");
    const bool hasSpecularTexture = matInfo.m_textures.contains("specular");
    const bool hasDiffuseTexture = matInfo.m_textures.contains("diffuse");

    if (hasNormalTexture && !m_importer->allMeshesForMaterialHaveTangents(materialIndex))
        qWarning() << "WARNING: Tangent vectors not exported while the material requires it. (hint: try -t)";

    if (hasNormalTexture && hasSpecularTexture && hasDiffuseTexture) {
        if (opts.showLog)
            qDebug() << "Using program taking diffuse, specular, normal textures";
        return &m_progs[4];
    }

    if (hasNormalTexture && hasDiffuseTexture) {
        if (opts.showLog)
            qDebug() << "Using program taking diffuse, normal textures";
        return &m_progs[3];
    }

    if (hasSpecularTexture && hasDiffuseTexture) {
        if (opts.showLog)
            qDebug() << "Using program taking diffuse, specular textures";
        return &m_progs[2];
    }

    if (hasDiffuseTexture) {
        if (opts.showLog)
            qDebug() << "Using program taking diffuse texture";
        return &m_progs[1];
    }

    if (opts.showLog)
        qDebug() << "Using program without textures";
    return &m_progs[0];
}

QString GltfExporter::exportNode(const Importer::Node *n, QJsonObject &nodes)
{
    QJsonObject node;
    node["name"] = n->name;
    QJsonArray children;
    for (const Importer::Node *c : n->children) {
        if (nodeIsUseful(c))
            children << exportNode(c, nodes);
    }
    node["children"] = children;
    QJsonArray matrix;
    const float *mtxp = n->transformation.constData();
    for (int j = 0; j < 16; ++j)
        matrix.append(*mtxp++);
    node["matrix"] = matrix;
    QJsonArray meshList;
    for (int j = 0; j < n->meshes.count(); ++j)
        meshList.append(m_importer->meshInfo(n->meshes[j]).name);
    if (!meshList.isEmpty()) {
        node["meshes"] = meshList;
    } else {
        QHash<QString, Importer::CameraInfo> cam = m_importer->cameraInfo();
        if (cam.contains(n->name))
            node["camera"] = cam[n->name].name;
    }

    nodes[n->uniqueName] = node;
    return n->uniqueName;
}

static inline QJsonArray col2jsvec(const QVector<float> &color, bool alpha = false)
{
    QJsonArray arr;
    arr << color[0] << color[1] << color[2];
    if (alpha)
        arr << color[3];
    return arr;
}

static inline QJsonArray vec2jsvec(const QVector<float> &v)
{
    QJsonArray arr;
    for (int i = 0; i < v.count(); ++i)
        arr << v[i];
    return arr;
}

static inline void promoteColorsToRGBA(QJsonObject *obj)
{
    QJsonObject::iterator it = obj->begin(), itEnd = obj->end();
    while (it != itEnd) {
        QJsonArray arr = it.value().toArray();
        if (arr.count() == 3) {
            const QString key = it.key();
            if (key == QStringLiteral("ambient")
                    || key == QStringLiteral("diffuse")
                    || key == QStringLiteral("specular")) {
                arr.append(1);
                *it = arr;
            }
        }
        ++it;
    }
}

void GltfExporter::exportMaterials(QJsonObject &materials, QHash<QString, QString> *textureNameMap)
{
    for (uint i = 0; i < m_importer->materialCount(); ++i) {
        Importer::MaterialInfo matInfo = m_importer->materialInfo(i);
        QJsonObject material;
        material["name"] = matInfo.originalName;

        bool opaque = true;
        QJsonObject vals;
        for (QHash<QByteArray, QString>::const_iterator it = matInfo.m_textures.constBegin(); it != matInfo.m_textures.constEnd(); ++it) {
            if (!textureNameMap->contains(it.value()))
                textureNameMap->insert(it.value(), newTextureName());
            QByteArray key = it.key();
            if (key == QByteArrayLiteral("normal")) // avoid clashing with the vertex normals
                key = QByteArrayLiteral("normalmap");
            // alpha is supported for diffuse textures, but have to check the image data to decide if blending is needed
            if (key == QByteArrayLiteral("diffuse")) {
                QString imgFn = opts.outDir + it.value();
                if (m_imageHasAlpha.contains(imgFn)) {
                    if (m_imageHasAlpha[imgFn])
                        opaque = false;
                } else {
#ifdef HAS_QIMAGE
                    QImage img(imgFn);
                    if (!img.isNull()) {
                        if (img.hasAlphaChannel()) {
                            for (int y = 0; opaque && y < img.height(); ++y)
                                for (int x = 0; opaque && x < img.width(); ++x)
                                    if (qAlpha(img.pixel(x, y)) < 255)
                                        opaque = false;
                        }
                        m_imageHasAlpha[imgFn] = !opaque;
                    } else {
                        qWarning() << "WARNING: Cannot determine presence of alpha for" << imgFn;
                    }
#else
                    qWarning() << "WARNING: No image support, assuming all textures are opaque";
#endif
                }
            }
            vals[key] = textureNameMap->value(it.value());
        }
        for (QHash<QByteArray, float>::const_iterator it = matInfo.m_values.constBegin();
             it != matInfo.m_values.constEnd(); ++it) {
            if (vals.contains(it.key()))
                continue;
            vals[it.key()] = it.value();
        }
        for (QHash<QByteArray, QVector<float> >::const_iterator it = matInfo.m_colors.constBegin();
             it != matInfo.m_colors.constEnd(); ++it) {
            if (vals.contains(it.key()))
                continue;
            // alpha is supported for the diffuse color. < 1 will enable blending.
            const bool alpha = it.key() == QStringLiteral("diffuse");
            if (alpha && it.value()[3] < 1.0f)
                opaque = false;
            vals[it.key()] = col2jsvec(it.value(), alpha);
        }
        if (opts.shaders)
            material["values"] = vals;

        ProgramInfo *prog = chooseProgram(i);
        TechniqueInfo techniqueInfo;
        bool needsNewTechnique = true;
        for (int j = 0; j < m_techniques.count(); ++j) {
            if (m_techniques[j].prog == prog) {
                techniqueInfo = m_techniques[j];
                needsNewTechnique = opaque != techniqueInfo.opaque;
            }
            if (!needsNewTechnique)
                break;
        }
        if (needsNewTechnique) {
            QString techniqueName = newTechniqueName();
            techniqueInfo = TechniqueInfo(techniqueName, opaque, prog);
            m_techniques.append(techniqueInfo);
            m_usedPrograms.insert(prog);
        }

        if (opts.shaders) {
            if (opts.showLog)
                qDebug().noquote() << "Material #" << i << "->" << techniqueInfo.name;

            material["technique"] = techniqueInfo.name;
            if (opts.genCore) {
                material["techniqueCore"] = techniqueInfo.coreName;
                material["techniqueGL2"] = techniqueInfo.gl2Name;
            }
        }

        if (opts.commonMat) {
            // The built-in shaders we output are of little use in practice.
            // Ideally we want Qt3D's own standard materials in order to have our
            // models participate in lighting for example. To achieve this, output
            // a KHR_materials_common block which Qt3D's loader will recognize and
            // prefer over the shader-based techniques.
            if (!prog->commonTechniqueName.isEmpty()) {
                QJsonObject commonMat;
                commonMat["technique"] = prog->commonTechniqueName;
                // Set the values as-is. "normalmap" is our own extension, not in the spec.
                // However, RGB colors have to be promoted to RGBA since the spec uses
                // vec4, and all types are pre-defined for common material values.
                promoteColorsToRGBA(&vals);
                commonMat["values"] = vals;
                if (!opaque)
                    commonMat["transparent"] = true;
                QJsonObject extensions;
                extensions["KHR_materials_common"] = commonMat;
                material["extensions"] = extensions;
            }
        }

        materials[matInfo.name] = material;
    }
}

void GltfExporter::writeShader(const QString &src, const QString &dst, const QVector<QPair<QByteArray, QByteArray> > &substTab)
{
    for (const Shader shader : shaders) {
        QByteArray name = src.toUtf8();
        if (!qstrcmp(shader.name, name.constData())) {
            QString outfn = opts.outDir + dst;
            QFile outf(outfn);
            if (outf.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
                m_files.insert(QFileInfo(outf.fileName()).fileName());
                if (opts.showLog)
                    qDebug() << "Writing" << outfn;
                const auto lines = QString::fromUtf8(shader.text).split('\n');
                for (QString line : lines) {
                    for (const auto &subst : substTab)
                        line.replace(subst.first, subst.second);
                    line += QStringLiteral("\n");
                    outf.write(line.toUtf8());
                }
            }
            return;
        }
    }
    qWarning() << "ERROR: No shader found for" << src;
}

void GltfExporter::exportParameter(QJsonObject &dst, const QVector<ProgramInfo::Param> &params)
{
    for (const ProgramInfo::Param &param : params) {
        QJsonObject parameter;
        parameter["type"] = int(param.type);
        if (!param.semantic.isEmpty())
            parameter["semantic"] = param.semantic;
        if (param.name == QStringLiteral("lightIntensity"))
            parameter["value"] = QJsonArray() << 1 << 1 << 1;
        if (param.name == QStringLiteral("lightPosition"))
            parameter["value"] = QJsonArray() << 0 << 0 << 0 << 1;
        dst[param.name] = parameter;
    }
}

namespace {
struct ProgramNames
{
    QString name;
    QString coreName;
};
}

void GltfExporter::exportTechniques(QJsonObject &obj, const QString &basename)
{
    if (!opts.shaders)
        return;

    QJsonObject shaders;
    QHash<QString, QString> shaderMap;
    for (ProgramInfo *prog : qAsConst(m_usedPrograms)) {
        QString newName;
        if (!shaderMap.contains(prog->vertShader)) {
            QJsonObject vertexShader;
            vertexShader["type"] = 35633;
            if (newName.isEmpty())
                newName = newShaderName();
            QString key = basename + QStringLiteral("_") + newName + QStringLiteral("_v");
            QString fn = QString(QStringLiteral("%1.vert")).arg(key);
            vertexShader["uri"] = fn;
            writeShader(prog->vertShader, fn, m_subst_es2);
            if (opts.genCore) {
                QJsonObject coreVertexShader;
                QString coreKey = QString(QStringLiteral("%1_core").arg(key));
                fn = QString(QStringLiteral("%1.vert")).arg(coreKey);
                coreVertexShader["type"] = 35633;
                coreVertexShader["uri"] = fn;
                writeShader(prog->vertShader, fn, m_subst_core);
                shaders[coreKey] = coreVertexShader;
                shaderMap.insert(QString(prog->vertShader + QStringLiteral("_core")), coreKey);
            }
            shaders[key] = vertexShader;
            shaderMap.insert(prog->vertShader, key);
        }
        if (!shaderMap.contains(prog->fragShader)) {
            QJsonObject fragmentShader;
            fragmentShader["type"] = 35632;
            if (newName.isEmpty())
                newName = newShaderName();
            QString key = basename + QStringLiteral("_") + newName + QStringLiteral("_f");
            QString fn = QString(QStringLiteral("%1.frag")).arg(key);
            fragmentShader["uri"] = fn;
            writeShader(prog->fragShader, fn, m_subst_es2);
            if (opts.genCore) {
                QJsonObject coreFragmentShader;
                QString coreKey = QString(QStringLiteral("%1_core").arg(key));
                fn = QString(QStringLiteral("%1.frag")).arg(coreKey);
                coreFragmentShader["type"] = 35632;
                coreFragmentShader["uri"] = fn;
                writeShader(prog->fragShader, fn, m_subst_core);
                shaders[coreKey] = coreFragmentShader;
                shaderMap.insert(QString(prog->fragShader + QStringLiteral("_core")), coreKey);
            }
            shaders[key] = fragmentShader;
            shaderMap.insert(prog->fragShader, key);
        }
    }
    obj["shaders"] = shaders;

    QJsonObject programs;
    QHash<const ProgramInfo *, ProgramNames> programMap;
    for (const ProgramInfo *prog : qAsConst(m_usedPrograms)) {
        QJsonObject program;
        program["vertexShader"] = shaderMap[prog->vertShader];
        program["fragmentShader"] = shaderMap[prog->fragShader];
        QJsonArray attrs;
        for (const ProgramInfo::Param &param : prog->attributes) {
            attrs << param.nameInShader;
        }
        program["attributes"] = attrs;
        QString programName = newProgramName();
        programMap[prog].name = programName;
        programs[programMap[prog].name] = program;
        if (opts.genCore) {
            program["vertexShader"] = shaderMap[QString(prog->vertShader + QLatin1String("_core"))];
            program["fragmentShader"] = shaderMap[QString(prog->fragShader + QLatin1String("_core"))];
            QJsonArray attrs;
            for (const ProgramInfo::Param &param : prog->attributes) {
                attrs << param.nameInShader;
            }
            program["attributes"] = attrs;
            programMap[prog].coreName = programName + QLatin1String("_core");
            programs[programMap[prog].coreName] = program;
        }
    }
    obj["programs"] = programs;

    QJsonObject techniques;
    for (const TechniqueInfo &techniqueInfo : qAsConst(m_techniques)) {
        QJsonObject technique;
        QJsonObject parameters;
        const ProgramInfo *prog = techniqueInfo.prog;
        exportParameter(parameters, prog->attributes);
        exportParameter(parameters, prog->uniforms);
        technique["parameters"] = parameters;
        technique["program"] = programMap[prog].name;
        QJsonObject progAttrs;
        for (const ProgramInfo::Param &param : prog->attributes) {
            progAttrs[param.nameInShader] = param.name;
        }
        technique["attributes"] = progAttrs;
        QJsonObject progUniforms;
        for (const ProgramInfo::Param &param : prog->uniforms) {
            progUniforms[param.nameInShader] = param.name;
        }
        technique["uniforms"] = progUniforms;
        QJsonObject states;
        QJsonArray enabledStates;
        enabledStates << GLT_DEPTH_TEST << GLT_CULL_FACE;
        if (!techniqueInfo.opaque) {
            enabledStates << GLT_BLEND;
            QJsonObject funcs;
            // GL_ONE, GL_ONE_MINUS_SRC_ALPHA
            funcs["blendFuncSeparate"] = QJsonArray() << 1 << 771 << 1 << 771;
            states["functions"] = funcs;
        }
        states["enable"] = enabledStates;
        technique["states"] = states;
        techniques[techniqueInfo.name] = technique;

        if (opts.genCore) {
            //GL2 (same as ES2)
            techniques[techniqueInfo.gl2Name] = technique;

            //Core
            technique["program"] = programMap[prog].coreName;
            techniques[techniqueInfo.coreName] = technique;
        }
    }
    obj["techniques"] = techniques;
}

void GltfExporter::exportAnimations(QJsonObject &obj,
                                    QVector<Importer::BufferInfo> &bufList,
                                    QVector<Importer::MeshInfo::BufferView> &bvList,
                                    QVector<Importer::MeshInfo::Accessor> &accList)
{
    const auto animationInfos = m_importer->animations();
    if (animationInfos.empty()) {
        obj["animations"] = QJsonObject();
        return;
    }

    QString bvName = newBufferViewName();
    QByteArray extraData;

    int sz = 0;
    for (const Importer::AnimationInfo &ai : animationInfos)
        sz += ai.keyFrames.count() * (1 + 3 + 4 + 3) * sizeof(float);
    extraData.resize(sz);

    float *base = reinterpret_cast<float *>(extraData.data());
    float *p = base;

    QJsonObject animations;
    for (const Importer::AnimationInfo &ai : animationInfos) {
        QJsonObject animation;
        animation["name"] = ai.name;
        animation["count"] = ai.keyFrames.count();
        QJsonObject samplers;
        QJsonArray channels;

        if (ai.hasTranslation) {
            QJsonObject sampler;
            sampler["input"] = QStringLiteral("TIME");
            sampler["interpolation"] = QStringLiteral("LINEAR");
            sampler["output"] = QStringLiteral("translation");
            samplers["sampler_translation"] = sampler;
            QJsonObject channel;
            channel["sampler"] = QStringLiteral("sampler_translation");
            QJsonObject target;
            target["id"] = ai.targetNode;
            target["path"] = QStringLiteral("translation");
            channel["target"] = target;
            channels << channel;
        }
        if (ai.hasRotation) {
            QJsonObject sampler;
            sampler["input"] = QStringLiteral("TIME");
            sampler["interpolation"] = QStringLiteral("LINEAR");
            sampler["output"] = QStringLiteral("rotation");
            samplers["sampler_rotation"] = sampler;
            QJsonObject channel;
            channel["sampler"] = QStringLiteral("sampler_rotation");
            QJsonObject target;
            target["id"] = ai.targetNode;
            target["path"] = QStringLiteral("rotation");
            channel["target"] = target;
            channels << channel;
        }
        if (ai.hasScale) {
            QJsonObject sampler;
            sampler["input"] = QStringLiteral("TIME");
            sampler["interpolation"] = QStringLiteral("LINEAR");
            sampler["output"] = QStringLiteral("scale");
            samplers["sampler_scale"] = sampler;
            QJsonObject channel;
            channel["sampler"] = QStringLiteral("sampler_scale");
            QJsonObject target;
            target["id"] = ai.targetNode;
            target["path"] = QStringLiteral("scale");
            channel["target"] = target;
            channels << channel;
        }

        animation["samplers"] = samplers;
        animation["channels"] = channels;
        QJsonObject parameters;

        // Multiple animations sharing the same data should ideally use the
        // same accessors. This we unfortunately cannot do due to assimp's/our
        // own data structures so everything will get its own accessor and data
        // for now.

        Importer::MeshInfo::Accessor acc;
        acc.name = newAccessorName();
        acc.bufferView = bvName;
        acc.count = ai.keyFrames.count();
        acc.componentType = GLT_FLOAT;
        acc.type = QStringLiteral("SCALAR");
        acc.offset = uint((p - base) * sizeof(float));
        for (const Importer::KeyFrame &kf : ai.keyFrames)
            *p++ = kf.t;
        parameters["TIME"] = acc.name;
        accList << acc;

        if (ai.hasTranslation) {
            acc.name = newAccessorName();
            acc.componentType = GLT_FLOAT;
            acc.type = QStringLiteral("VEC3");
            acc.offset = uint((p - base) * sizeof(float));
            QVector<float> lastV;
            for (const Importer::KeyFrame &kf : ai.keyFrames) {
                const QVector<float> *v = kf.transValid ? &kf.trans : &lastV;
                *p++ = v->at(0);
                *p++ = v->at(1);
                *p++ = v->at(2);
                if (kf.transValid)
                    lastV = *v;
            }
            parameters["translation"] = acc.name;
            accList << acc;
        }
        if (ai.hasRotation) {
            acc.name = newAccessorName();
            acc.componentType = GLT_FLOAT;
            acc.type = QStringLiteral("VEC4");
            acc.offset = uint((p - base) * sizeof(float));
            QVector<float> lastV;
            for (const Importer::KeyFrame &kf : ai.keyFrames) {
                const QVector<float> *v = kf.rotValid ? &kf.rot : &lastV;
                *p++ = v->at(1); // x
                *p++ = v->at(2); // y
                *p++ = v->at(3); // z
                *p++ = v->at(0); // w
                if (kf.rotValid)
                    lastV = *v;
            }
            parameters["rotation"] = acc.name;
            accList << acc;
        }
        if (ai.hasScale) {
            acc.name = newAccessorName();
            acc.componentType = GLT_FLOAT;
            acc.type = QStringLiteral("VEC3");
            acc.offset = uint((p - base) * sizeof(float));
            QVector<float> lastV;
            for (const Importer::KeyFrame &kf : ai.keyFrames) {
                const QVector<float> *v = kf.scaleValid ? &kf.scale : &lastV;
                *p++ = v->at(0);
                *p++ = v->at(1);
                *p++ = v->at(2);
                if (kf.scaleValid)
                    lastV = *v;
            }
            parameters["scale"] = acc.name;
            accList << acc;
        }
        animation["parameters"] = parameters;

        animations[newAnimationName()] = animation;
    }
    obj["animations"] = animations;

    // Now all the key frame data is in extraData. Append it to the first buffer
    // and create a single buffer view for it.
    if (!extraData.isEmpty()) {
        if (bufList.isEmpty()) {
            Importer::BufferInfo b;
            b.name = QStringLiteral("buf");
            bufList << b;
        }
        Importer::BufferInfo &buf(bufList[0]);
        Importer::MeshInfo::BufferView bv;
        bv.name = bvName;
        bv.offset = buf.data.size();
        bv.length = uint((p - base) * sizeof(float));
        bv.componentType = GLT_FLOAT;
        bvList << bv;
        extraData.resize(bv.length);
        buf.data += extraData;
        if (opts.showLog)
            qDebug().noquote() << "Animation data in buffer uses" << extraData.size() << "bytes";
    }
}

void GltfExporter::save(const QString &inputFilename)
{
    if (opts.showLog)
        qDebug() << "Exporting";

    m_files.clear();
    m_techniques.clear();
    m_usedPrograms.clear();

    QFile f;
    QString basename = QFileInfo(inputFilename).baseName();
    QString bufNameTempl = basename + QStringLiteral("_%1.bin");

    copyExternalTextures(inputFilename);
    exportEmbeddedTextures();
    compressTextures();

    m_obj = QJsonObject();

    QVector<Importer::BufferInfo> bufList = m_importer->buffers();
    QVector<Importer::MeshInfo::BufferView> bvList = m_importer->bufferViews();
    QVector<Importer::MeshInfo::Accessor> accList = m_importer->accessors();

    // Animations add data to the buffer so process them first.
    exportAnimations(m_obj, bufList, bvList, accList);

    QJsonObject asset;
    asset["generator"] = QString(QStringLiteral("qgltf %1")).arg(QCoreApplication::applicationVersion());
    asset["version"] = QStringLiteral("1.0");
    asset["premultipliedAlpha"] = true;
    m_obj["asset"] = asset;

    for (int i = 0; i < bufList.count(); ++i) {
        QString bufName = bufNameTempl.arg(i + 1);
        f.setFileName(opts.outDir + bufName);
        if (opts.showLog)
            qDebug().noquote() << (opts.compress ? "Writing (compressed)" : "Writing") << (opts.outDir + bufName);
        if (f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
            m_files.insert(QFileInfo(f.fileName()).fileName());
            QByteArray data = bufList[i].data;
            if (opts.compress)
                data = qCompress(data);
            f.write(data);
            f.close();
        }
    }

    QJsonObject buffers;
    for (int i = 0; i < bufList.count(); ++i) {
        QJsonObject buffer;
        buffer["byteLength"] = bufList[i].data.size();
        buffer["type"] = QStringLiteral("arraybuffer");
        buffer["uri"] = bufNameTempl.arg(i + 1);
        if (opts.compress)
            buffer["compression"] = QStringLiteral("Qt");
        buffers[bufList[i].name] = buffer;
    }
    m_obj["buffers"] = buffers;

    QJsonObject bufferViews;
    for (const Importer::MeshInfo::BufferView &bv : qAsConst(bvList)) {
        QJsonObject bufferView;
        bufferView["buffer"] = bufList[bv.bufIndex].name;
        bufferView["byteLength"] = int(bv.length);
        bufferView["byteOffset"] = int(bv.offset);
        if (bv.target)
            bufferView["target"] = int(bv.target);
        bufferViews[bv.name] = bufferView;
    }
    m_obj["bufferViews"] = bufferViews;

    QJsonObject accessors;
    for (const Importer::MeshInfo::Accessor &acc : qAsConst(accList)) {
        QJsonObject accessor;
        accessor["bufferView"] = acc.bufferView;
        accessor["byteOffset"] = int(acc.offset);
        accessor["byteStride"] = int(acc.stride);
        accessor["count"] = int(acc.count);
        accessor["componentType"] = int(acc.componentType);
        accessor["type"] = acc.type;
        if (!acc.minVal.isEmpty() && !acc.maxVal.isEmpty()) {
            accessor["min"] = vec2jsvec(acc.minVal);
            accessor["max"] = vec2jsvec(acc.maxVal);
        }
        accessors[acc.name] = accessor;
    }
    m_obj["accessors"] = accessors;

    QJsonObject meshes;
    for (uint i = 0; i < m_importer->meshCount(); ++i) {
        const Importer::MeshInfo meshInfo = m_importer->meshInfo(i);
        QJsonObject mesh;
        mesh["name"] = meshInfo.originalName;
        QJsonArray prims;
        QJsonObject prim;
        prim["mode"] = 4; // triangles
        QJsonObject attrs;
        for (const Importer::MeshInfo::Accessor &acc : meshInfo.accessors) {
            if (acc.usage != QStringLiteral("INDEX"))
                attrs[acc.usage] = acc.name;
            else
                prim["indices"] = acc.name;
        }
        prim["attributes"] = attrs;
        prim["material"] = m_importer->materialInfo(meshInfo.materialIndex).name;
        prims.append(prim);
        mesh["primitives"] = prims;
        meshes[meshInfo.name] = mesh;
    }
    m_obj["meshes"] = meshes;

    QJsonObject cameras;
    const auto cameraInfos = m_importer->cameraInfo();
    for (const Importer::CameraInfo &camInfo : cameraInfos) {
        QJsonObject camera;
        QJsonObject persp;
        persp["aspect_ratio"] = camInfo.aspectRatio;
        persp["yfov"] = camInfo.yfov;
        persp["znear"] = camInfo.znear;
        persp["zfar"] = camInfo.zfar;
        camera["perspective"] = persp;
        camera["type"] = QStringLiteral("perspective");
        cameras[camInfo.name] = camera;
    }
    m_obj["cameras"] = cameras;

    QJsonArray sceneNodes;
    QJsonObject nodes;
    for (const Importer::Node *n : qAsConst(m_importer->rootNode()->children)) {
        if (nodeIsUseful(n))
            sceneNodes << exportNode(n, nodes);
    }
    m_obj["nodes"] = nodes;

    QJsonObject scenes;
    QJsonObject defaultScene;
    defaultScene["nodes"] = sceneNodes;
    scenes["defaultScene"] = defaultScene;
    m_obj["scenes"] = scenes;
    m_obj["scene"] = QStringLiteral("defaultScene");

    QJsonObject materials;
    QHash<QString, QString> textureNameMap;
    exportMaterials(materials, &textureNameMap);
    m_obj["materials"] = materials;

    QJsonObject textures;
    QHash<QString, QString> imageMap; // uri -> key
    for (QHash<QString, QString>::const_iterator it = textureNameMap.constBegin(); it != textureNameMap.constEnd(); ++it) {
        QJsonObject texture;
        if (!imageMap.contains(it.key()))
            imageMap[it.key()] = newImageName();
        texture["source"] = imageMap[it.key()];
        texture["format"] = 0x1908; // RGBA
        const bool compressed = m_compressedTextures.contains(it.key());
        texture["internalFormat"] = !compressed ? 0x1908 : 0x8D64; // RGBA / ETC1
        texture["sampler"] = !compressed ? QStringLiteral("sampler_mip_rep") : QStringLiteral("sampler_nonmip_rep");
        texture["target"] = 3553; // TEXTURE_2D
        texture["type"] = 5121; // UNSIGNED_BYTE
        textures[it.value()] = texture;
    }
    m_obj["textures"] = textures;

    QJsonObject images;
    for (QHash<QString, QString>::const_iterator it = imageMap.constBegin(); it != imageMap.constEnd(); ++it) {
        QJsonObject image;
        image["uri"] = m_compressedTextures.contains(it.key()) ? m_compressedTextures[it.key()] : it.key();
        images[it.value()] = image;
    }
    m_obj["images"] = images;

    QJsonObject samplers;
    QJsonObject sampler;
    sampler["magFilter"] = 9729; // LINEAR
    sampler["minFilter"] = 9987; // LINEAR_MIPMAP_LINEAR
    sampler["wrapS"] = 10497; // REPEAT
    sampler["wrapT"] = 10497;
    samplers["sampler_mip_rep"] = sampler;
    // Compressed textures may not support mipmapping with GLES.
    if (!m_compressedTextures.isEmpty()) {
        sampler["minFilter"] = 9729; // LINEAR
        samplers["sampler_nonmip_rep"] = sampler;
    }
    m_obj["samplers"] = samplers;

    exportTechniques(m_obj, basename);

    m_doc.setObject(m_obj);

    QString gltfName = opts.outDir + basename + QStringLiteral(".qgltf");
    f.setFileName(gltfName);
    if (opts.showLog)
        qDebug().noquote() << (opts.genBin ? "Writing (binary JSON)" : "Writing") << gltfName;

    if (opts.genBin) {
        if (f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
            m_files.insert(QFileInfo(f.fileName()).fileName());
            QByteArray json = m_doc.toBinaryData();
            f.write(json);
            f.close();
        }
    } else {
        if (f.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
            m_files.insert(QFileInfo(f.fileName()).fileName());
            QByteArray json = m_doc.toJson(opts.compact ? QJsonDocument::Compact : QJsonDocument::Indented);
            f.write(json);
            f.close();
        }
    }

    QString qrcName = opts.outDir + basename + QStringLiteral(".qrc");
    f.setFileName(qrcName);
    if (opts.showLog)
        qDebug().noquote() << "Writing" << qrcName;
    if (f.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
        QByteArray pre = "<RCC><qresource prefix=\"/models\">\n";
        QByteArray post = "</qresource></RCC>\n";
        f.write(pre);
        for (const QString &file : qAsConst(m_files)) {
            QString line = QString(QStringLiteral("  <file>%1</file>\n")).arg(file);
            f.write(line.toUtf8());
        }
        f.write(post);
        f.close();
    }

    if (opts.showLog)
        qDebug() << "Done\n";
}

static const char *description =
        "qgltf uses Assimp to import a variety of 3D model formats "
        "and export it into fast-to-load, optimized glTF "
        "assets embedded into Qt resource files.\n\n"
        "Note: this tool should typically not be invoked directly. Instead, "
        "let qmake manage it based on QT3D_MODELS in the .pro file.\n\n"
        "For standard Qt 3D usage the recommended options are -b -S.";

int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);
    app.setApplicationVersion(QStringLiteral("0.2"));
    app.setApplicationName(QStringLiteral("Qt glTF converter"));

    QCommandLineParser cmdLine;
    cmdLine.addHelpOption();
    cmdLine.addVersionOption();
    cmdLine.setApplicationDescription(QString::fromUtf8(description));
    QCommandLineOption outDirOpt(QStringLiteral("d"), QStringLiteral("Place all output data into <dir>"), QStringLiteral("dir"));
    cmdLine.addOption(outDirOpt);
    QCommandLineOption binOpt(QStringLiteral("b"), QStringLiteral("Store binary JSON data in the .qgltf file"));
    cmdLine.addOption(binOpt);
    QCommandLineOption compactOpt(QStringLiteral("m"), QStringLiteral("Store compact JSON in the .qgltf file"));
    cmdLine.addOption(compactOpt);
    QCommandLineOption compOpt(QStringLiteral("c"), QStringLiteral("qCompress() vertex/index data in the .bin file"));
    cmdLine.addOption(compOpt);
    QCommandLineOption tangentOpt(QStringLiteral("t"), QStringLiteral("Generate tangent vectors"));
    cmdLine.addOption(tangentOpt);
    QCommandLineOption nonInterleavedOpt(QStringLiteral("n"), QStringLiteral("Use non-interleaved buffer layout"));
    cmdLine.addOption(nonInterleavedOpt);
    QCommandLineOption scaleOpt(QStringLiteral("e"), QStringLiteral("Scale vertices by the float scale factor <factor>"), QStringLiteral("factor"));
    cmdLine.addOption(scaleOpt);
    QCommandLineOption coreOpt(QStringLiteral("g"), QStringLiteral("Generate OpenGL 3.2+ core profile shaders too"));
    cmdLine.addOption(coreOpt);
    QCommandLineOption etc1Opt(QStringLiteral("1"), QStringLiteral("Generate ETC1 compressed textures by invoking etc1tool (PNG only)"));
    cmdLine.addOption(etc1Opt);
    QCommandLineOption noCommonMatOpt(QStringLiteral("T"), QStringLiteral("Do not generate KHR_materials_common block"));
    cmdLine.addOption(noCommonMatOpt);
    QCommandLineOption noShadersOpt(QStringLiteral("S"), QStringLiteral("Do not generate shaders/programs/techniques"));
    cmdLine.addOption(noShadersOpt);
    QCommandLineOption silentOpt(QStringLiteral("s"), QStringLiteral("Silence debug output"));
    cmdLine.addOption(silentOpt);
    cmdLine.process(app);
    opts.outDir = cmdLine.value(outDirOpt);
    opts.genBin = cmdLine.isSet(binOpt);
    opts.compact = cmdLine.isSet(compactOpt);
    opts.compress = cmdLine.isSet(compOpt);
    opts.genTangents = cmdLine.isSet(tangentOpt);
    opts.interleave = !cmdLine.isSet(nonInterleavedOpt);
    opts.scale = 1;
    if (cmdLine.isSet(scaleOpt)) {
        bool ok = false;
        float v;
        v = cmdLine.value(scaleOpt).toFloat(&ok);
        if (ok)
            opts.scale = v;
    }
    opts.genCore = cmdLine.isSet(coreOpt);
    opts.texComp = cmdLine.isSet(etc1Opt) ? Options::ETC1 : Options::NoTextureCompression;
    opts.commonMat = !cmdLine.isSet(noCommonMatOpt);
    opts.shaders = !cmdLine.isSet(noShadersOpt);
    opts.showLog = !cmdLine.isSet(silentOpt);
    if (!opts.outDir.isEmpty()) {
        if (!opts.outDir.endsWith('/'))
            opts.outDir.append('/');
        QDir().mkpath(opts.outDir);
    }

    const auto fileNames = cmdLine.positionalArguments();
    if (fileNames.isEmpty())
        cmdLine.showHelp();

    AssimpImporter importer;
    GltfExporter exporter(&importer);
    for (const QString &fn : fileNames) {
        if (!importer.load(fn)) {
            qWarning() << "Failed to import" << fn;
            continue;
        }
        exporter.save(fn);
    }

    return 0;
}