Skip to content

Commit

Permalink
Correctly working "zoom full" + more robust near/far plane config
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Sep 15, 2017
1 parent 9ac3e9c commit b234fc5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/3d/cameracontroller.cpp
Expand Up @@ -240,12 +240,12 @@ void CameraController::frameTriggered( float dt )
}
}

void CameraController::resetView()
void CameraController::resetView( float distance )
{
setCameraData( 0, 0, 1000 );
setCameraData( 0, 0, distance );
// a basic setup to make frustum depth range long enough that it does not cull everything
mCamera->setNearPlane( 1 );
mCamera->setFarPlane( 10000 );
mCamera->setNearPlane( distance / 2 );
mCamera->setFarPlane( distance * 2 );

emit cameraChanged();
}
Expand Down
2 changes: 1 addition & 1 deletion src/3d/cameracontroller.h
Expand Up @@ -29,7 +29,7 @@ class _3D_EXPORT CameraController : public Qt3DCore::QEntity
void frameTriggered( float dt );

//! Move camera back to the initial position (looking down towards origin of world's coordinates)
void resetView();
void resetView( float distance );

signals:
void cameraChanged();
Expand Down
30 changes: 28 additions & 2 deletions src/3d/scene.cpp
Expand Up @@ -58,7 +58,7 @@ Scene::Scene( const Map3D &map, Qt3DExtras::QForwardRenderer *defaultFrameGraph,
mCameraController = new CameraController( this ); // attaches to the scene
mCameraController->setViewport( viewportRect );
mCameraController->setCamera( camera );
mCameraController->resetView();
mCameraController->resetView( 1000 );

// create terrain entity

Expand Down Expand Up @@ -147,6 +147,13 @@ Scene::Scene( const Map3D &map, Qt3DExtras::QForwardRenderer *defaultFrameGraph,
onCameraChanged();
}

void Scene::viewZoomFull()
{
QgsRectangle extent = mMap.terrainGenerator()->extent();
float side = qMax( extent.width(), extent.height() );
mCameraController->resetView( side ); // assuming FOV being 45 degrees
}

SceneState _sceneState( CameraController *cameraController )
{
Qt3DRender::QCamera *camera = cameraController->camera();
Expand Down Expand Up @@ -187,6 +194,13 @@ void Scene::onCameraChanged()
float far = 0;

QList<ChunkNode *> activeNodes = mTerrain->getActiveNodes();

// it could be that there are no active nodes - they could be all culled or because root node
// is not yet loaded - we still need at least something to understand bounds of our scene
// so lets use the root node
if ( activeNodes.isEmpty() )
activeNodes << mTerrain->getRootNode();

Q_FOREACH ( ChunkNode *node, activeNodes )
{
// project each corner of bbox to camera coordinates
Expand All @@ -206,14 +220,26 @@ void Scene::onCameraChanged()
far = dst;
}
}
//qDebug() << "near/far" << near << far;
if ( near < 1 )
near = 1; // does not really make sense to use negative far plane (behind camera)

if ( near == 1e9 && far == 0 )
{
// the update didn't work out... this should not happen
// well at least temprarily use some conservative starting values
qDebug() << "oops... this should not happen! couldn't determine near/far plane. defaulting to 1...1e9";
near = 1;
far = 1e9;
}

// set near/far plane - with some tolerance in front/behind expected near/far planes
camera->setFarPlane( far * 2 );
camera->setNearPlane( near / 2 );
}
else
qDebug() << "no terrain - not setting near/far plane";

//qDebug() << "camera near/far" << mCameraController->camera()->nearPlane() << mCameraController->camera()->farPlane();
}

void Scene::onFrameTriggered( float dt )
Expand Down
2 changes: 2 additions & 0 deletions src/3d/scene.h
Expand Up @@ -39,6 +39,8 @@ class _3D_EXPORT Scene : public Qt3DCore::QEntity
CameraController *cameraController() { return mCameraController; }
Terrain *terrain() { return mTerrain; }

void viewZoomFull();

private slots:
void onCameraChanged();
void onFrameTriggered( float dt );
Expand Down
4 changes: 3 additions & 1 deletion src/app/3d/qgs3dmapcanvas.cpp
Expand Up @@ -55,9 +55,11 @@ void Qgs3DMapCanvas::setMap( Map3D *map )

delete mMap;
mMap = map;

resetView();
}

void Qgs3DMapCanvas::resetView()
{
mScene->cameraController()->resetView();
mScene->viewZoomFull();
}

0 comments on commit b234fc5

Please sign in to comment.