summaryrefslogtreecommitdiffstats
path: root/src/render/backend/entity.cpp
blob: 9aa0da9464de86c36dc595128952db203eeba35b (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/****************************************************************************
**
** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
** 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$
**
****************************************************************************/

#include "entity_p.h"
#include <Qt3DRenderer/private/managers_p.h>
#include <Qt3DRenderer/private/renderer_p.h>
#include <Qt3DRenderer/qabstractlight.h>
#include <Qt3DRenderer/qlayer.h>
#include <Qt3DRenderer/qmaterial.h>
#include <Qt3DRenderer/qmesh.h>
#include <Qt3DRenderer/private/renderlogging_p.h>
#include <Qt3DRenderer/sphere.h>
#include <Qt3DRenderer/qshaderdata.h>
#include <Qt3DRenderer/qgeometryrenderer.h>
#include <Qt3DRenderer/private/geometryrenderermanager_p.h>

#include <Qt3DCore/qcameralens.h>
#include <Qt3DCore/qentity.h>
#include <Qt3DCore/qscenepropertychange.h>
#include <Qt3DCore/qtransform.h>

#include <QMatrix4x4>
#include <QString>

QT_BEGIN_NAMESPACE

using namespace Qt3D;

namespace Qt3DRender {
namespace Render {

Entity::Entity()
    : QBackendNode()
    , m_renderer(Q_NULLPTR)
    , m_localBoundingVolume(new Sphere)
    , m_worldBoundingVolume(new Sphere)
{
}

Entity::~Entity()
{
    cleanup();
}

void Entity::cleanup()
{
    if (m_renderer != Q_NULLPTR) {
        Entity *parentEntity = parent();
        if (parentEntity != Q_NULLPTR)
            parentEntity->removeChildHandle(m_handle);
        for (int i = 0; i < m_childrenHandles.size(); i++)
            m_renderer->renderNodesManager()->release(m_childrenHandles[i]);
        // We need to release using peerUuid otherwise the handle will be cleared
        // but would still remain in the Id to Handle table
        m_renderer->worldMatrixManager()->releaseResource(peerUuid());
        m_worldTransform = HMatrix();

        // Release all component will have to perform their own release when they receive the
        // NodeDeleted/NodeAboutToBeDeleted notification
        qCDebug(Render::RenderNodes) << Q_FUNC_INFO;

        // Clear components
        m_transformComponent = Qt3D::QNodeId();
        m_cameraComponent = Qt3D::QNodeId();
        m_materialComponent = Qt3D::QNodeId();
        m_geometryRendererComponent = Qt3D::QNodeId();
        m_layerComponents.clear();
        m_shaderDataComponents.clear();
    }
    delete m_localBoundingVolume;
    delete m_worldBoundingVolume;
    m_localBoundingVolume = Q_NULLPTR;
    m_worldBoundingVolume = Q_NULLPTR;


}

void Entity::setParentHandle(HEntity parentHandle)
{
    Q_ASSERT(m_renderer);
    // Remove ourselves from previous parent children list
    Entity *parent = m_renderer->renderNodesManager()->data(parentHandle);
    if (parent != Q_NULLPTR && parent->m_childrenHandles.contains(m_handle))
        parent->m_childrenHandles.remove(m_handle);
    m_parentHandle = parentHandle;
    parent = m_renderer->renderNodesManager()->data(parentHandle);
    if (parent != Q_NULLPTR && !parent->m_childrenHandles.contains(m_handle))
        parent->m_childrenHandles.append(m_handle);
}

void Entity::setRenderer(Renderer *renderer)
{
    m_renderer = renderer;
}

void Entity::setHandle(HEntity handle)
{
    m_handle = handle;
}

void Entity::updateFromPeer(Qt3D::QNode *peer)
{
    QEntity *entity = static_cast<QEntity *>(peer);
    const QNodeId parentEntityId = entity->parentEntityId();

    m_objectName = peer->objectName();
    m_worldTransform = m_renderer->worldMatrixManager()->getOrAcquireHandle(peerUuid());

    // TO DO: Suboptimal -> Maybe have a Hash<QComponent, QEntityList> instead
    m_transformComponent = QNodeId();
    m_materialComponent = QNodeId();
    m_cameraComponent = QNodeId();
    m_geometryRendererComponent = QNodeId();
    m_layerComponents.clear();
    m_shaderDataComponents.clear();

    Q_FOREACH (QComponent *comp, entity->components())
        addComponent(comp);

    if (!parentEntityId.isNull()) {
        setParentHandle(m_renderer->renderNodesManager()->lookupHandle(parentEntityId));
    } else {
        qCDebug(Render::RenderNodes) << Q_FUNC_INFO << "No parent entity found for Entity" << peerUuid();
    }
}

void Entity::sceneChangeEvent(const Qt3D::QSceneChangePtr &e)
{
    QScenePropertyChangePtr propertyChange = qSharedPointerCast<QScenePropertyChange>(e);
    switch (e->type()) {

    case ComponentAdded: {
        QNodePtr nodePtr = propertyChange->value().value<QNodePtr>();
        QComponent *component = qobject_cast<QComponent *>(nodePtr.data());
        qCDebug(Render::RenderNodes) << Q_FUNC_INFO << "Component Added" << m_objectName << component->objectName();
        addComponent(component);
        break;
    }

    case ComponentRemoved: {
        QNodeId nodeId = propertyChange->value().value<QNodeId>();
        qCDebug(Render::RenderNodes) << Q_FUNC_INFO << "Component Removed";
        removeComponent(nodeId);
        break;
    }

    default:
        break;
    }
}

void Entity::dump() const
{
    static int depth = 0;
    QString indent(2 * depth++, QChar::fromLatin1(' '));
    qCDebug(Backend) << indent + m_objectName;
    foreach (const Entity *child, children())
        child->dump();
    --depth;
}

Entity *Entity::parent() const
{
    return m_renderer->renderNodesManager()->data(m_parentHandle);
}

void Entity::appendChildHandle(HEntity childHandle)
{
    if (!m_childrenHandles.contains(childHandle)) {
        m_childrenHandles.append(childHandle);
        Entity *child = m_renderer->renderNodesManager()->data(childHandle);
        if (child != Q_NULLPTR)
            child->m_parentHandle = m_handle;
    }
}

void Entity::removeChildHandle(HEntity childHandle)
{
    // TO DO : Check if a QList here wouldn't be more performant
    if (m_childrenHandles.contains(childHandle)) {
        m_childrenHandles.removeAt(m_childrenHandles.indexOf(childHandle));
    }
}

QVector<Entity *> Entity::children() const
{
    QVector<Entity *> childrenVector;
    childrenVector.reserve(m_childrenHandles.size());
    foreach (HEntity handle, m_childrenHandles) {
        Entity *child = m_renderer->renderNodesManager()->data(handle);
        if (child != Q_NULLPTR)
            childrenVector.append(child);
    }
    return childrenVector;
}

QMatrix4x4 *Entity::worldTransform()
{
    return m_renderer->worldMatrixManager()->data(m_worldTransform);
}

const QMatrix4x4 *Entity::worldTransform() const
{
    return m_renderer->worldMatrixManager()->data(m_worldTransform);
}

void Entity::addComponent(Qt3D::QComponent *component)
{
    // The backend element is always created when this method is called
    // If that's not the case something has gone wrong

    if (qobject_cast<Qt3D::QTransform*>(component) != Q_NULLPTR)
        m_transformComponent = component->id();
    else if (qobject_cast<QCameraLens *>(component) != Q_NULLPTR)
        m_cameraComponent = component->id();
    else if (qobject_cast<QLayer *>(component) != Q_NULLPTR)
        m_layerComponents.append(component->id());
    else if (qobject_cast<QMaterial *>(component) != Q_NULLPTR)
        m_materialComponent = component->id();
    else if (qobject_cast<QShaderData *>(component) != Q_NULLPTR)
        m_shaderDataComponents.append(component->id());
    else if (qobject_cast<QGeometryRenderer *>(component) != Q_NULLPTR)
        m_geometryRendererComponent = component->id();
}

void Entity::removeComponent(const Qt3D::QNodeId &nodeId)
{
    if (m_transformComponent == nodeId)
        m_transformComponent = QNodeId();
    else if (m_cameraComponent == nodeId)
        m_cameraComponent = QNodeId();
    else if (m_layerComponents.contains(nodeId))
        m_layerComponents.removeAll(nodeId);
    else if (m_materialComponent == nodeId)
        m_materialComponent = QNodeId();
    else if (m_shaderDataComponents.contains(nodeId))
        m_shaderDataComponents.removeAll(nodeId);
    else if (m_geometryRendererComponent == nodeId)
        m_geometryRendererComponent = QNodeId();
}

template<>
HMaterial Entity::componentHandle<Material>() const
{
    return m_renderer->materialManager()->lookupHandle(m_materialComponent);
}

template<>
Material *Entity::renderComponent<Material>() const
{
    return m_renderer->materialManager()->lookupResource(m_materialComponent);
}

template<>
HCamera Entity::componentHandle<CameraLens>() const
{
    return m_renderer->cameraManager()->lookupHandle(m_cameraComponent);
}

template<>
CameraLens *Entity::renderComponent<CameraLens>() const
{
    return m_renderer->cameraManager()->lookupResource(m_cameraComponent);
}

template<>
HTransform Entity::componentHandle<Transform>() const
{
    return m_renderer->transformManager()->lookupHandle(m_transformComponent);
}

template<>
Transform *Entity::renderComponent<Transform>() const
{
    return m_renderer->transformManager()->lookupResource(m_transformComponent);
}

template<>
HGeometryRenderer Entity::componentHandle<GeometryRenderer>() const
{
    return m_renderer->geometryRendererManager()->lookupHandle(m_geometryRendererComponent);
}

template<>
GeometryRenderer *Entity::renderComponent<GeometryRenderer>() const
{
    return m_renderer->geometryRendererManager()->lookupResource(m_geometryRendererComponent);
}

template<>
Qt3D::QNodeId Entity::componentUuid<Transform>() const { return m_transformComponent; }

template<>
Qt3D::QNodeId Entity::componentUuid<CameraLens>() const { return m_cameraComponent; }

template<>
Qt3D::QNodeId Entity::componentUuid<Material>() const { return m_materialComponent; }

template<>
QList<HLayer> Entity::componentsHandle<Layer>() const
{
    QList<HLayer> layerHandles;
    Q_FOREACH (const QNodeId &id, m_layerComponents)
        layerHandles.append(m_renderer->layerManager()->lookupHandle(id));
    return layerHandles;
}

template<>
QList<Layer *> Entity::renderComponents<Layer>() const
{
    QList<Layer *> layers;
    Q_FOREACH (const QNodeId &id, m_layerComponents)
        layers.append(m_renderer->layerManager()->lookupResource(id));
    return layers;
}

template<>
QList<Qt3D::QNodeId> Entity::componentsUuid<Layer>() const { return m_layerComponents; }

template<>
QList<HShaderData> Entity::componentsHandle<ShaderData>() const
{
    QList<HShaderData> shaderDataHandles;
    Q_FOREACH (const QNodeId &id, m_shaderDataComponents)
        shaderDataHandles.append(m_renderer->shaderDataManager()->lookupHandle(id));
    return shaderDataHandles;
}

template<>
QList<ShaderData *> Entity::renderComponents<ShaderData>() const
{
    QList<ShaderData *> shaderDatas;
    Q_FOREACH (const QNodeId &id, m_shaderDataComponents)
        shaderDatas.append(m_renderer->shaderDataManager()->lookupResource(id));
    return shaderDatas;
}

template<>
QList<Qt3D::QNodeId> Entity::componentsUuid<ShaderData>() const { return m_shaderDataComponents; }

template<>
Qt3D::QNodeId Entity::componentUuid<GeometryRenderer>() const { return m_geometryRendererComponent; }


RenderEntityFunctor::RenderEntityFunctor(Renderer *renderer)
    : m_renderer(renderer)
{
}

Qt3D::QBackendNode *RenderEntityFunctor::create(Qt3D::QNode *frontend, const Qt3D::QBackendNodeFactory *factory) const
{
    HEntity renderNodeHandle = m_renderer->renderNodesManager()->getOrAcquireHandle(frontend->id());
    Entity *entity = m_renderer->renderNodesManager()->data(renderNodeHandle);
    entity->setFactory(factory);
    entity->setRenderer(m_renderer);
    entity->setHandle(renderNodeHandle);
    entity->setPeer(frontend);
    return entity;
}

Qt3D::QBackendNode *RenderEntityFunctor::get(const Qt3D::QNodeId &id) const
{
    return m_renderer->renderNodesManager()->lookupResource(id);
}

void RenderEntityFunctor::destroy(const Qt3D::QNodeId &id) const
{
    m_renderer->renderNodesManager()->releaseResource(id);
}

} // namespace Render
} // namespace Qt3DRender

QT_END_NAMESPACE