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>
27#include "moc_qgstexturematerial.cpp"
30QgsTextureMaterial::QgsTextureMaterial( QNode *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 ) )
41QgsTextureMaterial::~QgsTextureMaterial() =
default;
44void QgsTextureMaterial::init()
46 connect( mTextureParameter, &Qt3DRender::QParameter::valueChanged,
47 this, &QgsTextureMaterial::handleTextureChanged );
49 Qt3DRender::QEffect *effect =
new Qt3DRender::QEffect();
51 effect->addParameter( mTextureParameter );
53 mGL3Shader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/texture.frag" ) ) ) );
54 mGL3Shader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/texture.vert" ) ) ) );
56 mGL3Technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
57 mGL3Technique->graphicsApiFilter()->setMajorVersion( 3 );
58 mGL3Technique->graphicsApiFilter()->setMinorVersion( 1 );
59 mGL3Technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
61 mFilterKey->setParent(
this );
62 mFilterKey->setName( QStringLiteral(
"renderingStyle" ) );
63 mFilterKey->setValue( QStringLiteral(
"forward" ) );
65 mGL3Technique->addFilterKey( mFilterKey );
66 mGL3RenderPass->setShaderProgram( mGL3Shader );
67 mGL3Technique->addRenderPass( mGL3RenderPass );
68 effect->addTechnique( mGL3Technique );
73void QgsTextureMaterial::setTexture( Qt3DRender::QAbstractTexture *texture )
75 mTextureParameter->setValue( QVariant::fromValue( texture ) );
78Qt3DRender::QAbstractTexture *QgsTextureMaterial::texture()
const
80 return mTextureParameter->value().value<Qt3DRender::QAbstractTexture *>();
83void QgsTextureMaterial::handleTextureChanged(
const QVariant &var )
85 emit textureChanged( var.value<Qt3DRender::QAbstractTexture *>() );