1// Copyright (C) 2016 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 <Qt3DCore/qbuffer.h>
5#include <Qt3DCore/qattribute.h>
6#include <Qt3DCore/qgeometry.h>
7#include <Qt3DRender/qmaterial.h>
8#include <Qt3DRender/qgeometryrenderer.h>
9
10#include <Qt3DExtras/private/qtext2dmaterial_p.h>
11
12#include "distancefieldtextrenderer_p.h"
13#include "distancefieldtextrenderer_p_p.h"
14
15QT_BEGIN_NAMESPACE
16
17namespace Qt3DExtras {
18
19using namespace Qt3DCore;
20
21DistanceFieldTextRendererPrivate::DistanceFieldTextRendererPrivate()
22 : m_renderer(nullptr)
23 , m_geometry(nullptr)
24 , m_positionAttr(nullptr)
25 , m_texCoordAttr(nullptr)
26 , m_indexAttr(nullptr)
27 , m_vertexBuffer(nullptr)
28 , m_indexBuffer(nullptr)
29 , m_material(nullptr)
30{
31}
32
33DistanceFieldTextRendererPrivate::~DistanceFieldTextRendererPrivate()
34{
35}
36
37void DistanceFieldTextRendererPrivate::init()
38{
39 Q_Q(DistanceFieldTextRenderer);
40
41 m_renderer = new Qt3DRender::QGeometryRenderer(q);
42 m_renderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
43
44 m_geometry = new Qt3DCore::QGeometry(m_renderer);
45 m_renderer->setGeometry(m_geometry);
46
47 m_vertexBuffer = new Qt3DCore::QBuffer(m_geometry);
48 m_indexBuffer = new Qt3DCore::QBuffer(m_geometry);
49
50 m_positionAttr = new Qt3DCore::QAttribute(m_geometry);
51 m_positionAttr->setName(Qt3DCore::QAttribute::defaultPositionAttributeName());
52 m_positionAttr->setVertexBaseType(Qt3DCore::QAttribute::Float);
53 m_positionAttr->setAttributeType(Qt3DCore::QAttribute::VertexAttribute);
54 m_positionAttr->setVertexSize(3);
55 m_positionAttr->setByteStride(5 * sizeof(float));
56 m_positionAttr->setByteOffset(0);
57 m_positionAttr->setBuffer(m_vertexBuffer);
58
59 m_texCoordAttr = new Qt3DCore::QAttribute(m_geometry);
60 m_texCoordAttr->setName(Qt3DCore::QAttribute::defaultTextureCoordinateAttributeName());
61 m_texCoordAttr->setVertexBaseType(Qt3DCore::QAttribute::Float);
62 m_texCoordAttr->setAttributeType(Qt3DCore::QAttribute::VertexAttribute);
63 m_texCoordAttr->setVertexSize(2);
64 m_texCoordAttr->setByteStride(5 * sizeof(float));
65 m_texCoordAttr->setByteOffset(3 * sizeof(float));
66 m_texCoordAttr->setBuffer(m_vertexBuffer);
67
68 m_indexAttr = new Qt3DCore::QAttribute(m_geometry);
69 m_indexAttr->setAttributeType(Qt3DCore::QAttribute::IndexAttribute);
70 m_indexAttr->setVertexBaseType(Qt3DCore::QAttribute::UnsignedShort);
71 m_indexAttr->setBuffer(m_indexBuffer);
72
73 m_geometry->addAttribute(attribute: m_positionAttr);
74 m_geometry->setBoundingVolumePositionAttribute(m_positionAttr);
75 m_geometry->addAttribute(attribute: m_texCoordAttr);
76 m_geometry->addAttribute(attribute: m_indexAttr);
77
78 m_material = new QText2DMaterial(q);
79
80 q->addComponent(comp: m_renderer);
81 q->addComponent(comp: m_material);
82}
83
84DistanceFieldTextRenderer::DistanceFieldTextRenderer(QNode *parent)
85 : QEntity(*new DistanceFieldTextRendererPrivate(), parent)
86{
87 Q_D(DistanceFieldTextRenderer);
88 d->init();
89}
90
91DistanceFieldTextRenderer::~DistanceFieldTextRenderer()
92{
93}
94
95void DistanceFieldTextRenderer::setGlyphData(Qt3DRender::QAbstractTexture *glyphTexture,
96 const std::vector<float> &vertexData,
97 const std::vector<quint16> &indexData)
98{
99 Q_D(DistanceFieldTextRenderer);
100
101 const size_t vertexCount = vertexData.size() / 5;
102
103 d->m_vertexBuffer->setData(QByteArray((char*) vertexData.data(), vertexData.size() * sizeof(float)));
104 d->m_indexBuffer->setData(QByteArray((char*) indexData.data(), indexData.size() * sizeof(quint16)));
105 d->m_positionAttr->setCount(uint(vertexCount));
106 d->m_texCoordAttr->setCount(uint(vertexCount));
107 d->m_indexAttr->setCount(uint(indexData.size()));
108
109 d->m_material->setDistanceFieldTexture(glyphTexture);
110}
111
112void DistanceFieldTextRenderer::setColor(const QColor &color)
113{
114 Q_D(DistanceFieldTextRenderer);
115 d->m_material->setColor(color);
116}
117
118} // namespace Qt3DExtras
119
120QT_END_NAMESPACE
121
122#include "moc_distancefieldtextrenderer_p.cpp"
123

source code of qt3d/src/extras/text/distancefieldtextrenderer.cpp