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