为什么最后创建的Qt3DCore :: QEntity始终位于场景视图的前景中?

时间:2016-12-09 15:06:15

标签: c++ qt3d qt5.7

在我的Qt3D场景中,我的3D对象不会根据其空间位置显示。无论我如何旋转相机,最后创建的QEntity始终位于前景中。 在我的示例中,QEntity redBigSphere始终位于前景中,即使它在相机设置中指定的默认视图中位于smallGreenSphere后面的空间位置。

main.cpp

#include <QGuiApplication>
#include <Qt3DCore>
#include <Qt3DRender>
#include <Qt3DExtras>

Qt3DCore::QEntity* createTestScene() {

  Qt3DCore::QEntity* root = new Qt3DCore::QEntity;
  Qt3DCore::QEntity* smallGreenSphere = new Qt3DCore::QEntity(root);
  Qt3DCore::QEntity* redBigSphere = new Qt3DCore::QEntity(root);

  Qt3DExtras::QSphereMesh* bigMesh = new Qt3DExtras::QSphereMesh;
  bigMesh->setRadius(5);
  bigMesh->setRings(100);
  bigMesh->setSlices(20);

  Qt3DExtras::QSphereMesh* smallMesh = new Qt3DExtras::QSphereMesh;
  smallMesh->setRadius(2);
  smallMesh->setRings(100);
  smallMesh->setSlices(20);

  Qt3DExtras::QPhongMaterial* redMaterial = new Qt3DExtras::QPhongMaterial;
  redMaterial->setSpecular(Qt::white);
  redMaterial->setShininess(10);
  redMaterial->setAmbient(Qt::red);

  Qt3DExtras::QPhongMaterial* greenMaterial = new Qt3DExtras::QPhongMaterial;
  greenMaterial->setSpecular(Qt::white);
  greenMaterial->setShininess(10);
  greenMaterial->setAmbient(Qt::green);

  Qt3DCore::QTransform* transform =  new Qt3DCore::QTransform;
  transform->setTranslation(QVector3D(0,0,10));

  redBigSphere->addComponent(redMaterial);
  redBigSphere->addComponent(bigMesh);

  smallGreenSphere->addComponent(greenMaterial);
  smallGreenSphere->addComponent(smallMesh);
  smallGreenSphere->addComponent(transform);

  return root;
}


int main(int argc, char* argv[]) {

  QGuiApplication app(argc, argv);
  Qt3DExtras::Qt3DWindow view;
  Qt3DCore::QEntity* scene = createTestScene();

  // camera
  Qt3DRender::QCamera *camera = view.camera();
  camera->lens()->setPerspectiveProjection(45, 16./9., 0.1,1000.);
  camera->setPosition(QVector3D(30., 0, 60.));
  camera->setViewCenter(QVector3D(0, 0, 0));

  // manipulator
  Qt3DExtras::QOrbitCameraController* manipulator = new Qt3DExtras::QOrbitCameraController(scene);
  manipulator->setLinearSpeed(50);
  manipulator->setLookSpeed(180);
  manipulator->setCamera(camera);

view.setRootEntity(scene);

view.show();

return app.exec();
}

(结果图片:http://imgur.com/a/A7tIW

如果我交换实例化的顺序,smallGreenSphere总是在前台。

Qt3DCore::QEntity* redBigSphere = new Qt3DCore::QEntity(root);
Qt3DCore::QEntity* smallGreenSphere = new Qt3DCore::QEntity(root);

(结果图片:http://imgur.com/a/fygTU

无论实例化的顺序如何,我如何获得正确的3D视图?

我是Qt和Qt3D的新手,所以请在你的答案中考虑这一点。 (由于我的声誉很低,我只能提供图像的链接。)

1 个答案:

答案 0 :(得分:0)

我在Qt论坛中更详细地发布了这个问题,人们可以在那里重现它。 https://forum.qt.io/topic/74419/a-weird-bug-why-is-there-no-spatial-depth-in-my-3d-scene/24

问题似乎与aside-hidden具体相关。但是,使用Qt5.7.0它现在对我来说很好。