1/****************************************************************************
2**
3** Copyright (C) 2014 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 "qtext2dmaterial_p.h"
41#include "qtext2dmaterial_p_p.h"
42#include <Qt3DRender/qfilterkey.h>
43#include <Qt3DRender/qmaterial.h>
44#include <Qt3DRender/qeffect.h>
45#include <Qt3DRender/qtechnique.h>
46#include <Qt3DRender/qshaderprogram.h>
47#include <Qt3DRender/qparameter.h>
48#include <Qt3DRender/qrenderpass.h>
49#include <Qt3DRender/qgraphicsapifilter.h>
50#include <Qt3DRender/qblendequation.h>
51#include <Qt3DRender/qblendequationarguments.h>
52#include <Qt3DRender/qdepthtest.h>
53#include <Qt3DRender/qabstracttexture.h>
54#include <QUrl>
55#include <QVector3D>
56#include <QVector4D>
57
58QT_BEGIN_NAMESPACE
59
60namespace Qt3DExtras {
61
62QText2DMaterialPrivate::QText2DMaterialPrivate()
63 : QMaterialPrivate()
64 , m_effect(new Qt3DRender::QEffect())
65 , m_distanceFieldTexture(nullptr)
66 , m_textureParameter(new Qt3DRender::QParameter(QStringLiteral("distanceFieldTexture"), QVariant(0)))
67 , m_textureSizeParameter(new Qt3DRender::QParameter(QStringLiteral("textureSize"), QVariant(256.f)))
68 , m_colorParameter(new Qt3DRender::QParameter(QStringLiteral("color"), QVariant(QColor(255, 255, 255, 255))))
69 , m_gl3Technique(new Qt3DRender::QTechnique())
70 , m_gl2Technique(new Qt3DRender::QTechnique())
71 , m_es2Technique(new Qt3DRender::QTechnique())
72 , m_gl3RenderPass(new Qt3DRender::QRenderPass())
73 , m_gl2RenderPass(new Qt3DRender::QRenderPass())
74 , m_es2RenderPass(new Qt3DRender::QRenderPass())
75 , m_gl3ShaderProgram(new Qt3DRender::QShaderProgram())
76 , m_gl2es2ShaderProgram(new Qt3DRender::QShaderProgram())
77 , m_blend(new Qt3DRender::QBlendEquation())
78 , m_blendArgs(new Qt3DRender::QBlendEquationArguments())
79 , m_depthTest(new Qt3DRender::QDepthTest())
80{
81}
82
83void QText2DMaterialPrivate::init()
84{
85 m_gl3ShaderProgram->setVertexShaderCode(Qt3DRender::QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/gl3/distancefieldtext.vert"))));
86 m_gl3ShaderProgram->setFragmentShaderCode(Qt3DRender::QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/gl3/distancefieldtext.frag"))));
87
88 m_gl2es2ShaderProgram->setVertexShaderCode(Qt3DRender::QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/es2/distancefieldtext.vert"))));
89 m_gl2es2ShaderProgram->setFragmentShaderCode(Qt3DRender::QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/es2/distancefieldtext.frag"))));
90
91 m_blend->setBlendFunction(Qt3DRender::QBlendEquation::Add);
92 m_blendArgs->setSourceRgba(Qt3DRender::QBlendEquationArguments::SourceAlpha);
93 m_blendArgs->setDestinationRgba(Qt3DRender::QBlendEquationArguments::OneMinusSourceAlpha);
94 m_depthTest->setDepthFunction(Qt3DRender::QDepthTest::LessOrEqual);
95
96 m_gl3RenderPass->setShaderProgram(m_gl3ShaderProgram);
97 m_gl3RenderPass->addRenderState(state: m_blend);
98 m_gl3RenderPass->addRenderState(state: m_blendArgs);
99 m_gl3RenderPass->addRenderState(state: m_depthTest);
100
101 m_gl2RenderPass->setShaderProgram(m_gl2es2ShaderProgram);
102 m_gl2RenderPass->addRenderState(state: m_blend);
103 m_gl2RenderPass->addRenderState(state: m_blendArgs);
104 m_gl2RenderPass->addRenderState(state: m_depthTest);
105
106 m_es2RenderPass->setShaderProgram(m_gl2es2ShaderProgram);
107 m_es2RenderPass->addRenderState(state: m_blend);
108 m_es2RenderPass->addRenderState(state: m_blendArgs);
109 m_es2RenderPass->addRenderState(state: m_depthTest);
110
111 m_gl3Technique->graphicsApiFilter()->setApi(Qt3DRender::QGraphicsApiFilter::OpenGL);
112 m_gl3Technique->graphicsApiFilter()->setMajorVersion(3);
113 m_gl3Technique->graphicsApiFilter()->setMinorVersion(1);
114 m_gl3Technique->graphicsApiFilter()->setProfile(Qt3DRender::QGraphicsApiFilter::CoreProfile);
115 m_gl3Technique->addRenderPass(pass: m_gl3RenderPass);
116
117 m_gl2Technique->graphicsApiFilter()->setApi(Qt3DRender::QGraphicsApiFilter::OpenGL);
118 m_gl2Technique->graphicsApiFilter()->setMajorVersion(2);
119 m_gl2Technique->graphicsApiFilter()->setMinorVersion(0);
120 m_gl2Technique->graphicsApiFilter()->setProfile(Qt3DRender::QGraphicsApiFilter::NoProfile);
121 m_gl2Technique->addRenderPass(pass: m_gl2RenderPass);
122
123 m_es2Technique->graphicsApiFilter()->setApi(Qt3DRender::QGraphicsApiFilter::OpenGLES);
124 m_es2Technique->graphicsApiFilter()->setMajorVersion(2);
125 m_es2Technique->graphicsApiFilter()->setMinorVersion(0);
126 m_es2Technique->graphicsApiFilter()->setProfile(Qt3DRender::QGraphicsApiFilter::NoProfile);
127 m_es2Technique->addRenderPass(pass: m_es2RenderPass);
128
129 Qt3DRender::QFilterKey *filterKey = new Qt3DRender::QFilterKey(q_func());
130 filterKey->setName(QStringLiteral("renderingStyle"));
131 filterKey->setValue(QStringLiteral("forward"));
132 m_gl3Technique->addFilterKey(filterKey);
133 m_gl2Technique->addFilterKey(filterKey);
134 m_es2Technique->addFilterKey(filterKey);
135
136 m_effect->addTechnique(t: m_gl3Technique);
137 m_effect->addTechnique(t: m_gl2Technique);
138 m_effect->addTechnique(t: m_es2Technique);
139 m_effect->addParameter(parameter: m_textureParameter);
140 m_effect->addParameter(parameter: m_textureSizeParameter);
141 m_effect->addParameter(parameter: m_colorParameter);
142
143 q_func()->setEffect(m_effect);
144}
145
146QText2DMaterial::QText2DMaterial(Qt3DCore::QNode *parent)
147 : QMaterial(*new QText2DMaterialPrivate(), parent)
148{
149 Q_D(QText2DMaterial);
150 d->init();
151}
152
153QText2DMaterial::~QText2DMaterial()
154{
155}
156
157void QText2DMaterial::setColor(const QColor &color)
158{
159 Q_D(QText2DMaterial);
160 d->m_colorParameter->setValue(QVariant::fromValue(value: color));
161}
162
163void QText2DMaterial::setDistanceFieldTexture(Qt3DRender::QAbstractTexture *tex)
164{
165 Q_D(QText2DMaterial);
166 d->m_distanceFieldTexture = tex;
167
168 if (tex) {
169 d->m_textureParameter->setValue(QVariant::fromValue(value: tex));
170 d->m_textureSizeParameter->setValue(QVariant::fromValue(value: static_cast<float>(tex->width())));
171 } else {
172 d->m_textureParameter->setValue(0);
173 d->m_textureSizeParameter->setValue(QVariant::fromValue(value: 1.f));
174 }
175}
176
177} // namespace Qt3DExtras
178
179QT_END_NAMESPACE
180

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