summaryrefslogtreecommitdiff
path: root/src/threed/scene/qglscenenode.cpp
blob: abe3b959d41747d4decfa79a11b789cd2bba368a (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
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qglabstractscene.h"
#include "qglscenenode.h"
#include "qglscenenode_p.h"
#include "qglpicknode.h"
#include "qglpainter.h"
#include "qgeometrydata.h"
#include "qglmaterialcollection.h"
#include "qglrendersequencer.h"
#include "qglabstracteffect.h"
#include "qgraphicstransform3d.h"

#ifndef QT_NO_DEBUG_STREAM
#include "qglmaterialcollection.h"
#include "qgltexture2d.h"
#endif

#include <QtGui/qmatrix4x4.h>
#if !defined(QT_NO_THREAD)
#include <QtCore/qthread.h>
#include <QtWidgets/qapplication.h>
#endif

QT_BEGIN_NAMESPACE

/*!
    \class QGLSceneNode
    \brief The QGLSceneNode class defines a node in a 3D scene.
    \since 4.8
    \ingroup qt3d
    \ingroup qt3d::scene

    QGLSceneNode represents one component of a scene.  The QGLSceneNode
    class manages materials, effects and transformations for itself and
    for its child nodes.

    As a general approach to 3D applications, a tree of QGLSceneNodes may
    be constructed, geometry added to them, materials and transformations
    applied, all during application initialization; and then by simply
    calling the draw() function the scene is easily rendered for each frame.

    \section1 Geometry

    Multiple QGLSceneNodes can reference the same geometry, whilst
    applying different transformations and treatments to it.  Since
    QGLSceneNode is a QObject sub class it cannot be copied directly, so
    instead use the clone() function for this purpose.

    A scene node allows referencing into sub-parts of geometry, via the start
    and count properties.

    The start index is an offset into the geometry at which drawing will start.
    The default start index is 0, so that drawing will start from the beginning
    of the geometry.  The count dictates how many vertices will be drawn.  The
    default count is 0, which instructs the underlying geometry to draw all
    vertices.

    A node may have no geometry, that is \c{geometry().count() == 0}.  This is
    useful for example to have one node controlling or collecting together
    several child nodes to be manipulated as a unit.

    \section1 Materials

    Also a node may have a local material.  This allows drawing the same geometry
    with different materials (which includes different textures).

    When accessing a QGLSceneNode via QML, or for simple applications, the
    pointer based material functions are convenient and intuitive, saving the
    trouble of adding the material pointer to the correct palette:
    \list
        \i material()
        \i setMaterial()
        \i backMaterial()
        \i setBackMaterial()
    \endlist

    For more complex applications; for example building model loaders, or for
    larger scenes; where you need to explicitly manage materials via a palette,
    use the index based functions:
    \list
        \i materialIndex()
        \i setMaterialIndex()
        \i backMaterialIndex()
        \i setBackMaterialIndex()
    \endlist

    The behaviour of both with respect to faces is the same - if a material()
    is specified but no backMaterial() is specified, then the material() is
    applied to both faces; if both material() and backMaterial() are non-null
    then they are applied to their specific faces.

    \section1 Transformations

    Typically the local transformation matrix is set by the process that
    constructed the node:  in the case of an imported model, it is likely
    to have been specified by the model file.  To make individual changes
    to the location or orientation of this node, use the position() and
    transforms() properties.

    \section1 Scene Graph

    Use childNodes() to obtain the list of child nodes, and add and remove
    child nodes by the addNode() and removeNode() methods.  If a QGLSceneNode
    is constructed with a QGLSceneNode parent, then addNode() will be
    called implicitly on the parent.

    A child may be a child multiple times, a child may be under more than one
    parent, and several parents may reference the same child.  There is however
    no protection against cycles, so a child must not be a parent of itself,
    even if indirectly.

    A child node for the purposes of rendering means a child added via the
    addNode() method.  The default QGLSceneNode constructor will check to
    see if its parent is a QGLSceneNode and add itself via the addNode()
    function if it is.

    To help debug a scene, use the qDumpScene() function to get a printout
    on stderr of the entire structure of the scene below the argument node.

    \section1 Debugging Lighting Normals

    The ViewNormals option is an advanced feature for use when inspecting
    and debugging models or geometry in a scene.  The lighting normals
    are displayed as a straight line from the vertex pointing in
    the direction of the lighting normal.  This is useful for
    example to show where normals are inverted or wrongly
    calculated.

    The setting of the ViewNormals flag is not propagated to child nodes,
    instead set the flag to true for the node or nodes where its
    needed.  To set the flag on all child nodes use code like:

    \code
    foreach (QGLSceneNode *node, scene.allChildren())
        node->setNormalViewEnabled(true);
    \endcode

    \image spiky-teapot.png

    \sa QGLAbstractScene
*/

/*!
    \enum QGLSceneNode::Option
    This enum defines option flags for QGLSceneNode.  By default the none of the flags
    are set, so the options() function returns QGLSceneNode::NoOptions

    \value NoOptions Do not enable any QGLSceneNode options.
    \value CullBoundingBox Perform a cull using boundingBox() before
        attempting to draw the geometry().
    \value ViewNormals Enables the display of lighting normals for
        debugging purposes.
    \value ReportCulling Send a signal when an object is displayed or culled.
    \value HideNode Hide this node so it, and all its children, are excluded from rendering.
    \sa setOptions()
*/

/*!
    Constructs a new scene node and attaches it to \a parent.  If parent is
    a QGLSceneNode then this node is added to it as a child.
*/
QGLSceneNode::QGLSceneNode(QObject *parent)
    : QObject(parent)
    , d_ptr(new QGLSceneNodePrivate())
{
    QGLSceneNode *sceneParent = qobject_cast<QGLSceneNode*>(parent);
    if (sceneParent)
        sceneParent->addNode(this);
}

/*!
    \internal
    Used by clone().
*/
QGLSceneNode::QGLSceneNode(QGLSceneNodePrivate *d, QObject *parent)
    : QObject(parent)
    , d_ptr(d)
{
    QGLSceneNode *sceneParent = qobject_cast<QGLSceneNode*>(parent);
    if (sceneParent)
        sceneParent->addNode(this);
}

/*!
    Constructs a new scene node referencing \a geometry and attaches it to
    \a parent.    If parent is a QGLSceneNode then this node is added to it
    as a child.
*/
QGLSceneNode::QGLSceneNode(const QGeometryData &geometry, QObject *parent)
    : QObject(parent)
    , d_ptr(new QGLSceneNodePrivate())
{
    Q_D(QGLSceneNode);
    d->geometry = geometry;
    QGLSceneNode *sceneParent = qobject_cast<QGLSceneNode*>(parent);
    if (sceneParent)
        sceneParent->addNode(this);
}

/*!
    Destroys this scene node.
*/
QGLSceneNode::~QGLSceneNode()
{
    Q_D(QGLSceneNode);

    // Detach ourselves from our children.  The children will be
    // deleted separately when their QObject::parent() deletes them.
    for (int index = 0; index < d->childNodes.count(); ++index)
        d->childNodes.at(index)->d_ptr->parentNodes.removeOne(this);

    // Detach ourselves from our remaining parents, and notify them
    // to update their bounding boxes.  This won't be needed if we
    // are recursively destroying a tree of nodes because the parent
    // already detached from this node above.
    for (int index = 0; index < d->parentNodes.count(); ++index) {
        QGLSceneNode *parent = d->parentNodes.at(index);
        parent->d_ptr->childNodes.removeOne(this);
        parent->invalidateBoundingBox();
    }
}

/*!
    Returns the drawing options associated with this node.
    The default is 0 (no options set).

    \sa setOptions(), setOption()
*/
QGLSceneNode::Options QGLSceneNode::options() const
{
    Q_D(const QGLSceneNode);
    return d->options;
}

/*!
    \qmlproperty enumeration QGLSceneNode::options

    Defines the settings of various options configurabled on
    nodes in the mesh.

    \list
    \o NoOptions Use no options.  This is the default.
    \o CullBoundingBox Use the camera position to cull the whole node if possible.
    \o ViewNormals Turn on normals debugging mode visually depict lighting normals.
    \o ReportCulling Send a signal when an object is displayed or culled.
    \endlist
*/

/*!
    Sets the drawing \a options associated with this node.

    \sa options(), setOption()
*/
void QGLSceneNode::setOptions(QGLSceneNode::Options options)
{
    Q_D(QGLSceneNode);
    if (d->options != options) {
        d->options = options;
        emit updated();
    }
}

/*!
    Enables or disables \a option according to \a value.

    \sa options(), setOptions()
*/
void QGLSceneNode::setOption(QGLSceneNode::Option option, bool value)
{
    Q_D(QGLSceneNode);
    QGLSceneNode::Options opts = d->options;
    if (value)
        opts |= option;
    else
        opts &= ~option;
    if (d->options != opts) {
        d->options = opts;
        emit updated();
    }
}

/*!
    Returns the geometry associated with this node, or a null QGeometryData
    if no geometry has been associated with it.

    \sa setGeometry()
*/
QGeometryData QGLSceneNode::geometry() const
{
    Q_D(const QGLSceneNode);
    return d->geometry;
}

/*!
    Sets the geometry associated with this node to be \a geometry.
    Typically the \a geometry will be some type of mesh object.  The
    default implementation of the QGLSceneNode::draw() method will call
    the geometry's draw() method.

    \sa geometry()
*/
void QGLSceneNode::setGeometry(QGeometryData geometry)
{
    Q_D(QGLSceneNode);
    d->geometry = geometry;
    emit updated();
}

/*!
    Returns a bounding box for the portion of the geometry referenced by
    this scene node.  If the value of start() is 0, and count() is the same
    as geometry()->size() then the bounding box will be the same as
    geometry()->boundingBox().  However if the scene node only references
    some part of the geometry, a bounding box for this section is calculated.

    If this scene node has child nodes then the bounding box will be the
    calculated union of the bounding box for this nodes geometry (if any) and
    the bounding boxes of the children.

    The calculated value is cached and returned on subsequent calls, but
    could be expensive to calculate initially.
*/
QBox3D QGLSceneNode::boundingBox() const
{
    Q_D(const QGLSceneNode);
    if (d->boxValid)
        return d->bb;
    d->bb = QBox3D();
    if (d->geometry.count() > 0)
    {
        if (d->start == 0 && (d->count == d->geometry.count() || d->count == 0))
        {
            d->bb = d->geometry.boundingBox();
        }
        else
        {
            QGL::IndexArray indices = d->geometry.indices();
            for (int i = d->start; i < (d->start + d->count); ++i)
            {
                int ix = indices.at(i);
                d->bb.unite(d->geometry.vertexAt(ix));
            }
        }
    }
    QList<QGLSceneNode*>::const_iterator it = d->childNodes.constBegin();
    for ( ; it != d->childNodes.constEnd(); ++it)
    {
        QGLSceneNode *n = *it;
        QBox3D b = n->boundingBox();
        d->bb.unite(b);
    }
    d->bb.transform(transform());
    d->boxValid = true;
    return d->bb;
}

// Calculate the resulting matrix from the position, local transform,
// and list of transforms.
QMatrix4x4 QGLSceneNode::transform() const
{
    Q_D(const QGLSceneNode);
    QMatrix4x4 m;
    if (!d->translate.isNull())
        m.translate(d->translate);
    if (!d->localTransform.isIdentity())
        m *= d->localTransform;
    for (int index = d->transforms.size() - 1; index >= 0; --index)
        d->transforms.at(index)->applyTo(&m);
    return m;
}

/*!
    Returns the local transform associated with this node.  If no
    local transform has been explicitly set, this method returns a
    QMatrix4x4 set to the identity matrix.

    The local transform is typically set during model loading or
    geometry construction, and is a feature of the geometry.

    In general to change the location or orientation of the node
    use the position() or transforms() properties instead.

    \sa setLocalTransform(), position(), transforms()
*/
QMatrix4x4 QGLSceneNode::localTransform() const
{
    Q_D(const QGLSceneNode);
    return d->localTransform;
}

/*!
    Sets the local transform associated with this node to be \a transform.
    The default implementation of the QGLSceneNode::draw() method will
    apply this transform to the QGLPainter before drawing any geometry.

    \sa localTransform()
*/
void QGLSceneNode::setLocalTransform(const QMatrix4x4 &transform)
{
    Q_D(QGLSceneNode);
    if (d->localTransform != transform)
    {
        d->localTransform = transform;
        emit updated();
        invalidateTransform();
    }
}

/*!
    \property QGLSceneNode::position
    \brief The amounts of x, y and z axis translation for this node.

    Since most nodes are situated relative to \c{(0, 0, 0)} when imported as
    part of a model or constructed programatically, the translation is
    effectively the position of the model in the scene.

    The x, y and z axis translations can also be specified individually as
    separate properties \l x, \l y, and \l z

    \sa x(), y(), z()
*/
QVector3D QGLSceneNode::position() const
{
    Q_D(const QGLSceneNode);
    return d->translate;
}

void QGLSceneNode::setPosition(const QVector3D &p)
{
    Q_D(QGLSceneNode);
    if (p != d->translate)
    {
        d->translate = p;
        emit updated();
        invalidateTransform();
    }
}

/*!
    \property QGLSceneNode::x
    \brief The amount of x axis translation for this node.

    \sa position()
*/
qreal QGLSceneNode::x() const
{
    Q_D(const QGLSceneNode);
    return d->translate.x();
}

void QGLSceneNode::setX(qreal x)
{
    Q_D(QGLSceneNode);
    if (x != d->translate.x())
    {
        d->translate.setX(x);
        emit updated();
        invalidateTransform();
    }
}

/*!
    \property QGLSceneNode::y
    \brief The amount of y axis translation for this node.

    \sa position()
*/
qreal QGLSceneNode::y() const
{
    Q_D(const QGLSceneNode);
    return d->translate.y();
}

void QGLSceneNode::setY(qreal y)
{
    Q_D(QGLSceneNode);
    if (y != d->translate.y())
    {
        d->translate.setY(y);
        emit updated();
        invalidateTransform();
    }
}

/*!
    \property QGLSceneNode::z
    \brief The amount of z axis translation for this node.

    \sa position()
*/
qreal QGLSceneNode::z() const
{
    Q_D(const QGLSceneNode);
    return d->translate.z();
}

void QGLSceneNode::setZ(qreal z)
{
    Q_D(QGLSceneNode);
    if (z != d->translate.z())
    {
        d->translate.setZ(z);
        emit updated();
        invalidateTransform();
    }
}

/*!
    Returns the list of transformations to apply to this node.
    The default is the empty list.

    The transformations are applied to the node itself, so a
    QGraphicsScale3D followed by a QGraphicsTranslation3D will
    first scale the node in its local co-ordinate system,
    and then translate the node a new location.

    In the mathematical sense, the transformations are applied to
    the modelview matrix in the reverse order in which they appear
    in this list.

    The position() is applied after all other transformations
    have been applied.

    \sa setTransforms(), addTransform(), position()
*/
QList<QGraphicsTransform3D *> QGLSceneNode::transforms() const
{
    Q_D(const QGLSceneNode);
    return d->transforms;
}

/*!
    Sets the list of transformations to apply to this node to \a transforms.

    The transformations are applied to the node itself, so a
    QGraphicsScale3D followed by a QGraphicsTranslation3D will
    first scale the node in its local co-ordinate system,
    and then translate the node a new location.

    In the mathematical sense, the transformations are applied to
    the modelview matrix in the reverse order in which they appear
    in \a transforms.

    The position() is applied after all other transformations
    have been applied.

    \sa transforms(), addTransform(), position()
*/
void QGLSceneNode::setTransforms(const QList<QGraphicsTransform3D *> &transforms)
{
    Q_D(QGLSceneNode);
    for (int index = 0; index < d->transforms.size(); ++index) {
        QGraphicsTransform3D *transform = d->transforms.at(index);
        disconnect(transform, SIGNAL(transformChanged()), this, SLOT(transformChanged()));
    }
    d->transforms.clear();
    for (int index = 0; index < transforms.size(); ++index) {
        QGraphicsTransform3D *transform = transforms.at(index);
        if (transform) {
            connect(transform, SIGNAL(transformChanged()), this, SLOT(transformChanged()));
            d->transforms.append(transform);
        }
    }
    emit updated();
    invalidateTransform();
}

/*!
    Adds a single \a transform to this node, to be applied to the
    node after all current members of transformations() have been applied.

    In the mathematical sense, \a transform is applied to the modelview
    matrix before the current members of transformations() are applied
    in reverse order.

    \sa transforms(), setTransforms()
*/
void QGLSceneNode::addTransform(QGraphicsTransform3D *transform)
{
    Q_D(QGLSceneNode);
    if (!transform)
        return;     // Avoid nulls getting into the transform list.
    connect(transform, SIGNAL(transformChanged()), this, SLOT(transformChanged()));
    d->transforms.append(transform);
    emit updated();
    invalidateTransform();
}

/*!
    \internal
*/
void QGLSceneNode::transformChanged()
{
    invalidateTransform();
    emit updated();
}

/*!
    Returns the drawing mode to use to render geometry().  The default
    is QGL::Triangles.

    \sa setDrawingMode()
*/
QGL::DrawingMode QGLSceneNode::drawingMode() const
{
    Q_D(const QGLSceneNode);
    return d->drawingMode;
}

/*!
    Sets the drawing \a mode to use to render geometry().

    Note: this function changes the drawing mode, but the underlying
    geometry() still consists of the triangles that were added.
    Thus, this function is only useful for converting the drawing mode
    into QGL::Points to display the geometry() as a point cloud
    instead of a triangle mesh.  The other enums from QGL::DrawingMode
    will give unpredictable results.

    \sa drawingMode()
*/
void QGLSceneNode::setDrawingMode(QGL::DrawingMode mode)
{
    Q_D(QGLSceneNode);
    if (d->drawingMode != mode)
    {
        d->drawingMode = mode;
        emit updated();
    }
}

/*!
    Returns the drawing width for this node.

    Drawing width is used only when drawing lines or points (ie. when
    the drawing mode is points, lines, line-strips, etc).

    \sa drawingMode()
*/
qreal QGLSceneNode::drawingWidth() const
{
    Q_D(const QGLSceneNode);
    return d->drawingWidth;
}

/*!
    Sets the drawing \a width to the given value.

    Drawing width is used only when drawing lines or points (ie. when
    the drawing mode is points, lines, line-strips, etc).

    \sa drawingMode()
*/
void QGLSceneNode::setDrawingWidth(qreal width)
{
    Q_D(QGLSceneNode);
    d->drawingWidth = width;
}


/*!
    Returns the local effect associated with this node.  The default value
    is QGL::FlatColor.  If the value of hasEffect() is false, then this
    the value of effect() is ignored.

    \sa setEffect(), hasEffect()
*/
QGL::StandardEffect QGLSceneNode::effect() const
{
    Q_D(const QGLSceneNode);
    return d->localEffect;
}

/*!
    Sets the local effect associated with this node to be \a effect.  hasEffect()
    will return true after calling this method.

    The QGLSceneNode::draw() function will ensure that \a effect is applied to the
    QGLPainter before drawing any geometry.

    \sa effect(), hasEffect()
*/
void QGLSceneNode::setEffect(QGL::StandardEffect effect)
{
    Q_D(QGLSceneNode);
    if (d->localEffect != effect || !d->hasEffect) {
        d->localEffect = effect;
        d->hasEffect = true;
        emit updated();
    }
}

/*!
    Returns the user effect associated with this node, or NULL if one is not
    set.  The default value is NULL.  If the value of hasEffect() is false,
    then this effect is ignored.

    \sa setUserEffect(), hasEffect()
*/
QGLAbstractEffect *QGLSceneNode::userEffect() const
{
    Q_D(const QGLSceneNode);
    return d->customEffect;
}

/*!
    Sets the local effect associated with this node to be the custom
    \a effect.  hasEffect() will return true after calling this method.

    This custom effect will supersede any standard effect.

    The default implementation of QGLSceneNode::apply() will set this effect
    during initialization of the model.

    The default implementation of the QGLSceneNode::draw() method will
    ensure that \a effect is applied to the QGLPainter before drawing
    any geometry.

    \sa userEffect(), hasEffect()
*/
void QGLSceneNode::setUserEffect(QGLAbstractEffect *effect)
{
    Q_D(QGLSceneNode);
    if (d->customEffect != effect || !d->hasEffect) {
        d->customEffect = effect;
        d->hasEffect = true;
        emit updated();
    }
}


 /*!
     Returns true if the local effect on this node is enabled, otherwise
     returns false.

     \sa setEffectEnabled(), setEffect()
 */
bool QGLSceneNode::hasEffect() const
{
    Q_D(const QGLSceneNode);
    return d->hasEffect;
}

/*!
    Sets whether the current value of effect() or userEffect() will be
    applied to the QGLPainter prior to drawing.  If \a enabled is true,
    then the effect is applied, otherwise it is not.

     \sa setEffect(), effect(), hasEffect()
*/
void QGLSceneNode::setEffectEnabled(bool enabled)
{
    Q_D(QGLSceneNode);
    if (d->hasEffect != enabled) {
        d->hasEffect = enabled;
        emit updated();
    }
}

/*!
    Returns the starting index within geometry() that should be used
    to render fragments for this scene node.  The default value is 0,
    indicating that the 0'th logical vertex in geometry() is the start.

    \sa setStart(), count()
*/
int QGLSceneNode::start() const
{
    Q_D(const QGLSceneNode);
    return d->start;
}

/*!
    Sets the \a start index within geometry() that should be used
    to render fragments for this scene node.

    \sa start(), setCount()
*/
void QGLSceneNode::setStart(int start)
{
    Q_D(QGLSceneNode);
    if (start != d->start)
    {
        d->start = start;
        emit updated();
        invalidateBoundingBox();
    }
}

/*!
    Returns the count of the vertices to render from geometry()
    for this scene node.  The default is zero, meaning that this node
    uses all vertices from start() up to the last logical vertex
    in the underlying geometry().

    \sa setCount(), start()
*/
int QGLSceneNode::count() const
{
    Q_D(const QGLSceneNode);
    return d->count;
}

/*!
    Sets the \a count of the vertices to render from geometry()
    for this scene node.

    \sa count(), setStart()
*/
void QGLSceneNode::setCount(int count)
{
    Q_D(QGLSceneNode);
    if (count != d->count)
    {
        d->count = count;
        emit updated();
        invalidateBoundingBox();
    }
}

/*!
    Returns the material index for this scene node.

    \sa setMaterialIndex()
*/
int QGLSceneNode::materialIndex() const
{
    Q_D(const QGLSceneNode);
    return d->material;
}

/*!
    Sets the material index for this scene node to \a material.

    \sa materialIndex()
*/
void QGLSceneNode::setMaterialIndex(int material)
{
    Q_D(QGLSceneNode);
    if (d->material != material) {
        d->material = material;
        emit updated();
    }
}

/*!
    Returns the back material index for this scene node.

    \sa setBackMaterialIndex()
*/
int QGLSceneNode::backMaterialIndex() const
{
    Q_D(const QGLSceneNode);
    return d->backMaterial;
}

/*!
    Sets the back material index for this scene node to \a material.

    \sa materialIndex()
*/
void QGLSceneNode::setBackMaterialIndex(int material)
{
    Q_D(QGLSceneNode);
    if (d->backMaterial != material) {
        d->backMaterial = material;
        emit updated();
    }
}

/*!
    \property QGLSceneNode::material
    \brief This property is a pointer to a QGLMaterial instance for this node.

    This material is applied to all faces if the backMaterial() property
    is set to null, which is the default.  If the backMaterial() property is non-null
    then this material is only applied to the front faces.

    To apply a material to the back faces use the backMaterial() property.

    Getting this property is exactly equivalent to
    \c{palette()->material(materialIndex())}.

    Setting this property causes the material if not already in this nodes palette to be
    added, and then the corresponding index to be set for this scene node.

    Setting this property is equivalent to:
    \code
    int index = d->palette->indexOf(material);
    if (index == -1)
        index = d->palette->addMaterial(material);
    setMaterialIndex(index);
    \endcode

    If setting this property, when no palette exists one is created, as a
    convenience - but this is suitable only for experimental code and for
    \bold{very small numbers of nodes}.  In debug mode a warning is
    printed in this case.

    Generally one common palette should be created, and set on each node.  This
    also allows nodes to share materials and their textures.

    \sa materialIndex(), setMaterialIndex()
*/
QGLMaterial *QGLSceneNode::material() const
{
    Q_D(const QGLSceneNode);
    if (d->palette)
        return d->palette->material(d->material);
    return 0;
}

void QGLSceneNode::setMaterial(QGLMaterial *material)
{
    Q_D(QGLSceneNode);
    if (!d->palette)
        d->palette = QSharedPointer<QGLMaterialCollection>(new QGLMaterialCollection(this));
    int ix = d->palette->indexOf(material);
    if (ix == -1)
        ix = d->palette->addMaterial(material);
    setMaterialIndex(ix);
}

/*!
    \property QGLSceneNode::backMaterial
    \brief This property is a pointer to any QGLMaterial instance for this node's back faces.

    This material is applied to the back faces, if non-null.  The default value
    of this property is null.  When this property is null, any non-null material
    set on the material() property will be applied to front and back faces.

    To apply a material to the front faces use the material() property.

    Getting this property is exactly equivalent to
    \c{palette()->material(backMaterialIndex())}.

    Setting this property causes the material if not already in this nodes palette to be
    added, and then the corresponding index to be set for this scene node.

    Setting this property is exactly equivalent to:
    \code
    int index = d->palette->indexOf(material);
    if (index == -1)
        index = d->palette->addMaterial(material);
    setBackMaterialIndex(index);
    \endcode
*/
QGLMaterial *QGLSceneNode::backMaterial() const
{
    Q_D(const QGLSceneNode);
    if (d->palette)
        return d->palette->material(d->backMaterial);
    return 0;
}

void QGLSceneNode::setBackMaterial(QGLMaterial *material)
{
    Q_D(QGLSceneNode);
    if (!d->palette)
        d->palette = QSharedPointer<QGLMaterialCollection>(new QGLMaterialCollection(this));
    int ix = d->palette->indexOf(material);
    if (ix == -1)
        ix = d->palette->addMaterial(material);
    setBackMaterialIndex(ix);
}
/*!
    Returns the palette of materials used by this scene node, or NULL
    if no palette has been set.

    \sa setPalette()
*/
QSharedPointer<QGLMaterialCollection> QGLSceneNode::palette() const
{
    Q_D(const QGLSceneNode);
    return d->palette;
}

/*!
    Sets the palette of materials for this scene node to \a palette.

    \sa palette()
*/
void QGLSceneNode::setPalette(QSharedPointer<QGLMaterialCollection> palette)
{
    Q_D(QGLSceneNode);
    if (d->palette.data() != palette.data()) {
        d->palette = palette;
        emit updated();
    }
}

/*!
    Returns a list of the child nodes for this node.  This list is not
    recursively generated, it includes only the nodes which are
    immediate children of this node.

    \sa allChildren()
*/
QList<QGLSceneNode*> QGLSceneNode::children() const
{
    Q_D(const QGLSceneNode);
    return d->childNodes;
}

/*!
    Returns a list including recursively all child nodes under
    this node.  Each child node only appears once, even if it is included
    multiple times in the scene graph.

    \sa children()
*/
QList<QGLSceneNode*> QGLSceneNode::allChildren() const
{
    Q_D(const QGLSceneNode);
    QList<QGLSceneNode*> allSceneNodes;
    QList<QGLSceneNode*> gather;
    QList<QGLSceneNode*>::const_iterator it = d->childNodes.constBegin();
    for ( ; it != d->childNodes.constEnd(); ++it)
        if (!gather.contains(*it))
            gather.append(*it);
    while (gather.count() > 0)
    {
        QGLSceneNode *node = gather.takeFirst();
        if (!allSceneNodes.contains(node))
        {
            allSceneNodes.append(node);
            gather.append(node->children());
        }
    }
    return allSceneNodes;
}

/*!
    Adds the \a node to the list of child nodes for this node.

    This function does nothing if \a node is null or is already a child
    of this node.  If the \a node is the node itself, a warning about an
    attempt to add a node to itself is printed, and no add takes place.

    If the aim is to have the same geometry displayed several times under a
    given node, each time with different transformations, use the clone()
    call to create copies of the node and then apply the transformations to
    the copies.

    Alternatively, create modifier nodes with the transformations and add the
    geometry bearing node to each with addNode():
    \code
    QGLBuilder builder;
    builder << CarWheel(5.0f); // some car wheel geometry
    QGLSceneNode wheel = builder.finalizedSceneNode();
    QGLSceneNode frontLeft = new QGLSceneNode(m_sceneRoot);
    frontLeft->addNode(wheel);
    frontLeft->setPosition(QVector3D(1.0f, 2.0f, 0.0f));
    QGLSceneNode frontRight = new QGLSceneNode(m_sceneRoot);
    frontRight->addNode(wheel);
    frontRight->setPosition(QVector3D(-1.0f, 2.0f, 0.0f));
    QGLSceneNode backLeft = new QGLSceneNode(m_sceneRoot);
    backLeft->addNode(wheel);
    backLeft->setPosition(QVector3D(1.0f, -2.0f, 0.0f));
    QGLSceneNode backRight = new QGLSceneNode(m_sceneRoot);
    backRight->addNode(wheel);
    backRight->setPosition(QVector3D(-1.0f, -2.0f, 0.0f));
    \endcode

    Because a child node can be validly added to many different nodes,
    calling addNode() does not normally affect the QObject::parent()
    ownership.  However, if \a node does not currently have a
    QObject::parent(), the parent will be set to this node.

    \sa removeNode(), clone(), addNodes()
*/
void QGLSceneNode::addNode(QGLSceneNode *node)
{
    Q_D(QGLSceneNode);
    bool alreadyAdded = node && node->d_ptr->parentNodes.contains(this);
    if (!node || node == this || alreadyAdded)
        return;     // Invalid node, or already under this parent.
    invalidateBoundingBox();
    d->childNodes.append(node);
    node->d_ptr->parentNodes.append(this);
    if (!node->parent())
        node->setParent(this);
    connect(node, SIGNAL(updated()), this, SIGNAL(updated()));
    emit updated();
}

/*!
    Adds the members of \a nodes to the list of child nodes
    for this node.

    \sa addNode(), removeNodes()
*/
void QGLSceneNode::addNodes(const QList<QGLSceneNode *> &nodes)
{
    Q_D(QGLSceneNode);
    for (int index = 0; index < nodes.count(); ++index) {
        QGLSceneNode *node = nodes.at(index);
        if (!node || node->d_ptr->parentNodes.contains(this))
            continue;   // Invalid node, or already under this parent.
        d->childNodes.append(node);
        node->d_ptr->parentNodes.append(this);
        if (!node->parent())
            node->setParent(this);
        connect(node, SIGNAL(updated()), this, SIGNAL(updated()));
    }
    invalidateBoundingBox();
    emit updated();
}

/*!
    Removes the child node matching \a node from this node.

    If the QObject::parent() ownership of \a node was set to this
    node, then its parent will be changed to another parent node if it
    had multiple parents.

    If \a node had only a single parent, then its parent will be set to null,
    effectively detaching it from the QObject ownership rules of the scene
    graph.  The caller is then responsible for deleting \a node.

    If the QObject::parent() of \a node was not a scene node parent,
    then it will be left unmodified.

    \sa addNode(), removeNodes()
*/
void QGLSceneNode::removeNode(QGLSceneNode *node)
{
    Q_D(QGLSceneNode);
    if (!node || !node->d_ptr->parentNodes.contains(this))
        return;     // Invalid node or not attached to this parent.
    d->childNodes.removeOne(node);
    node->d_ptr->parentNodes.removeOne(this);
    if (node->parent() == this) {
        // Transfer QObject ownership to another parent, or null.
        if (!node->d_ptr->parentNodes.isEmpty())
            node->setParent(node->d_ptr->parentNodes[0]);
        else
            node->setParent(0);
    }
    disconnect(node, SIGNAL(updated()), this, SIGNAL(updated()));
    invalidateBoundingBox();
    emit updated();
}

/*!
    Removes the members of \a nodes from the list of child nodes
    for this node.

    \sa removeNode(), addNodes()
*/
void QGLSceneNode::removeNodes(const QList<QGLSceneNode *> &nodes)
{
    Q_D(QGLSceneNode);
    for (int index = 0; index < nodes.count(); ++index) {
        QGLSceneNode *node = nodes.at(index);
        if (!node || !node->d_ptr->parentNodes.contains(this))
            continue;   // Invalid node or not attached to this parent.
        d->childNodes.removeOne(node);
        node->d_ptr->parentNodes.removeOne(this);
        if (node->parent() == this) {
            // Transfer QObject ownership to another parent, or null.
            if (!node->d_ptr->parentNodes.isEmpty())
                node->setParent(node->d_ptr->parentNodes[0]);
            else
                node->setParent(0);
        }
        disconnect(node, SIGNAL(updated()), this, SIGNAL(updated()));
    }
    invalidateBoundingBox();
    emit updated();
}

void QGLSceneNode::invalidateBoundingBox() const
{
    Q_D(const QGLSceneNode);
    d->boxValid = false;
    d->invalidateParentBoundingBox();
}

void QGLSceneNode::invalidateTransform() const
{
    invalidateBoundingBox();
}

void QGLSceneNode::drawNormalIndicators(QGLPainter *painter)
{
    Q_D(QGLSceneNode);
    QVector3DArray verts;
    QGL::IndexArray indices = d->geometry.indices();
    for (int i = d->start; i < (d->start + d->count); ++i)
    {
        int ix = indices[i];
        QVector3D a = d->geometry.vertexAt(ix);
        QVector3D b = a + d->geometry.normalAt(ix);
        verts.append(a, b);
    }
    painter->setVertexAttribute(QGL::Position, QGLAttributeValue(verts));
    glLineWidth(2.0f);
    painter->draw(QGL::Lines, verts.size());
}

const QGLMaterial *QGLSceneNode::setPainterMaterial(int material, QGLPainter *painter,
                                              QGL::Face faces, bool &changedTex)
{
    Q_D(QGLSceneNode);
    QGLMaterial *mat = d->palette->material(material);
    const QGLMaterial *saveMat = 0;
    if (painter->faceMaterial(faces) != mat)
    {
        saveMat = painter->faceMaterial(faces);
        painter->setFaceMaterial(faces, mat);
        int texUnit = 0;
        for (int i = 0; i < mat->textureLayerCount(); ++i)
        {
            QGLTexture2D *tex = mat->texture(i);
            if (tex)
            {
                painter->glActiveTexture(GL_TEXTURE0 + texUnit);
                tex->bind();
                changedTex = true;
                ++texUnit;
            }
        }
    }
    return saveMat;
}

/*!
    Draws the geometry of the node onto the \a painter.

    This is the function which performs the actual drawing step in the
    draw function below.

    \list
    \o calls draw(start, count) on this nodes geometry object (if any)
    \endlist

    Override this function to perform special processing on this node,
    after transformation & culling are applied and before sequencing of
    materials & effects are done; but just before (or after) the
    actual draw step.

    This default implementation simply draws the nodes geometry onto
    the painter.

    Example:
    \code
    void MySpecialNode::geometryDraw(QGLPainter *painter)
    {
        // at this point the node has survived culling, the model-view
        // matrix is transformed into this nodes frame of reference,
        // materials & effects have been applied as this node appears
        // in its place in the render sequence

        doMySpecialProcessing();

        // call parent implementation to do actual draw
        QGLSceneNode::geometryDraw(painter);
    }
    \endcode
*/
void QGLSceneNode::drawGeometry(QGLPainter *painter)
{
    Q_D(QGLSceneNode);
    if (d->count && d->geometry.count() > 0)
        d->geometry.draw(painter, d->start, d->count, d->drawingMode, d->drawingWidth);
}

/*!
    Draws this scene node and its children on the \a painter.

    In detail this function:
    \list
    \o ensures the effect specified by effect() is current on the painter
    \o sets the nodes materials onto the painter, if valid materials are present
    \o moves the model-view to the x, y, z position
    \o applies any local transforms() that may be set for this node
    \o calls draw() for all the child nodes
    \o calls draw(start, count) on this nodes geometry object (if any)
    \o restores the geometry's original materials if they were changed
    \o restores the model-view matrix if any local transform was applied
    \endlist

    Note that the draw() method does \bold not restore the effect.  If the first
    step above results in a change to the current QGL::Standard effect then it
    will remain set to that effect.  In general any painting method should
    ensure the effect it requires is current.

    The way draw is implemented ensures that this nodes effects, materials and
    transformations will apply by default to its child nodes.  Transformations
    are cumulative, but effects and materials override those of any parent node.

    Note that if the HideNode option is set for this node, neither it nor its
    children will be drawn.
*/
void QGLSceneNode::draw(QGLPainter *painter)
{
    Q_D(QGLSceneNode);
    if (d->options & HideNode)
        return;
    bool wasTransformed = false;

    QGLRenderSequencer *seq = painter->renderSequencer();

    if (seq->top() != this)
    {
        QMatrix4x4 m = transform();

        if (!m.isIdentity())
        {
            painter->modelViewMatrix().push();
            painter->modelViewMatrix() *= m;
            wasTransformed = true;
        }

        if (d->options & CullBoundingBox)
        {
            QBox3D bb = boundingBox();
            if (bb.isFinite() && !bb.isNull() && painter->isCullable(bb))
            {
                if (!d->culled && d->options & ReportCulling)
                {
                    d->culled = true;
                    emit culled();
                }
                if (wasTransformed)
                    painter->modelViewMatrix().pop();
                return;
            }
            else
            {
                if (d->culled && d->options & ReportCulling)
                {
                    d->culled = false;
                    emit displayed();
                }
            }
        }
    }

    if (seq->top() == NULL)
    {
        seq->setTop(this);
        while (true)
        {
            draw(painter);  // recursively draw myself for each state
            if (!seq->nextInSequence())
                break;
        }
        seq->reset();
    }
    else
    {
        bool stateEntered = false;
        if (d->childNodes.size() > 0)
        {
            seq->beginState(this);
            stateEntered = true;
            QList<QGLSceneNode*>::iterator cit = d->childNodes.begin();
            for ( ; cit != d->childNodes.end(); ++cit)
                (*cit)->draw(painter);
        }

        if (d->count && (d->geometry.count() > 0) && seq->renderInSequence(this))
        {
            bool idSaved = false;
            int id = -1;
            if (d->pickNode && painter->isPicking())
            {
                idSaved = true;
                id = painter->objectPickId();
                painter->setObjectPickId(d->pickNode->id());
            }

            if (!stateEntered)
            {
                stateEntered = true;
                seq->beginState(this);
            }
            seq->applyState();

            drawGeometry(painter);

            if (idSaved)
                painter->setObjectPickId(id);

            if (d->options & ViewNormals)
                drawNormalIndicators(painter);
        }
        if (stateEntered)
            seq->endState(this);
    }
    if (wasTransformed)
        painter->modelViewMatrix().pop();
}

/*!
    Returns the pick node for this scene node, if one was set; otherwise
    NULL (0) is returned.

    \sa setPickNode()
*/
QGLPickNode *QGLSceneNode::pickNode() const
{
    Q_D(const QGLSceneNode);
    return d->pickNode;
}

/*!
    Sets the pick node for this scene node to \a node.

    \sa pickNode()
*/
void QGLSceneNode::setPickNode(QGLPickNode *node)
{
    Q_D(QGLSceneNode);
    // TODO - resolve recursive picking - not supported by
    // color based pick AFAICT
    d->pickNode = node;
    node->setTarget(this);
}

/*!
    Return a specific scene node based on a unqiue path specified by
    \a nodePath.  This path contains the names of the nodes in the
    scene-graph from the root node through to a specific target node.

    Individual node names are separated by a double colon "::".

    For example, given the following scene graph:

          Item2
         /
    Item1
         \
          Item3 - Item4

    We could get the QGLSceneNode for "Item4" using the following function
    call:

    \code
    QString searchPath("Item1::Item3::Item4");
    QGLSceneNode * targetNode = findSceneNode(searchPath);
    \endcode

    The function will automatically skip unnamed nodes.  For example, consider
    this scene graph:

                      Item2
                     /
    <unnamed> - Item1
                     \
                      Item3 - <unnamed> - Item4


    The function \a nodePath will return the QGLSceneNode for node "Item4".

    \code
    "Item1::Item3::Item4"
    \endcode

    This is because the unnamed nodes are skipped.  Likewise blank or unnamed
    nodes in the search path will be ignored, so the following string will
    give the same result as the previous example:

    \code
    "::Item1::Item3::::Item4"
    \endcode

    The function assumes that there are no repeated instances of the same
    name at any given ply of the scenegraph.  For example, the following
    scenegraph is invalid because of the repeat of Sphere1 in the same layer
    of the graph:

          Sphere1
         /
    Cube1
         \
          Sphere1

    The function also assumes that the names of nodes do not contain
    double colons.  For example, a node with the name: "Sphere::1" would
    result in an invalid graph.

    If a third party model contains such names it is still possible to retrieve
    the nodes by using the variant of findSceneNode() which takes a list
    of node-names rather than the double-colon separated string in \a nodePath.

    \sa findSceneNode()
*/
QGLSceneNode *QGLSceneNode::findSceneNode(QString &nodePath)
{
    QRegExp splitExpression(QLatin1String("::"));
    QStringList nodePathList = nodePath.split(splitExpression);
    return findSceneNode(nodePathList);
}

/*!
    This functions exactly the same way as the previous findSceneNode
    function except in this case the \a nodePath is specified as
    a QStringList composed of the individual nodes along the path
    to the target node.

    The other findSceneNode function acts by splitting its nodePath
    into a QStringList wherever a double colon (::) is found and
    passing the result to this function.

    \sa findSceneNode()
*/
QGLSceneNode *QGLSceneNode::findSceneNode(QStringList &nodePath)
{
    //Skip objects with blank names
    if (!objectName().isEmpty())
    {
        //If nodePath list's head is empty, delete it
        while (nodePath.first().isEmpty())
            nodePath.removeFirst();
        //If nodePath list is empty, fail test (return null)
        if (nodePath.isEmpty())
            return 0;
        //If nodePath's head matches this node...
        if (nodePath.first()==objectName())
        {
            //If nodePath's tail is empty, return this node (success)
            nodePath.removeFirst();
            if (nodePath.isEmpty()) {
                return this;
            }
        } else {
            //If there is no match, return a failure.
            return 0;
        }
    }

    //Otherwise check child nodes against the remainder of nodePath
    QList<QGLSceneNode*>childNodes = children();
    for (int i=0; i<childNodes.count(); i++) {
       QGLSceneNode *result = childNodes.at(i)->findSceneNode(nodePath);
       if (result)
           return result;
    }

    //If we reach this point we've failed to find a matching node - return failure.
    return 0;
}

/*!
    This function retrieve a sub-branch of the scenegraph with its root at the
    current node.  The exact node to retrieve is specified by the double-colon
    separated path described by \a name.

    This node will be located using the method described by findSceneNode() and
    will be copied using the clone() function.

    If a \a parent is specified then the sub-node will have that parent added to
    it.

    If the user sets the \a forceCopy parameter to true then the cloneWithChildren()
    function shall be used instead of the clone() function while copying.  This
    will result in all of the child-nodes being forcibly copied as well resulting
    in less efficient memory usage, though allowing the modification of the newly
    created branch without altering the original in any way.

    The inverse of this function, except(), can be used to get everything except
    a specific sub-branch.

    \sa findSceneNode(), clone(), cloneWithChildren(), get(), except()
*/
QGLSceneNode *QGLSceneNode::get(const QString &name, QObject *parent, bool forceCopy)
{
    //function stub only - not for use; api review only
    Q_UNUSED(name);
    Q_UNUSED(parent);
    Q_UNUSED(forceCopy);
    return 0;
}

/*!
    This function is similar to the previous instance of the get() function.  In
    this case, however, the user can specify multiple node-paths to get.

    These nodes will be cloned in an identical way to the single-node instance of
    get, using the \a parent and \a forceCopy parameters in the same way.

    The resulting set of branches will then be added to a newly created root node,
    creating a composite branch.  For example, consider the simple scenegraph below:

         Node 1 - Node 2 - Node 3
        /
    Root
        \
         Node 4 - Node 5 - Node 6

    If we now call the get() function with the following code.

    \code
    QString branch1("Root::Node 1::Node 2");
    QString branch2("Root::Node 4::Node 5");
    QStringList branchList;
    branchList << branch1 << branch2;
    QGLSceneNode *newBranch = get(branchList);
    \endcode

    In this case the function will create a new root node for the output, and
    graft the two branches onto it, as follows:

             Node 2 - Node 3
            /
    New-Root
            \
             Node 5 - Node 6

    \sa findSceneNode(), clone(), cloneWithChildren(), get(), except()
*/
QGLSceneNode *QGLSceneNode::get(const QStringList &names, QObject *parent, bool forceCopy)
{
    //function stub only - not for use; api review only
    Q_UNUSED(names);
    Q_UNUSED(parent);
    Q_UNUSED(forceCopy);
    return 0;
}

/*!
    This function creates a copy of the scenegraph with its root at the
    current node, with a specific sub-branch specified by \a name removed from
    it.  The exact node from which to prune is specified by the double-colon
    separated path described by \a name.

    The function will create a copy of the scenegraph using the cloneWithChildren()
    method, from which the specified sub-branch shall be removed.

    If a \a parent is specified then the sub-node will have that parent added to
    it.

    The inverse of this function, get(), will return only the sub-branch, leaving
    the current scengraph intact.

    For example, consider the following scenegraph:

         Node 1 - Node 2
        /
    Root
        \
         Node 3 - Node 4

    By using the following code:

    \code
    QString branch("Root::Node1");
    QGLSceneNode *newGraph = except(branch);
    \endcode

    We can create a copy of the scenegraph with the branch pruned, as shown below:

    Root
        \
         Node 3 - Node 4

    \sa findSceneNode(), clone(), cloneWithChildren(), get()
*/
QGLSceneNode *QGLSceneNode::except(const QString &name, QObject *parent)
{
    //function stub only - not for use; api review only
    Q_UNUSED(name);
    Q_UNUSED(parent);
    return 0;
}

/*!
    Like the previous version of except, this creates a copy of the scenegraph
    with its root at the current node.  However, this version allows users to
    prune multiple sub-branches using the list of sub-branches specified by
    the \a names list.

    Thus for the scenegraph:

         Node 1 - Node 2 - Node 3
        /
    Root
        \
         Node 4 - Node 5 - Node 6

    The code:

    \code
    QString branch1("Root::Node1::Node2");
    QString branch2("Root::Node4::Node5}");
    QStringList branchList;
    branchList << branch1 << branch2;
    QGLSceneNode *newGraph = except(branchList);
    \endcode

    Would yield the scenegraph:

         Node 1
        /
    Root
        \
         Node 4

    Once again, this copy will be reparented with the parameter \a parent if it
    exists.

    \sa findSceneNode(), clone(), cloneWithChildren(), get()
*/
QGLSceneNode *QGLSceneNode::except(const QStringList &names, QObject *parent)
{
    //function stub only - not for use; api review only
    Q_UNUSED(names);
    Q_UNUSED(parent);
    return 0;
}

/*!
    Creates a new QGLSceneNode that is a copy of this scene node, with
    \a parent as the parent of the new copy.  If parent is NULL then parent
    is set to this nodes parent.

    The copy will reference the same underlying geometry, child nodes, and
    have all effects, transforms and other properties copied from this node.
    The only property that is not copied is pickNode().

    \sa cloneNoChildren()
*/
QGLSceneNode *QGLSceneNode::clone(QObject *parent) const
{
    Q_D(const QGLSceneNode);
    QGLSceneNode *node = new QGLSceneNode
        (new QGLSceneNodePrivate(d), parent ? parent : this->parent());
    for (int index = 0; index < d->transforms.size(); ++index)
        node->addTransform(d->transforms.at(index)->clone(node));
    node->addNodes(d->childNodes);
    return node;
}

/*!
    Creates a new QGLSceneNode that is a copy of this scene node, with
    \a parent as the parent of the new copy.  If parent is NULL then parent
    is set to this nodes parent.

    The copy will reference the same underlying geometry, will create
    clones of all of the child nodes, and have all effects, transforms and other
    properties copied from this node.  The only property that is not copied is
    pickNode().

    \sa cloneNoChildren()
*/
QGLSceneNode * QGLSceneNode::cloneWithChildren(QObject *parent) const
{
    Q_D(const QGLSceneNode);

    QGLSceneNode *node = new QGLSceneNode(new QGLSceneNodePrivate(d), parent?parent:this->parent());

    for (int index = 0; index < d->transforms.size(); ++index)
        node->addTransform(d->transforms.at(index)->clone(node));

    int count = children().count();
    for (int index = 0; index < count; ++index)
    {
        node->addNode(this->children().at(index)->cloneWithChildren());
    }

    return node;
}

/*!
    Creates a new QGLSceneNode that is a copy of this scene node, with
    \a parent as the parent of the new copy.  If parent is NULL then parent
    is set to this nodes parent.

    The copy will reference the same underlying geometry, and
    have all effects, transforms and other properties copied from this node.
    The children() and pickNodes() are not cloned.

    \sa clone()
*/
QGLSceneNode *QGLSceneNode::cloneNoChildren(QObject *parent) const
{
    Q_D(const QGLSceneNode);
    QGLSceneNode *node = new QGLSceneNode
        (new QGLSceneNodePrivate(d), parent ? parent : this->parent());
    for (int index = 0; index < d->transforms.size(); ++index)
        node->addTransform(d->transforms.at(index)->clone(node));
    return node;
}

/*!
    Creates a new QGLSceneNode that is a copy of this scene node, with
    \a parent as the parent of the new copy.  If parent is NULL then parent
    is set to this nodes parent.

    The copy will reference the same underlying geometry and
    have all effects, transforms and other properties copied from this node.

    The copy returned will have the same child nodes, except all child nodes
    whose objectName() is equal to \a name.

    \sa clone(), only()
*/
QGLSceneNode *QGLSceneNode::allExcept(const QString &name, QObject *parent) const
{
    Q_D(const QGLSceneNode);
    QGLSceneNode *node = cloneNoChildren(parent);
    for (int index = 0; index < d->childNodes.count(); ++index) {
        QGLSceneNode *child = d->childNodes.at(index);
        if (child->objectName() != name)
            node->addNode(child);
    }
    return node;
}

/*!
    Creates a new QGLSceneNode that is a copy of this scene node, with
    \a parent as the parent of the new copy.  If parent is NULL then parent
    is set to this nodes parent.

    The copy will reference the same underlying geometry and
    have all effects, transforms and other properties copied from this node.

    The copy returned will have only one child node.  This child node will be
    the first child node of this one which has its objectName() equal to \a name.

    \sa clone(), allExcept()
*/
QGLSceneNode *QGLSceneNode::only(const QString &name, QObject *parent) const
{
    Q_D(const QGLSceneNode);
    QGLSceneNode *node = cloneNoChildren(parent);
    for (int index = 0; index < d->childNodes.count(); ++index) {
        QGLSceneNode *child = d->childNodes.at(index);
        if (child->objectName() == name) {
            node->addNode(child);
            break;
        }
    }
    return node;
}

/*!
    Creates a new QGLSceneNode that is a copy of this scene node, with
    \a parent as the parent of the new copy.  If parent is NULL then parent
    is set to this nodes parent.

    The copy will reference the same underlying geometry and
    have all effects, transforms and other properties copied from this node.

    The copy returned will have the same child nodes, except all child nodes
    whose objectName() is in the list of \a names.

    \sa clone(), only()
*/
QGLSceneNode *QGLSceneNode::allExcept(const QStringList &names, QObject *parent) const
{
    Q_D(const QGLSceneNode);
    QGLSceneNode *node = cloneNoChildren(parent);
    QSet<QString> chk = QSet<QString>::fromList(names);
    for (int index = 0; index < d->childNodes.count(); ++index) {
        QGLSceneNode *child = d->childNodes.at(index);
        if (!chk.contains(child->objectName()))
            node->addNode(child);
    }
    return node;
}

/*!
    Creates a new QGLSceneNode that is a copy of this scene node, with
    \a parent as the parent of the new copy.  If parent is NULL then parent
    is set to this nodes parent.

    The copy will reference the same underlying geometry and
    have all effects, transforms and other properties copied from this node.

    The copy returned will have only the child nodes from this
    whose objectName() is in the list of \a names.

    \sa clone(), allExcept()
*/
QGLSceneNode *QGLSceneNode::only(const QStringList &names, QObject *parent) const
{
    Q_D(const QGLSceneNode);
    QGLSceneNode *node = cloneNoChildren(parent);
    QSet<QString> chk = QSet<QString>::fromList(names);
    for (int index = 0; index < d->childNodes.count(); ++index) {
        QGLSceneNode *child = d->childNodes.at(index);
        if (chk.contains(child->objectName()))
            node->addNode(child);
    }
    return node;
}

/*!
    \fn QGLSceneNode::updated()
    Signals that some property of this scene node, or one of its children,
    has changed in a manner that will require that the node be redrawn.
*/

/*!
    \fn QGLSceneNode::culled()
    Signals that the node was culled due to falling wholly outside the view
    frustum.  This signal can only fire if both QGLSceneNode::CullBoundingBox
    and QGLSceneNode::ReportCulling options are both set.
*/

/*!
    \fn QGLSceneNode::displayed()
    Signals that the node was displayed - or at least its geometry was sent
    to the GPU for rendering, since the GPU might still clip or occlude the
    node.  This signal can only fire if both QGLSceneNode::CullBoundingBox
    and QGLSceneNode::ReportCulling options are both set.
*/

/*!
    \relates QGLSceneNode
    Print a description of \a node, and all its descendants, to the console.
    This function uses qDebug for its output, so in the case of Windows the
    output will go to the debug window.  If QT_NO_DEBUG_OUTPUT or QT_NO_DEBUG
    has been defined, (as in release mode) this function will exit without
    doing anything.

    If \a detailed is true (which it is by default) then all the properties
    of each node are printed, including materials, geometry and transforms.

    If \a detailed is false, then just one line is printed with the name and
    some identifying information including a unique id for the node.

    The \a indent and \a loop parameters are used internally.
*/
void qDumpScene(QGLSceneNode *node, bool detailed, int indent, const QSet<QGLSceneNode *> &loop)
{
#if defined(QT_NO_DEBUG_STREAM) || defined(QT_NO_DEBUG) || defined(QT_NO_DEBUG_OUTPUT)
    Q_UNUSED(node);
    Q_UNUSED(detailed);
    Q_UNUSED(indent);
    Q_UNUSED(loop);
#else
#if !defined(QT_NO_THREAD)
    QCoreApplication *app = QApplication::instance();
    QThread *appThread = 0;
    if (app)
        appThread = QApplication::instance()->thread();
#endif
    QSet<QGLSceneNode *> lp = loop;
    lp.insert(node);
    QString ind;
    ind.fill(QLatin1Char(' '), indent * 4);
    if (detailed)
    {
        qDebug("\n%s ======== Node: %p - %s =========", qPrintable(ind), node,
               qPrintable(node->objectName()));
    }
    else
    {
        qDebug("\n%s Node: %p - %s", qPrintable(ind), node,
               qPrintable(node->objectName()));
        return;
    }
#if !defined(QT_NO_THREAD)
    if (appThread && appThread != node->thread())
        qDebug("\n%s        from thread: %p", qPrintable(ind), node->thread());
#endif
    qDebug("%s start: %d   count: %d   children:", qPrintable(ind), node->start(), node->count());
    {
        QList<QGLSceneNode*> children = node->children();
        QList<QGLSceneNode*>::const_iterator it = children.constBegin();
        for (int i = 0; it != children.constEnd(); ++it, ++i)
            qDebug("%s    %d: %p  ", qPrintable(ind), i, *it);
    }
    if (!node->position().isNull())
    {
        QVector3D p = node->position();
        qDebug("%s position: (%0.4f, %0.4f, %0.4f)", qPrintable(ind),
                p.x(), p.y(), p.z());
    }
    if (node->localTransform().isIdentity())
    {
        qDebug("%s local transform: identity", qPrintable(ind));
    }
    else
    {
        qDebug("%s local transform:", qPrintable(ind));
        QMatrix4x4 m = node->localTransform();
        for (int i = 0; i < 4; ++i)
            qDebug("%s     %0.4f   %0.4f   %0.4f   %0.4f",
                    qPrintable(ind), m(i, 0), m(i, 1), m(i, 2), m(i, 3));
    }
    QList<QGraphicsTransform3D*> tx = node->transforms();
    if (tx.size() > 0)
        qDebug("%s transforms list:", qPrintable(ind));
    for (int i = 0; i < tx.size(); ++i)
    {
        const QMetaObject *obj = tx.at(i)->metaObject();
        qDebug("%s     %s", qPrintable(ind), obj->className());
    }
    if (!node->geometry().isEmpty())
    {
        qDebug("%s geometry: %d indexes, %d vertices",
                qPrintable(ind), node->geometry().count(), node->geometry().count(QGL::Position));
    }
    else
    {
        qDebug("%s geometry: NULL", qPrintable(ind));
    }
    if (node->materialIndex() != -1)
    {
        qDebug("%s material: %d", qPrintable(ind), node->materialIndex());
        QGLMaterial *mat = node->material();
        QGLMaterialCollection *pal = node->palette().data();
        if (pal)
            qDebug("%s palette: %p", qPrintable(ind), pal);
        else
            qDebug("%s no palette", qPrintable(ind));
        if (pal)
        {
            mat = pal->material(node->materialIndex());
            if (mat)
                qDebug("%s mat name from pal: %s ", qPrintable(ind),
                        qPrintable(pal->material(node->materialIndex())->objectName()));
            else
                qDebug("%s indexed material %d does not exist in palette!",
                        qPrintable(ind), node->materialIndex());
        }
        if (mat)
        {
            QString mat_spx = QString(QLatin1String(" Amb: %1 - Diff: %2 - Spec: %3 - Shin: %4"))
                    .arg((mat->ambientColor().name()))
                    .arg(mat->diffuseColor().name())
                    .arg(mat->specularColor().name())
                    .arg(mat->shininess());
            if (mat->objectName().isEmpty())
                qDebug("%s    material pointer %p: %s", qPrintable(ind), mat, qPrintable(mat_spx));
            else
                qDebug("%s    \"%s\": %s", qPrintable(ind),
                        qPrintable(mat->objectName()),  qPrintable(mat_spx));
            for (int i = 0; i < mat->textureLayerCount(); ++i)
            {
                if (mat->texture(i) != 0)
                {
                    QGLTexture2D *tex = mat->texture(i);
                    if (tex->objectName().isEmpty())
                        qDebug("%s         texture %p", qPrintable(ind), tex);
                    else
                        qDebug("%s         texture %s", qPrintable(ind),
                                qPrintable(tex->objectName()));
                    QSize sz = tex->size();
                    qDebug(" - size: %d (w) x %d (h)", sz.width(), sz.height());
                }
            }
        }
        else
        {
            qDebug("%s - could not find indexed material!!", qPrintable(ind));
        }
    }
    else
    {
        qDebug("%s material: NONE", qPrintable(ind));
    }

    if (node->hasEffect())
    {
        if (node->userEffect())
        {
            qDebug("%s user effect %p", qPrintable(ind),
                    node->userEffect());
        }
        else
        {
            switch (node->effect())
            {
            case QGL::FlatColor:
                qDebug("%s flat color effect", qPrintable(ind)); break;
            case QGL::FlatPerVertexColor:
                qDebug("%s flat per vertex color effect", qPrintable(ind)); break;
            case QGL::FlatReplaceTexture2D:
                qDebug("%s flat replace texture 2D effect", qPrintable(ind)); break;
            case QGL::FlatDecalTexture2D:
                qDebug("%s flat decal texture 2D effect", qPrintable(ind)); break;
            case QGL::LitMaterial:
                qDebug("%s lit material effect", qPrintable(ind)); break;
            case QGL::LitDecalTexture2D:
                qDebug("%s lit decal texture 2D effect", qPrintable(ind)); break;
            case QGL::LitModulateTexture2D:
                qDebug("%s lit modulate texture 2D effect", qPrintable(ind)); break;
            }
        }
    }
    else
    {
        qDebug("%s no effect set", qPrintable(ind));
    }
    QList<QGLSceneNode*> children = node->children();
    QList<QGLSceneNode*>::const_iterator it = children.constBegin();
    for ( ; it != children.constEnd(); ++it)
        if (!lp.contains(*it))
            qDumpScene(*it, indent + 1);
#endif
}

#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QGLSceneNode &node)
{
    dbg << &node << "\n    start:" << node.start() << " count:" << node.count();
    QList<QGLSceneNode*> children = node.children();
    QList<QGLSceneNode*>::const_iterator it = children.constBegin();
    for ( ; it != children.constEnd(); ++it)
        dbg << "\n        child:" << *it;

    if (node.localTransform().isIdentity())
        dbg << "\n    local transform: identity";
    else
        dbg << "\n    local transform:\n" << node.localTransform();

    if (node.geometry().count() > 0)
    {
        QGLMaterial *mat = node.material();
        QString mdesc;
        if (mat)
            mdesc = mat->objectName();
        dbg << "\n    geometry:" << node.geometry();
        dbg << "\n    material" << node.material() << ": " << mat << mdesc;
    }
    else
    {
        dbg << "\n    geometry: NULL";
        dbg << "\n    material" << node.material();
    }

    if (node.hasEffect())
    {
        if (node.userEffect())
        {
            dbg << "\n   user effect";
        }
        else
        {
            switch (node.effect())
            {
            case QGL::FlatColor:
                dbg << "\n    flat color effect"; break;
            case QGL::FlatPerVertexColor:
                dbg << "\n    flat per vertex color effect"; break;
            case QGL::FlatReplaceTexture2D:
                dbg << "\n    flat replace texture 2D effect"; break;
            case QGL::FlatDecalTexture2D:
                dbg << "\n    flat decal texture 2D effect"; break;
            case QGL::LitMaterial:
                dbg << "\n    lit material effect"; break;
            case QGL::LitDecalTexture2D:
                dbg << "\n    lit decal texture 2D effect"; break;
            case QGL::LitModulateTexture2D:
                dbg << "\n    lit modulate texture 2D effect"; break;
            }
        }
    }
    else
    {
        dbg << "\n    no effect set";
    }
    return dbg;
}

#endif // QT_NO_DEBUG_STREAM

QT_END_NAMESPACE