QGIS API Documentation 4.1.0-Master (3b8ef1f72a3)
Loading...
Searching...
No Matches
qgsphongtexturedmaterial3dhandler.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsphongtexturedmaterial3dhandler.cpp
3 --------------------------------------
4 Date : August 2020
5 Copyright : (C) 2020 by Nyall Dawson
6 Email : nyall dot dawson at gmail 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
17
18#include "qgsapplication.h"
20#include "qgsimagecache.h"
21#include "qgsimagetexture.h"
26
27#include <QMap>
28#include <QString>
29#include <Qt3DCore/QEntity>
30#include <Qt3DRender/QEffect>
31#include <Qt3DRender/QGraphicsApiFilter>
32#include <Qt3DRender/QPaintedTextureImage>
33#include <Qt3DRender/QParameter>
34#include <Qt3DRender/QTechnique>
35#include <Qt3DRender/QTexture>
36
37using namespace Qt::StringLiterals;
38
40{
41 const QgsPhongTexturedMaterialSettings *phongSettings = dynamic_cast< const QgsPhongTexturedMaterialSettings * >( settings );
42 Q_ASSERT( phongSettings );
43
44 switch ( technique )
45 {
52 {
53 if ( context.isHighlighted() )
54 {
55 return new QgsHighlightMaterial( technique );
56 }
57
58 bool fitsInCache = false;
59 const QImage textureSourceImage = QgsApplication::imageCache()->pathAsImage( phongSettings->diffuseTexturePath(), QSize(), true, 1.0, fitsInCache );
60 ( void ) fitsInCache;
61
62 // No texture image was provided.
63 // Fallback to QgsPhongMaterialSettings.
64 if ( textureSourceImage.isNull() )
65 {
67 phongSettings.setAmbient( phongSettings.ambient() );
68 phongSettings.setDiffuse( QColor::fromRgbF( 0.7f, 0.7f, 0.7f, 1.0f ) ); // default diffuse color from QDiffuseSpecularMaterial
69 phongSettings.setOpacity( phongSettings.opacity() );
70 phongSettings.setShininess( phongSettings.shininess() );
71 phongSettings.setSpecular( phongSettings.specular() );
72 QgsMaterial *material = QgsPhongMaterial3DHandler().toMaterial( &phongSettings, technique, context );
73 return material;
74 }
75
76 QgsPhongTexturedMaterial *material = new QgsPhongTexturedMaterial();
77 material->setObjectName( u"phongTexturedMaterial"_s );
78
79 int opacity = static_cast<int>( phongSettings->opacity() * 255.0 );
80 QColor ambient = context.isSelected() ? context.selectionColor().darker() : phongSettings->ambient();
81 material->setAmbient( QColor( ambient.red(), ambient.green(), ambient.blue(), opacity ) );
82 material->setSpecular( QColor( phongSettings->specular().red(), phongSettings->specular().green(), phongSettings->specular().blue(), opacity ) );
83 material->setShininess( static_cast<float>( phongSettings->shininess() ) );
84 material->setOpacity( static_cast<float>( phongSettings->opacity() ) );
85
86 // TODO : if ( context.isSelected() ) dampen the color of diffuse texture
87 // with context.map().selectionColor()
88 QgsImageTexture *textureImage = new QgsImageTexture( textureSourceImage );
89 Qt3DRender::QTexture2D *texture = new Qt3DRender::QTexture2D();
90 texture->addTextureImage( textureImage );
91
92 texture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::Repeat );
93 texture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::Repeat );
94
95 texture->setGenerateMipMaps( true );
96 texture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
97 texture->setMinificationFilter( Qt3DRender::QTexture2D::LinearMipMapLinear );
98
99 material->setDiffuseTexture( texture );
100 material->setDiffuseTextureScale( static_cast<float>( phongSettings->textureScale() ) );
101 material->setDiffuseTextureRotation( static_cast<float>( phongSettings->textureRotation() ) );
102
103 return material;
104 }
105
108 return nullptr;
109 }
110 return nullptr;
111}
112
114{
115 const QgsPhongTexturedMaterialSettings *phongSettings = dynamic_cast< const QgsPhongTexturedMaterialSettings * >( settings );
116 Q_ASSERT( phongSettings );
117
118 QMap<QString, QString> parameters;
119 parameters[u"Ka"_s] = u"%1 %2 %3"_s.arg( phongSettings->ambient().redF() ).arg( phongSettings->ambient().greenF() ).arg( phongSettings->ambient().blueF() );
120 parameters[u"Ks"_s] = u"%1 %2 %3"_s.arg( phongSettings->specular().redF() ).arg( phongSettings->specular().greenF() ).arg( phongSettings->specular().blueF() );
121 parameters[u"Ns"_s] = QString::number( phongSettings->shininess() );
122 return parameters;
123}
124
125void QgsPhongTexturedMaterial3DHandler::addParametersToEffect( Qt3DRender::QEffect *effect, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &materialContext ) const
126{
127 const QgsPhongTexturedMaterialSettings *phongSettings = dynamic_cast< const QgsPhongTexturedMaterialSettings * >( settings );
128 Q_ASSERT( phongSettings );
129
130 const QColor ambientColor = materialContext.isSelected() ? materialContext.selectionColor().darker() : phongSettings->ambient();
131
132 Qt3DRender::QParameter *ambientParameter = new Qt3DRender::QParameter( u"ambientColor"_s, ambientColor );
133 Qt3DRender::QParameter *specularParameter = new Qt3DRender::QParameter( u"specularColor"_s, phongSettings->specular() );
134 Qt3DRender::QParameter *shininessParameter = new Qt3DRender::QParameter( u"shininess"_s, static_cast<float>( phongSettings->shininess() ) );
135
136 effect->addParameter( ambientParameter );
137 effect->addParameter( specularParameter );
138 effect->addParameter( shininessParameter );
139}
140
141bool QgsPhongTexturedMaterial3DHandler::updatePreviewScene( Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext & ) const
142{
143 const QgsPhongTexturedMaterialSettings *phongSettings = qgis::down_cast< const QgsPhongTexturedMaterialSettings * >( settings );
144
145 QgsMaterial *material = sceneRoot->findChild<QgsMaterial *>();
146 if ( material->objectName() != "phongTexturedMaterial"_L1 )
147 return false;
148
149 Qt3DRender::QEffect *effect = material->effect();
150
151 if ( Qt3DRender::QParameter *p = findParameter( effect, u"ambientColor"_s ) )
152 p->setValue( phongSettings->ambient() );
153
154 Qt3DRender::QTexture2D *texture = material->findChild<Qt3DRender::QTexture2D *>();
155 bool fitsInCache;
156 texture->addTextureImage( new QgsImageTexture( QgsApplication::imageCache()->pathAsImage( phongSettings->diffuseTexturePath(), QSize(), true, 1.0, fitsInCache ) ) );
157 texture->removeTextureImage( texture->textureImages().at( 0 ) );
158
159 if ( Qt3DRender::QParameter *p = findParameter( effect, u"texCoordScale"_s ) )
160 p->setValue( phongSettings->textureScale() );
161 if ( Qt3DRender::QParameter *p = findParameter( effect, u"texCoordRotation"_s ) )
162 p->setValue( phongSettings->textureRotation() );
163 if ( Qt3DRender::QParameter *p = findParameter( effect, u"specularColor"_s ) )
164 p->setValue( phongSettings->specular() );
165 if ( Qt3DRender::QParameter *p = findParameter( effect, u"shininess"_s ) )
166 p->setValue( phongSettings->shininess() );
167 if ( Qt3DRender::QParameter *p = findParameter( effect, u"opacity"_s ) )
168 p->setValue( phongSettings->opacity() );
169 return true;
170}
MaterialRenderingTechnique
Material rendering techniques.
Definition qgis.h:4327
@ Points
Point based rendering, requires point data.
Definition qgis.h:4331
@ Triangles
Triangle based rendering (default).
Definition qgis.h:4328
@ TrianglesFromModel
Triangle based rendering, using a model object source.
Definition qgis.h:4333
@ Lines
Line based rendering, requires line data.
Definition qgis.h:4329
@ Billboards
Flat billboard rendering.
Definition qgis.h:4335
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
Definition qgis.h:4334
@ InstancedPoints
Instanced based rendering, requiring triangles and point data.
Definition qgis.h:4330
@ TrianglesWithFixedTexture
Triangle based rendering, using a fixed, non-user-configurable texture (e.g. for terrain rendering).
Definition qgis.h:4332
static Qt3DRender::QParameter * findParameter(Qt3DRender::QEffect *effect, const QString &name)
Finds an existing parameter in an effect by name.
Abstract base class for material settings.
static QgsImageCache * imageCache()
Returns the application's image cache, used for caching resampled versions of raster images.
QImage pathAsImage(const QString &path, const QSize size, const bool keepAspectRatio, const double opacity, bool &fitsInCache, bool blocking=false, double targetDpi=96, int frameNumber=-1, bool *isMissing=nullptr)
Returns the specified path rendered as an image.
Holds an image that can be used as a texture in the 3D view.
Context settings for a material.
QColor selectionColor() const
Returns the color for representing materials in a selected state.
bool isSelected() const
Returns true if the material should represent a selected state.
bool isHighlighted() const
Returns true if the material should represent a highlighted state.
Base class for all materials used within QGIS 3D views.
Definition qgsmaterial.h:40
3D handler for the Phong shading material.
QgsMaterial * toMaterial(const QgsAbstractMaterialSettings *settings, Qgis::MaterialRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QgsMaterial object representing the material settings.
Basic shading material used for rendering based on the Phong shading model with three color component...
void setOpacity(double opacity)
Sets opacity of the surface.
void setDiffuse(const QColor &diffuse)
Sets diffuse color component.
void setShininess(double shininess)
Sets shininess of the surface.
double opacity() const
Returns the opacity of the surface.
QColor specular() const
Returns specular color component.
QColor ambient() const
Returns ambient color component.
void setAmbient(const QColor &ambient)
Sets ambient color component.
double shininess() const
Returns shininess of the surface.
void setSpecular(const QColor &specular)
Sets specular color component.
QgsMaterial * toMaterial(const QgsAbstractMaterialSettings *settings, Qgis::MaterialRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QgsMaterial object representing the material settings.
QMap< QString, QString > toExportParameters(const QgsAbstractMaterialSettings *settings) const override
Returns the parameters to be exported to .mtl file.
void addParametersToEffect(Qt3DRender::QEffect *effect, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &materialContext) const override
Adds parameters from the material settings to a destination effect.
bool updatePreviewScene(Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context) const override
Updates an existing material preview scene with new material settings.
A Phong shading model with diffuse texture map.
QString diffuseTexturePath() const
Returns the diffuse texture path.
QColor specular() const
Returns specular color component.
double textureRotation() const
Returns the texture rotation, in degrees.
double shininess() const
Returns shininess of the surface.
double opacity() const
Returns the opacity of the surface.
double textureScale() const
Returns the texture scale The texture scale changes the size of the displayed texture in the 3D scene...
QColor ambient() const
Returns ambient color component.