| 1 | // Copyright (C) 2017 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 | #ifndef QT3DRENDER_RENDER_BUFFERVISITOR_P_H |
| 5 | #define QT3DRENDER_RENDER_BUFFERVISITOR_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists for the convenience |
| 12 | // of other Qt classes. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <Qt3DCore/qnodeid.h> |
| 19 | #include <Qt3DCore/qattribute.h> |
| 20 | #include <Qt3DCore/private/buffervisitor_p.h> |
| 21 | #include <Qt3DRender/private/trianglesvisitor_p.h> |
| 22 | #include <Qt3DRender/private/attribute_p.h> |
| 23 | #include <Qt3DRender/private/buffer_p.h> |
| 24 | #include <Qt3DRender/private/bufferutils_p.h> |
| 25 | #include <Qt3DRender/private/geometryrenderer_p.h> |
| 26 | #include <Qt3DRender/private/geometry_p.h> |
| 27 | #include <Qt3DRender/private/managers_p.h> |
| 28 | #include <Qt3DRender/private/nodemanagers_p.h> |
| 29 | |
| 30 | QT_BEGIN_NAMESPACE |
| 31 | |
| 32 | namespace Qt3DCore { |
| 33 | class QEntity; |
| 34 | } |
| 35 | |
| 36 | namespace Qt3DRender { |
| 37 | namespace Render { |
| 38 | |
| 39 | |
| 40 | template <typename ValueType, Qt3DCore::QAttribute::VertexBaseType VertexBaseType, uint dataSize> |
| 41 | class Q_AUTOTEST_EXPORT BufferVisitor : public Qt3DCore::BufferVisitor<ValueType, VertexBaseType, dataSize> |
| 42 | { |
| 43 | public: |
| 44 | explicit BufferVisitor(NodeManagers *manager) |
| 45 | : m_manager(manager) |
| 46 | { |
| 47 | } |
| 48 | virtual ~BufferVisitor() = default; |
| 49 | |
| 50 | bool apply(Qt3DRender::Render::Attribute *attribute, |
| 51 | Qt3DRender::Render::Attribute *indexAttribute, |
| 52 | int drawVertexCount, |
| 53 | bool primitiveRestartEnabled, |
| 54 | int primitiveRestartIndex) |
| 55 | { |
| 56 | if (attribute->vertexBaseType() != VertexBaseType) |
| 57 | return false; |
| 58 | if (attribute->vertexSize() < dataSize) |
| 59 | return false; |
| 60 | |
| 61 | auto data = m_manager->lookupResource<Buffer, BufferManager>(id: attribute->bufferId())->data(); |
| 62 | auto vertexBuffer = BufferTypeInfo::castToType<VertexBaseType>(data, attribute->byteOffset()); |
| 63 | |
| 64 | if (indexAttribute) { |
| 65 | auto indexData = m_manager->lookupResource<Buffer, BufferManager>(id: indexAttribute->bufferId())->data(); |
| 66 | switch (indexAttribute->vertexBaseType()) { |
| 67 | case Qt3DCore::QAttribute::UnsignedShort: { |
| 68 | auto indexBuffer = BufferTypeInfo::castToType<Qt3DCore::QAttribute::UnsignedShort>(u: indexData, byteOffset: indexAttribute->byteOffset()); |
| 69 | this->traverseCoordinateIndexed(vertexBuffer, indexBuffer, attribute->byteStride(), drawVertexCount, |
| 70 | primitiveRestartEnabled, primitiveRestartIndex); |
| 71 | break; |
| 72 | } |
| 73 | case Qt3DCore::QAttribute::UnsignedInt: { |
| 74 | auto indexBuffer = BufferTypeInfo::castToType<Qt3DCore::QAttribute::UnsignedInt>(u: indexData, byteOffset: indexAttribute->byteOffset()); |
| 75 | this->traverseCoordinateIndexed(vertexBuffer, indexBuffer, attribute->byteStride(), drawVertexCount, |
| 76 | primitiveRestartEnabled, primitiveRestartIndex); |
| 77 | break; |
| 78 | } |
| 79 | case Qt3DCore::QAttribute::UnsignedByte: { |
| 80 | auto indexBuffer = BufferTypeInfo::castToType<Qt3DCore::QAttribute::UnsignedByte>(u: indexData, byteOffset: indexAttribute->byteOffset()); |
| 81 | this->traverseCoordinateIndexed(vertexBuffer, indexBuffer, attribute->byteStride(), drawVertexCount, |
| 82 | primitiveRestartEnabled, primitiveRestartIndex); |
| 83 | break; |
| 84 | } |
| 85 | default: Q_UNREACHABLE(); |
| 86 | } |
| 87 | } else { |
| 88 | switch (dataSize) { |
| 89 | case 1: this->traverseCoordinates1(vertexBuffer, attribute->byteStride(), drawVertexCount); break; |
| 90 | case 2: this->traverseCoordinates2(vertexBuffer, attribute->byteStride(), drawVertexCount); break; |
| 91 | case 3: this->traverseCoordinates3(vertexBuffer, attribute->byteStride(), drawVertexCount); break; |
| 92 | case 4: this->traverseCoordinates4(vertexBuffer, attribute->byteStride(), drawVertexCount); break; |
| 93 | default: Q_UNREACHABLE(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | protected: |
| 101 | NodeManagers *m_manager; |
| 102 | }; |
| 103 | |
| 104 | typedef BufferVisitor<float, Qt3DCore::QAttribute::Float, 3> Buffer3fVisitor; |
| 105 | |
| 106 | } // namespace Render |
| 107 | |
| 108 | } // namespace Qt3DRender |
| 109 | |
| 110 | QT_END_NAMESPACE |
| 111 | |
| 112 | |
| 113 | #endif // QT3DRENDER_RENDER_BUFFERVISITOR_P_H |
| 114 | |