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