summaryrefslogtreecommitdiffstats
path: root/src/render/frontend/qspheremesh.cpp
blob: 7e103e8fe5584f0a74db7af5d4125bf715f0da3b (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
/****************************************************************************
**
** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
** Copyright (C) 2015 The Qt Company Ltd and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3$
** 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 http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later 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 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef _USE_MATH_DEFINES
# define _USE_MATH_DEFINES // For MSVC
#endif

#include "qspheremesh.h"
#include <Qt3DRenderer/private/renderlogging_p.h>
#include "qbuffer.h"
#include "qattribute.h"
#include "qmeshdata.h"
#include <private/qabstractmesh_p.h>

#include <qmath.h>

QT_BEGIN_NAMESPACE

namespace Qt3D {

class SphereMeshFunctor : public QAbstractMeshFunctor
{
public:
    SphereMeshFunctor(int rings, int slices, float radius, bool generateTangents);
    QMeshDataPtr operator ()() Q_DECL_OVERRIDE;
    bool operator ==(const QAbstractMeshFunctor &other) const Q_DECL_OVERRIDE;

private:
    int m_rings;
    int m_slices;
    float m_radius;
    bool m_generateTangents;
};

class QSphereMeshPrivate : public QAbstractMeshPrivate
{
    QSphereMeshPrivate()
        : QAbstractMeshPrivate()
        , m_generateTangents(false)
        , m_rings(16)
        , m_slices(16)
        , m_radius(1.0)
    {}

    Q_DECLARE_PUBLIC (QSphereMesh)
    bool m_generateTangents;
    int m_rings;
    int m_slices;
    float m_radius;
};

QSphereMesh::QSphereMesh(QNode *parent)
    : QAbstractMesh(*new QSphereMeshPrivate, parent)
{
    update();
}

void QSphereMesh::copy(const QNode *ref)
{
    QAbstractMesh::copy(ref);
    const QSphereMesh *mesh = static_cast<const QSphereMesh*>(ref);
    d_func()->m_generateTangents = mesh->d_func()->m_generateTangents;
    d_func()->m_rings = mesh->d_func()->m_rings;
    d_func()->m_slices = mesh->d_func()->m_slices;
    d_func()->m_radius = mesh->d_func()->m_radius;
}

void QSphereMesh::setRings(int rings)
{
    Q_D(QSphereMesh);
    if (rings != d->m_rings) {
        d->m_rings = rings;
        emit ringsChanged();
        QAbstractMesh::update();
    }
}

void QSphereMesh::setSlices(int slices)
{
    Q_D(QSphereMesh);
    if (slices != d->m_slices) {
        d->m_slices = slices;
        emit slicesChanged();
        QAbstractMesh::update();
    }
}

void QSphereMesh::setRadius(float radius)
{
    Q_D(QSphereMesh);
    if (radius != d->m_radius) {
        d->m_radius = radius;
        emit radiusChanged();
        QAbstractMesh::update();
    }
}

void QSphereMesh::setGenerateTangents(bool gen)
{
    Q_D(QSphereMesh);
    if (d->m_generateTangents != gen) {
        d->m_generateTangents = gen;
        emit generateTangentsChanged();
        QAbstractMesh::update();
    }
}

bool QSphereMesh::generateTangents() const
{
    Q_D(const QSphereMesh);
    return d->m_generateTangents;
}

QAbstractMeshFunctorPtr QSphereMesh::meshFunctor() const
{
    Q_D(const QSphereMesh);
    return QAbstractMeshFunctorPtr(new SphereMeshFunctor(d->m_rings, d->m_slices, d->m_radius, d->m_generateTangents));
}

int QSphereMesh::rings() const
{
    Q_D(const QSphereMesh);
    return d->m_rings;
}

int QSphereMesh::slices() const
{
    Q_D(const QSphereMesh);
    return d->m_slices;
}

float QSphereMesh::radius() const
{
    Q_D(const QSphereMesh);
    return d->m_radius;
}

QMeshDataPtr createSphereMesh(double radius, int rings, int slices, bool hasTangents)
{
    QMeshDataPtr mesh(new QMeshData(QMeshData::Triangles));

    int nVerts  = ( slices + 1 ) * ( rings + 1 ); // One extra line of latitude
    QByteArray bufferBytes;
    // vec3 pos, vec2 texCoord, vec3 normal, vec4 tangent
    quint32 elementSize = 3 + 2 + 3 + (hasTangents ? 4 : 0);
    quint32 stride = elementSize * sizeof(float);
    bufferBytes.resize(stride * nVerts);

    float* fptr = reinterpret_cast<float*>(bufferBytes.data());

    const float dTheta = (M_PI * 2) / static_cast<float>( slices );
    const float dPhi = M_PI / static_cast<float>( rings );
    const float du = 1.0f / static_cast<float>( slices );
    const float dv = 1.0f / static_cast<float>( rings );

    // Iterate over latitudes (rings)
    for ( int lat = 0; lat < rings + 1; ++lat )
    {
        const float phi = M_PI_2 - static_cast<float>( lat ) * dPhi;
        const float cosPhi = qCos( phi );
        const float sinPhi = qSin( phi );
        const float v = 1.0f - static_cast<float>( lat ) * dv;

        // Iterate over longitudes (slices)
        for ( int lon = 0; lon < slices + 1; ++lon )
        {
            const float theta = static_cast<float>( lon ) * dTheta;
            const float cosTheta = qCos( theta );
            const float sinTheta = qSin( theta );
            const float u = static_cast<float>( lon ) * du;

            *fptr++ = radius * cosTheta * cosPhi;
            *fptr++ = radius * sinPhi;
            *fptr++ = radius * sinTheta * cosPhi;

            *fptr++ = u;
            *fptr++ = v;

            *fptr++   = cosTheta * cosPhi;
            *fptr++ = sinPhi;
            *fptr++ = sinTheta * cosPhi;

            if (hasTangents) {
                *fptr++ = sinTheta;
                *fptr++ = 0.0;
                *fptr++ = -cosTheta;
                *fptr++ = 1.0;
            }
        }
    }

    BufferPtr buf(new Buffer(QOpenGLBuffer::VertexBuffer));
    buf->setUsage(QOpenGLBuffer::StaticDraw);
    buf->setData(bufferBytes);

    mesh->addAttribute(QMeshData::defaultPositionAttributeName(), AttributePtr(new Attribute(buf, GL_FLOAT_VEC3, nVerts, 0, stride)));
    quint32 offset = sizeof(float) * 3;

    mesh->addAttribute(QMeshData::defaultTextureCoordinateAttributeName(), AttributePtr(new Attribute(buf, GL_FLOAT_VEC2, nVerts, offset, stride)));
    offset += sizeof(float) * 2;

    mesh->addAttribute(QMeshData::defaultNormalAttributeName(), AttributePtr(new Attribute(buf, GL_FLOAT_VEC3, nVerts, offset, stride)));
    offset += sizeof(float) * 3;

    if (hasTangents) {
        mesh->addAttribute(QMeshData::defaultTangentAttributeName(), AttributePtr(new Attribute(buf, GL_FLOAT_VEC4, nVerts, offset, stride)));
        offset += sizeof(float) * 4;
    }

    int faces = (slices * 2) * (rings - 2); // two tris per slice, for all middle rings
    faces += 2 * slices; // tri per slice for both top and bottom

    QByteArray indexBytes;
    int indices = faces * 3;
    Q_ASSERT(indices < 65536);
    indexBytes.resize(indices * sizeof(quint16));
    quint16* indexPtr = reinterpret_cast<quint16*>(indexBytes.data());

    // top cap
    {
        const int nextRingStartIndex = slices + 1;
        for ( int j = 0; j < slices; ++j )
        {
            *indexPtr++ = nextRingStartIndex + j;
            *indexPtr++ = 0;
            *indexPtr++ = nextRingStartIndex + j + 1;
        }
    }

    for ( int i = 1; i < (rings - 1); ++i )
    {
        const int ringStartIndex = i * ( slices + 1 );
        const int nextRingStartIndex = ( i + 1 ) * ( slices + 1 );

        for ( int j = 0; j < slices; ++j )
        {
            // Split the quad into two triangles
            *indexPtr++ = ringStartIndex + j;
            *indexPtr++ = ringStartIndex + j + 1;
            *indexPtr++ = nextRingStartIndex + j;
            *indexPtr++ = nextRingStartIndex + j;
            *indexPtr++ = ringStartIndex + j + 1;
            *indexPtr++ = nextRingStartIndex + j + 1;
        }
    }

    // bottom cap
    {
        const int ringStartIndex = (rings - 1) * ( slices + 1);
        const int nextRingStartIndex = (rings) * ( slices + 1);
        for ( int j = 0; j < slices; ++j )
        {
            *indexPtr++ = ringStartIndex + j + 1;
            *indexPtr++ = nextRingStartIndex;
            *indexPtr++ = ringStartIndex + j;
        }
    }

    BufferPtr indexBuffer(new Buffer(QOpenGLBuffer::IndexBuffer));
    indexBuffer->setUsage(QOpenGLBuffer::StaticDraw);
    indexBuffer->setData(indexBytes);
    mesh->setIndexAttribute(AttributePtr(new Attribute(indexBuffer, GL_UNSIGNED_SHORT, indices, 0, 0)));

    mesh->computeBoundsFromAttribute(QMeshData::defaultPositionAttributeName());
    qCDebug(Render::Frontend) << "computed sphere bounds is:" << mesh->boundingBox();

    return mesh;
}

SphereMeshFunctor::SphereMeshFunctor(int rings, int slices, float radius, bool generateTangents)
    : QAbstractMeshFunctor()
    , m_rings(rings)
    , m_slices(slices)
    , m_radius(radius)
    , m_generateTangents(generateTangents)
{
}

QMeshDataPtr SphereMeshFunctor::operator ()()
{
    return createSphereMesh(m_radius, m_rings, m_slices, m_generateTangents);
}

bool SphereMeshFunctor::operator ==(const QAbstractMeshFunctor &other) const
{
    const SphereMeshFunctor *otherFunctor = dynamic_cast<const SphereMeshFunctor *>(&other);
    if (otherFunctor != Q_NULLPTR)
        return (otherFunctor->m_rings == m_rings &&
                otherFunctor->m_slices == m_slices &&
                otherFunctor->m_radius == m_radius &&
                otherFunctor->m_generateTangents == m_generateTangents);
    return false;
}

} //Qt3D

QT_END_NAMESPACE