| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2017 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 "qtexturematerial.h" | 
| 41 | #include "qtexturematerial_p.h" | 
| 42 | #include <Qt3DRender/qfilterkey.h> | 
| 43 | #include <Qt3DRender/qmaterial.h> | 
| 44 | #include <Qt3DRender/qeffect.h> | 
| 45 | #include <Qt3DRender/qtexture.h> | 
| 46 | #include <Qt3DRender/qtechnique.h> | 
| 47 | #include <Qt3DRender/qshaderprogram.h> | 
| 48 | #include <Qt3DRender/qparameter.h> | 
| 49 | #include <Qt3DRender/qrenderpass.h> | 
| 50 | #include <Qt3DRender/qgraphicsapifilter.h> | 
| 51 | #include <Qt3DRender/qblendequation.h> | 
| 52 | #include <Qt3DRender/qblendequationarguments.h> | 
| 53 | #include <Qt3DRender/qnodepthmask.h> | 
| 54 | #include <QUrl> | 
| 55 |  | 
| 56 | QT_BEGIN_NAMESPACE | 
| 57 |  | 
| 58 | using namespace Qt3DRender; | 
| 59 |  | 
| 60 | namespace Qt3DExtras { | 
| 61 |  | 
| 62 | QTextureMaterialPrivate::() | 
| 63 |     : QMaterialPrivate() | 
| 64 |     , m_textureEffect(new QEffect) | 
| 65 |     , m_textureParameter(new QParameter(QStringLiteral("diffuseTexture" ), new QTexture2D)) | 
| 66 |     , m_textureTransformParameter(new QParameter(QStringLiteral("texCoordTransform" ), QVariant::fromValue(value: QMatrix3x3()))) | 
| 67 |     , m_textureGL3Technique(new QTechnique) | 
| 68 |     , m_textureGL2Technique(new QTechnique) | 
| 69 |     , m_textureES2Technique(new QTechnique) | 
| 70 |     , m_textureGL3RenderPass(new QRenderPass) | 
| 71 |     , m_textureGL2RenderPass(new QRenderPass) | 
| 72 |     , m_textureES2RenderPass(new QRenderPass) | 
| 73 |     , m_textureGL3Shader(new QShaderProgram) | 
| 74 |     , m_textureGL2ES2Shader(new QShaderProgram) | 
| 75 |     , m_noDepthMask(new QNoDepthMask()) | 
| 76 |     , m_blendState(new QBlendEquationArguments()) | 
| 77 |     , m_blendEquation(new QBlendEquation()) | 
| 78 |     , m_filterKey(new QFilterKey) | 
| 79 | { | 
| 80 | } | 
| 81 |  | 
| 82 | void QTextureMaterialPrivate::() | 
| 83 | { | 
| 84 |     connect(sender: m_textureParameter, signal: &Qt3DRender::QParameter::valueChanged, | 
| 85 |             receiverPrivate: this, slot: &QTextureMaterialPrivate::handleTextureChanged); | 
| 86 |     connect(sender: m_textureTransformParameter, signal: &Qt3DRender::QParameter::valueChanged, | 
| 87 |             receiverPrivate: this, slot: &QTextureMaterialPrivate::handleTextureTransformChanged); | 
| 88 |  | 
| 89 |     m_textureGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/gl3/unlittexture.vert" )))); | 
| 90 |     m_textureGL3Shader->setFragmentShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/gl3/unlittexture.frag" )))); | 
| 91 |     m_textureGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/es2/unlittexture.vert" )))); | 
| 92 |     m_textureGL2ES2Shader->setFragmentShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/es2/unlittexture.frag" )))); | 
| 93 |  | 
| 94 |     m_textureGL3Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL); | 
| 95 |     m_textureGL3Technique->graphicsApiFilter()->setMajorVersion(3); | 
| 96 |     m_textureGL3Technique->graphicsApiFilter()->setMinorVersion(1); | 
| 97 |     m_textureGL3Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::CoreProfile); | 
| 98 |  | 
| 99 |     m_textureGL2Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL); | 
| 100 |     m_textureGL2Technique->graphicsApiFilter()->setMajorVersion(2); | 
| 101 |     m_textureGL2Technique->graphicsApiFilter()->setMinorVersion(0); | 
| 102 |     m_textureGL2Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::NoProfile); | 
| 103 |  | 
| 104 |     m_textureES2Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGLES); | 
| 105 |     m_textureES2Technique->graphicsApiFilter()->setMajorVersion(2); | 
| 106 |     m_textureES2Technique->graphicsApiFilter()->setMinorVersion(0); | 
| 107 |     m_textureES2Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::NoProfile); | 
| 108 |  | 
| 109 |     m_noDepthMask->setEnabled(false); | 
| 110 |     m_blendState->setEnabled(false); | 
| 111 |     m_blendState->setSourceRgb(QBlendEquationArguments::SourceAlpha); | 
| 112 |     m_blendState->setDestinationRgb(QBlendEquationArguments::OneMinusSourceAlpha); | 
| 113 |     m_blendEquation->setEnabled(false); | 
| 114 |     m_blendEquation->setBlendFunction(QBlendEquation::Add); | 
| 115 |  | 
| 116 |     Q_Q(QTextureMaterial); | 
| 117 |     m_filterKey->setParent(q); | 
| 118 |     m_filterKey->setName(QStringLiteral("renderingStyle" )); | 
| 119 |     m_filterKey->setValue(QStringLiteral("forward" )); | 
| 120 |  | 
| 121 |     m_textureGL3Technique->addFilterKey(filterKey: m_filterKey); | 
| 122 |     m_textureGL2Technique->addFilterKey(filterKey: m_filterKey); | 
| 123 |     m_textureES2Technique->addFilterKey(filterKey: m_filterKey); | 
| 124 |  | 
| 125 |     m_textureGL3RenderPass->setShaderProgram(m_textureGL3Shader); | 
| 126 |     m_textureGL2RenderPass->setShaderProgram(m_textureGL2ES2Shader); | 
| 127 |     m_textureES2RenderPass->setShaderProgram(m_textureGL2ES2Shader); | 
| 128 |  | 
| 129 |     m_textureGL3RenderPass->addRenderState(state: m_noDepthMask); | 
| 130 |     m_textureGL3RenderPass->addRenderState(state: m_blendState); | 
| 131 |     m_textureGL3RenderPass->addRenderState(state: m_blendEquation); | 
| 132 |  | 
| 133 |     m_textureGL2RenderPass->addRenderState(state: m_noDepthMask); | 
| 134 |     m_textureGL2RenderPass->addRenderState(state: m_blendState); | 
| 135 |     m_textureGL2RenderPass->addRenderState(state: m_blendEquation); | 
| 136 |  | 
| 137 |     m_textureES2RenderPass->addRenderState(state: m_noDepthMask); | 
| 138 |     m_textureES2RenderPass->addRenderState(state: m_blendState); | 
| 139 |     m_textureES2RenderPass->addRenderState(state: m_blendEquation); | 
| 140 |  | 
| 141 |     m_textureGL3Technique->addRenderPass(pass: m_textureGL3RenderPass); | 
| 142 |     m_textureGL2Technique->addRenderPass(pass: m_textureGL2RenderPass); | 
| 143 |     m_textureES2Technique->addRenderPass(pass: m_textureES2RenderPass); | 
| 144 |  | 
| 145 |     m_textureEffect->addTechnique(t: m_textureGL3Technique); | 
| 146 |     m_textureEffect->addTechnique(t: m_textureGL2Technique); | 
| 147 |     m_textureEffect->addTechnique(t: m_textureES2Technique); | 
| 148 |  | 
| 149 |     m_textureEffect->addParameter(parameter: m_textureParameter); | 
| 150 |     m_textureEffect->addParameter(parameter: m_textureTransformParameter); | 
| 151 |  | 
| 152 |     q->setEffect(m_textureEffect); | 
| 153 | } | 
| 154 |  | 
| 155 | void QTextureMaterialPrivate::handleTextureChanged(const QVariant &var) | 
| 156 | { | 
| 157 |     Q_Q(QTextureMaterial); | 
| 158 |     emit q->textureChanged(texture: var.value<QAbstractTexture *>()); | 
| 159 | } | 
| 160 |  | 
| 161 | void QTextureMaterialPrivate::handleTextureTransformChanged(const QVariant &var) | 
| 162 | { | 
| 163 |     Q_Q(QTextureMaterial); | 
| 164 |     const QMatrix3x3 matrix = var.value<QMatrix3x3>(); | 
| 165 |     emit q->textureTransformChanged(textureTransform: matrix); | 
| 166 |     emit q->textureOffsetChanged(textureOffset: QVector2D(matrix(0, 2), matrix(1, 2))); | 
| 167 | } | 
| 168 |  | 
| 169 | /*! | 
| 170 |     \class Qt3DExtras::QTextureMaterial | 
| 171 |     \ingroup qt3d-extras-materials | 
| 172 |     \brief The QTextureMaterial provides a default implementation of a simple unlit | 
| 173 |     texture material. | 
| 174 |     \inmodule Qt3DExtras | 
| 175 |     \since 5.9 | 
| 176 |     \inherits Qt3DRender::QMaterial | 
| 177 |  | 
| 178 |     This material uses an effect with a single render pass approach. Techniques are provided | 
| 179 |     for OpenGL 2, OpenGL 3 or above as well as OpenGL ES 2. | 
| 180 | */ | 
| 181 |  | 
| 182 | /*! | 
| 183 |     Constructs a new QTextureMaterial instance with parent object \a parent. | 
| 184 |  */ | 
| 185 | QTextureMaterial::(QNode *parent) | 
| 186 |     : QMaterial(*new QTextureMaterialPrivate, parent) | 
| 187 | { | 
| 188 |     Q_D(QTextureMaterial); | 
| 189 |     d->init(); | 
| 190 | } | 
| 191 |  | 
| 192 | /*! | 
| 193 |     Destroys the QTextureMaterial instance. | 
| 194 | */ | 
| 195 | QTextureMaterial::() | 
| 196 | { | 
| 197 | } | 
| 198 |  | 
| 199 | /*! | 
| 200 |     \property QTextureMaterial::texture | 
| 201 |  | 
| 202 |     Holds the current texture used by the material. | 
| 203 | */ | 
| 204 | QAbstractTexture *QTextureMaterial::() const | 
| 205 | { | 
| 206 |     Q_D(const QTextureMaterial); | 
| 207 |     return d->m_textureParameter->value().value<QAbstractTexture *>(); | 
| 208 | } | 
| 209 |  | 
| 210 | /*! | 
| 211 |     \property QTextureMaterial::textureOffset | 
| 212 |  | 
| 213 |     This is a utility property. It sets the translation component of the general | 
| 214 |     texture transform matrix | 
| 215 |  | 
| 216 | */ | 
| 217 | QVector2D QTextureMaterial::() const | 
| 218 | { | 
| 219 |     Q_D(const QTextureMaterial); | 
| 220 |     const QMatrix3x3 matrix = d->m_textureTransformParameter->value().value<QMatrix3x3>(); | 
| 221 |     return QVector2D(matrix(0, 2), matrix(1, 2)); | 
| 222 | } | 
| 223 |  | 
| 224 |  | 
| 225 | /*! | 
| 226 |     \property QTextureMaterial::textureTransform | 
| 227 |  | 
| 228 |     Holds the current texture transform. It is applied to texture | 
| 229 |     coordinates at render time. Defaults to identity matrix. | 
| 230 |  | 
| 231 | */ | 
| 232 | QMatrix3x3 QTextureMaterial::() const | 
| 233 | { | 
| 234 |     Q_D(const QTextureMaterial); | 
| 235 |     return d->m_textureTransformParameter->value().value<QMatrix3x3>(); | 
| 236 | } | 
| 237 |  | 
| 238 | void QTextureMaterial::(QAbstractTexture *texture) | 
| 239 | { | 
| 240 |     Q_D(QTextureMaterial); | 
| 241 |     d->m_textureParameter->setValue(QVariant::fromValue(value: texture)); | 
| 242 | } | 
| 243 |  | 
| 244 | void QTextureMaterial::(QVector2D textureOffset) | 
| 245 | { | 
| 246 |     Q_D(QTextureMaterial); | 
| 247 |     QMatrix3x3 matrix = d->m_textureTransformParameter->value().value<QMatrix3x3>(); | 
| 248 |     matrix(0, 2) = textureOffset.x(); | 
| 249 |     matrix(1, 2) = textureOffset.y(); | 
| 250 |     d->m_textureTransformParameter->setValue(QVariant::fromValue(value: matrix)); | 
| 251 | } | 
| 252 |  | 
| 253 | void QTextureMaterial::(const QMatrix3x3 &matrix) | 
| 254 | { | 
| 255 |     Q_D(QTextureMaterial); | 
| 256 |     d->m_textureTransformParameter->setValue(QVariant::fromValue(value: matrix)); | 
| 257 | } | 
| 258 |  | 
| 259 | /*! | 
| 260 |     \property QTextureMaterial::alphaBlending | 
| 261 |  | 
| 262 |     Indicates if the alpha information coming from the diffuse property will | 
| 263 |     be taken into account during rendering. Defaults to false. | 
| 264 | */ | 
| 265 | /*! | 
| 266 |     \qmlproperty bool TextureMaterial::alphaBlending | 
| 267 |  | 
| 268 |     Indicates if the alpha information coming from the diffuse property will | 
| 269 |     be taken into account during rendering. Defaults to false. | 
| 270 | */ | 
| 271 | bool QTextureMaterial::() const | 
| 272 | { | 
| 273 |     Q_D(const QTextureMaterial); | 
| 274 |     return d->m_noDepthMask->isEnabled(); | 
| 275 | } | 
| 276 |  | 
| 277 | void QTextureMaterial::(bool enabled) | 
| 278 | { | 
| 279 |     Q_D(QTextureMaterial); | 
| 280 |     d->m_noDepthMask->setEnabled(enabled); | 
| 281 |     d->m_blendState->setEnabled(enabled); | 
| 282 |     d->m_blendEquation->setEnabled(enabled); | 
| 283 | } | 
| 284 |  | 
| 285 | } // namespace Qt3DExtras | 
| 286 |  | 
| 287 | QT_END_NAMESPACE | 
| 288 |  |