18#include <Qt3DRender/QEffect>
19#include <Qt3DRender/QGraphicsApiFilter>
20#include <Qt3DRender/QParameter>
21#include <Qt3DRender/QTechnique>
26QgsPhongTexturedMaterial::QgsPhongTexturedMaterial( QNode *parent )
28 , mAmbientParameter( new
Qt3DRender::QParameter( QStringLiteral(
"ambientColor" ), QColor::fromRgbF( 0.05f, 0.05f, 0.05f, 1.0f ) ) )
29 , mDiffuseTextureParameter( new
Qt3DRender::QParameter( QStringLiteral(
"diffuseTexture" ), QVariant() ) )
30 , mDiffuseTextureScaleParameter( new
Qt3DRender::QParameter( QStringLiteral(
"texCoordScale" ), 1.0f ) )
31 , mSpecularParameter( new
Qt3DRender::QParameter( QStringLiteral(
"specularColor" ), QColor::fromRgbF( 0.01f, 0.01f, 0.01f, 1.0f ) ) )
32 , mShininessParameter( new
Qt3DRender::QParameter( QStringLiteral(
"shininess" ), 150.0f ) )
33 , mOpacityParameter( new
Qt3DRender::QParameter( QStringLiteral(
"opacity" ), 1.0f ) )
38QgsPhongTexturedMaterial::~QgsPhongTexturedMaterial() =
default;
41void QgsPhongTexturedMaterial::init()
43 connect( mAmbientParameter, &Qt3DRender::QParameter::valueChanged,
44 this, &QgsPhongTexturedMaterial::handleAmbientChanged );
45 connect( mDiffuseTextureParameter, &Qt3DRender::QParameter::valueChanged,
46 this, &QgsPhongTexturedMaterial::handleDiffuseTextureChanged );
47 connect( mDiffuseTextureScaleParameter, &Qt3DRender::QParameter::valueChanged,
48 this, &QgsPhongTexturedMaterial::handleDiffuseTextureScaleChanged );
49 connect( mSpecularParameter, &Qt3DRender::QParameter::valueChanged,
50 this, &QgsPhongTexturedMaterial::handleSpecularChanged );
51 connect( mShininessParameter, &Qt3DRender::QParameter::valueChanged,
52 this, &QgsPhongTexturedMaterial::handleShininessChanged );
53 connect( mOpacityParameter, &Qt3DRender::QParameter::valueChanged,
54 this, &QgsPhongTexturedMaterial::handleOpacityChanged );
56 Qt3DRender::QEffect *effect =
new Qt3DRender::QEffect();
57 effect->addParameter( mAmbientParameter );
58 effect->addParameter( mDiffuseTextureParameter );
59 effect->addParameter( mDiffuseTextureScaleParameter );
60 effect->addParameter( mSpecularParameter );
61 effect->addParameter( mShininessParameter );
62 effect->addParameter( mOpacityParameter );
64 Qt3DRender::QShaderProgram *gL3Shader =
new Qt3DRender::QShaderProgram();
65 gL3Shader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/default.vert" ) ) ) );
66 gL3Shader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/diffuseSpecular.frag" ) ) ) );
68 Qt3DRender::QTechnique *gL3Technique =
new Qt3DRender::QTechnique();
69 gL3Technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
70 gL3Technique->graphicsApiFilter()->setMajorVersion( 3 );
71 gL3Technique->graphicsApiFilter()->setMinorVersion( 1 );
72 gL3Technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
74 Qt3DRender::QFilterKey *filterKey =
new Qt3DRender::QFilterKey(
this );
75 filterKey->setName( QStringLiteral(
"renderingStyle" ) );
76 filterKey->setValue( QStringLiteral(
"forward" ) );
78 gL3Technique->addFilterKey( filterKey );
80 Qt3DRender::QRenderPass *gL3RenderPass =
new Qt3DRender::QRenderPass();
81 gL3RenderPass->setShaderProgram( gL3Shader );
82 gL3Technique->addRenderPass( gL3RenderPass );
83 effect->addTechnique( gL3Technique );
88void QgsPhongTexturedMaterial::setAmbient(
const QColor &ambient )
90 mAmbientParameter->setValue( ambient );
93void QgsPhongTexturedMaterial::setDiffuseTexture( Qt3DRender::QAbstractTexture *diffuseTexture )
95 mDiffuseTextureParameter->setValue( QVariant::fromValue( diffuseTexture ) );
98void QgsPhongTexturedMaterial::setDiffuseTextureScale(
float diffuseTextureScale )
100 mDiffuseTextureScaleParameter->setValue( diffuseTextureScale );
103void QgsPhongTexturedMaterial::setSpecular(
const QColor &specular )
105 mSpecularParameter->setValue( specular );
108void QgsPhongTexturedMaterial::setShininess(
float shininess )
110 mShininessParameter->setValue( shininess );
113void QgsPhongTexturedMaterial::setOpacity(
float opacity )
115 mOpacityParameter->setValue( opacity );
118QColor QgsPhongTexturedMaterial::ambient()
const
120 return mAmbientParameter->value().value<QColor>();
123Qt3DRender::QAbstractTexture *QgsPhongTexturedMaterial::diffuseTexture()
const
125 return mDiffuseTextureParameter->value().value<Qt3DRender::QAbstractTexture *>();
128float QgsPhongTexturedMaterial::diffuseTextureScale()
const
130 return mDiffuseTextureScaleParameter->value().toFloat();
133QColor QgsPhongTexturedMaterial::specular()
const
135 return mSpecularParameter->value().value<QColor>();
138float QgsPhongTexturedMaterial::shininess()
const
140 return mShininessParameter->value().toFloat();
143float QgsPhongTexturedMaterial::opacity()
const
145 return mOpacityParameter->value().toFloat();
148void QgsPhongTexturedMaterial::handleAmbientChanged(
const QVariant &var )
150 emit ambientChanged( var.value<QColor>() );
153void QgsPhongTexturedMaterial::handleDiffuseTextureChanged(
const QVariant &var )
155 emit diffuseTextureChanged( var.value<Qt3DRender::QAbstractTexture *>() );
158void QgsPhongTexturedMaterial::handleDiffuseTextureScaleChanged(
const QVariant &var )
160 emit diffuseTextureScaleChanged( var.toFloat() );
163void QgsPhongTexturedMaterial::handleSpecularChanged(
const QVariant &var )
165 emit specularChanged( var.value<QColor>() );
168void QgsPhongTexturedMaterial::handleShininessChanged(
const QVariant &var )
170 emit shininessChanged( var.toFloat() );
173void QgsPhongTexturedMaterial::handleOpacityChanged(
const QVariant &var )
175 emit opacityChanged( var.toFloat() );