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>
29QgsTextureMaterial::QgsTextureMaterial( QNode *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 ) )
40QgsTextureMaterial::~QgsTextureMaterial() =
default;
43void QgsTextureMaterial::init()
45 connect( mTextureParameter, &Qt3DRender::QParameter::valueChanged,
46 this, &QgsTextureMaterial::handleTextureChanged );
48 Qt3DRender::QEffect *effect =
new Qt3DRender::QEffect();
50 effect->addParameter( mTextureParameter );
52 mGL3Shader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/texture.frag" ) ) ) );
53 mGL3Shader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/texture.vert" ) ) ) );
55 mGL3Technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
56 mGL3Technique->graphicsApiFilter()->setMajorVersion( 3 );
57 mGL3Technique->graphicsApiFilter()->setMinorVersion( 1 );
58 mGL3Technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
60 mFilterKey->setParent(
this );
61 mFilterKey->setName( QStringLiteral(
"renderingStyle" ) );
62 mFilterKey->setValue( QStringLiteral(
"forward" ) );
64 mGL3Technique->addFilterKey( mFilterKey );
65 mGL3RenderPass->setShaderProgram( mGL3Shader );
66 mGL3Technique->addRenderPass( mGL3RenderPass );
67 effect->addTechnique( mGL3Technique );
72void QgsTextureMaterial::setTexture( Qt3DRender::QAbstractTexture *texture )
74 mTextureParameter->setValue( QVariant::fromValue( texture ) );
77Qt3DRender::QAbstractTexture *QgsTextureMaterial::texture()
const
79 return mTextureParameter->value().value<Qt3DRender::QAbstractTexture *>();
82void QgsTextureMaterial::handleTextureChanged(
const QVariant &var )
84 emit textureChanged( var.value<Qt3DRender::QAbstractTexture *>() );