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

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