Skip to content
Permalink
Browse files

Set initial extent of a new 3D view to extent of the main canvas

  • Loading branch information
wonder-sk committed Nov 22, 2017
1 parent d03b6f4 commit b288a5f5faf8c414b53fee209a9ff2ae1a3c8436
@@ -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 );
@@ -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 );

@@ -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
{
@@ -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 );
}
@@ -26,6 +26,7 @@ namespace Qt3DExtras
class Qgs3DMapSettings;
class Qgs3DMapScene;
class QgsCameraController;
class QgsPointXY;


class Qgs3DMapCanvas : public QWidget
@@ -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;

@@ -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
}

2 comments on commit b288a5f

@nirvn

This comment has been minimized.

Copy link
Contributor

@nirvn nirvn replied Nov 23, 2017

Nice one.

@palmerj

This comment has been minimized.

Copy link
Contributor

@palmerj palmerj replied Nov 23, 2017

Yes thanks @wonder-sk 👍

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