Skip to content

Commit b234fc5

Browse files
committedSep 15, 2017
Correctly working "zoom full" + more robust near/far plane config
1 parent 9ac3e9c commit b234fc5

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed
 

‎src/3d/cameracontroller.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,12 @@ void CameraController::frameTriggered( float dt )
240240
}
241241
}
242242

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

250250
emit cameraChanged();
251251
}

‎src/3d/cameracontroller.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class _3D_EXPORT CameraController : public Qt3DCore::QEntity
2929
void frameTriggered( float dt );
3030

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

3434
signals:
3535
void cameraChanged();

‎src/3d/scene.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Scene::Scene( const Map3D &map, Qt3DExtras::QForwardRenderer *defaultFrameGraph,
5858
mCameraController = new CameraController( this ); // attaches to the scene
5959
mCameraController->setViewport( viewportRect );
6060
mCameraController->setCamera( camera );
61-
mCameraController->resetView();
61+
mCameraController->resetView( 1000 );
6262

6363
// create terrain entity
6464

@@ -147,6 +147,13 @@ Scene::Scene( const Map3D &map, Qt3DExtras::QForwardRenderer *defaultFrameGraph,
147147
onCameraChanged();
148148
}
149149

150+
void Scene::viewZoomFull()
151+
{
152+
QgsRectangle extent = mMap.terrainGenerator()->extent();
153+
float side = qMax( extent.width(), extent.height() );
154+
mCameraController->resetView( side ); // assuming FOV being 45 degrees
155+
}
156+
150157
SceneState _sceneState( CameraController *cameraController )
151158
{
152159
Qt3DRender::QCamera *camera = cameraController->camera();
@@ -187,6 +194,13 @@ void Scene::onCameraChanged()
187194
float far = 0;
188195

189196
QList<ChunkNode *> activeNodes = mTerrain->getActiveNodes();
197+
198+
// it could be that there are no active nodes - they could be all culled or because root node
199+
// is not yet loaded - we still need at least something to understand bounds of our scene
200+
// so lets use the root node
201+
if ( activeNodes.isEmpty() )
202+
activeNodes << mTerrain->getRootNode();
203+
190204
Q_FOREACH ( ChunkNode *node, activeNodes )
191205
{
192206
// project each corner of bbox to camera coordinates
@@ -206,14 +220,26 @@ void Scene::onCameraChanged()
206220
far = dst;
207221
}
208222
}
209-
//qDebug() << "near/far" << near << far;
210223
if ( near < 1 )
211224
near = 1; // does not really make sense to use negative far plane (behind camera)
212225

226+
if ( near == 1e9 && far == 0 )
227+
{
228+
// the update didn't work out... this should not happen
229+
// well at least temprarily use some conservative starting values
230+
qDebug() << "oops... this should not happen! couldn't determine near/far plane. defaulting to 1...1e9";
231+
near = 1;
232+
far = 1e9;
233+
}
234+
213235
// set near/far plane - with some tolerance in front/behind expected near/far planes
214236
camera->setFarPlane( far * 2 );
215237
camera->setNearPlane( near / 2 );
216238
}
239+
else
240+
qDebug() << "no terrain - not setting near/far plane";
241+
242+
//qDebug() << "camera near/far" << mCameraController->camera()->nearPlane() << mCameraController->camera()->farPlane();
217243
}
218244

219245
void Scene::onFrameTriggered( float dt )

‎src/3d/scene.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class _3D_EXPORT Scene : public Qt3DCore::QEntity
3939
CameraController *cameraController() { return mCameraController; }
4040
Terrain *terrain() { return mTerrain; }
4141

42+
void viewZoomFull();
43+
4244
private slots:
4345
void onCameraChanged();
4446
void onFrameTriggered( float dt );

‎src/app/3d/qgs3dmapcanvas.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ void Qgs3DMapCanvas::setMap( Map3D *map )
5555

5656
delete mMap;
5757
mMap = map;
58+
59+
resetView();
5860
}
5961

6062
void Qgs3DMapCanvas::resetView()
6163
{
62-
mScene->cameraController()->resetView();
64+
mScene->viewZoomFull();
6365
}

0 commit comments

Comments
 (0)
Please sign in to comment.