Skip to content

Commit

Permalink
Set initial extent of a new 3D view to extent of the main canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Nov 22, 2017
1 parent d03b6f4 commit b288a5f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/3d/qgscameracontroller.cpp
Expand Up @@ -282,7 +282,12 @@ void QgsCameraController::frameTriggered( float dt )

void QgsCameraController::resetView( float distance )
{
setCameraData( 0, 0, distance );
setViewFromTop( 0, 0, distance );
}

void QgsCameraController::setViewFromTop( float worldX, float worldY, float distance, float yaw )
{
setCameraData( worldX, worldY, distance, 0, yaw );
// a basic setup to make frustum depth range long enough that it does not cull everything
mCamera->setNearPlane( distance / 2 );
mCamera->setFarPlane( distance * 2 );
Expand Down
7 changes: 5 additions & 2 deletions src/3d/qgscameracontroller.h
Expand Up @@ -57,6 +57,9 @@ class _3D_EXPORT QgsCameraController : public Qt3DCore::QEntity
//! Move camera back to the initial position (looking down towards origin of world's coordinates)
void resetView( float distance );

//! Sets camera to look down towards given point in world coordinate, in given distance from plane with zero elevation
void setViewFromTop( float worldX, float worldY, float distance, float yaw = 0 );

//! Moves the point toward which the camera is looking - this is used when world origin changes (e.g. after terrain generator changes)
void translateWorld( const QVector3D &vWorld );

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

bool operator==( const CameraData &other ) const
{
Expand Down
7 changes: 7 additions & 0 deletions src/app/3d/qgs3dmapcanvas.cpp
Expand Up @@ -82,3 +82,10 @@ void Qgs3DMapCanvas::resetView()
{
mScene->viewZoomFull();
}

void Qgs3DMapCanvas::setViewFromTop( const QgsPointXY &center, float distance, float rotation )
{
float worldX = center.x() - mMap->originX();
float worldY = center.y() - mMap->originY();
mScene->cameraController()->setViewFromTop( worldX, -worldY, distance, rotation );
}
4 changes: 4 additions & 0 deletions src/app/3d/qgs3dmapcanvas.h
Expand Up @@ -26,6 +26,7 @@ namespace Qt3DExtras
class Qgs3DMapSettings;
class Qgs3DMapScene;
class QgsCameraController;
class QgsPointXY;


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

//! Sets camera position to look down at the given point (in map coordinates) in given distance from plane with zero elevation
void setViewFromTop( const QgsPointXY &center, float distance, float rotation = 0 );

protected:
void resizeEvent( QResizeEvent *ev ) override;

Expand Down
4 changes: 4 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -10244,6 +10244,10 @@ void QgisApp::new3DMapCanvas()
map->setTerrainGenerator( flatTerrain );

dock->setMapSettings( map );

QgsRectangle extent = mMapCanvas->extent();
float dist = qMax( extent.width(), extent.height() );
dock->mapCanvas3D()->setViewFromTop( mMapCanvas->extent().center(), dist, mMapCanvas->rotation() );
}
#endif
}
Expand Down

2 comments on commit b288a5f

@nirvn
Copy link
Contributor

@nirvn nirvn commented on b288a5f Nov 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one.

@palmerj
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes thanks @wonder-sk 👍

Please sign in to comment.