Importing a mesh (obj) file



  • 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.



  • @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.



  • @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?



  • @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();
    

    }



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


  • Qt Champions 2017

    @Dimis Then please mark this thread as solved.

    Thanks.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.