QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgspointlightsettings.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspointlightsettings.cpp
3  --------------------------------------
4  Date : November 2018
5  Copyright : (C) 2018 by Martin Dobias
6  Email : wonder dot sk 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 
16 #include "qgspointlightsettings.h"
17 #include "qgssymbollayerutils.h"
18 #include "qgs3dmapsettings.h"
19 
20 #include <QDomDocument>
21 
22 #include <Qt3DCore/QEntity>
23 #include <Qt3DRender/QPointLight>
24 #include <Qt3DExtras/QPhongMaterial>
25 #include <Qt3DExtras/QSphereMesh>
26 
28 {
30 }
31 
33 {
34  return new QgsPointLightSettings( *this );
35 }
36 
37 Qt3DCore::QEntity *QgsPointLightSettings::createEntity( const Qgs3DMapSettings &map, Qt3DCore::QEntity *parent ) const
38 {
39  Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity();
40  Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform;
41  lightTransform->setTranslation( QVector3D( position().x(),
42  position().y(),
43  position().z() ) );
44 
45  Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight;
46  light->setColor( color() );
47  light->setIntensity( intensity() );
48 
49  light->setConstantAttenuation( constantAttenuation() );
50  light->setLinearAttenuation( linearAttenuation() );
51  light->setQuadraticAttenuation( quadraticAttenuation() );
52 
53  lightEntity->addComponent( light );
54  lightEntity->addComponent( lightTransform );
55 
56  if ( !map.showLightSourceOrigins() )
57  {
58  lightEntity->setParent( parent );
59  return lightEntity;
60  }
61  else
62  {
63  Qt3DCore::QEntity *originEntity = new Qt3DCore::QEntity();
64 
65  Qt3DCore::QTransform *trLightOriginCenter = new Qt3DCore::QTransform;
66  trLightOriginCenter->setTranslation( lightTransform->translation() );
67  originEntity->addComponent( trLightOriginCenter );
68 
69  Qt3DExtras::QPhongMaterial *materialLightOriginCenter = new Qt3DExtras::QPhongMaterial;
70  materialLightOriginCenter->setAmbient( color() );
71  originEntity->addComponent( materialLightOriginCenter );
72 
73  Qt3DExtras::QSphereMesh *rendererLightOriginCenter = new Qt3DExtras::QSphereMesh;
74  rendererLightOriginCenter->setRadius( 20 );
75  originEntity->addComponent( rendererLightOriginCenter );
76 
77  originEntity->setEnabled( true );
78 
79  Qt3DCore::QEntity *groupEntity = new Qt3DCore::QEntity( parent );
80  lightEntity->setParent( groupEntity );
81  originEntity->setParent( groupEntity );
82  groupEntity->setEnabled( true );
83  return groupEntity;
84  }
85 }
86 
87 QDomElement QgsPointLightSettings::writeXml( QDomDocument &doc, const QgsReadWriteContext & ) const
88 {
89  QDomElement elemLight = doc.createElement( QStringLiteral( "point-light" ) );
90  elemLight.setAttribute( QStringLiteral( "x" ), mPosition.x() );
91  elemLight.setAttribute( QStringLiteral( "y" ), mPosition.y() );
92  elemLight.setAttribute( QStringLiteral( "z" ), mPosition.z() );
93  elemLight.setAttribute( QStringLiteral( "color" ), QgsSymbolLayerUtils::encodeColor( mColor ) );
94  elemLight.setAttribute( QStringLiteral( "intensity" ), mIntensity );
95  elemLight.setAttribute( QStringLiteral( "attenuation-0" ), mConstantAttenuation );
96  elemLight.setAttribute( QStringLiteral( "attenuation-1" ), mLinearAttenuation );
97  elemLight.setAttribute( QStringLiteral( "attenuation-2" ), mQuadraticAttenuation );
98  return elemLight;
99 }
100 
101 void QgsPointLightSettings::readXml( const QDomElement &elem, const QgsReadWriteContext & )
102 {
103  mPosition.set( elem.attribute( QStringLiteral( "x" ) ).toDouble(),
104  elem.attribute( QStringLiteral( "y" ) ).toDouble(),
105  elem.attribute( QStringLiteral( "z" ) ).toDouble() );
106  mColor = QgsSymbolLayerUtils::decodeColor( elem.attribute( QStringLiteral( "color" ) ) );
107  mIntensity = elem.attribute( QStringLiteral( "intensity" ) ).toFloat();
108  mConstantAttenuation = elem.attribute( QStringLiteral( "attenuation-0" ) ).toDouble();
109  mLinearAttenuation = elem.attribute( QStringLiteral( "attenuation-1" ) ).toDouble();
110  mQuadraticAttenuation = elem.attribute( QStringLiteral( "attenuation-2" ) ).toDouble();
111 }
112 
114 {
115  return mPosition == other.mPosition && mColor == other.mColor && mIntensity == other.mIntensity &&
116  mConstantAttenuation == other.mConstantAttenuation && mLinearAttenuation == other.mLinearAttenuation &&
117  mQuadraticAttenuation == other.mQuadraticAttenuation;
118 }
QgsPointLightSettings::constantAttenuation
float constantAttenuation() const
Returns constant attenuation (A_0)
Definition: qgspointlightsettings.h:67
Qgs3DMapSettings::showLightSourceOrigins
bool showLightSourceOrigins() const
Returns whether to show light source origins as a sphere (for debugging)
Definition: qgs3dmapsettings.h:378
QgsSymbolLayerUtils::encodeColor
static QString encodeColor(const QColor &color)
Definition: qgssymbollayerutils.cpp:64
QgsPointLightSettings::position
QgsVector3D position() const
Returns position of the light (in 3D world coordinates)
Definition: qgspointlightsettings.h:52
QgsPointLightSettings::type
Qgis::LightSourceType type() const override
Returns the light source type.
Definition: qgspointlightsettings.cpp:27
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsVector3D::y
double y() const
Returns Y coordinate.
Definition: qgsvector3d.h:64
QgsPointLightSettings::color
QColor color() const
Returns color of the light.
Definition: qgspointlightsettings.h:57
QgsPointLightSettings::writeXml
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context=QgsReadWriteContext()) const override
Writes the light source's configuration to a new DOM element and returns it.
Definition: qgspointlightsettings.cpp:87
qgssymbollayerutils.h
Qgis::LightSourceType::Point
@ Point
Point light source.
QgsPointLightSettings::createEntity
Qt3DCore::QEntity * createEntity(const Qgs3DMapSettings &map, Qt3DCore::QEntity *parent) const override
Creates an entity representing the light source.
Definition: qgspointlightsettings.cpp:37
QgsVector3D::set
void set(double x, double y, double z)
Sets vector coordinates.
Definition: qgsvector3d.h:69
QgsSymbolLayerUtils::decodeColor
static QColor decodeColor(const QString &str)
Definition: qgssymbollayerutils.cpp:69
Qgis::LightSourceType
LightSourceType
Light source types for 3D scenes.
Definition: qgis.h:1837
qgspointlightsettings.h
QgsPointLightSettings::QgsPointLightSettings
QgsPointLightSettings()=default
Construct a point light with default values.
QgsVector3D::z
double z() const
Returns Z coordinate.
Definition: qgsvector3d.h:66
QgsPointLightSettings
Definition of a point light in a 3D map scene.
Definition: qgspointlightsettings.h:39
QgsPointLightSettings::quadraticAttenuation
float quadraticAttenuation() const
Returns quadratic attenuation (A_2)
Definition: qgspointlightsettings.h:77
Qgs3DMapSettings
Definition of the world.
Definition: qgs3dmapsettings.h:57
QgsPointLightSettings::intensity
float intensity() const
Returns intensity of the light.
Definition: qgspointlightsettings.h:62
qgs3dmapsettings.h
QgsPointLightSettings::linearAttenuation
float linearAttenuation() const
Returns linear attenuation (A_1)
Definition: qgspointlightsettings.h:72
QgsPointLightSettings::operator==
bool operator==(const QgsPointLightSettings &other)
Definition: qgspointlightsettings.cpp:113
QgsVector3D::x
double x() const
Returns X coordinate.
Definition: qgsvector3d.h:62
QgsPointLightSettings::readXml
void readXml(const QDomElement &elem, const QgsReadWriteContext &context=QgsReadWriteContext()) override
Reads configuration from a DOM element previously written using writeXml().
Definition: qgspointlightsettings.cpp:101
QgsPointLightSettings::clone
QgsPointLightSettings * clone() const override
Returns a copy of the light source.
Definition: qgspointlightsettings.cpp:32