Issue loading and displaying STL file with Qt3D
-
Hello all.
I'm new to Qt3D, but have years of experience in Qt generally. I am trying to display an STL file in a Qt3DWindow. I started with the example code in Qt 3D Basic shapes C++ example and added the code I think should display the STL as a QMesh.
I also added a torus to the view as a sanity check that the camera and controller are working, and they are indeed working (I am able to view the torus).
Additionally, I hooked the QMesh::statusChanged signal to verify that the stl mesh successfully loads (and it does).
Also, I've verified that the stl file loads in meshlab without issue.
So the torus is visible, but the stl mesh is not.
Just for completeness, my environment is Qt 6.7.1 on Fedora 40.
I suspect I have overlooked something fundamental and would appreciate any eyes on the code that might direct me as to what I've missed here.
The whole project is at https://github.com/StevePunak/test3d.git but the relevant code is here (thanks in advance!):
Qt3DExtras::Qt3DWindow *view = new Qt3DExtras::Qt3DWindow(); view->defaultFrameGraph()->setClearColor(QColor(Qt::white)); // Root entity Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); // ----------------------- My STL mesh code BEGIN ----------------------- // Transform for positioning and scaling Qt3DCore::QTransform *stlTransform = new Qt3DCore::QTransform(); stlTransform->setScale(1.0f); // Material for the STL Qt3DExtras::QPhongMaterial *stlMaterial = new Qt3DExtras::QPhongMaterial(); stlMaterial->setAmbient(QColor(Qt::blue)); // STL mesh Qt3DRender::QMesh *stlMesh = new Qt3DRender::QMesh(); stlMesh->setSource(QUrl::fromLocalFile(":/storm.stl")); connect(stlMesh, &Qt3DRender::QMesh::statusChanged, this, &MainWindow::onMeshStatusChanged); // sanity check // Entity for the mesh Qt3DCore::QEntity *meshEntity = new Qt3DCore::QEntity(rootEntity); meshEntity->addComponent(stlMesh); meshEntity->addComponent(stlTransform); meshEntity->addComponent(stlMaterial); // ----------------------- My STL mesh code END ----------------------- // Create Torus from Qt example as a sanity check Qt3DExtras::QPhongMaterial *torusMaterial = new Qt3DExtras::QPhongMaterial(); torusMaterial->setAmbient(QColor(Qt::blue)); Qt3DCore::QEntity *torusEntity = new Qt3DCore::QEntity(rootEntity); Qt3DExtras::QTorusMesh *torusMesh = new Qt3DExtras::QTorusMesh; torusMesh->setRadius(5); torusMesh->setMinorRadius(1); torusMesh->setRings(100); torusMesh->setSlices(20); Qt3DCore::QTransform *torusTransform = new Qt3DCore::QTransform; torusTransform->setScale3D(QVector3D(1.5, 1, 0.5)); torusTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1, 0, 0), 45.0f)); torusEntity->addComponent(torusMesh); torusEntity->addComponent(torusTransform); torusEntity->addComponent(torusMaterial); // Camera Qt3DRender::QCamera *camera = view->camera(); camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f); camera->setPosition(QVector3D(0, 0, 10.0f)); camera->setViewCenter(QVector3D(0, 0, 0)); // Camera controller Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(rootEntity); camController->setCamera(camera); view->setRootEntity(rootEntity); view->show();
-
So @karlphillip on StackOverflow helped me resolve this issue. It turned out the issue had to do with scaling the STL mesh so that it became visible. I had to scale it down to < .01 and there it was.
I hope this helps someone else.
-