Skip to content

Commit

Permalink
Add option to update camera through debug panel
Browse files Browse the repository at this point in the history
  • Loading branch information
Withalion committed Nov 19, 2024
1 parent dae9bdd commit c759ce2
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 15 deletions.
93 changes: 79 additions & 14 deletions src/app/3d/qgs3ddebugwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,21 @@ Qgs3DDebugWidget::Qgs3DDebugWidget( Qgs3DMapCanvas *canvas, QWidget *parent ) :
mLookingX->setRange( std::numeric_limits<float>::lowest(), std::numeric_limits<float>::max() );
mLookingY->setRange( std::numeric_limits<float>::lowest(), std::numeric_limits<float>::max() );
mLookingZ->setRange( std::numeric_limits<float>::lowest(), std::numeric_limits<float>::max() );
connect( mNearPlane, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, &Qgs3DDebugWidget::castCameraInputValue );
connect( mFarPlane, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, &Qgs3DDebugWidget::castCameraInputValue );
connect( mCameraX, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, &Qgs3DDebugWidget::castCameraInputValue );
connect( mCameraY, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, &Qgs3DDebugWidget::castCameraInputValue );
connect( mCameraZ, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, &Qgs3DDebugWidget::castCameraInputValue );
connect( mLookingX, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, &Qgs3DDebugWidget::castCameraInputValue );
connect( mLookingY, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, &Qgs3DDebugWidget::castCameraInputValue );
connect( mLookingZ, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, &Qgs3DDebugWidget::castCameraInputValue );
}

/**
* Sets up the interactive elements with values from Qgs3DMapSettings
*/
void Qgs3DDebugWidget::setMapSettings( Qgs3DMapSettings *mapSettings )
{
mMap = mapSettings;

// set up the checkbox block
whileBlocking( chkShowTileInfo )->setChecked( mMap->showTerrainTilesInfo() );
whileBlocking( chkShowBoundingBoxes )->setChecked( mMap->showTerrainBoundingBoxes() );
whileBlocking( chkShowCameraViewCenter )->setChecked( mMap->showCameraViewCenter() );
Expand All @@ -80,6 +87,7 @@ void Qgs3DDebugWidget::setMapSettings( Qgs3DMapSettings *mapSettings )
connect( chkStopUpdates, &QCheckBox::toggled, this, [ = ]( const bool enabled ) {mMap->setStopUpdates( enabled ); } );
connect( chkDebugOverlay, &QCheckBox::toggled, this, [ = ]( const bool enabled ) {mMap->setIsDebugOverlayEnabled( enabled ); } );

// set up the shadow map block
whileBlocking( mDebugShadowMapGroupBox )->setChecked( mMap->debugShadowMapEnabled() );
whileBlocking( mDebugShadowMapCornerComboBox )->setCurrentIndex( mMap->debugShadowMapCorner() );
whileBlocking( mDebugShadowMapSizeSpinBox )->setValue( mMap->debugShadowMapSize() );
Expand All @@ -97,6 +105,7 @@ void Qgs3DDebugWidget::setMapSettings( Qgs3DMapSettings *mapSettings )
mMap->setDebugShadowMapSettings( mDebugShadowMapGroupBox->isChecked() && mMap->shadowSettings().renderShadows(), static_cast<Qt::Corner>( mDebugShadowMapCornerComboBox->currentIndex() ), value );
} );

// set up the depth map block
whileBlocking( mDebugDepthMapGroupBox )->setChecked( mMap->debugDepthMapEnabled() );
whileBlocking( mDebugDepthMapCornerComboBox )->setCurrentIndex( mMap->debugDepthMapCorner() );
whileBlocking( mDebugDepthMapSizeSpinBox )->setValue( mMap->debugDepthMapSize() );
Expand All @@ -112,19 +121,75 @@ void Qgs3DDebugWidget::setMapSettings( Qgs3DMapSettings *mapSettings )
{
mMap->setDebugDepthMapSettings( mDebugDepthMapGroupBox->isChecked(), static_cast<Qt::Corner>( mDebugDepthMapCornerComboBox->currentIndex() ), value );
} );

// connect signals emitted by castCameraInputValue
connect( this, &Qgs3DDebugWidget::nearPlaneChanged, m3DMapCanvas->cameraController()->camera(), &Qt3DRender::QCamera::setNearPlane );
connect( this, &Qgs3DDebugWidget::farPlaneChanged, m3DMapCanvas->cameraController()->camera(), &Qt3DRender::QCamera::setFarPlane );
connect( this, &Qgs3DDebugWidget::positionChanged, m3DMapCanvas->cameraController()->camera(), &Qt3DRender::QCamera::setPosition );
}

