1// Copyright (C) 2020 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "pickingproxy_p.h"
5#include <Qt3DRender/private/managers_p.h>
6#include <Qt3DRender/private/qpickingproxy_p.h>
7#include <Qt3DCore/private/qgeometryview_p.h>
8#include <Qt3DCore/private/qnode_p.h>
9
10#include <memory>
11
12QT_BEGIN_NAMESPACE
13
14using namespace Qt3DCore;
15
16namespace Qt3DRender {
17namespace Render {
18
19PickingProxy::PickingProxy()
20 : BackendNode(ReadWrite)
21 , m_instanceCount(0)
22 , m_vertexCount(0)
23 , m_indexOffset(0)
24 , m_firstInstance(0)
25 , m_firstVertex(0)
26 , m_indexBufferByteOffset(0)
27 , m_restartIndexValue(-1)
28 , m_verticesPerPatch(0)
29 , m_primitiveRestartEnabled(false)
30 , m_dirty(false)
31 , m_valid(false)
32 , m_primitiveType(Qt3DCore::QGeometryView::Triangles)
33 , m_manager(nullptr)
34{
35}
36
37PickingProxy::~PickingProxy()
38{
39}
40
41void PickingProxy::cleanup()
42{
43 BackendNode::setEnabled(false);
44 m_instanceCount = 0;
45 m_vertexCount = 0;
46 m_indexOffset = 0;
47 m_firstInstance = 0;
48 m_firstVertex = 0;
49 m_indexBufferByteOffset = 0;
50 m_restartIndexValue = -1;
51 m_verticesPerPatch = 0;
52 m_primitiveRestartEnabled = false;
53 m_primitiveType = QGeometryView::Triangles;
54 m_geometryId = Qt3DCore::QNodeId();
55 m_dirty = false;
56 m_valid = false;
57}
58
59void PickingProxy::setManager(PickingProxyManager *manager)
60{
61 m_manager = manager;
62}
63
64void PickingProxy::syncFromFrontEnd(const QNode *frontEnd, bool firstTime)
65{
66 BackendNode::syncFromFrontEnd(frontEnd, firstTime);
67 const QPickingProxy *node = qobject_cast<const QPickingProxy *>(object: frontEnd);
68 if (!node)
69 return;
70
71 auto propertyUpdater = [this](const auto *node) {
72 m_dirty |= m_instanceCount != node->instanceCount();
73 m_instanceCount = node->instanceCount();
74 m_dirty |= m_vertexCount != node->vertexCount();
75 m_vertexCount = node->vertexCount();
76 m_dirty |= m_indexOffset != node->indexOffset();
77 m_indexOffset = node->indexOffset();
78 m_dirty |= m_firstInstance != node->firstInstance();
79 m_firstInstance = node->firstInstance();
80 m_dirty |= m_firstVertex != node->firstVertex();
81 m_firstVertex = node->firstVertex();
82 m_dirty |= m_indexBufferByteOffset != node->indexBufferByteOffset();
83 m_indexBufferByteOffset = node->indexBufferByteOffset();
84 m_dirty |= m_restartIndexValue != node->restartIndexValue();
85 m_restartIndexValue = node->restartIndexValue();
86 m_dirty |= m_verticesPerPatch != node->verticesPerPatch();
87 m_verticesPerPatch = node->verticesPerPatch();
88 m_dirty |= m_primitiveRestartEnabled != node->primitiveRestartEnabled();
89 m_primitiveRestartEnabled = node->primitiveRestartEnabled();
90 m_dirty |= m_primitiveType != static_cast<QGeometryView::PrimitiveType>(node->primitiveType());
91 m_primitiveType = static_cast<QGeometryView::PrimitiveType>(node->primitiveType());
92 m_dirty |= (node->geometry() && m_geometryId != node->geometry()->id()) || (!node->geometry() && !m_geometryId.isNull());
93 m_geometryId = node->geometry() ? node->geometry()->id() : Qt3DCore::QNodeId();
94 };
95
96 m_dirty = false;
97
98 if (!node->view()) {
99 m_dirty |= m_valid;
100 m_valid = false;
101 } else {
102 m_dirty |= !m_valid;
103 m_valid = true;
104 propertyUpdater(node->view());
105 }
106
107 markDirty(changes: AbstractRenderer::GeometryDirty);
108}
109
110void PickingProxy::unsetDirty()
111{
112 m_dirty = false;
113}
114
115PickingProxyFunctor::PickingProxyFunctor(AbstractRenderer *renderer, PickingProxyManager *manager)
116 : m_manager(manager)
117 , m_renderer(renderer)
118{
119}
120
121Qt3DCore::QBackendNode *PickingProxyFunctor::create(Qt3DCore::QNodeId id) const
122{
123 PickingProxy *node = m_manager->getOrCreateResource(id);
124 node->setManager(m_manager);
125 node->setRenderer(m_renderer);
126 return node;
127}
128
129Qt3DCore::QBackendNode *PickingProxyFunctor::get(Qt3DCore::QNodeId id) const
130{
131 return m_manager->lookupResource(id);
132}
133
134void PickingProxyFunctor::destroy(Qt3DCore::QNodeId id) const
135{
136 m_manager->releaseResource(id);
137}
138
139} // namespace Render
140} // namespace Qt3DRender
141
142QT_END_NAMESPACE
143

source code of qt3d/src/render/geometry/pickingproxy.cpp