1 | // Copyright (C) 2020 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #ifndef QSSGMESHBVHBUILDER_H |
5 | #define QSSGMESHBVHBUILDER_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | #include <QtQuick3DUtils/private/qtquick3dutilsglobal_p.h> |
18 | #include <QtQuick3DUtils/private/qssgmeshbvh_p.h> |
19 | #include <QtQuick3DUtils/private/qssgmesh_p.h> |
20 | |
21 | QT_BEGIN_NAMESPACE |
22 | |
23 | class Q_QUICK3DUTILS_EXPORT QSSGMeshBVHBuilder |
24 | { |
25 | public: |
26 | QSSGMeshBVHBuilder(const QSSGMesh::Mesh &mesh); |
27 | QSSGMeshBVHBuilder(const QByteArray &vertexBuffer, |
28 | int stride, |
29 | int posOffset, |
30 | bool hasUV = false, |
31 | int uvOffset = -1, |
32 | bool hasIndexBuffer = false, |
33 | const QByteArray &indexBuffer = QByteArray(), |
34 | QSSGRenderComponentType indexBufferType = QSSGRenderComponentType::Int32); |
35 | |
36 | QSSGMeshBVH* buildTree(); |
37 | |
38 | private: |
39 | enum class Axis |
40 | { |
41 | None = -1, |
42 | X = 0, |
43 | Y = 1, |
44 | Z = 2 |
45 | }; |
46 | struct Split { |
47 | Axis axis; |
48 | float pos; |
49 | }; |
50 | |
51 | QVector<QSSGMeshBVHTriangle*> calculateTriangleBounds(quint32 indexOffset, quint32 indexCount) const; |
52 | quint32 getIndexBufferValue(quint32 index) const; |
53 | QVector3D getVertexBufferValuePosition(quint32 index) const; |
54 | QVector2D getVertexBufferValueUV(quint32 index) const; |
55 | |
56 | QSSGMeshBVHNode *splitNode(QSSGMeshBVHNode *node, quint32 offset, quint32 count, quint32 depth = 0); |
57 | QSSGBounds3 getBounds(quint32 offset, quint32 count) const; |
58 | Split getOptimalSplit(const QSSGBounds3 &nodeBounds, quint32 offset, quint32 count) const; |
59 | static Axis getLongestDimension(const QSSGBounds3 &nodeBounds); |
60 | float getAverageValue(quint32 offset, quint32 count, Axis axis) const; |
61 | quint32 partition(quint32 offset, quint32 count, const Split &split); |
62 | |
63 | QSSGMesh::Mesh m_mesh; |
64 | QSSGRenderComponentType m_indexBufferComponentType; |
65 | QByteArray m_indexBufferData; |
66 | QByteArray m_vertexBufferData; |
67 | quint32 m_vertexStride; |
68 | bool m_hasPositionData = false; |
69 | quint32 m_vertexPosOffset; |
70 | bool m_hasUVData = false; |
71 | quint32 m_vertexUVOffset; |
72 | bool m_hasIndexBuffer = true; |
73 | |
74 | |
75 | QVector<QSSGMeshBVHTriangle *> m_triangleBounds; |
76 | QVector<QSSGMeshBVHNode *> m_roots; |
77 | quint32 m_maxTreeDepth = 40; |
78 | quint32 m_maxLeafTriangles = 10; |
79 | }; |
80 | |
81 | QT_END_NAMESPACE |
82 | |
83 | #endif // QSSGMESHBVHBUILDER_H |
84 | |