/**
* Update the state of navigation widget from camera's state
*/
void Qgs3DDebugWidget::updateFromCamera() const
{
mNearPlane->setValue( m3DMapCanvas->cameraController()->camera()->nearPlane() );
mFarPlane->setValue( m3DMapCanvas->cameraController()->camera()->farPlane() );
mCameraX->setValue( m3DMapCanvas->cameraController()->camera()->position().x() );
mCameraY->setValue( m3DMapCanvas->cameraController()->camera()->position().y() );
mCameraZ->setValue( m3DMapCanvas->cameraController()->camera()->position().z() );
mLookingX->setValue( m3DMapCanvas->cameraController()->lookingAtPoint().x() );
mLookingY->setValue( m3DMapCanvas->cameraController()->lookingAtPoint().y() );
mLookingZ->setValue( m3DMapCanvas->cameraController()->lookingAtPoint().z() );
whileBlocking( mNearPlane )->setValue( m3DMapCanvas->cameraController()->camera()->nearPlane() );
whileBlocking( mFarPlane )->setValue( m3DMapCanvas->cameraController()->camera()->farPlane() );
whileBlocking( mCameraX )->setValue( m3DMapCanvas->cameraController()->camera()->position().x() );
whileBlocking( mCameraY )->setValue( m3DMapCanvas->cameraController()->camera()->position().y() );
whileBlocking( mCameraZ )->setValue( m3DMapCanvas->cameraController()->camera()->position().z() );
whileBlocking( mLookingX )->setValue( m3DMapCanvas->cameraController()->lookingAtPoint().x() );
whileBlocking( mLookingY )->setValue( m3DMapCanvas->cameraController()->lookingAtPoint().y() );
whileBlocking( mLookingZ )->setValue( m3DMapCanvas->cameraController()->lookingAtPoint().z() );
}

void Qgs3DDebugWidget::castCameraInputValue( const double value )
{
if ( sender() == mCameraX || sender() == mCameraY || sender() == mCameraZ )
{
auto *newPosition = new QVector3D( m3DMapCanvas->cameraController()->camera()->position().x(), m3DMapCanvas->cameraController()->camera()->position().y(), m3DMapCanvas->cameraController()->camera()->position().z() );
if ( sender() == mCameraX )
{
newPosition->setX( static_cast<float>( value ) );
emit positionChanged( *newPosition );
}
else if ( sender() == mCameraY )
{
newPosition->setY( static_cast<float>( value ) );
emit positionChanged( *newPosition );
}
else if ( sender() == mCameraZ )
{
newPosition->setZ( static_cast<float>( value ) );
emit positionChanged( *newPosition );
}
}
else if ( sender() == mLookingX || sender() == mLookingY || sender() == mLookingZ )
{
const float distance = m3DMapCanvas->cameraController()->distance();
const float pitch = m3DMapCanvas->cameraController()->pitch();
const float yaw = m3DMapCanvas->cameraController()->yaw();
QgsVector3D newLookingAt =
m3DMapCanvas->cameraController()->lookingAtPoint();
if ( sender() == mLookingX )
{
newLookingAt.setX( value );
m3DMapCanvas->cameraController()->setLookingAtPoint( newLookingAt, distance, pitch, yaw );
}
else if ( sender() == mLookingY )
{
newLookingAt.setY( value );
m3DMapCanvas->cameraController()->setLookingAtPoint( newLookingAt, distance, pitch, yaw );
}
else if ( sender() == mLookingZ )
{
newLookingAt.setZ( value );
m3DMapCanvas->cameraController()->setLookingAtPoint( newLookingAt, distance, pitch, yaw );
}
}
else if ( sender() == mNearPlane )
{
emit nearPlaneChanged( static_cast<float>( value ) );
}
else if ( sender() == mFarPlane )
{
emit farPlaneChanged( static_cast<float>( value ) );
}
}
32 changes: 32 additions & 0 deletions src/app/3d/qgs3ddebugwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,43 @@ class Qgs3DDebugWidget : public QWidget, Ui::Q3DDebugWidget
public:
explicit Qgs3DDebugWidget( Qgs3DMapCanvas *canvas, QWidget *parent = nullptr );

/**
* Sets the map settings and necessary connections based on Qgs3DMapSettings.
* \note We need separate function as Qgs3DMapCanvasWidget, which wraps this widget, also gets it later.
*/
void setMapSettings( Qgs3DMapSettings *mapSettings );

signals:

/**
* Emitted when the near plane value is changed by user in the QDoubleSpinBox widget.
*/
void nearPlaneChanged( float value );

/**
* Emitted when the far plane value is changed by user in the QDoubleSpinBox widget.
*/
void farPlaneChanged( float value );

/**
* Emitted when the camera position value is changed by user in the QDoubleSpinBox widget.
*/
void positionChanged( const QVector3D &position );

public slots:

/**
* Function updates the camera info values when the user moves in the scene.
*/
void updateFromCamera() const;

private slots:

/**
* Necessary function which casts the new camera info value from signals and either emits new signals or changes the values directly.
*/
void castCameraInputValue( double value );

private:
Qgs3DMapSettings *mMap = nullptr;
Qgs3DMapCanvas *m3DMapCanvas = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/app/3d/qgs3dmapcanvaswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,10 @@ void Qgs3DMapCanvasWidget::setMapSettings( Qgs3DMapSettings *map )
whileBlocking( mActionSync3DNavTo2D )->setChecked( map->viewSyncMode().testFlag( Qgis::ViewSyncModeFlag::Sync3DTo2D ) );
whileBlocking( mShowFrustumPolyogon )->setChecked( map->viewFrustumVisualizationEnabled() );

mCanvas->setMapSettings( map );
connect( map, &Qgs3DMapSettings::showDebugPanelChanged, this, qOverload<bool>( &Qgs3DMapCanvasWidget::toggleDebugWidget ) );
toggleDebugWidget( map->showDebugPanel() );
mDebugWidget->setMapSettings( map );
mCanvas->setMapSettings( map );

connect( mCanvas->scene(), &Qgs3DMapScene::totalPendingJobsCountChanged, this, &Qgs3DMapCanvasWidget::onTotalPendingJobsCountChanged );
connect( mCanvas->scene(), &Qgs3DMapScene::gpuMemoryLimitReached, this, &Qgs3DMapCanvasWidget::onGpuMemoryLimitReached );
Expand Down

0 comments on commit c759ce2

Please sign in to comment.