summaryrefslogtreecommitdiffstats
path: root/src/plugins/geometryloaders/default/basegeometryloader_p.h
blob: 3e1c8f24292d721454edc6027832a83bdd4831cb (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
/****************************************************************************
**
** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#ifndef QT3DRENDER_BASEGEOMETRYLOADER_H
#define QT3DRENDER_BASEGEOMETRYLOADER_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists for the convenience
// of other Qt classes.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QtCore/QObject>
#include <QtCore/QVector>

#include <QtGui/QVector2D>
#include <QtGui/QVector3D>
#include <QtGui/QVector4D>

#include <Qt3DRender/private/qgeometryloaderinterface_p.h>

#include <private/qlocale_tools_p.h>

QT_BEGIN_NAMESPACE

class QIODevice;
class QString;

namespace Qt3DRender {

class QGeometry;

class BaseGeometryLoader : public QGeometryLoaderInterface
{
    Q_OBJECT
public:
    BaseGeometryLoader();

    void setLoadTextureCoordinatesEnabled(bool b) { m_loadTextureCoords = b; }
    bool isLoadTextureCoordinatesEnabled() const { return m_loadTextureCoords; }

    void setTangentGenerationEnabled(bool b) { m_generateTangents = b; }
    bool isTangentGenerationEnabled() const { return m_generateTangents; }

    void setMeshCenteringEnabled(bool b) { m_centerMesh = b; }
    bool isMeshCenteringEnabled() const { return m_centerMesh; }

    bool hasNormals() const { return !m_normals.isEmpty(); }
    bool hasTextureCoordinates() const { return !m_texCoords.isEmpty(); }
    bool hasTangents() const { return !m_tangents.isEmpty(); }

    QVector<QVector3D> vertices() const { return m_points; }
    QVector<QVector3D> normals() const { return m_normals; }
    QVector<QVector2D> textureCoordinates() const { return m_texCoords; }
    QVector<QVector4D> tangents() const { return m_tangents; }
    QVector<unsigned int> indices() const { return m_indices; }

    QGeometry *geometry() const override;

    bool load(QIODevice *ioDev, const QString &subMesh = QString()) override;

protected:
    virtual bool doLoad(QIODevice *ioDev, const QString &subMesh = QString()) = 0;

    void generateAveragedNormals(const QVector<QVector3D>& points,
                                 QVector<QVector3D>& normals,
                                 const QVector<unsigned int>& faces) const;
    void generateGeometry();
    void generateTangents(const QVector<QVector3D>& points,
                          const QVector<QVector3D>& normals,
                          const QVector<unsigned int>& faces,
                          const QVector<QVector2D>& texCoords,
                          QVector<QVector4D>& tangents) const;
    void center(QVector<QVector3D>& points);

    bool m_loadTextureCoords;
    bool m_generateTangents;
    bool m_centerMesh;

    QVector<QVector3D> m_points;
    QVector<QVector3D> m_normals;
    QVector<QVector2D> m_texCoords;
    QVector<QVector4D> m_tangents;
    QVector<unsigned int> m_indices;

    QGeometry *m_geometry;
};

struct FaceIndices
{
    FaceIndices()
        : positionIndex(std::numeric_limits<unsigned int>::max())
        , texCoordIndex(std::numeric_limits<unsigned int>::max())
        , normalIndex(std::numeric_limits<unsigned int>::max())
    {}

    FaceIndices(unsigned int posIndex, unsigned int tcIndex, unsigned int nIndex)
        : positionIndex(posIndex)
        , texCoordIndex(tcIndex)
        , normalIndex(nIndex)
    {}

    bool operator == (const FaceIndices &other) const
    {
        return positionIndex == other.positionIndex &&
               texCoordIndex == other.texCoordIndex &&
               normalIndex == other.normalIndex;
    }

    unsigned int positionIndex;
    unsigned int texCoordIndex;
    unsigned int normalIndex;
};
QT3D_DECLARE_TYPEINFO(Qt3DRender, FaceIndices, Q_PRIMITIVE_TYPE)

struct ByteArraySplitterEntry
{
    int start;
    int size;
};
QT3D_DECLARE_TYPEINFO(Qt3DRender, ByteArraySplitterEntry, Q_PRIMITIVE_TYPE)

/*
 * A helper class to split a QByteArray and access its sections without
 * additional memory allocations.
 */
class ByteArraySplitter
{
public:
    explicit ByteArraySplitter(const char *begin, const char *end, char delimiter, Qt::SplitBehavior splitBehavior)
        : m_input(begin)
    {
        int position = 0;
        int lastPosition = 0;
        for (auto it = begin; it != end; ++it) {
            if (*it == delimiter) {
                if (position > lastPosition || splitBehavior == Qt::KeepEmptyParts) { // skip multiple consecutive delimiters
                    const ByteArraySplitterEntry entry = { lastPosition, position - lastPosition };
                    m_entries.append(entry);
                }
                lastPosition = position + 1;
            }

            ++position;
        }

        const ByteArraySplitterEntry entry = { lastPosition, position - lastPosition };
        m_entries.append(entry);
    }

    int size() const
    {
        return m_entries.size();
    }

    const char *charPtrAt(int index) const
    {
        return m_input + m_entries[index].start;
    }

    float floatAt(int index) const
    {
        return qstrntod(m_input + m_entries[index].start, m_entries[index].size, nullptr, nullptr);
    }

    int intAt(int index) const
    {
        return strtol(m_input + m_entries[index].start, nullptr, 10);
    }

    QString stringAt(int index) const
    {
        return QString::fromLatin1(m_input + m_entries[index].start, m_entries[index].size);
    }

    ByteArraySplitter splitterAt(int index, char delimiter, Qt::SplitBehavior splitBehavior) const
    {
        return ByteArraySplitter(m_input + m_entries[index].start, m_input + m_entries[index].start + m_entries[index].size, delimiter, splitBehavior);
    }

private:
    QVarLengthArray<ByteArraySplitterEntry, 16> m_entries;
    const char *m_input;
};

} // namespace Qt3DRender

QT_END_NAMESPACE

#endif // QT3DRENDER_BASEGEOMETRYLOADER_H