QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsphongtexturedmaterial.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsphongtexturedmaterial.cpp
3 --------------------------------------
4 Date : August 2024
5 Copyright : (C) 2024 by Jean Felder
6 Email : jean dot felder at oslandia dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include <QUrl>
17
18#include <Qt3DRender/QEffect>
19#include <Qt3DRender/QGraphicsApiFilter>
20#include <Qt3DRender/QParameter>
21#include <Qt3DRender/QTechnique>
22
24#include "moc_qgsphongtexturedmaterial.cpp"
25
27QgsPhongTexturedMaterial::QgsPhongTexturedMaterial( QNode *parent )
28 : QgsMaterial( 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 ) )
35{
36 init();
37}
38
39QgsPhongTexturedMaterial::~QgsPhongTexturedMaterial() = default;
40
41
42void QgsPhongTexturedMaterial::init()
43{
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 );
50
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 );
58
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" ) ) ) );
62
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 );
68
69 Qt3DRender::QFilterKey *filterKey = new Qt3DRender::QFilterKey( this );
70 filterKey->setName( QStringLiteral( "renderingStyle" ) );
71 filterKey->setValue( QStringLiteral( "forward" ) );
72
73 gL3Technique->addFilterKey( filterKey );
74
75 Qt3DRender::QRenderPass *gL3RenderPass = new Qt3DRender::QRenderPass();
76 gL3RenderPass->setShaderProgram( gL3Shader );
77 gL3Technique->addRenderPass( gL3RenderPass );
78 effect->addTechnique( gL3Technique );
79
80 setEffect( effect );
81}
82
83void QgsPhongTexturedMaterial::setAmbient( const QColor &ambient )
84{
85 mAmbientParameter->setValue( ambient );
86}
87
88void QgsPhongTexturedMaterial::setDiffuseTexture( Qt3DRender::QAbstractTexture *diffuseTexture )
89{
90 mDiffuseTextureParameter->setValue( QVariant::fromValue( diffuseTexture ) );
91}
92
93void QgsPhongTexturedMaterial::setDiffuseTextureScale( float diffuseTextureScale )
94{
95 mDiffuseTextureScaleParameter->setValue( diffuseTextureScale );
96}
97
98void QgsPhongTexturedMaterial::setSpecular( const QColor &specular )
99{
100 mSpecularParameter->setValue( specular );
101}
102
103void QgsPhongTexturedMaterial::setShininess( float shininess )
104{
105 mShininessParameter->setValue( shininess );
106}
107
108void QgsPhongTexturedMaterial::setOpacity( float opacity )
109{
110 mOpacityParameter->setValue( opacity );
111}
112
113QColor QgsPhongTexturedMaterial::ambient() const
114{
115 return mAmbientParameter->value().value<QColor>();
116}
117
118Qt3DRender::QAbstractTexture *QgsPhongTexturedMaterial::diffuseTexture() const
119{
120 return mDiffuseTextureParameter->value().value<Qt3DRender::QAbstractTexture *>();
121}
122
123float QgsPhongTexturedMaterial::diffuseTextureScale() const
124{
125 return mDiffuseTextureScaleParameter->value().toFloat();
126}
127
128QColor QgsPhongTexturedMaterial::specular() const
129{
130 return mSpecularParameter->value().value<QColor>();
131}
132
133float QgsPhongTexturedMaterial::shininess() const
134{
135 return mShininessParameter->value().toFloat();
136}
137
138float QgsPhongTexturedMaterial::opacity() const
139{
140 return mOpacityParameter->value().toFloat();
141}
142
143void QgsPhongTexturedMaterial::handleAmbientChanged( const QVariant &var )
144{
145 emit ambientChanged( var.value<QColor>() );
146}
147
148void QgsPhongTexturedMaterial::handleDiffuseTextureChanged( const QVariant &var )
149{
150 emit diffuseTextureChanged( var.value<Qt3DRender::QAbstractTexture *>() );
151}
152
153void QgsPhongTexturedMaterial::handleDiffuseTextureScaleChanged( const QVariant &var )
154{
155 emit diffuseTextureScaleChanged( var.toFloat() );
156}
157
158void QgsPhongTexturedMaterial::handleSpecularChanged( const QVariant &var )
159{
160 emit specularChanged( var.value<QColor>() );
161}
162
163void QgsPhongTexturedMaterial::handleShininessChanged( const QVariant &var )
164{
165 emit shininessChanged( var.toFloat() );
166}
167
168void QgsPhongTexturedMaterial::handleOpacityChanged( const QVariant &var )
169{
170 emit opacityChanged( var.toFloat() );
171}
172