QGIS API Documentation 3.39.0-Master (bca3cdb6021)
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
29QgsTextureMaterial::QgsTextureMaterial( QNode *parent )
30 : QMaterial( parent )
31 , mTextureParameter( new Qt3DRender::QParameter( QStringLiteral( "diffuseTexture" ), new Qt3DRender::QTexture2D ) )
32 , mGL3Technique( new Qt3DRender::QTechnique( this ) )
33 , mGL3RenderPass( new Qt3DRender::QRenderPass( this ) )
34 , mGL3Shader( new Qt3DRender::QShaderProgram( this ) )
35 , mFilterKey( new Qt3DRender::QFilterKey( this ) )
36{
37 init();
38}
39
40QgsTextureMaterial::~QgsTextureMaterial() = default;
41
42
43void QgsTextureMaterial::init()
44{
45 connect( mTextureParameter, &Qt3DRender::QParameter::valueChanged,
46 this, &QgsTextureMaterial::handleTextureChanged );
47
48 Qt3DRender::QEffect *effect = new Qt3DRender::QEffect();
49
50 effect->addParameter( mTextureParameter );
51
52 mGL3Shader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/texture.frag" ) ) ) );
53 mGL3Shader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/texture.vert" ) ) ) );
54
55 mGL3Technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
56 mGL3Technique->graphicsApiFilter()->setMajorVersion( 3 );
57 mGL3Technique->graphicsApiFilter()->setMinorVersion( 1 );
58 mGL3Technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
59
60 mFilterKey->setParent( this );
61 mFilterKey->setName( QStringLiteral( "renderingStyle" ) );
62 mFilterKey->setValue( QStringLiteral( "forward" ) );
63
64 mGL3Technique->addFilterKey( mFilterKey );
65 mGL3RenderPass->setShaderProgram( mGL3Shader );
66 mGL3Technique->addRenderPass( mGL3RenderPass );
67 effect->addTechnique( mGL3Technique );
68
69 setEffect( effect );
70}
71
72void QgsTextureMaterial::setTexture( Qt3DRender::QAbstractTexture *texture )
73{
74 mTextureParameter->setValue( QVariant::fromValue( texture ) );
75}
76
77Qt3DRender::QAbstractTexture *QgsTextureMaterial::texture() const
78{
79 return mTextureParameter->value().value<Qt3DRender::QAbstractTexture *>();
80}
81
82void QgsTextureMaterial::handleTextureChanged( const QVariant &var )
83{
84 emit textureChanged( var.value<Qt3DRender::QAbstractTexture *>() );
85}
86