Skip to content

Commit bd66507

Browse files
committed
Fix more tests
1 parent 25685b6 commit bd66507

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

src/3d/qgs3dutils.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,21 @@ typedef Qt3DCore::QBuffer Qt3DQBuffer;
5959
// declared here as Qgs3DTypes has no cpp file
6060
const char *Qgs3DTypes::PROP_NAME_3D_RENDERER_FLAG = "PROP_NAME_3D_RENDERER_FLAG";
6161

62-
static void waitForFrame( Qgs3DMapScene *scene )
62+
void Qgs3DUtils::waitForFrame( QgsAbstract3DEngine &engine, Qgs3DMapScene *scene )
6363
{
64+
// Set policy to always render frame, so we don't wait forever.
65+
Qt3DRender::QRenderSettings::RenderPolicy oldPolicy = engine.renderSettings()->renderPolicy();
66+
engine.renderSettings()->setRenderPolicy( Qt3DRender::QRenderSettings::RenderPolicy::Always );
67+
6468
// Wait for at least one frame to render
6569
Qt3DLogic::QFrameAction *frameAction = new Qt3DLogic::QFrameAction();
6670
scene->addComponent( frameAction );
6771
QEventLoop evLoop;
6872
QObject::connect( frameAction, &Qt3DLogic::QFrameAction::triggered, &evLoop, &QEventLoop::quit );
6973
evLoop.exec();
7074
scene->removeComponent( frameAction );
75+
76+
engine.renderSettings()->setRenderPolicy( oldPolicy );
7177
}
7278

