在我的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 的新手,所以请在你的回答中考虑这一点。(由于我的声誉低,我只能提供图片的链接。)