QGIS API Documentation 4.1.0-Master (01362494303)
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 "qgs3dutils.h"
19#include "qgsapplication.h"
21#include "qgsimagecache.h"
22#include "qgsimagetexture.h"
27
28#include <QMap>
29#include <QString>
30#include <Qt3DCore/QEntity>
31#include <Qt3DRender/QEffect>
32#include <Qt3DRender/QGraphicsApiFilter>
33#include <Qt3DRender/QPaintedTextureImage>
34#include <Qt3DRender/QParameter>
35#include <Qt3DRender/QTechnique>
36#include <Qt3DRender/QTexture>
37
38using namespace Qt::StringLiterals;
39
41{
42 const QgsPhongTexturedMaterialSettings *phongSettings = dynamic_cast< const QgsPhongTexturedMaterialSettings * >( settings );
43 Q_ASSERT( phongSettings );
44
45 switch ( technique )
46 {
53 {
54 if ( context.isHighlighted() )
55 {
56 return new QgsHighlightMaterial( technique );
57 }
58
59 bool fitsInCache = false;
60 const QImage textureSourceImage = QgsApplication::imageCache()->pathAsImage( phongSettings->diffuseTexturePath(), QSize(), true, 1.0, fitsInCache );
61 ( void ) fitsInCache;
62
63 // No texture image was provided.
64 // Fallback to QgsPhongMaterialSettings.
65 if ( textureSourceImage.isNull() )
66 {
68 phongSettings.setAmbient( phongSettings.ambient() );
69 phongSettings.setDiffuse( QColor::fromRgbF( 0.7f, 0.7f, 0.7f, 1.0f ) ); // default diffuse color from QDiffuseSpecularMaterial
70 phongSettings.setOpacity( phongSettings.opacity() );
71 phongSettings.setShininess( phongSettings.shininess() );
72 phongSettings.setSpecular( phongSettings.specular() );
73 QgsMaterial *material = QgsPhongMaterial3DHandler().toMaterial( &phongSettings, technique, context );
74 return material;
75 }
76
77 QgsPhongTexturedMaterial *material = new QgsPhongTexturedMaterial();
78 material->setObjectName( u"phongTexturedMaterial"_s );
79
80 int opacity = static_cast<int>( phongSettings->opacity() * 255.0 );
81 QColor ambient = context.isSelected() ? context.selectionColor().darker() : phongSettings->ambient();
82 material->setAmbient( QColor( ambient.red(), ambient.green(), ambient.blue(), opacity ) );
83 const QColor specular = phongSettings->specular();
84 material->setSpecular( QColor( specular.red(), specular.green(), specular.blue(), opacity ) );
85 material->setShininess( static_cast<float>( phongSettings->shininess() ) );
86 material->setOpacity( static_cast<float>( phongSettings->opacity() ) );
87
88 // TODO : if ( context.isSelected() ) dampen the color of diffuse texture
89 // with context.map().selectionColor()
90 QgsImageTexture *textureImage = new QgsImageTexture( textureSourceImage );
91 Qt3DRender::QTexture2D *texture = new Qt3DRender::QTexture2D();
92 texture->addTextureImage( textureImage );
93
94 texture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::Repeat );
95 texture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::Repeat );
96 texture->setFormat( Qt3DRender::QAbstractTexture::SRGB8_Alpha8 );
97 texture->setGenerateMipMaps( true );
98 texture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
99 texture->setMinificationFilter( Qt3DRender::QTexture2D::LinearMipMapLinear );
100
101 material->setDiffuseTexture( texture );
102 material->setDiffuseTextureScale( static_cast<float>( phongSettings->textureScale() ) );
103 material->setDiffuseTextureRotation( static_cast<float>( phongSettings->textureRotation() ) );
104
105 return material;
106 }
107
110 return nullptr;
111 }
112 return nullptr;
113}
114
116{
117 const QgsPhongTexturedMaterialSettings *phongSettings = dynamic_cast< const QgsPhongTexturedMaterialSettings * >( settings );
118 Q_ASSERT( phongSettings );
119
120 QMap<QString, QString> parameters;
121 parameters[u"Ka"_s] = u"%1 %2 %3"_s.arg( phongSettings->ambient().redF() ).arg( phongSettings->ambient().greenF() ).arg( phongSettings->ambient().blueF() );
122 parameters[u"Ks"_s] = u"%1 %2 %3"_s.arg( phongSettings->specular().redF() ).arg( phongSettings->specular().greenF() ).arg( phongSettings->specular().blueF() );
123 parameters[u"Ns"_s] = QString::number( phongSettings->shininess() );
124 return parameters;
125}
126
127void QgsPhongTexturedMaterial3DHandler::addParametersToEffect( Qt3DRender::QEffect *effect, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &materialContext ) const
128{
129 const QgsPhongTexturedMaterialSettings *phongSettings = dynamic_cast< const QgsPhongTexturedMaterialSettings * >( settings );
130 Q_ASSERT( phongSettings );
131
132 const QColor ambientColor = Qgs3DUtils::srgbToLinear( materialContext.isSelected() ? materialContext.selectionColor().darker() : phongSettings->ambient() );
133
134 Qt3DRender::QParameter *ambientParameter = new Qt3DRender::QParameter( u"ambientColor"_s, ambientColor );
135 Qt3DRender::QParameter *specularParameter = new Qt3DRender::QParameter( u"specularColor"_s, Qgs3DUtils::srgbToLinear( phongSettings->specular() ) );
136 Qt3DRender::QParameter *shininessParameter = new Qt3DRender::QParameter( u"shininess"_s, static_cast<float>( phongSettings->shininess() ) );
137
138 effect->addParameter( ambientParameter );
139 effect->addParameter( specularParameter );
140 effect->addParameter( shininessParameter );
141}
142
143bool QgsPhongTexturedMaterial3DHandler::updatePreviewScene( Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext & ) const
144{
145 const QgsPhongTexturedMaterialSettings *phongSettings = qgis::down_cast< const QgsPhongTexturedMaterialSettings * >( settings );
146
147 QgsMaterial *material = sceneRoot->findChild<QgsMaterial *>();
148 if ( material->objectName() != "phongTexturedMaterial"_L1 )
149 return false;
150
151 Qt3DRender::QEffect *effect = material->effect();
152
153 if ( Qt3DRender::QParameter *p = findParameter( effect, u"ambientColor"_s ) )
154 p->setValue( Qgs3DUtils::srgbToLinear( phongSettings->ambient() ) );
155
156 Qt3DRender::QTexture2D *texture = material->findChild<Qt3DRender::QTexture2D *>();
157 bool fitsInCache;
158 texture->addTextureImage( new QgsImageTexture( QgsApplication::imageCache()->pathAsImage( phongSettings->diffuseTexturePath(), QSize(), true, 1.0, fitsInCache ) ) );
159 texture->removeTextureImage( texture->textureImages().at( 0 ) );
160
161 if ( Qt3DRender::QParameter *p = findParameter( effect, u"texCoordScale"_s ) )
162 p->setValue( phongSettings->textureScale() );
163 if ( Qt3DRender::QParameter *p = findParameter( effect, u"texCoordRotation"_s ) )
164 p->setValue( phongSettings->textureRotation() );
165 if ( Qt3DRender::QParameter *p = findParameter( effect, u"specularColor"_s ) )
166 p->setValue( Qgs3DUtils::srgbToLinear( phongSettings->specular() ) );
167 if ( Qt3DRender::QParameter *p = findParameter( effect, u"shininess"_s ) )
168 p->setValue( phongSettings->shininess() );
169 if ( Qt3DRender::QParameter *p = findParameter( effect, u"opacity"_s ) )
170 p->setValue( phongSettings->opacity() );
171 return true;
172}
MaterialRenderingTechnique
Material rendering techniques.
Definition qgis.h:4342
@ Points
Point based rendering, requires point data.
Definition qgis.h:4346
@ Triangles
Triangle based rendering (default).
Definition qgis.h:4343
@ TrianglesFromModel
Triangle based rendering, using a model object source.
Definition qgis.h:4348
@ Lines
Line based rendering, requires line data.
Definition qgis.h:4344
@ Billboards
Flat billboard rendering.
Definition qgis.h:4350
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
Definition qgis.h:4349
@ InstancedPoints
Instanced based rendering, requiring triangles and point data.
Definition qgis.h:4345
@ TrianglesWithFixedTexture
Triangle based rendering, using a fixed, non-user-configurable texture (e.g. for terrain rendering).
Definition qgis.h:4347
static QColor srgbToLinear(const QColor &color)
Converts a SRGB color to a linear color.
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.