In myQt3D
scene, my 3D objects are not displayed according to their spatial positioning. The last created QEntity
is always in the foreground, regardless how I rotate the camera.
In my example, the QEntity
redBigSphere
is always in the foreground even though it is spatially positioned behind smallGreenSphere
in the default view specified in the camera settings.
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();
}
(Image of the result: https://i.sstatic.net/IjyKB.jpg)
If I swap the order of instantiation,smallGreenSphere
is always in the foreground.
Qt3DCore::QEntity* redBigSphere = new Qt3DCore::QEntity(root);
Qt3DCore::QEntity* smallGreenSphere = new Qt3DCore::QEntity(root);
(Image of the result: https://i.sstatic.net/ESGH6.jpg)
How can I obtain the correct 3D view regardless of the order of instantiation?
I am new to Qt and Qt3D so please consider this in your answers. (Due to my low reputation I can only provide the links to the images.)
I posted the question in greater detail in the Qt Forums and people could reproduce it there. https://forum.qt.io/topic/74419/a-weird-bug-why-is-there-no-spatial-depth-in-my-3d-scene/24
The problem seems to be connected specifically with Qt5.7.0
. However, with Qt5.7.1
it now works fine for me.