1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 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 <Qt3DRender/qmaterial.h>
41 #include <Qt3DRender/qbuffer.h>
42 #include <Qt3DRender/qattribute.h>
43 #include <Qt3DRender/qgeometry.h>
44 #include <Qt3DRender/qgeometryrenderer.h>
45
46 #include <Qt3DExtras/private/qtext2dmaterial_p.h>
47
48 #include "distancefieldtextrenderer_p.h"
49 #include "distancefieldtextrenderer_p_p.h"
50
51 QT_BEGIN_NAMESPACE
52
53 namespace Qt3DExtras {
54
55 using namespace Qt3DCore;
56
DistanceFieldTextRendererPrivate()57 DistanceFieldTextRendererPrivate::DistanceFieldTextRendererPrivate()
58 : m_renderer(nullptr)
59 , m_geometry(nullptr)
60 , m_positionAttr(nullptr)
61 , m_texCoordAttr(nullptr)
62 , m_indexAttr(nullptr)
63 , m_vertexBuffer(nullptr)
64 , m_indexBuffer(nullptr)
65 , m_material(nullptr)
66 {
67 }
68
~DistanceFieldTextRendererPrivate()69 DistanceFieldTextRendererPrivate::~DistanceFieldTextRendererPrivate()
70 {
71 }
72
init()73 void DistanceFieldTextRendererPrivate::init()
74 {
75 Q_Q(DistanceFieldTextRenderer);
76
77 m_renderer = new Qt3DRender::QGeometryRenderer(q);
78 m_renderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
79
80 m_geometry = new Qt3DRender::QGeometry(m_renderer);
81 m_renderer->setGeometry(m_geometry);
82
83 m_vertexBuffer = new Qt3DRender::QBuffer(m_geometry);
84 m_indexBuffer = new Qt3DRender::QBuffer(m_geometry);
85
86 m_positionAttr = new Qt3DRender::QAttribute(m_geometry);
87 m_positionAttr->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
88 m_positionAttr->setVertexBaseType(Qt3DRender::QAttribute::Float);
89 m_positionAttr->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
90 m_positionAttr->setVertexSize(3);
91 m_positionAttr->setByteStride(5 * sizeof(float));
92 m_positionAttr->setByteOffset(0);
93 m_positionAttr->setBuffer(m_vertexBuffer);
94
95 m_texCoordAttr = new Qt3DRender::QAttribute(m_geometry);
96 m_texCoordAttr->setName(Qt3DRender::QAttribute::defaultTextureCoordinateAttributeName());
97 m_texCoordAttr->setVertexBaseType(Qt3DRender::QAttribute::Float);
98 m_texCoordAttr->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
99 m_texCoordAttr->setVertexSize(2);
100 m_texCoordAttr->setByteStride(5 * sizeof(float));
101 m_texCoordAttr->setByteOffset(3 * sizeof(float));
102 m_texCoordAttr->setBuffer(m_vertexBuffer);
103
104 m_indexAttr = new Qt3DRender::QAttribute(m_geometry);
105 m_indexAttr->setAttributeType(Qt3DRender::QAttribute::IndexAttribute);
106 m_indexAttr->setVertexBaseType(Qt3DRender::QAttribute::UnsignedShort);
107 m_indexAttr->setBuffer(m_indexBuffer);
108
109 m_geometry->addAttribute(m_positionAttr);
110 m_geometry->setBoundingVolumePositionAttribute(m_positionAttr);
111 m_geometry->addAttribute(m_texCoordAttr);
112 m_geometry->addAttribute(m_indexAttr);
113
114 m_material = new QText2DMaterial(q);
115
116 q->addComponent(m_renderer);
117 q->addComponent(m_material);
118 }
119
DistanceFieldTextRenderer(QNode * parent)120 DistanceFieldTextRenderer::DistanceFieldTextRenderer(QNode *parent)
121 : QEntity(*new DistanceFieldTextRendererPrivate(), parent)
122 {
123 Q_D(DistanceFieldTextRenderer);
124 d->init();
125 }
126
~DistanceFieldTextRenderer()127 DistanceFieldTextRenderer::~DistanceFieldTextRenderer()
128 {
129 }
130
setGlyphData(Qt3DRender::QAbstractTexture * glyphTexture,const QVector<float> & vertexData,const QVector<quint16> & indexData)131 void DistanceFieldTextRenderer::setGlyphData(Qt3DRender::QAbstractTexture *glyphTexture,
132 const QVector<float> &vertexData,
133 const QVector<quint16> &indexData)
134 {
135 Q_D(DistanceFieldTextRenderer);
136
137 const int vertexCount = vertexData.size() / 5;
138
139 d->m_vertexBuffer->setData(QByteArray((char*) vertexData.data(), vertexData.size() * sizeof(float)));
140 d->m_indexBuffer->setData(QByteArray((char*) indexData.data(), indexData.size() * sizeof(quint16)));
141 d->m_positionAttr->setCount(vertexCount);
142 d->m_texCoordAttr->setCount(vertexCount);
143 d->m_indexAttr->setCount(indexData.size());
144
145 d->m_material->setDistanceFieldTexture(glyphTexture);
146 }
147
setColor(const QColor & color)148 void DistanceFieldTextRenderer::setColor(const QColor &color)
149 {
150 Q_D(DistanceFieldTextRenderer);
151 d->m_material->setColor(color);
152 }
153
154 } // namespace Qt3DExtras
155
156 QT_END_NAMESPACE
157