| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "pointsvisitor_p.h" |
| 41 | #include <Qt3DCore/qentity.h> |
| 42 | #include <Qt3DRender/qgeometryrenderer.h> |
| 43 | #include <Qt3DRender/private/managers_p.h> |
| 44 | #include <Qt3DRender/private/nodemanagers_p.h> |
| 45 | #include <Qt3DRender/private/buffermanager_p.h> |
| 46 | #include <Qt3DRender/private/geometryrenderer_p.h> |
| 47 | #include <Qt3DRender/private/geometryrenderermanager_p.h> |
| 48 | #include <Qt3DRender/private/geometry_p.h> |
| 49 | #include <Qt3DRender/private/attribute_p.h> |
| 50 | #include <Qt3DRender/private/buffer_p.h> |
| 51 | #include <Qt3DRender/private/trianglesvisitor_p.h> |
| 52 | #include <Qt3DRender/private/visitorutils_p.h> |
| 53 | |
| 54 | QT_BEGIN_NAMESPACE |
| 55 | |
| 56 | namespace Qt3DRender { |
| 57 | |
| 58 | namespace Render { |
| 59 | |
| 60 | namespace { |
| 61 | |
| 62 | // indices, vertices are already offset |
| 63 | template<typename Index, typename Vertex> |
| 64 | void traverseCoordinatesIndexed(Index *indices, |
| 65 | Vertex *vertices, |
| 66 | const BufferInfo &indexInfo, |
| 67 | const BufferInfo &vertexInfo, |
| 68 | PointsVisitor *visitor) |
| 69 | { |
| 70 | uint i = 0; |
| 71 | const uint verticesStride = vertexInfo.byteStride / sizeof(Vertex); |
| 72 | const uint maxVerticesDataSize = qMin(a: vertexInfo.dataSize, b: 3U); |
| 73 | |
| 74 | uint ndx; |
| 75 | Vector3D abc; |
| 76 | while (i < indexInfo.count) { |
| 77 | ndx = indices[i]; |
| 78 | const uint idx = ndx * verticesStride; |
| 79 | for (uint j = 0; j < maxVerticesDataSize; ++j) { |
| 80 | abc[j] = vertices[idx + j]; |
| 81 | } |
| 82 | visitor->visit(ndx, c: abc); |
| 83 | ++i; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // vertices are already offset |
| 88 | template<typename Vertex> |
| 89 | void traverseCoordinates(Vertex *vertices, |
| 90 | const BufferInfo &vertexInfo, |
| 91 | PointsVisitor *visitor) |
| 92 | { |
| 93 | const uint verticesStride = vertexInfo.byteStride / sizeof(Vertex); |
| 94 | const uint maxVerticesDataSize = qMin(a: vertexInfo.dataSize, b: 3U); |
| 95 | |
| 96 | uint ndx = 0; |
| 97 | Vector3D abc; |
| 98 | while (ndx < vertexInfo.count) { |
| 99 | const uint idx = ndx * verticesStride; |
| 100 | for (uint j = 0; j < maxVerticesDataSize; ++j) |
| 101 | abc[j] = vertices[idx + j]; |
| 102 | visitor->visit(ndx, c: abc); |
| 103 | ++ndx; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | template<typename Index, typename Visitor> |
| 108 | struct IndexedVertexExecutor |
| 109 | { |
| 110 | template<typename Vertex> |
| 111 | void operator ()(const BufferInfo &vertexInfo, Vertex * vertices) |
| 112 | { |
| 113 | traverseCoordinatesIndexed(m_indices, vertices, m_indexBufferInfo, vertexInfo, m_visitor); |
| 114 | } |
| 115 | |
| 116 | BufferInfo m_indexBufferInfo; |
| 117 | Index *m_indices; |
| 118 | Qt3DRender::QGeometryRenderer::PrimitiveType m_primitiveType; |
| 119 | Visitor* m_visitor; |
| 120 | }; |
| 121 | |
| 122 | template<typename Visitor> |
| 123 | struct IndexExecutor |
| 124 | { |
| 125 | template<typename Index> |
| 126 | void operator ()( const BufferInfo &indexInfo, Index *indices) |
| 127 | { |
| 128 | IndexedVertexExecutor<Index, Visitor> exec; |
| 129 | exec.m_primitiveType = m_primitiveType; |
| 130 | exec.m_indices = indices; |
| 131 | exec.m_indexBufferInfo = indexInfo; |
| 132 | exec.m_visitor = m_visitor; |
| 133 | Qt3DRender::Render::Visitor::processBuffer(m_vertexBufferInfo, exec); |
| 134 | } |
| 135 | |
| 136 | BufferInfo m_vertexBufferInfo; |
| 137 | Qt3DRender::QGeometryRenderer::PrimitiveType m_primitiveType; |
| 138 | Visitor* m_visitor; |
| 139 | }; |
| 140 | |
| 141 | template<typename Visitor> |
| 142 | struct VertexExecutor |
| 143 | { |
| 144 | template<typename Vertex> |
| 145 | void operator ()(const BufferInfo &vertexInfo, Vertex *vertices) |
| 146 | { |
| 147 | switch (m_primitiveType) { |
| 148 | case Qt3DRender::QGeometryRenderer::Points: |
| 149 | traverseCoordinates(vertices, vertexInfo, m_visitor); |
| 150 | return; |
| 151 | default: |
| 152 | Q_UNREACHABLE(); |
| 153 | return; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | Qt3DRender::QGeometryRenderer::PrimitiveType m_primitiveType; |
| 158 | Visitor* m_visitor; |
| 159 | }; |
| 160 | |
| 161 | } // anonymous |
| 162 | |
| 163 | |
| 164 | PointsVisitor::~PointsVisitor() |
| 165 | { |
| 166 | |
| 167 | } |
| 168 | |
| 169 | void PointsVisitor::apply(const Qt3DCore::QEntity *entity) |
| 170 | { |
| 171 | GeometryRenderer *renderer = m_manager->geometryRendererManager()->lookupResource(id: entity->id()); |
| 172 | apply(renderer, id: entity->id()); |
| 173 | } |
| 174 | |
| 175 | void PointsVisitor::apply(const GeometryRenderer *renderer, const Qt3DCore::QNodeId id) |
| 176 | { |
| 177 | m_nodeId = id; |
| 178 | if (renderer && renderer->instanceCount() == 1) { |
| 179 | Visitor::visitPrimitives<VertexExecutor<PointsVisitor>, |
| 180 | IndexExecutor<PointsVisitor>, PointsVisitor>(manager: m_manager, renderer, visitor: this); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | } // namespace Render |
| 185 | |
| 186 | } // namespace Qt3DRender |
| 187 | |
| 188 | QT_END_NAMESPACE |
| 189 | |