QGIS API Documentation 4.1.0-Master (ca2ac17535b)
Loading...
Searching...
No Matches
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
17
18#include "qgs3dmapsettings.h"
19#include "qgs3dutils.h"
20#include "qgscolorutils.h"
21#include "qgsgeotransform.h"
22#include "qgssymbollayerutils.h"
23
24#include <QDomDocument>
25#include <QString>
26#include <Qt3DCore/QEntity>
27#include <Qt3DExtras/QPhongMaterial>
28#include <Qt3DExtras/QSphereMesh>
29#include <Qt3DRender/QPointLight>
30
31using namespace Qt::StringLiterals;
32
37
39{
40 auto res = std::make_unique< QgsPointLightSettings >( *this );
41 res->mId = mId;
42 return res.release();
43}
44
45Qt3DCore::QEntity *QgsPointLightSettings::createEntity( const Qgs3DMapSettings &map, Qt3DCore::QEntity *parent ) const
46{
47 Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity();
48 QgsGeoTransform *lightTransform = new QgsGeoTransform;
49 lightTransform->setOrigin( map.origin() );
50 lightTransform->setGeoTranslation( position().toVector3D() );
51
52 Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight;
53 light->setColor( Qgs3DUtils::srgbToLinear( color() ) );
54 light->setIntensity( static_cast< float >( intensity() ) );
55
56 light->setConstantAttenuation( static_cast< float >( constantAttenuation() ) );
57 light->setLinearAttenuation( static_cast< float >( linearAttenuation() ) );
58 light->setQuadraticAttenuation( static_cast< float >( quadraticAttenuation() ) );
59
60 lightEntity->addComponent( light );
61 lightEntity->addComponent( lightTransform );
62
63 if ( !map.showLightSourceOrigins() )
64 {
65 lightEntity->setParent( parent );
66 return lightEntity;
67 }
68 else
69 {
70 Qt3DCore::QEntity *originEntity = new Qt3DCore::QEntity();
71
72 QgsGeoTransform *originTransform = new QgsGeoTransform;
73 originTransform->setOrigin( map.origin() );
74 originTransform->setGeoTranslation( position().toVector3D() );
75 originEntity->addComponent( originTransform );
76
77 Qt3DExtras::QPhongMaterial *materialLightOriginCenter = new Qt3DExtras::QPhongMaterial;
78 materialLightOriginCenter->setAmbient( color() );
79 originEntity->addComponent( materialLightOriginCenter );
80
81 Qt3DExtras::QSphereMesh *rendererLightOriginCenter = new Qt3DExtras::QSphereMesh;
82 rendererLightOriginCenter->setRadius( 20 );
83 originEntity->addComponent( rendererLightOriginCenter );
84
85 originEntity->setEnabled( true );
86
87 Qt3DCore::QEntity *groupEntity = new Qt3DCore::QEntity( parent );
88 lightEntity->setParent( groupEntity );
89 originEntity->setParent( groupEntity );
90 groupEntity->setEnabled( true );
91 return groupEntity;
92 }
93}
94
95QDomElement QgsPointLightSettings::writeXml( QDomDocument &doc, const QgsReadWriteContext & ) const
96{
97 QDomElement elemLight = doc.createElement( u"point-light"_s );
98 elemLight.setAttribute( u"id"_s, mId );
99 elemLight.setAttribute( u"x"_s, mPosition.x() );
100 elemLight.setAttribute( u"y"_s, mPosition.y() );
101 elemLight.setAttribute( u"z"_s, mPosition.z() );
102 elemLight.setAttribute( u"color"_s, QgsColorUtils::colorToString( mColor ) );
103 elemLight.setAttribute( u"intensity"_s, mIntensity );
104 elemLight.setAttribute( u"attenuation-0"_s, mConstantAttenuation );
105 elemLight.setAttribute( u"attenuation-1"_s, mLinearAttenuation );
106 elemLight.setAttribute( u"attenuation-2"_s, mQuadraticAttenuation );
107 return elemLight;
108}
109
110void QgsPointLightSettings::readXml( const QDomElement &elem, const QgsReadWriteContext & )
111{
112 if ( elem.hasAttribute( u"id"_s ) )
113 mId = elem.attribute( u"id"_s );
114
115 mPosition.set( elem.attribute( u"x"_s ).toDouble(), elem.attribute( u"y"_s ).toDouble(), elem.attribute( u"z"_s ).toDouble() );
116 mColor = QgsColorUtils::colorFromString( elem.attribute( u"color"_s ) );
117 mIntensity = elem.attribute( u"intensity"_s ).toDouble();
118 mConstantAttenuation = elem.attribute( u"attenuation-0"_s ).toDouble();
119 mLinearAttenuation = elem.attribute( u"attenuation-1"_s ).toDouble();
120 mQuadraticAttenuation = elem.attribute( u"attenuation-2"_s ).toDouble();
121}
122
124{
125 return mId == other.mId
126 && mPosition == other.mPosition
127 && mColor == other.mColor
128 && qgsDoubleNear( mIntensity, other.mIntensity )
129 && qgsDoubleNear( mConstantAttenuation, other.mConstantAttenuation )
130 && qgsDoubleNear( mLinearAttenuation, other.mLinearAttenuation )
131 && qgsDoubleNear( mQuadraticAttenuation, other.mQuadraticAttenuation );
132}
LightSourceType
Light source types for 3D scenes.
Definition qgis.h:4404
@ Point
Point light source.
Definition qgis.h:4405
Definition of the world.
bool showLightSourceOrigins() const
Returns whether to show light source origins as a sphere (for debugging).
QgsVector3D origin() const
Returns coordinates in map CRS at which 3D scene has origin (0,0,0).
static QColor srgbToLinear(const QColor &color)
Converts a SRGB color to a linear color.
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
QString mId
Unique light ID.
QgsPointLightSettings()=default
Construct a point light with default values.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context=QgsReadWriteContext()) const override
Writes the light source's configuration to a new DOM element and returns it.
Qt3DCore::QEntity * createEntity(const Qgs3DMapSettings &map, Qt3DCore::QEntity *parent) const override
Creates an entity representing the light source.
double linearAttenuation() const
Returns linear attenuation (A_1).
bool operator==(const QgsPointLightSettings &other) const
QgsPointLightSettings * clone() const override
Returns a copy of the light source.
double quadraticAttenuation() const
Returns quadratic attenuation (A_2).
Qgis::LightSourceType type() const override
Returns the light source type.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context=QgsReadWriteContext()) override
Reads configuration from a DOM element previously written using writeXml().
QgsVector3D position() const
Returns position of the light (in 3D map coordinates).
double intensity() const
Returns intensity of the light.
double constantAttenuation() const
Returns constant attenuation (A_0).
QColor color() const
Returns color of the light.
A container for the context for various read/write operations on objects.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:7236