1 /*
2 qt3dgeometryextension.cpp
3
4 This file is part of GammaRay, the Qt application inspection and
5 manipulation tool.
6
7 Copyright (C) 2016-2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8 Author: Volker Krause <volker.krause@kdab.com>
9
10 Licensees holding valid commercial KDAB GammaRay licenses may use this file in
11 accordance with GammaRay Commercial License Agreement provided with the Software.
12
13 Contact info@kdab.com if any conditions of this licensing are not clear to you.
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation, either version 2 of the License, or
18 (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 */
28
29 #include "qt3dgeometryextension.h"
30
31 #include <core/propertycontroller.h>
32 #include <core/util.h>
33
34 #include <Qt3DRender/QAttribute>
35 #include <Qt3DRender/QBuffer>
36 #include <Qt3DRender/QBufferDataGenerator>
37 #include <Qt3DRender/QGeometry>
38 #include <Qt3DRender/QGeometryRenderer>
39
40 #include <Qt3DCore/QEntity>
41
42 #include <QDebug>
43
44 using namespace GammaRay;
45
Qt3DGeometryExtension(GammaRay::PropertyController * controller)46 Qt3DGeometryExtension::Qt3DGeometryExtension(GammaRay::PropertyController *controller)
47 : Qt3DGeometryExtensionInterface(controller->objectBaseName() + ".qt3dGeometry", controller)
48 , PropertyControllerExtension(controller->objectBaseName() + ".qt3dGeometry")
49 , m_geometry(nullptr)
50 {
51 }
52
~Qt3DGeometryExtension()53 Qt3DGeometryExtension::~Qt3DGeometryExtension()
54 {
55 }
56
setQObject(QObject * object)57 bool Qt3DGeometryExtension::setQObject(QObject *object)
58 {
59 Qt3DRender::QGeometryRenderer *geometry = nullptr;
60
61 // try to find a half-way related QGeometryRenderer instance
62 if ((geometry = qobject_cast<Qt3DRender::QGeometryRenderer *>(object))) {
63 } else if (auto entity = qobject_cast<Qt3DCore::QEntity *>(object)) {
64 foreach (auto component, entity->components()) {
65 if ((geometry = qobject_cast<Qt3DRender::QGeometryRenderer *>(component)))
66 break;
67 }
68 } else if (auto geo = qobject_cast<Qt3DRender::QGeometry *>(object)) {
69 return setQObject(geo->parent());
70 } else if (auto attr = qobject_cast<Qt3DRender::QAttribute *>(object)) {
71 return setQObject(attr->parent());
72 } else if (auto buffer = qobject_cast<Qt3DRender::QBuffer *>(object)) {
73 return setQObject(buffer->parent());
74 }
75
76 if (geometry == m_geometry)
77 return geometry;
78 m_geometry = geometry;
79
80 if (!geometry)
81 return false;
82
83 updateGeometryData();
84 return true;
85 }
86
updateGeometryData()87 void Qt3DGeometryExtension::updateGeometryData()
88 {
89 Qt3DGeometryData data;
90 if (!m_geometry || !m_geometry->geometry()) {
91 setGeometryData(data);
92 return;
93 }
94
95 QHash<Qt3DRender::QBuffer *, uint> bufferMap;
96 data.attributes.reserve(m_geometry->geometry()->attributes().size());
97 foreach (auto attr, m_geometry->geometry()->attributes()) {
98 if (attr->count() == 0) // ignore empty/invalid attributes
99 continue;
100 Qt3DGeometryAttributeData attrData;
101 attrData.name = attr->name();
102 attrData.attributeType = attr->attributeType();
103 attrData.byteOffset = attr->byteOffset();
104 attrData.byteStride = attr->byteStride();
105 attrData.count = attr->count();
106 attrData.divisor = attr->divisor();
107 attrData.vertexBaseType = attr->vertexBaseType();
108 attrData.vertexSize = attr->vertexSize();
109
110 const auto bufferIt = bufferMap.constFind(attr->buffer());
111 if (bufferIt != bufferMap.constEnd()) {
112 attrData.bufferIndex = bufferIt.value();
113 } else {
114 Qt3DGeometryBufferData buffer;
115 buffer.name = Util::displayString(attr->buffer());
116 buffer.type = attr->buffer()->type();
117 auto generator = attr->buffer()->dataGenerator();
118 if (generator)
119 buffer.data = (*generator.data())();
120 else
121 buffer.data = attr->buffer()->data();
122
123 attrData.bufferIndex = data.buffers.size();
124 bufferMap.insert(attr->buffer(), attrData.bufferIndex);
125 data.buffers.push_back(buffer);
126 }
127 data.attributes.push_back(attrData);
128 }
129
130 setGeometryData(data);
131 }
132