| 1 | // Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include "tessellatedquadmesh.h" |
| 5 | |
| 6 | #include <Qt3DCore/qattribute.h> |
| 7 | #include <Qt3DCore/qbuffer.h> |
| 8 | #include <Qt3DCore/qgeometry.h> |
| 9 | |
| 10 | class TessellatedGeometry : public Qt3DCore::QGeometry |
| 11 | { |
| 12 | Q_OBJECT |
| 13 | public: |
| 14 | TessellatedGeometry(Qt3DCore::QNode *parent = nullptr) |
| 15 | : Qt3DCore::QGeometry(parent) |
| 16 | , m_positionAttribute(new Qt3DCore::QAttribute(this)) |
| 17 | , m_vertexBuffer(new Qt3DCore::QBuffer(this)) |
| 18 | { |
| 19 | const float positionData[] = { |
| 20 | -0.8f, -0.8f, 0.0f, |
| 21 | 0.8f, -0.8f, 0.0f, |
| 22 | 0.8f, 0.8f, 0.0f, |
| 23 | -0.8f, 0.8f, 0.0f |
| 24 | }; |
| 25 | |
| 26 | const int nVerts = 4; |
| 27 | const int size = nVerts * 3 * sizeof(float); |
| 28 | QByteArray positionBytes; |
| 29 | positionBytes.resize(size); |
| 30 | memcpy(positionBytes.data(), positionData, size); |
| 31 | |
| 32 | m_vertexBuffer->setData(positionBytes); |
| 33 | |
| 34 | m_positionAttribute->setName(Qt3DCore::QAttribute::defaultPositionAttributeName()); |
| 35 | m_positionAttribute->setVertexBaseType(Qt3DCore::QAttribute::Float); |
| 36 | m_positionAttribute->setVertexSize(3); |
| 37 | m_positionAttribute->setCount(nVerts); |
| 38 | m_positionAttribute->setByteStride(3 * sizeof(float)); |
| 39 | m_positionAttribute->setBuffer(m_vertexBuffer); |
| 40 | |
| 41 | addAttribute(m_positionAttribute); |
| 42 | } |
| 43 | |
| 44 | private: |
| 45 | Qt3DCore::QAttribute *m_positionAttribute; |
| 46 | Qt3DCore::QBuffer *m_vertexBuffer; |
| 47 | }; |
| 48 | |
| 49 | TessellatedQuadMesh::TessellatedQuadMesh(Qt3DCore::QNode *parent) |
| 50 | : Qt3DRender::QGeometryRenderer(parent) |
| 51 | { |
| 52 | setPrimitiveType(Qt3DRender::QGeometryRenderer::Patches); |
| 53 | setVerticesPerPatch(4); |
| 54 | setGeometry(new TessellatedGeometry(this)); |
| 55 | } |
| 56 | |
| 57 | #include "tessellatedquadmesh.moc" |