Skip to content

Commit b288a5f

Browse files
committed
Set initial extent of a new 3D view to extent of the main canvas
1 parent d03b6f4 commit b288a5f

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

src/3d/qgscameracontroller.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,12 @@ void QgsCameraController::frameTriggered( float dt )
282282

283283
void QgsCameraController::resetView( float distance )
284284
{
285-
setCameraData( 0, 0, distance );
285+
setViewFromTop( 0, 0, distance );
286+
}
287+
288+
void QgsCameraController::setViewFromTop( float worldX, float worldY, float distance, float yaw )
289+
{
290+
setCameraData( worldX, worldY, distance, 0, yaw );
286291
// a basic setup to make frustum depth range long enough that it does not cull everything
287292
mCamera->setNearPlane( distance / 2 );
288293
mCamera->setFarPlane( distance * 2 );

src/3d/qgscameracontroller.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ class _3D_EXPORT QgsCameraController : public Qt3DCore::QEntity
5757
//! Move camera back to the initial position (looking down towards origin of world's coordinates)
5858
void resetView( float distance );
5959

60+
//! Sets camera to look down towards given point in world coordinate, in given distance from plane with zero elevation
61+
void setViewFromTop( float worldX, float worldY, float distance, float yaw = 0 );
62+
6063
//! Moves the point toward which the camera is looking - this is used when world origin changes (e.g. after terrain generator changes)
6164
void translateWorld( const QVector3D &vWorld );
6265

@@ -85,8 +88,8 @@ class _3D_EXPORT QgsCameraController : public Qt3DCore::QEntity
8588
{
8689
float x = 0, y = 0; // ground point towards which the camera is looking
8790
float dist = 40; // distance of camera from the point it is looking at
88-
float pitch = 0; // aircraft nose up/down (0 = looking straight down to the plane)
89-
float yaw = 0; // aircraft nose left/right
91+
float pitch = 0; // aircraft nose up/down (0 = looking straight down to the plane). angle in degrees
92+
float yaw = 0; // aircraft nose left/right. angle in degrees
9093

9194
bool operator==( const CameraData &other ) const
9295
{

src/app/3d/qgs3dmapcanvas.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,10 @@ void Qgs3DMapCanvas::resetView()
8282
{
8383
mScene->viewZoomFull();
8484
}
85+
86+
void Qgs3DMapCanvas::setViewFromTop( const QgsPointXY &center, float distance, float rotation )
87+
{
88+
float worldX = center.x() - mMap->originX();
89+
float worldY = center.y() - mMap->originY();
90+
mScene->cameraController()->setViewFromTop( worldX, -worldY, distance, rotation );
91+
}

src/app/3d/qgs3dmapcanvas.h

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace Qt3DExtras
2626
class Qgs3DMapSettings;
2727
class Qgs3DMapScene;
2828
class QgsCameraController;
29+
class QgsPointXY;
2930

3031

3132
class Qgs3DMapCanvas : public QWidget
@@ -46,6 +47,9 @@ class Qgs3DMapCanvas : public QWidget
4647
//! Resets camera position to the default: looking down at the origin of world coordinates
4748
void resetView();
4849

50+
//! Sets camera position to look down at the given point (in map coordinates) in given distance from plane with zero elevation
51+
void setViewFromTop( const QgsPointXY &center, float distance, float rotation = 0 );
52+
4953
protected:
5054
void resizeEvent( QResizeEvent *ev ) override;
5155

src/app/qgisapp.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -10244,6 +10244,10 @@ void QgisApp::new3DMapCanvas()
1024410244
map->setTerrainGenerator( flatTerrain );
1024510245

1024610246
dock->setMapSettings( map );
10247+
10248+
QgsRectangle extent = mMapCanvas->extent();
10249+
float dist = qMax( extent.width(), extent.height() );
10250+
dock->mapCanvas3D()->setViewFromTop( mMapCanvas->extent().center(), dist, mMapCanvas->rotation() );
1024710251
}
1024810252
#endif
1024910253
}

0 commit comments

Comments
 (0)