How can I load a 3D model(like *.obj files etc.) in QT
-
HI,
Now, I am doing a project in which I want to display a 3D model(robot) and control it like moving its leg or arm.
So, I am trying to load a *.obj files into QT, but I failed.
I search on the Internet and some people say I can try the Assimp, but i can't find how to use it. I include <AssimpIO> but resulted in a "file not found" compiler error. it seems QT do not support Assimp now?
Is there any way to load 3D model(like *.obj files etc.) into QT? -
Hi and welcome to devnet,
Did you took a look at the Qt3D module documentation and the accompanying examples ?
The Audio Visualizer Example loads .obj files.
By the way, it's Qt. QT stands for Apple QuickTime which doesn't seem to be what you are looking for.
-
Qt 3D initially support obj modules without materials. Take a try.
-
@jiancaiyang Thanks, I have found the Qt Documentation about the Qt 3D C++ Classes(https://doc.qt.io/qt-5/qt3d-cpp.html) and I know there is a AssimpIO Class(https://doc.qt.io/qt-5/qt3drender-assimpio.html), but i can't use it(I include <AssimpIO> but resulted in a "file not found" compiler error ) I don't whether QT don't support Assimp now or not. is there any way to load obj modules except AssimpIO Class?
-
Which version of Qt are you using ?
Did you add
QT += 3drender
to your .pro file ? If so, did you re-runqmake
after that ? -
I did it this way in Qt3D:
- Create a .qrc file as resource
- add it to your .pro:
RESOURCES += \ obj.qrc
- content of .qrc file looks like this, make sure the .obj file is in the right directory:
<RCC> <qresource prefix="/"> <file>object1.obj</file> <file>object2.obj</file> </qresource> </RCC>
- create a QEntity with all needed components like QTransform, QMaterial and especially QMesh
- Add the .obj as source for the mesh:
Qt3DRender::QMesh *mesh = new Qt3DRender::QMesh(); mesh->setSource(QUrl(QStringLiteral("qrc:/object1.obj")));
- Add mesh and other components to entity:
Qt3DCore::QEntity *object = new Qt3DCore::QEntity(rootEntity); object->addComponent(mesh);
- Finished
-
Thanks for all the help.I have found to load the 3D model file(.obj) into Qt and control it.
Initially, I try to use C++ to complete all the function(display and control), but I find it a little complex for me. So I decide using QML to display the model and using C++ to control it.I post the approach I used:
First, loading and displaying the 3D model using QML, you can refer to Qt 3D: Wireframe QML Example and Qt 3D: Scene3D QML Example. I think these two example include almost all the detail you need when loading and displaying the 3D model.
Second, i notice Entity has a property:Transform, which decide the position and angle of the 3D model displaying in the scene. The Transform accept scale, matrix, translation or rotation to define the property. So i define a C++ class, the class has some member variables that are QMatrix4x4 type. Obviously, I use QMatrix4x4 to store the coordinate matrix.
Third, I export the C ++ object into QML as a property that QML can use(refer to setContextProperty).
Fourth, bind property like this:
Transform {
id: transform
matrix: matrix.matrix
}Fifth, when i want to change the position or angle, i just use C++ to change the QMatrix4x4 member variables, QML will update the scene automatically.
Finished~~~
-
@guy-incognito Thank you for your help. Finally I use QML to display the 3D model and C++ to control it, but you still help me a lot. Thanks