QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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,
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 );
56
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 );
64
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" ) ) ) );
68
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 );
74
75 Qt3DRender::QFilterKey *filterKey = new Qt3DRender::QFilterKey( this );
76 filterKey->setName( QStringLiteral( "renderingStyle" ) );
77 filterKey->setValue( QStringLiteral( "forward" ) );
78
79 gL3Technique->addFilterKey( filterKey );
80
81 Qt3DRender::QRenderPass *gL3RenderPass = new Qt3DRender::QRenderPass();
82 gL3RenderPass->setShaderProgram( gL3Shader );
83 gL3Technique->addRenderPass( gL3RenderPass );
84 effect->addTechnique( gL3Technique );
85
86 setEffect( effect );
87}
88
89void QgsPhongTexturedMaterial::setAmbient( const QColor &ambient )
90{
91 mAmbientParameter->setValue( ambient );
92}
93
94void QgsPhongTexturedMaterial::setDiffuseTexture( Qt3DRender::QAbstractTexture *diffuseTexture )
95{
96 mDiffuseTextureParameter->setValue( QVariant::fromValue( diffuseTexture ) );
97}
98
99void QgsPhongTexturedMaterial::setDiffuseTextureScale( float diffuseTextureScale )
100{
101 mDiffuseTextureScaleParameter->setValue( diffuseTextureScale );
102}
103
104void QgsPhongTexturedMaterial::setSpecular( const QColor &specular )
105{
106 mSpecularParameter->setValue( specular );
107}
108
109void QgsPhongTexturedMaterial::setShininess( float shininess )
110{
111 mShininessParameter->setValue( shininess );
112}
113
114void QgsPhongTexturedMaterial::setOpacity( float opacity )
115{
116 mOpacityParameter->setValue( opacity );
117}
118
119QColor QgsPhongTexturedMaterial::ambient() const
120{
121 return mAmbientParameter->value().value<QColor>();
122}
123
124Qt3DRender::QAbstractTexture *QgsPhongTexturedMaterial::diffuseTexture() const
125{
126 return mDiffuseTextureParameter->value().value<Qt3DRender::QAbstractTexture *>();
127}
128
129float QgsPhongTexturedMaterial::diffuseTextureScale() const
130{
131 return mDiffuseTextureScaleParameter->value().toFloat();
132}
133
134QColor QgsPhongTexturedMaterial::specular() const
135{
136 return mSpecularParameter->value().value<QColor>();
137}
138
139float QgsPhongTexturedMaterial::shininess() const
140{
141 return mShininessParameter->value().toFloat();
142}
143
144float QgsPhongTexturedMaterial::opacity() const
145{
146 return mOpacityParameter->value().toFloat();
147}
148
149void QgsPhongTexturedMaterial::handleAmbientChanged( const QVariant &var )
150{
151 emit ambientChanged( var.value<QColor>() );
152}
153
154void QgsPhongTexturedMaterial::handleDiffuseTextureChanged( const QVariant &var )
155{
156 emit diffuseTextureChanged( var.value<Qt3DRender::QAbstractTexture *>() );
157}
158
159void QgsPhongTexturedMaterial::handleDiffuseTextureScaleChanged( const QVariant &var )
160{
161 emit diffuseTextureScaleChanged( var.toFloat() );
162}
163
164void QgsPhongTexturedMaterial::handleSpecularChanged( const QVariant &var )
165{
166 emit specularChanged( var.value<QColor>() );
167}
168
169void QgsPhongTexturedMaterial::handleShininessChanged( const QVariant &var )
170{
171 emit shininessChanged( var.toFloat() );
172}
173
174void QgsPhongTexturedMaterial::handleOpacityChanged( const QVariant &var )
175{
176 emit opacityChanged( var.toFloat() );
177}
178