QGIS API Documentation 4.1.0-Master (01362494303)
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
42
43Qt3DCore::QEntity *QgsPointLightSettings::createEntity( const Qgs3DMapSettings &map, Qt3DCore::QEntity *parent ) const
44{
45 Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity();
46 QgsGeoTransform *lightTransform = new QgsGeoTransform;
47 lightTransform->setOrigin( map.origin() );
48 lightTransform->setGeoTranslation( position().toVector3D() );
49
50 Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight;
51 light->setColor( Qgs3DUtils::srgbToLinear( color() ) );
52 light->setIntensity( intensity() );
53
54 light->setConstantAttenuation( constantAttenuation() );
55 light->setLinearAttenuation( linearAttenuation() );
56 light->setQuadraticAttenuation( quadraticAttenuation() );
57
58 lightEntity->addComponent( light );
59 lightEntity->addComponent( lightTransform );
60
61 if ( !map.showLightSourceOrigins() )
62 {
63 lightEntity->setParent( parent );
64 return lightEntity;
65 }
66 else
67 {
68 Qt3DCore::QEntity *originEntity = new Qt3DCore::QEntity();
69
70 QgsGeoTransform *originTransform = new QgsGeoTransform;
71 originTransform->setOrigin( map.origin() );
72 originTransform->setGeoTranslation( position().toVector3D() );
73 originEntity->addComponent( originTransform );
74
75 Qt3DExtras::QPhongMaterial *materialLightOriginCenter = new Qt3DExtras::QPhongMaterial;
76 materialLightOriginCenter->setAmbient( color() );
77 originEntity->addComponent( materialLightOriginCenter );
78
79 Qt3DExtras::QSphereMesh *rendererLightOriginCenter = new Qt3DExtras::QSphereMesh;
80 rendererLightOriginCenter->setRadius( 20 );
81 originEntity->addComponent( rendererLightOriginCenter );
82
83 originEntity->setEnabled( true );
84
85 Qt3DCore::QEntity *groupEntity = new Qt3DCore::QEntity( parent );
86 lightEntity->setParent( groupEntity );
87 originEntity->setParent( groupEntity );
88 groupEntity->setEnabled( true );
89 return groupEntity;
90 }
91}
92
93QDomElement QgsPointLightSettings::writeXml( QDomDocument &doc, const QgsReadWriteContext & ) const
94{
95 QDomElement elemLight = doc.createElement( u"point-light"_s );
96 elemLight.setAttribute( u"x"_s, mPosition.x() );
97 elemLight.setAttribute( u"y"_s, mPosition.y() );
98 elemLight.setAttribute( u"z"_s, mPosition.z() );
99 elemLight.setAttribute( u"color"_s, QgsColorUtils::colorToString( mColor ) );
100 elemLight.setAttribute( u"intensity"_s, mIntensity );
101 elemLight.setAttribute( u"attenuation-0"_s, mConstantAttenuation );
102 elemLight.setAttribute( u"attenuation-1"_s, mLinearAttenuation );
103 elemLight.setAttribute( u"attenuation-2"_s, mQuadraticAttenuation );
104 return elemLight;
105}
106
107void QgsPointLightSettings::readXml( const QDomElement &elem, const QgsReadWriteContext & )
108{
109 mPosition.set( elem.attribute( u"x"_s ).toDouble(), elem.attribute( u"y"_s ).toDouble(), elem.attribute( u"z"_s ).toDouble() );
110 mColor = QgsColorUtils::colorFromString( elem.attribute( u"color"_s ) );
111 mIntensity = elem.attribute( u"intensity"_s ).toFloat();
112 mConstantAttenuation = elem.attribute( u"attenuation-0"_s ).toDouble();
113 mLinearAttenuation = elem.attribute( u"attenuation-1"_s ).toDouble();
114 mQuadraticAttenuation = elem.attribute( u"attenuation-2"_s ).toDouble();
115}
116
118{
119 return mPosition == other.mPosition
120 && mColor == other.mColor
121 && mIntensity == other.mIntensity
122 && mConstantAttenuation == other.mConstantAttenuation
123 && mLinearAttenuation == other.mLinearAttenuation
124 && mQuadraticAttenuation == other.mQuadraticAttenuation;
125}
LightSourceType
Light source types for 3D scenes.
Definition qgis.h:4360
@ Point
Point light source.
Definition qgis.h:4361
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.
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.
bool operator==(const QgsPointLightSettings &other) const
float quadraticAttenuation() const
Returns quadratic attenuation (A_2).
QgsPointLightSettings * clone() const override
Returns a copy of the light source.
float linearAttenuation() const
Returns linear attenuation (A_1).
float intensity() const
Returns intensity of the light.
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).
float 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.