Skip to content
Permalink
Browse files

[3d][feature] Add option to show light source origins

If enabled, shows a sphere at light source origins, allowing easier repositioning
and placement of light sources relative to the scene contents

Fixes #37726
  • Loading branch information
nyalldawson committed Jul 15, 2020
1 parent b356d29 commit 573ace14bf04f7a73480c089c36692904dbc20a5
@@ -352,6 +352,21 @@ Returns whether to show camera's view center as a sphere (for debugging)

.. versionadded:: 3.4
%End

void setShowLightSourceOrigins( bool enabled );
%Docstring
Sets whether to show light source origins as a sphere (for debugging)

.. versionadded:: 3.16
%End

bool showLightSourceOrigins() const;
%Docstring
Returns whether to show light source origins as a sphere (for debugging)

.. versionadded:: 3.16
%End

void setShowLabels( bool enabled );
%Docstring
Sets whether to display labels on terrain tiles
@@ -491,6 +506,14 @@ Emitted when the flag whether camera's view center is shown has changed

.. versionadded:: 3.4
%End

void showLightSourceOriginsChanged();
%Docstring
Emitted when the flag whether light source origins are shown has changed.

.. versionadded:: 3.15
%End

void showLabelsChanged();
%Docstring
Emitted when the flag whether labels are displayed on terrain tiles has changed
@@ -93,6 +93,7 @@ Qgs3DMapScene::Qgs3DMapScene( const Qgs3DMapSettings &map, QgsAbstract3DEngine *
mCameraController->resetView( 1000 );

addCameraViewCenterEntity( mEngine->camera() );
updateLights();

// create terrain entity

@@ -105,6 +106,7 @@ Qgs3DMapScene::Qgs3DMapScene( const Qgs3DMapSettings &map, QgsAbstract3DEngine *
connect( &map, &Qgs3DMapSettings::terrainShadingChanged, this, &Qgs3DMapScene::createTerrain );
connect( &map, &Qgs3DMapSettings::pointLightsChanged, this, &Qgs3DMapScene::updateLights );
connect( &map, &Qgs3DMapSettings::directionalLightsChanged, this, &Qgs3DMapScene::updateLights );
connect( &map, &Qgs3DMapSettings::showLightSourceOriginsChanged, this, &Qgs3DMapScene::updateLights );
connect( &map, &Qgs3DMapSettings::fieldOfViewChanged, this, &Qgs3DMapScene::updateCameraLens );
connect( &map, &Qgs3DMapSettings::renderersChanged, this, &Qgs3DMapScene::onRenderersChanged );

@@ -115,7 +117,6 @@ Qgs3DMapScene::Qgs3DMapScene( const Qgs3DMapSettings &map, QgsAbstract3DEngine *
// listen to changes of layers in order to add/remove 3D renderer entities
connect( &map, &Qgs3DMapSettings::layersChanged, this, &Qgs3DMapScene::onLayersChanged );

updateLights();

#if 0
ChunkedEntity *testChunkEntity = new ChunkedEntity( AABB( -500, 0, -500, 500, 100, 500 ), 2.f, 3.f, 7, new TestChunkLoaderFactory );
@@ -453,6 +454,31 @@ void Qgs3DMapScene::updateLights()
for ( Qt3DCore::QEntity *entity : qgis::as_const( mLightEntities ) )
entity->deleteLater();
mLightEntities.clear();
for ( Qt3DCore::QEntity *entity : qgis::as_const( mLightOriginEntities ) )
entity->deleteLater();
mLightOriginEntities.clear();

auto createLightOriginEntity = [ = ]( QVector3D translation, const QColor & color )->Qt3DCore::QEntity *
{
Qt3DCore::QEntity *originEntity = new Qt3DCore::QEntity;

Qt3DCore::QTransform *trLightOriginCenter = new Qt3DCore::QTransform;
trLightOriginCenter->setTranslation( translation );
originEntity->addComponent( trLightOriginCenter );

Qt3DExtras::QPhongMaterial *materialLightOriginCenter = new Qt3DExtras::QPhongMaterial;
materialLightOriginCenter->setAmbient( color );
originEntity->addComponent( materialLightOriginCenter );

Qt3DExtras::QSphereMesh *rendererLightOriginCenter = new Qt3DExtras::QSphereMesh;
rendererLightOriginCenter->setRadius( 20 );
originEntity->addComponent( rendererLightOriginCenter );

originEntity->setEnabled( true );
originEntity->setParent( this );

return originEntity;
};

const auto newPointLights = mMap.pointLights();
for ( const QgsPointLightSettings &pointLightSettings : newPointLights )
@@ -475,6 +501,9 @@ void Qgs3DMapScene::updateLights()
lightEntity->addComponent( lightTransform );
lightEntity->setParent( this );
mLightEntities << lightEntity;

if ( mMap.showLightSourceOrigins() )
mLightOriginEntities << createLightOriginEntity( lightTransform->translation(), pointLightSettings.color() );
}

const auto newDirectionalLights = mMap.directionalLights();
@@ -162,6 +162,8 @@ class _3D_EXPORT Qgs3DMapScene : public Qt3DCore::QEntity
QList<Qgs3DMapScenePickHandler *> mPickHandlers;
//! List of lights in the scene
QList<Qt3DCore::QEntity *> mLightEntities;
//! List of light origins in the scene
QList<Qt3DCore::QEntity *> mLightOriginEntities;
};

#endif // QGS3DMAPSCENE_H
@@ -47,6 +47,7 @@ Qgs3DMapSettings::Qgs3DMapSettings( const Qgs3DMapSettings &other )
, mShowTerrainBoundingBoxes( other.mShowTerrainBoundingBoxes )
, mShowTerrainTileInfo( other.mShowTerrainTileInfo )
, mShowCameraViewCenter( other.mShowCameraViewCenter )
, mShowLightSources( other.mShowLightSources )
, mShowLabels( other.mShowLabels )
, mPointLights( other.mPointLights )
, mDirectionalLights( other.mDirectionalLights )
@@ -215,6 +216,7 @@ void Qgs3DMapSettings::readXml( const QDomElement &elem, const QgsReadWriteConte
mShowTerrainBoundingBoxes = elemDebug.attribute( QStringLiteral( "bounding-boxes" ), QStringLiteral( "0" ) ).toInt();
mShowTerrainTileInfo = elemDebug.attribute( QStringLiteral( "terrain-tile-info" ), QStringLiteral( "0" ) ).toInt();
mShowCameraViewCenter = elemDebug.attribute( QStringLiteral( "camera-view-center" ), QStringLiteral( "0" ) ).toInt();
mShowLightSources = elemDebug.attribute( QStringLiteral( "show-light-sources" ), QStringLiteral( "0" ) ).toInt();

QDomElement elemTemporalRange = elem.firstChildElement( QStringLiteral( "temporal-range" ) );
QDateTime start = QDateTime::fromString( elemTemporalRange.attribute( QStringLiteral( "start" ) ), Qt::ISODate );
@@ -308,6 +310,7 @@ QDomElement Qgs3DMapSettings::writeXml( QDomDocument &doc, const QgsReadWriteCon
elemDebug.setAttribute( QStringLiteral( "bounding-boxes" ), mShowTerrainBoundingBoxes ? 1 : 0 );
elemDebug.setAttribute( QStringLiteral( "terrain-tile-info" ), mShowTerrainTileInfo ? 1 : 0 );
elemDebug.setAttribute( QStringLiteral( "camera-view-center" ), mShowCameraViewCenter ? 1 : 0 );
elemDebug.setAttribute( QStringLiteral( "show-light-sources" ), mShowLightSources ? 1 : 0 );
elem.appendChild( elemDebug );

QDomElement elemTemporalRange = doc.createElement( QStringLiteral( "temporal-range" ) );
@@ -540,6 +543,15 @@ void Qgs3DMapSettings::setShowCameraViewCenter( bool enabled )
emit showCameraViewCenterChanged();
}

void Qgs3DMapSettings::setShowLightSourceOrigins( bool enabled )
{
if ( mShowLightSources == enabled )
return;

mShowLightSources = enabled;
emit showLightSourceOriginsChanged();
}

void Qgs3DMapSettings::setShowLabels( bool enabled )
{
if ( mShowLabels == enabled )
@@ -315,6 +315,19 @@ class _3D_EXPORT Qgs3DMapSettings : public QObject, public QgsTemporalRangeObjec
* \since QGIS 3.4
*/
bool showCameraViewCenter() const { return mShowCameraViewCenter; }

/**
* Sets whether to show light source origins as a sphere (for debugging)
* \since QGIS 3.16
*/
void setShowLightSourceOrigins( bool enabled );

/**
* Returns whether to show light source origins as a sphere (for debugging)
* \since QGIS 3.16
*/
bool showLightSourceOrigins() const { return mShowLightSources; }

//! Sets whether to display labels on terrain tiles
void setShowLabels( bool enabled );
//! Returns whether to display labels on terrain tiles
@@ -417,6 +430,13 @@ class _3D_EXPORT Qgs3DMapSettings : public QObject, public QgsTemporalRangeObjec
* \since QGIS 3.4
*/
void showCameraViewCenterChanged();

/**
* Emitted when the flag whether light source origins are shown has changed.
* \since QGIS 3.15
*/
void showLightSourceOriginsChanged();

//! Emitted when the flag whether labels are displayed on terrain tiles has changed
void showLabelsChanged();

@@ -460,6 +480,7 @@ class _3D_EXPORT Qgs3DMapSettings : public QObject, public QgsTemporalRangeObjec
bool mShowTerrainBoundingBoxes = false; //!< Whether to show bounding boxes of entities - useful for debugging
bool mShowTerrainTileInfo = false; //!< Whether to draw extra information about terrain tiles to the textures - useful for debugging
bool mShowCameraViewCenter = false; //!< Whether to show camera view center as a sphere - useful for debugging
bool mShowLightSources = false; //!< Whether to show the origin of light sources
bool mShowLabels = false; //!< Whether to display labels on terrain tiles
QList<QgsPointLightSettings> mPointLights; //!< List of point lights defined for the scene
QList<QgsDirectionalLightSettings> mDirectionalLights; //!< List of directional lights defined for the scene
@@ -102,6 +102,7 @@ Qgs3DMapConfigWidget::Qgs3DMapConfigWidget( Qgs3DMapSettings *map, QgsMapCanvas
chkShowTileInfo->setChecked( mMap->showTerrainTilesInfo() );
chkShowBoundingBoxes->setChecked( mMap->showTerrainBoundingBoxes() );
chkShowCameraViewCenter->setChecked( mMap->showCameraViewCenter() );
chkShowLightSourceOrigins->setChecked( mMap->showLightSourceOrigins() );

groupTerrainShading->setChecked( mMap->isTerrainShadingEnabled() );
widgetTerrainMaterial->setDiffuseVisible( false );
@@ -222,7 +223,7 @@ void Qgs3DMapConfigWidget::apply()
mMap->setShowTerrainTilesInfo( chkShowTileInfo->isChecked() );
mMap->setShowTerrainBoundingBoxes( chkShowBoundingBoxes->isChecked() );
mMap->setShowCameraViewCenter( chkShowCameraViewCenter->isChecked() );

mMap->setShowLightSourceOrigins( chkShowLightSourceOrigins->isChecked() );
mMap->setTerrainShadingEnabled( groupTerrainShading->isChecked() );
mMap->setTerrainShadingMaterial( widgetTerrainMaterial->material() );

0 comments on commit 573ace1

Please sign in to comment.
You can’t perform that action at this time.