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,
45 this, &QgsPhongTexturedMaterial::handleAmbientChanged );
46 connect( mDiffuseTextureParameter, &Qt3DRender::QParameter::valueChanged,
47 this, &QgsPhongTexturedMaterial::handleDiffuseTextureChanged );
48 connect( mDiffuseTextureScaleParameter, &Qt3DRender::QParameter::valueChanged,
49 this, &QgsPhongTexturedMaterial::handleDiffuseTextureScaleChanged );
50 connect( mSpecularParameter, &Qt3DRender::QParameter::valueChanged,
51 this, &QgsPhongTexturedMaterial::handleSpecularChanged );
52 connect( mShininessParameter, &Qt3DRender::QParameter::valueChanged,
53 this, &QgsPhongTexturedMaterial::handleShininessChanged );
54 connect( mOpacityParameter, &Qt3DRender::QParameter::valueChanged,
55 this, &QgsPhongTexturedMaterial::handleOpacityChanged );
57 Qt3DRender::QEffect *effect =
new Qt3DRender::QEffect();
58 effect->addParameter( mAmbientParameter );
59 effect->addParameter( mDiffuseTextureParameter );
60 effect->addParameter( mDiffuseTextureScaleParameter );
61 effect->addParameter( mSpecularParameter );
62 effect->addParameter( mShininessParameter );
63 effect->addParameter( mOpacityParameter );
65 Qt3DRender::QShaderProgram *gL3Shader =
new Qt3DRender::QShaderProgram();
66 gL3Shader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/default.vert" ) ) ) );
67 gL3Shader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/diffuseSpecular.frag" ) ) ) );
69 Qt3DRender::QTechnique *gL3Technique =
new Qt3DRender::QTechnique();
70 gL3Technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
71 gL3Technique->graphicsApiFilter()->setMajorVersion( 3 );
72 gL3Technique->graphicsApiFilter()->setMinorVersion( 1 );
73 gL3Technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
75 Qt3DRender::QFilterKey *filterKey =
new Qt3DRender::QFilterKey(
this );
76 filterKey->setName( QStringLiteral(
"renderingStyle" ) );
77 filterKey->setValue( QStringLiteral(
"forward" ) );
79 gL3Technique->addFilterKey( filterKey );
81 Qt3DRender::QRenderPass *gL3RenderPass =
new Qt3DRender::QRenderPass();
82 gL3RenderPass->setShaderProgram( gL3Shader );
83 gL3Technique->addRenderPass( gL3RenderPass );
84 effect->addTechnique( gL3Technique );
89void QgsPhongTexturedMaterial::setAmbient(
const QColor &ambient )
91 mAmbientParameter->setValue( ambient );
94void QgsPhongTexturedMaterial::setDiffuseTexture( Qt3DRender::QAbstractTexture *diffuseTexture )
96 mDiffuseTextureParameter->setValue( QVariant::fromValue( diffuseTexture ) );
99void QgsPhongTexturedMaterial::setDiffuseTextureScale(
float diffuseTextureScale )
101 mDiffuseTextureScaleParameter->setValue( diffuseTextureScale );
104void QgsPhongTexturedMaterial::setSpecular(
const QColor &specular )
106 mSpecularParameter->setValue( specular );
109void QgsPhongTexturedMaterial::setShininess(
float shininess )
111 mShininessParameter->setValue( shininess );
114void QgsPhongTexturedMaterial::setOpacity(
float opacity )
116 mOpacityParameter->setValue( opacity );
119QColor QgsPhongTexturedMaterial::ambient()
const
121 return mAmbientParameter->value().value<QColor>();
124Qt3DRender::QAbstractTexture *QgsPhongTexturedMaterial::diffuseTexture()
const
126 return mDiffuseTextureParameter->value().value<Qt3DRender::QAbstractTexture *>();
129float QgsPhongTexturedMaterial::diffuseTextureScale()
const
131 return mDiffuseTextureScaleParameter->value().toFloat();
134QColor QgsPhongTexturedMaterial::specular()
const
136 return mSpecularParameter->value().value<QColor>();
139float QgsPhongTexturedMaterial::shininess()
const
141 return mShininessParameter->value().toFloat();
144float QgsPhongTexturedMaterial::opacity()
const
146 return mOpacityParameter->value().toFloat();
149void QgsPhongTexturedMaterial::handleAmbientChanged(
const QVariant &var )
151 emit ambientChanged( var.value<QColor>() );
154void QgsPhongTexturedMaterial::handleDiffuseTextureChanged(
const QVariant &var )
156 emit diffuseTextureChanged( var.value<Qt3DRender::QAbstractTexture *>() );
159void QgsPhongTexturedMaterial::handleDiffuseTextureScaleChanged(
const QVariant &var )
161 emit diffuseTextureScaleChanged( var.toFloat() );
164void QgsPhongTexturedMaterial::handleSpecularChanged(
const QVariant &var )
166 emit specularChanged( var.value<QColor>() );
169void QgsPhongTexturedMaterial::handleShininessChanged(
const QVariant &var )
171 emit shininessChanged( var.toFloat() );
174void QgsPhongTexturedMaterial::handleOpacityChanged(
const QVariant &var )
176 emit opacityChanged( var.toFloat() );