Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT C++ loading a mesh having multiple material
Forum Updated to NodeBB v4.3 + New Features

QT C++ loading a mesh having multiple material

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++ 3d
4 Posts 1 Posters 633 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • GilboonetG Offline
    GilboonetG Offline
    Gilboonet
    wrote on last edited by Gilboonet
    #1

    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;
    }
    
    1 Reply Last reply
    0
    • GilboonetG Offline
      GilboonetG Offline
      Gilboonet
      wrote on last edited by
      #2

      I have this model
      Screenshot_20230621_152527.png
      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
      Screenshot_20230621_153134.png

      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;
      }
      
      
      1 Reply Last reply
      0
      • GilboonetG Offline
        GilboonetG Offline
        Gilboonet
        wrote on last edited by
        #3

        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.
        Screenshot_20230621_155754.png

        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;
        }
        
        1 Reply Last reply
        0
        • GilboonetG Offline
          GilboonetG Offline
          Gilboonet
          wrote on last edited by
          #4

          Screenshot_20230621_171641.png
          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved