1 | /**************************************************************************** |
---|---|
2 | ** |
3 | ** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #ifndef GEOMETRYTESTHELPER_H |
30 | #define GEOMETRYTESTHELPER_H |
31 | |
32 | #include <Qt3DRender/qattribute.h> |
33 | #include <Qt3DRender/qbuffer.h> |
34 | #include <Qt3DRender/qbufferdatagenerator.h> |
35 | #include <Qt3DRender/qgeometry.h> |
36 | |
37 | inline void generateGeometry(Qt3DRender::QGeometry &geometry) |
38 | { |
39 | // Get all attributes |
40 | const QVector<Qt3DRender::QAttribute *> attributes = geometry.attributes(); |
41 | |
42 | // Get all unique data generators from the buffers referenced by the attributes |
43 | QHash<Qt3DRender::QBufferDataGeneratorPtr, Qt3DRender::QBuffer *> dataGenerators; |
44 | for (const auto attribute : attributes) { |
45 | QT_WARNING_PUSH |
46 | QT_WARNING_DISABLE_DEPRECATED |
47 | const auto dataGenerator = attribute->buffer()->dataGenerator(); |
48 | if (!dataGenerators.contains(akey: dataGenerator)) |
49 | dataGenerators.insert(akey: dataGenerator, avalue: attribute->buffer()); |
50 | QT_WARNING_POP |
51 | } |
52 | |
53 | // Generate data for each buffer |
54 | const auto end = dataGenerators.end(); |
55 | for (auto it = dataGenerators.begin(); it != end; ++it) { |
56 | Qt3DRender::QBufferDataGeneratorPtr dataGenerator = it.key(); |
57 | const QByteArray data = (*dataGenerator)(); |
58 | |
59 | Qt3DRender::QBuffer *buffer = it.value(); |
60 | buffer->setData(data); |
61 | } |
62 | } |
63 | |
64 | template<typename IndexType> |
65 | IndexType extractIndexData(Qt3DRender::QAttribute *attribute, int index) |
66 | { |
67 | // Get the raw data |
68 | const IndexType *typedData = reinterpret_cast<const IndexType *>(attribute->buffer()->data().constData()); |
69 | |
70 | // Offset into the data taking stride and offset into account |
71 | const IndexType indexValue = *(typedData + index); |
72 | return indexValue; |
73 | } |
74 | |
75 | template<typename VertexType, typename IndexType> |
76 | VertexType extractVertexData(Qt3DRender::QAttribute *attribute, IndexType index) |
77 | { |
78 | // Get the raw data |
79 | const char *rawData = attribute->buffer()->data().constData(); |
80 | |
81 | // Offset into the data taking stride and offset into account |
82 | const char *vertexData = rawData + (index * attribute->byteStride() + attribute->byteOffset()); |
83 | |
84 | // Construct vertex from the typed data |
85 | VertexType vertex; |
86 | const Qt3DRender::QAttribute::VertexBaseType type = attribute->vertexBaseType(); |
87 | switch (type) |
88 | { |
89 | case Qt3DRender::QAttribute::Float: { |
90 | const float *typedVertexData = reinterpret_cast<const float *>(vertexData); |
91 | const int components = attribute->vertexSize(); |
92 | for (int i = 0; i < components; ++i) |
93 | vertex[i] = typedVertexData[i]; |
94 | break; |
95 | |
96 | // TODO: Handle other types as needed |
97 | } |
98 | |
99 | default: |
100 | qWarning() << "Unhandled type"; |
101 | Q_UNREACHABLE(); |
102 | break; |
103 | } |
104 | |
105 | return vertex; |
106 | } |
107 | |
108 | #endif // GEOMETRYTESTHELPER_H |
109 |