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. Importing a mesh (obj) file
Forum Updated to NodeBB v4.3 + New Features

Importing a mesh (obj) file

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.9k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    Dimis
    wrote on last edited by
    #1

    Hi all,

    I'm trying to load an *.obj file. My code is this:

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();
    
    Qt3DRender::QMesh *mesh = new Qt3DRender::QMesh();
        mesh->setSource(QUrl(QStringLiteral("qrc:/LabPart.obj")));
    
    
    
        Qt3DCore::QEntity *object = new Qt3DCore::QEntity(rootEntity);
        object->addComponent(mesh);
    
    
    return a.exec();
    

    }

    It doesn't return any error but it doesn't display the model, just a blank window.

    Any help please?

    Many thanks in advance.

    O 1 Reply Last reply
    0
    • D Dimis

      Hi all,

      I'm trying to load an *.obj file. My code is this:

      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);
      MainWindow w;
      w.show();

      Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();
      
      Qt3DRender::QMesh *mesh = new Qt3DRender::QMesh();
          mesh->setSource(QUrl(QStringLiteral("qrc:/LabPart.obj")));
      
      
      
          Qt3DCore::QEntity *object = new Qt3DCore::QEntity(rootEntity);
          object->addComponent(mesh);
      
      
      return a.exec();
      

      }

      It doesn't return any error but it doesn't display the model, just a blank window.

      Any help please?

      Many thanks in advance.

      O Offline
      O Offline
      ofmrew
      wrote on last edited by
      #2

      @Dimis I had a similar problem with a PLY file and it was the definition of the QUrl. You might want to check the QUrl to see if the file exist. I did not create the QUrl correctly. Try

      QUrl data = QUrl::fromLocalFile("/home/XXX/flyingwedge.ply");
      qDebug() << data.isValid() << data.toLocalFile() << QFileInfo(data.toLocalFile()).exists() << data.fileName();

      If the QUrl is incorrect you get no error message.

      D 1 Reply Last reply
      3
      • O ofmrew

        @Dimis I had a similar problem with a PLY file and it was the definition of the QUrl. You might want to check the QUrl to see if the file exist. I did not create the QUrl correctly. Try

        QUrl data = QUrl::fromLocalFile("/home/XXX/flyingwedge.ply");
        qDebug() << data.isValid() << data.toLocalFile() << QFileInfo(data.toLocalFile()).exists() << data.fileName();

        If the QUrl is incorrect you get no error message.

        D Offline
        D Offline
        Dimis
        wrote on last edited by
        #3

        @ofmrew I added these lines. it returned true. the path of file and that it exists, but still it doesn't display the model.

        I feel that something is missing. Any other suggestions?

        O 1 Reply Last reply
        0
        • D Dimis

          @ofmrew I added these lines. it returned true. the path of file and that it exists, but still it doesn't display the model.

          I feel that something is missing. Any other suggestions?

          O Offline
          O Offline
          ofmrew
          wrote on last edited by
          #4

          @Dimis Have you tried to import the file into MeshLab or Blender. I do not see where you set the root entity you created it but never set it in the view.

          Here is some code you can modify.

          int main(int argc, char *argv[])
          {
          QGuiApplication a(argc, argv);
          QUrl data = your code here.

          Qt3DExtras::Qt3DWindow view;
          Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity;
          Qt3DCore::QEntity *flyingwedge = new Qt3DCore::QEntity(rootEntity);
          
          Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial();
          material->setDiffuse(QColor(QRgb(0xbeb32b)));
          
          Qt3DRender::QMesh *flyingwedgeMesh = new Qt3DRender::QMesh;
          flyingwedgeMesh->setMeshName("FlyingWedge");
          flyingwedgeMesh->setSource(data);
          Qt3DCore::QTransform *flyingwedgeTransform = new Qt3DCore::QTransform;
          flyingwedgeTransform->setScale3D(QVector3D(1.0, 1.0, 1.0));
          Qt3DRender::QFrontFace *frontFace = new Qt3DRender::QFrontFace(rootEntity);
          frontFace->setDirection(Qt3DRender::QFrontFace::CounterClockWise);
          flyingwedge->addComponent(flyingwedgeMesh);
          flyingwedge->addComponent(material);
          flyingwedge->addComponent(flyingwedgeTransform);
          Qt3DRender::QCamera *camera = view.camera();
          camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
          camera->setPosition(QVector3D(0, 0, 40.0f));
          camera->setViewCenter(QVector3D(0, 0, 0));
          
          Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(rootEntity);
          Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight(lightEntity);
          light->setColor("white");
          light->setIntensity(0.5);
          lightEntity->addComponent(light);
          Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform(lightEntity);
          lightTransform->setTranslation(QVector3D(60, 0, 40.0f));
          lightEntity->addComponent(lightTransform);
          
          view.setRootEntity(rootEntity);
          view.show();
          return a.exec();
          

          }

          D 1 Reply Last reply
          2
          • O ofmrew

            @Dimis Have you tried to import the file into MeshLab or Blender. I do not see where you set the root entity you created it but never set it in the view.

            Here is some code you can modify.

            int main(int argc, char *argv[])
            {
            QGuiApplication a(argc, argv);
            QUrl data = your code here.

            Qt3DExtras::Qt3DWindow view;
            Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity;
            Qt3DCore::QEntity *flyingwedge = new Qt3DCore::QEntity(rootEntity);
            
            Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial();
            material->setDiffuse(QColor(QRgb(0xbeb32b)));
            
            Qt3DRender::QMesh *flyingwedgeMesh = new Qt3DRender::QMesh;
            flyingwedgeMesh->setMeshName("FlyingWedge");
            flyingwedgeMesh->setSource(data);
            Qt3DCore::QTransform *flyingwedgeTransform = new Qt3DCore::QTransform;
            flyingwedgeTransform->setScale3D(QVector3D(1.0, 1.0, 1.0));
            Qt3DRender::QFrontFace *frontFace = new Qt3DRender::QFrontFace(rootEntity);
            frontFace->setDirection(Qt3DRender::QFrontFace::CounterClockWise);
            flyingwedge->addComponent(flyingwedgeMesh);
            flyingwedge->addComponent(material);
            flyingwedge->addComponent(flyingwedgeTransform);
            Qt3DRender::QCamera *camera = view.camera();
            camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
            camera->setPosition(QVector3D(0, 0, 40.0f));
            camera->setViewCenter(QVector3D(0, 0, 0));
            
            Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(rootEntity);
            Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight(lightEntity);
            light->setColor("white");
            light->setIntensity(0.5);
            lightEntity->addComponent(light);
            Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform(lightEntity);
            lightTransform->setTranslation(QVector3D(60, 0, 40.0f));
            lightEntity->addComponent(lightTransform);
            
            view.setRootEntity(rootEntity);
            view.show();
            return a.exec();
            

            }

            D Offline
            D Offline
            Dimis
            wrote on last edited by
            #5

            @ofmrew Thanks very much, it worked. Also it works for STL files.

            aha_1980A 1 Reply Last reply
            1
            • D Dimis

              @ofmrew Thanks very much, it worked. Also it works for STL files.

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Dimis Then please mark this thread as solved.

              Thanks.

              Qt has to stay free or it will die.

              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