Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. Q: Qt3D - [c++] Apply QMaterial on a loaded obj file
Forum Updated to NodeBB v4.3 + New Features

Q: Qt3D - [c++] Apply QMaterial on a loaded obj file

Scheduled Pinned Locked Moved Solved Game Development
3 Posts 1 Posters 964 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.
  • K Offline
    K Offline
    kevin_d
    wrote on last edited by
    #1

    Hi!

    I imported a simple obj-file with the SceneLoader.
    The object is purposely visible multiple times inside the 3D Window, but I would like to apply individual colors (red or blue) on them. Best case would also be a alpha parameter, as QPhongAlphaMaterial is supposed to be capable. Is there any chance of applying a QMaterial* ?

    Unfortunately, no effect of transparency, or individual colors from applied material types (I tried all of the popular ones) is spottable, all the objects are black (but shaded from the white light source).

    Please help. This is my code:

    Qt3DRender::QMaterial *objectMaterial = getAnyQMaterial();
    
    Qt3DCore::QEntity *entity = new Qt3DCore::QEntity(root);
    
    Qt3DRender::QSceneLoader *loader = new Qt3DRender::QSceneLoader(root);
    QObject::connect(loader, &Qt3DRender::QSceneLoader::statusChanged, [loader, objectMaterial](Qt3DRender::QSceneLoader::Status s) 
    { 
    if (s == Qt3DRender::QSceneLoader::Ready)
        {
            QVector<Qt3DCore::QEntity*> entities = loader->entities();
            if (entities.size() > 0)
            {
                for (int e = 0; e < entities.size(); e++)
                {
    	        entities[e]->components().clear();
                    entities[e]->addComponent(objectMaterial);
                    entities[e]->setEnabled(true);
                }
            }
        } 
    });
        
    loader->setSource(QUrl::fromLocalFile("Resources/Objects/pyramid.obj"));
    entity->addComponent(objectMaterial);
    entity->addComponent(loader);
    
    1 Reply Last reply
    0
    • K Offline
      K Offline
      kevin_d
      wrote on last edited by
      #2

      This is a very crucial problem. Is there any way of coloring the resulting mesh?

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kevin_d
        wrote on last edited by
        #3

        I am very happy now. All the materials work.
        There were two problems:

        1.) Never reuse material references. Always create a new material which apply to the individual object.

        2.) The assignment of materials don't work recursively. As in http://man.hubwiz.com/docset/Qt_5.docset/Contents/Resources/Documents/doc.qt.io/qt-5/qt3drender-qsceneloader.html, it needs to traverse through the complete entities-tree of the loader, eg. with the SceneWalker, referenced here: https://code.qt.io/cgit/qt/qt3d.git/tree/tests/manual/assimp-cpp/main.cpp and assign the material to all entities found.

        Code here:

        void SceneWalker::walk(Qt3DCore::QEntity *ent, int depth)
        {
            // get entity in entities ...
            // apply material to entity
            // call walk recursively
        }
        
        void Scene::loadAndApply()
        {
            QPhongAlphaMaterial *_material= new QPhongAlphaMaterial;
            _material->setDiffuse(QColor(100,100,200));
            [...]
            SceneWalker *sceneWalker = new SceneWalker(_material);
            QObject::connect(loader, &Qt3DRender::QSceneLoader::statusChanged, [sceneWalker, loader](Qt3DRender::QSceneLoader::Status status)
            {
                if (status = Qt3DRender::QSceneLoader::Ready)
                    sceneWalker->onStatusChanged(loader);
            }
            entity->addComponent(loader);
        }
        

        best regards

        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