QGIS API Documentation 3.99.0-Master (d270888f95f)
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 "qgscolorutils.h"
20#include "qgsgeotransform.h"
21#include "qgssymbollayerutils.h"
22
23#include <QDomDocument>
24#include <QString>
25#include <Qt3DCore/QEntity>
26#include <Qt3DExtras/QPhongMaterial>
27#include <Qt3DExtras/QSphereMesh>
28#include <Qt3DRender/QPointLight>
29
30using namespace Qt::StringLiterals;
31
36
41
42Qt3DCore::QEntity *QgsPointLightSettings::createEntity( const Qgs3DMapSettings &map, Qt3DCore::QEntity *parent ) const
43{
44 Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity();
45 QgsGeoTransform *lightTransform = new QgsGeoTransform;
46 lightTransform->setOrigin( map.origin() );
47 lightTransform->setGeoTranslation( position().toVector3D() );
48
49 Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight;
50 light->setColor( color() );
51 light->setIntensity( intensity() );
52
53 light->setConstantAttenuation( constantAttenuation() );
54 light->setLinearAttenuation( linearAttenuation() );
55 light->setQuadraticAttenuation( quadraticAttenuation() );
56
57 lightEntity->addComponent( light );
58 lightEntity->addComponent( lightTransform );
59
60 if ( !map.showLightSourceOrigins() )
61 {
62 lightEntity->setParent( parent );
63 return lightEntity;
64 }
65 else
66 {
67 Qt3DCore::QEntity *originEntity = new Qt3DCore::QEntity();
68
69 QgsGeoTransform *originTransform = new QgsGeoTransform;
70 originTransform->setOrigin( map.origin() );
71 originTransform->setGeoTranslation( position().toVector3D() );
72 originEntity->addComponent( originTransform );
73
74 Qt3DExtras::QPhongMaterial *materialLightOriginCenter = new Qt3DExtras::QPhongMaterial;
75 materialLightOriginCenter->setAmbient( color() );
76 originEntity->addComponent( materialLightOriginCenter );
77
78 Qt3DExtras::QSphereMesh *rendererLightOriginCenter = new Qt3DExtras::QSphereMesh;
79 rendererLightOriginCenter->setRadius( 20 );
80 originEntity->addComponent( rendererLightOriginCenter );
81
82 originEntity->setEnabled( true );
83
84 Qt3DCore::QEntity *groupEntity = new Qt3DCore::QEntity( parent );
85 lightEntity->setParent( groupEntity );
86 originEntity->setParent( groupEntity );
87 groupEntity->setEnabled( true );
88 return groupEntity;
89 }
90}
91
92QDomElement QgsPointLightSettings::writeXml( QDomDocument &doc, const QgsReadWriteContext & ) const
93{
94 QDomElement elemLight = doc.createElement( u"point-light"_s );
95 elemLight.setAttribute( u"x"_s, mPosition.x() );
96 elemLight.setAttribute( u"y"_s, mPosition.y() );
97 elemLight.setAttribute( u"z"_s, mPosition.z() );
98 elemLight.setAttribute( u"color"_s, QgsColorUtils::colorToString( mColor ) );
99 elemLight.setAttribute( u"intensity"_s, mIntensity );
100 elemLight.setAttribute( u"attenuation-0"_s, mConstantAttenuation );
101 elemLight.setAttribute( u"attenuation-1"_s, mLinearAttenuation );
102 elemLight.setAttribute( u"attenuation-2"_s, mQuadraticAttenuation );
103 return elemLight;
104}
105
106void QgsPointLightSettings::readXml( const QDomElement &elem, const QgsReadWriteContext & )
107{
108 mPosition.set( elem.attribute( u"x"_s ).toDouble(), elem.attribute( u"y"_s ).toDouble(), elem.attribute( u"z"_s ).toDouble() );
109 mColor = QgsColorUtils::colorFromString( elem.attribute( u"color"_s ) );
110 mIntensity = elem.attribute( u"intensity"_s ).toFloat();
111 mConstantAttenuation = elem.attribute( u"attenuation-0"_s ).toDouble();
112 mLinearAttenuation = elem.attribute( u"attenuation-1"_s ).toDouble();
113 mQuadraticAttenuation = elem.attribute( u"attenuation-2"_s ).toDouble();
114}
115
117{
118 return mPosition == other.mPosition && mColor == other.mColor && mIntensity == other.mIntensity && mConstantAttenuation == other.mConstantAttenuation && mLinearAttenuation == other.mLinearAttenuation && mQuadraticAttenuation == other.mQuadraticAttenuation;
119}
LightSourceType
Light source types for 3D scenes.
Definition qgis.h:4202
@ Point
Point light source.
Definition qgis.h:4203
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 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.