7379
QImage Qgs3DUtils::captureSceneImage( QgsAbstract3DEngine &engine, Qgs3DMapScene *scene )
@@ -78,7 +84,7 @@ QImage Qgs3DUtils::captureSceneImage( QgsAbstract3DEngine &engine, Qgs3DMapScene
7884
// We need to change render policy to RenderPolicy::Always, since otherwise render capture node won't work
7985
engine.renderSettings()->setRenderPolicy( Qt3DRender::QRenderSettings::RenderPolicy::Always );
8086

81-
waitForFrame( scene );
87+
waitForFrame( engine, scene );
8288

8389
auto saveImageFcn = [&evLoop, &resImage]( const QImage &img ) {
8490
resImage = img;
@@ -124,11 +130,10 @@ QImage Qgs3DUtils::captureSceneDepthBuffer( QgsAbstract3DEngine &engine, Qgs3DMa
124130
// We need to change render policy to RenderPolicy::Always, since otherwise render capture node won't work
125131
engine.renderSettings()->setRenderPolicy( Qt3DRender::QRenderSettings::RenderPolicy::Always );
126132

127-
waitForFrame( scene );
128-
129133
auto requestImageFcn = [&engine, scene] {
130134
if ( scene->sceneState() == Qgs3DMapScene::Ready )
131135
{
136+
waitForFrame( engine, scene );
132137
engine.renderSettings()->setRenderPolicy( Qt3DRender::QRenderSettings::RenderPolicy::OnDemand );
133138
engine.requestDepthBufferCapture();
134139
}

src/3d/qgs3dutils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ class _3D_EXPORT Qgs3DUtils
6565
*/
6666
static QImage captureSceneImage( QgsAbstract3DEngine &engine, Qgs3DMapScene *scene );
6767

68+
/**
69+
* Waits for a frame to be rendered. Useful to trigger once-per-frame updates
70+
* \since QGIS 3.42
71+
*/
72+
static void waitForFrame( QgsAbstract3DEngine &engine, Qgs3DMapScene *scene );
73+
6874
/**
6975
* Captures the depth buffer of the current 3D scene of a 3D engine. The function waits
7076
* until the scene is not fully loaded/updated before capturing the image.

src/3d/qgscameracontroller.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ void QgsCameraController::onWheel( Qt3DInput::QWheelEvent *wheel )
572572
if ( mCurrentOperation != MouseOperation::ZoomWheel )
573573
{
574574
setMouseParameters( MouseOperation::ZoomWheel );
575+
// The actual zooming will happen after we get a new depth buffer
575576
}
576577
else
577578
{

tests/src/3d/testqgs3dcameracontroller.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -455,14 +455,22 @@ void TestQgs3DCameraController::testRotationCenterZoomWheelRotationCenter()
455455

456456
// look from the top
457457
scene->cameraController()->setLookingAtPoint( QgsVector3D( 0, 0, 0 ), 2500, 0, 0 );
458+
// XXX: Sometimes the near/far planes aren't calculated correctly, so they're
459+
// left at the too-deep default. This causes the rest of the test to fail in
460+
// weird ways every once in a while, so loop until we get good values.
461+
do {
462+
// Force recalcualtion of near/far planes.
463+
scene->cameraController()->mCameraChanged = true;
464+
465+
// this call is not used but ensures to synchronize the scene
466+
Qgs3DUtils::captureSceneImage( engine, scene );
467+
} while(scene->cameraController()->camera()->nearPlane() < 1000);
468+
458469
QVector3D initialCamViewCenter = scene->cameraController()->camera()->viewCenter();
459470
QVector3D initialCamPosition = scene->cameraController()->camera()->position();
460471
float initialPitch = scene->cameraController()->pitch();
461472
float initialYaw = scene->cameraController()->yaw();
462473

463-
// this call is not used but ensures to synchronize the scene
464-
Qgs3DUtils::captureSceneImage( engine, scene );
465-
466474
QMouseEvent mousePressEvent( QEvent::MouseButtonPress, midPos, Qt::LeftButton, Qt::LeftButton, Qt::ShiftModifier );
467475
scene->cameraController()->onMousePressed( new Qt3DInput::QMouseEvent( mousePressEvent ) );
468476

@@ -522,8 +530,8 @@ void TestQgs3DCameraController::testRotationCenterZoomWheelRotationCenter()
522530
depthImage = Qgs3DUtils::captureSceneDepthBuffer( engine, scene );
523531
scene->cameraController()->depthBufferCaptured( depthImage );
524532

525-
QGSCOMPARENEARVECTOR3D( scene->cameraController()->mZoomPoint, QVector3D( 283.2, -923.1, -27.0 ), 1.5 );
526-
QGSCOMPARENEARVECTOR3D( scene->cameraController()->cameraPose().centerPoint(), QVector3D( 99.4, -319.9, -8.8 ), 2.0 );
533+
QGSCOMPARENEARVECTOR3D( scene->cameraController()->mZoomPoint, QVector3D( 312.936, -950.772, -125.381 ), 1.5 );
534+
QGSCOMPARENEARVECTOR3D( scene->cameraController()->cameraPose().centerPoint(), QVector3D( 109.8, -329.4, -43.3 ), 2.0 );
527535
QGSCOMPARENEAR( scene->cameraController()->cameraPose().distanceFromCenterPoint(), 1631.9, 2.0 );
528536
QCOMPARE( scene->cameraController()->pitch(), initialPitch );
529537
QCOMPARE( scene->cameraController()->yaw(), initialYaw );
@@ -540,6 +548,7 @@ void TestQgs3DCameraController::testRotationCenterZoomWheelRotationCenter()
540548
initialPitch = scene->cameraController()->pitch();
541549
initialYaw = scene->cameraController()->yaw();
542550

551+
Qgs3DUtils::waitForFrame(engine, scene);
543552
// the first mouse event only updates the mouse position
544553
// the second one will update the camera
545554
QMouseEvent mouseMoveEvent3( QEvent::MouseMove, midPos + movement1 + movement2, Qt::LeftButton, Qt::LeftButton, Qt::ShiftModifier );
@@ -558,9 +567,9 @@ void TestQgs3DCameraController::testRotationCenterZoomWheelRotationCenter()
558567
QCOMPARE( scene->cameraController()->mCurrentOperation, QgsCameraController::MouseOperation::RotationCenter );
559568

560569
diffViewCenter = scene->cameraController()->camera()->viewCenter() - initialCamViewCenter;
561-
QGSCOMPARENEARVECTOR3D( diffViewCenter, QVector3D( 25.9, 7.1, 5.2 ), 1.0 );
570+
QGSCOMPARENEARVECTOR3D( diffViewCenter, QVector3D( 26.9, 7.3, 5.4 ), 2.0 );
562571
diffPosition = scene->cameraController()->camera()->position() - initialCamPosition;
563-
QGSCOMPARENEARVECTOR3D( diffPosition, QVector3D( -44.3, -9.1, -11.7 ), 1.0 );
572+
QGSCOMPARENEARVECTOR3D( diffPosition, QVector3D( -43.2, -9.1, -11.1 ), 1.0 );
564573
diffPitch = scene->cameraController()->pitch() - initialPitch;
565574
diffYaw = scene->cameraController()->yaw() - initialYaw;
566575
QGSCOMPARENEAR( diffPitch, 2.5, 0.1 );
@@ -857,9 +866,9 @@ void TestQgs3DCameraController::testTranslateZoomWheelTranslate()
857866
QCOMPARE( scene->cameraController()->mCurrentOperation, QgsCameraController::MouseOperation::Translation );
858867

859868
diffViewCenter = scene->cameraController()->camera()->viewCenter() - initialCamViewCenter;
860-
QGSCOMPARENEARVECTOR3D( diffViewCenter, QVector3D( -11.3, 11.3, 0.0 ), 1.0 );
869+
QGSCOMPARENEARVECTOR3D( diffViewCenter, QVector3D( -17.2, 17.2, 0.0 ), 1.0 );
861870
diffPosition = scene->cameraController()->camera()->position() - initialCamPosition;
862-
QGSCOMPARENEARVECTOR3D( diffPosition, QVector3D( -11.3, 11.3, 0.0 ), 1.0 );
871+
QGSCOMPARENEARVECTOR3D( diffPosition, QVector3D( -17.2, 17.2, 0.0 ), 1.0 );
863872
QCOMPARE( scene->cameraController()->pitch(), initialPitch );
864873
QCOMPARE( scene->cameraController()->yaw(), initialYaw );
865874

0 commit comments

Comments
 (0)