1/****************************************************************************
2**
3** Copyright (C) 2017 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:LGPL$
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 Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QT3DRENDER_BASEGEOMETRYLOADER_H
41#define QT3DRENDER_BASEGEOMETRYLOADER_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists for the convenience
48// of other Qt classes. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtCore/QObject>
55#include <QtCore/QVector>
56
57#include <QtGui/QVector2D>
58#include <QtGui/QVector3D>
59#include <QtGui/QVector4D>
60
61#include <Qt3DRender/private/qgeometryloaderinterface_p.h>
62
63#include <private/qlocale_tools_p.h>
64
65QT_BEGIN_NAMESPACE
66
67class QIODevice;
68class QString;
69
70namespace Qt3DRender {
71
72class QGeometry;
73
74class BaseGeometryLoader : public QGeometryLoaderInterface
75{
76 Q_OBJECT
77public:
78 BaseGeometryLoader();
79
80 void setLoadTextureCoordinatesEnabled(bool b) { m_loadTextureCoords = b; }
81 bool isLoadTextureCoordinatesEnabled() const { return m_loadTextureCoords; }
82
83 void setTangentGenerationEnabled(bool b) { m_generateTangents = b; }
84 bool isTangentGenerationEnabled() const { return m_generateTangents; }
85
86 void setMeshCenteringEnabled(bool b) { m_centerMesh = b; }
87 bool isMeshCenteringEnabled() const { return m_centerMesh; }
88
89 bool hasNormals() const { return !m_normals.isEmpty(); }
90 bool hasTextureCoordinates() const { return !m_texCoords.isEmpty(); }
91 bool hasTangents() const { return !m_tangents.isEmpty(); }
92
93 QVector<QVector3D> vertices() const { return m_points; }
94 QVector<QVector3D> normals() const { return m_normals; }
95 QVector<QVector2D> textureCoordinates() const { return m_texCoords; }
96 QVector<QVector4D> tangents() const { return m_tangents; }
97 QVector<unsigned int> indices() const { return m_indices; }
98
99 QGeometry *geometry() const override;
100
101 bool load(QIODevice *ioDev, const QString &subMesh = QString()) override;
102
103protected:
104 virtual bool doLoad(QIODevice *ioDev, const QString &subMesh = QString()) = 0;
105
106 void generateAveragedNormals(const QVector<QVector3D>& points,
107 QVector<QVector3D>& normals,
108 const QVector<unsigned int>& faces) const;
109 void generateGeometry();
110 void generateTangents(const QVector<QVector3D>& points,
111 const QVector<QVector3D>& normals,
112 const QVector<unsigned int>& faces,
113 const QVector<QVector2D>& texCoords,
114 QVector<QVector4D>& tangents) const;
115 void center(QVector<QVector3D>& points);
116
117 bool m_loadTextureCoords;
118 bool m_generateTangents;
119 bool m_centerMesh;
120
121 QVector<QVector3D> m_points;
122 QVector<QVector3D> m_normals;
123 QVector<QVector2D> m_texCoords;
124 QVector<QVector4D> m_tangents;
125 QVector<unsigned int> m_indices;
126
127 QGeometry *m_geometry;
128};
129
130struct FaceIndices
131{
132 FaceIndices()
133 : positionIndex(std::numeric_limits<unsigned int>::max())
134 , texCoordIndex(std::numeric_limits<unsigned int>::max())
135 , normalIndex(std::numeric_limits<unsigned int>::max())
136 {}
137
138 FaceIndices(unsigned int posIndex, unsigned int tcIndex, unsigned int nIndex)
139 : positionIndex(posIndex)
140 , texCoordIndex(tcIndex)
141 , normalIndex(nIndex)
142 {}
143
144 bool operator == (const FaceIndices &other) const
145 {
146 return positionIndex == other.positionIndex &&
147 texCoordIndex == other.texCoordIndex &&
148 normalIndex == other.normalIndex;
149 }
150
151 unsigned int positionIndex;
152 unsigned int texCoordIndex;
153 unsigned int normalIndex;
154};
155QT3D_DECLARE_TYPEINFO(Qt3DRender, FaceIndices, Q_PRIMITIVE_TYPE)
156
157struct ByteArraySplitterEntry
158{
159 int start;
160 int size;
161};
162QT3D_DECLARE_TYPEINFO(Qt3DRender, ByteArraySplitterEntry, Q_PRIMITIVE_TYPE)
163
164/*
165 * A helper class to split a QByteArray and access its sections without
166 * additional memory allocations.
167 */
168class ByteArraySplitter
169{
170public:
171 explicit ByteArraySplitter(const char *begin, const char *end, char delimiter, Qt::SplitBehavior splitBehavior)
172 : m_input(begin)
173 {
174 int position = 0;
175 int lastPosition = 0;
176 for (auto it = begin; it != end; ++it) {
177 if (*it == delimiter) {
178 if (position > lastPosition || splitBehavior == Qt::KeepEmptyParts) { // skip multiple consecutive delimiters
179 const ByteArraySplitterEntry entry = { .start: lastPosition, .size: position - lastPosition };
180 m_entries.append(t: entry);
181 }
182 lastPosition = position + 1;
183 }
184
185 ++position;
186 }
187
188 const ByteArraySplitterEntry entry = { .start: lastPosition, .size: position - lastPosition };
189 m_entries.append(t: entry);
190 }
191
192 int size() const
193 {
194 return m_entries.size();
195 }
196
197 const char *charPtrAt(int index) const
198 {
199 return m_input + m_entries[index].start;
200 }
201
202 float floatAt(int index) const
203 {
204 return qstrntod(s00: m_input + m_entries[index].start, len: m_entries[index].size, se: nullptr, ok: nullptr);
205 }
206
207 int intAt(int index) const
208 {
209 return strtol(nptr: m_input + m_entries[index].start, endptr: nullptr, base: 10);
210 }
211
212 QString stringAt(int index) const
213 {
214 return QString::fromLatin1(str: m_input + m_entries[index].start, size: m_entries[index].size);
215 }
216
217 ByteArraySplitter splitterAt(int index, char delimiter, Qt::SplitBehavior splitBehavior) const
218 {
219 return ByteArraySplitter(m_input + m_entries[index].start, m_input + m_entries[index].start + m_entries[index].size, delimiter, splitBehavior);
220 }
221
222private:
223 QVarLengthArray<ByteArraySplitterEntry, 16> m_entries;
224 const char *m_input;
225};
226
227} // namespace Qt3DRender
228
229QT_END_NAMESPACE
230
231#endif // QT3DRENDER_BASEGEOMETRYLOADER_H
232

source code of qt3d/src/plugins/geometryloaders/default/basegeometryloader_p.h