QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgstexturematerial.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstexturematerial.cpp
3 --------------------------------------
4 Date : March 2024
5 Copyright : (C) 2024 by Jean Felder
6 Email : jean dot felder at oslandia dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include <QUrl>
17
18#include <Qt3DRender/QEffect>
19#include <Qt3DRender/QGraphicsApiFilter>
20#include <Qt3DRender/QParameter>
21#include <Qt3DRender/QRenderPass>
22#include <Qt3DRender/QShaderProgram>
23#include <Qt3DRender/QTechnique>
24#include <Qt3DRender/QTexture>
25
26#include "qgstexturematerial.h"
27#include "moc_qgstexturematerial.cpp"
28
30QgsTextureMaterial::QgsTextureMaterial( QNode *parent )
31 : QgsMaterial( parent )
32 , mTextureParameter( new Qt3DRender::QParameter( QStringLiteral( "diffuseTexture" ), new Qt3DRender::QTexture2D ) )
33 , mGL3Technique( new Qt3DRender::QTechnique( this ) )
34 , mGL3RenderPass( new Qt3DRender::QRenderPass( this ) )
35 , mGL3Shader( new Qt3DRender::QShaderProgram( this ) )
36 , mFilterKey( new Qt3DRender::QFilterKey( this ) )
37{
38 init();
39}
40
41QgsTextureMaterial::~QgsTextureMaterial() = default;
42
43
44void QgsTextureMaterial::init()
45{
46 connect( mTextureParameter, &Qt3DRender::QParameter::valueChanged,
47 this, &QgsTextureMaterial::handleTextureChanged );
48
49 Qt3DRender::QEffect *effect = new Qt3DRender::QEffect();
50
51 effect->addParameter( mTextureParameter );
52
53 mGL3Shader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/texture.frag" ) ) ) );
54 mGL3Shader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/texture.vert" ) ) ) );
55
56 mGL3Technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
57 mGL3Technique->graphicsApiFilter()->setMajorVersion( 3 );
58 mGL3Technique->graphicsApiFilter()->setMinorVersion( 1 );
59 mGL3Technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
60
61 mFilterKey->setParent( this );
62 mFilterKey->setName( QStringLiteral( "renderingStyle" ) );
63 mFilterKey->setValue( QStringLiteral( "forward" ) );
64
65 mGL3Technique->addFilterKey( mFilterKey );
66 mGL3RenderPass->setShaderProgram( mGL3Shader );
67 mGL3Technique->addRenderPass( mGL3RenderPass );
68 effect->addTechnique( mGL3Technique );
69
70 setEffect( effect );
71}
72
73void QgsTextureMaterial::setTexture( Qt3DRender::QAbstractTexture *texture )
74{
75 mTextureParameter->setValue( QVariant::fromValue( texture ) );
76}
77
78Qt3DRender::QAbstractTexture *QgsTextureMaterial::texture() const
79{
80 return mTextureParameter->value().value<Qt3DRender::QAbstractTexture *>();
81}
82
83void QgsTextureMaterial::handleTextureChanged( const QVariant &var )
84{
85 emit textureChanged( var.value<Qt3DRender::QAbstractTexture *>() );
86}
87