QT C++ loading a mesh having multiple material
-
Hello, now that the 2D part of my application is ok, I'm working on the 3D part, a 3d scene showing the 3d model with each unfolded part (the purpose of my application) shown in its own color. I managed to use the QT 3D C++ simple example code to display the 3d model (an .obj file), but it only display it with everything using the first material when I expected it to use 4. Is there something to do to handle such multiple material model ?
If needed I could create one QEntity for each material and put a QGeometry with the vertices and the triangles, but for the moment I didn't find how to create a QGeometry from scratch.
I'm hoping to use this 3D view to not only view the model as I unfold it, but also be able to handle triangle picking, but I know that I'm a long way from there.
Does someone have any hint for me ?
my code :
Qt3DCore::QEntity *createScene() { // Root entity Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity; // Material Qt3DExtras::QGoochMaterial *mat1 = new Qt3DExtras::QGoochMaterial(rootEntity); mat1->setCool(QColor(Qt::white)); Qt3DExtras::QGoochMaterial *mat2 = new Qt3DExtras::QGoochMaterial(rootEntity); mat2->setCool(QColor(Qt::blue)); Qt3DExtras::QGoochMaterial *mat3 = new Qt3DExtras::QGoochMaterial(rootEntity); mat3->setCool(QColor(Qt::green)); Qt3DExtras::QGoochMaterial *mat4 = new Qt3DExtras::QGoochMaterial(rootEntity); mat4->setCool(QColor(Qt::red)); // Volume Qt3DCore::QEntity *Entity = new Qt3DCore::QEntity(rootEntity); Qt3DRender::QMesh *volumeMesh = new Qt3DRender::QMesh(); volumeMesh->setSource(QUrl::fromLocalFile("/home/gigi/Documents/modeles/assets/tdy186_4C.obj")); // Orbit Transform Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform; OrbitTransformController *controller = new OrbitTransformController(sphereTransform); controller->setTarget(sphereTransform); controller->setRadius(1.0f); Entity->addComponent(mat1); Entity->addComponent(mat2); Entity->addComponent(mat3); Entity->addComponent(mat4); Entity->addComponent(volumeMesh); Entity->addComponent(sphereTransform); return rootEntity; }
-
I have this model
I saved it into 4 differents files each of them having only triangles of one color, then I tried to load them into my scene but apparently only the last mesh appears. I tried to use separated material for each mesh, then the same one for them all, but only the last mesh appears. There is certainly something that I didn't get.here's what my code does
Qt3DCore::QEntity *createScene() { // Root entity Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity; // Material Qt3DExtras::QGoochMaterial *mat1 = new Qt3DExtras::QGoochMaterial(rootEntity); mat1->setCool(QColor(Qt::white)); Qt3DExtras::QGoochMaterial *mat2 = new Qt3DExtras::QGoochMaterial(rootEntity); mat2->setCool(QColor(Qt::blue)); Qt3DExtras::QGoochMaterial *mat3 = new Qt3DExtras::QGoochMaterial(rootEntity); mat3->setCool(QColor(Qt::green)); Qt3DExtras::QGoochMaterial *mat4 = new Qt3DExtras::QGoochMaterial(rootEntity); mat4->setCool(QColor(Qt::red)); // Volume Qt3DCore::QEntity *Entity = new Qt3DCore::QEntity(rootEntity); Qt3DRender::QMesh *volumeMesh1 = new Qt3DRender::QMesh(); volumeMesh1->setSource(QUrl::fromLocalFile("/home/gigi/Documents/modeles/assets/tdy186_4C_1.obj")); Qt3DRender::QMesh *volumeMesh2 = new Qt3DRender::QMesh(); volumeMesh2->setSource(QUrl::fromLocalFile("/home/gigi/Documents/modeles/assets/tdy186_4C_2.obj")); Qt3DRender::QMesh *volumeMesh3 = new Qt3DRender::QMesh(); volumeMesh3->setSource(QUrl::fromLocalFile("/home/gigi/Documents/modeles/assets/tdy186_4C_3.obj")); Qt3DRender::QMesh *volumeMesh4 = new Qt3DRender::QMesh(); volumeMesh4->setSource(QUrl::fromLocalFile("/home/gigi/Documents/modeles/assets/tdy186_4C_4.obj")); // Orbit Transform Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform; OrbitTransformController *controller = new OrbitTransformController(sphereTransform); controller->setTarget(sphereTransform); controller->setRadius(1.0f); Entity->addComponent(volumeMesh1); //Entity->addComponent(mat1); Entity->addComponent(volumeMesh2); //Entity->addComponent(mat2); Entity->addComponent(volumeMesh3); //Entity->addComponent(mat3); Entity->addComponent(volumeMesh4); //Entity->addComponent(mat4); Entity->addComponent(mat1); Entity->addComponent(sphereTransform); return rootEntity; }
-
Well, I managed to make it work by loading my .obj file as a scene and not as a mesh. My textures need to be better defined but it it promising for me.
Qt3DCore::QEntity *createScene() { // Root entity Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity; Qt3DCore::QEntity *Entity = new Qt3DCore::QEntity(rootEntity); Qt3DRender::QSceneLoader *sceneLoader = new Qt3DRender::QSceneLoader(Entity); sceneLoader->setSource(QUrl::fromLocalFile("/home/gigi/Documents/modeles/assets/tdy186_4C.obj")); // Orbit Transform Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform; OrbitTransformController *controller = new OrbitTransformController(sphereTransform); controller->setTarget(sphereTransform); controller->setRadius(1.0f); Entity->addComponent(sceneLoader); Entity->addComponent(sphereTransform); return rootEntity; }
-
Now that it's ok, I'm wondering whether is is possible to directly use a QByteArray or anything else instead of a file as on my application I edit the model graphically and would like to be able to reflect any change I make to the unfolded 2D pattern to the 3D View. In fact write the file won't take long but it would be great to avoid it.