Qt3D Simple Model (.obj) Viewer?
-
Hi,
I am currently trying to use Qt3D to have just a simple Wavefront .obj viewer in a window that you can pan around using mouse input. It appears that Qt3D underwent a major overhaul somewhat recently and therefore the examples of documentation for it now seem somewhat sparse (at least as far as I could see). There is an old example that seems to do exactly what I want, thought it probably wouldn't work anymore and regardless the files have been removed from the Qt git: https://wiki.qt.io/Loading-a-3D-Model
The closest I could find was this example: http://doc.qt.io/qt-5/qt3d-basicshapes-cpp-example.html
which essentially does exactly what I want, except that instead of there being the random for shapes I want the view to be of a .obj file. It seems like a simple enough task that doesn't require anything too complicated, but all the other examples or information I can find seems way over complicated and not really what I am looking for. I imagine I may be able to modify that example in someway to do what I want but I am not sure how to load in a model instead of creating a blank scene. I looked through some of the Qt3D classes documentation but couldn't find much and it seems like it is a huge thing to just look through blindly.
I was hoping that someone would have an idea on how to easily accomplish this. The old example didn't look to complicated, but again the code for it is gone and I doubt it works on the current version of Qt3D.
Thanks.
-
It should be something along the lines of:
Qt3DCore::QEntity * meshEntity = new Qt3DCore::QEntity(); Qt3DRender::QMesh * myMesh = new Qt3DRender::QMesh(); myMesh->setSource(QUrl::fromLocalFile("/my/file/mesh.obj")); meshEntity->addComponent(myMesh);
-
Thanks a bunch, I'll try it out.
-
I have modified and implemented the example code you provided and it mostly works (thanks again)!. The only problem is that the mesh that appears in the window (I am using that basic shapes example I linked to as a base) is partially invisible when viewed from one side. The .obj mesh I am using to test this is a smooth curved surface with some spike like portions coming off it. When viewed from the one side in the viewer only the spikes are visible and not the curved surface. When opening the .obj file using the Windows 3D Builder application this is not the case, and the entire model can be viewed from any angle.
Is there some kind of interpretation setting that I need to change when loading the mesh? Is there more information contained within the Waveform file, other than just the mesh, that Windows 3D Builder sees but that I am not passing into the program with just the line:
myMesh->setSource(QUrl::fromLocalFile("/my/file/mesh.obj"));
?
Is there a setting I need to change with the camera?
Sorry I don't really have any experience with 3D modeling I am just trying to implement this viewer into another program as a quick tool for part of a larger purpose.
Here is the model in question: https://www.mediafire.com/?200ypclraebv4b9
and here is what the code pretty much ended up being:
//Model shape data Qt3DRender::QMesh * ModelMesh = new Qt3DRender::QMesh(); ModelMesh->setSource(QUrl::fromLocalFile("C:/Users/Chris/Model.obj")); //Model Transform Qt3DRender::QPhongMaterial *ModelMaterial = new Qt3DRender::QPhongMaterial(); ModelMaterial->setDiffuse(QColor(QRgb(0x665423))); // Model ModelEntity = new Qt3DCore::QEntity(m_rootEntity); ModelEntity->addComponent(ModelMesh); ModelEntity->addComponent(ModelMaterial);
-
@oblivioncth said:
Is there some kind of interpretation setting that I need to change when loading the mesh? Is there more information contained within the Waveform file, other than just the mesh, that Windows 3D Builder sees but that I am not passing into the program with just the line:
I'm not intimate with Qt3D, I had played a bit with it some time ago, but the API was rapidly changing so I decided to wait a bit. That said it's possible that your model contains data that couldn't be parsed by the object loader in Qt3D. Perhaps you should make a first attempt with a standard mesh, something from here? (I know for a fact that the
lamp.obj
file should be loaded without problems).Is there a setting I need to change with the camera?
Usually you set the projection type for the camera lens, but that should be already done if you're using the example as a base guide.
Aside from that your code looks fine to me.
-
Ok, thanks for the insight. Ill try some things.