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