2

I am trying to create a minimal obj file viewer. Following is my code mostly copy pasted with some minor modifications.

#include <QGuiApplication>
#include <QApplication>

#include <Qt3DCore/QEntity>
#include <Qt3DCore/QTransform>
#include <Qt3DCore/QAspectEngine>

#include <Qt3DRender/qrenderaspect.h>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QMaterial>
#include <Qt3DRender/QMesh>

#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DExtras/QTorusMesh>
#include <Qt3DExtras/QOrbitCameraController>
#include <Qt3DExtras/QPhongMaterial>

Qt3DCore::QEntity* createTestScene()
{
    Qt3DCore::QEntity* root = new Qt3DCore::QEntity;
    Qt3DCore::QEntity* torus = new Qt3DCore::QEntity(root);

    Qt3DRender::QMesh* mesh  = new Qt3DRender::QMesh(root);
    mesh->setSource(QUrl::fromLocalFile("/Users/neel/model.obj"));

    Qt3DCore::QTransform* transform = new Qt3DCore::QTransform;
//    transform->setScale3D(QVector3D(1.5, 1, 0.5));
    transform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1,0,0), 45.f ));

    Qt3DRender::QMaterial* material = new Qt3DExtras::QPhongMaterial(root);

    torus->addComponent(mesh);
    torus->addComponent(transform);
    torus->addComponent(material);

    return root;
}

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
    Qt3DExtras::Qt3DWindow view;
    Qt3DCore::QEntity* scene = createTestScene();

    // camera
    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));

    // manipulator
    Qt3DExtras::QOrbitCameraController* manipulator = new Qt3DExtras::QOrbitCameraController(scene);
    manipulator->setLinearSpeed(50.f);
    manipulator->setLookSpeed(180.f);
    manipulator->setCamera(camera);

    view.setRootEntity(scene);
    view.show();

    return app.exec();
}

With the above I can load obj files but there are two problems.

I can Pan and zoom the model, however I cannot rotate using my mouse or trackpad. How can I add that functionality.

My obj files have different colours in them. With this code what I see is only black in. the entire model. In other obj viewers I can see multiple colours.

Neel Basu
  • 12,638
  • 12
  • 82
  • 146
  • When you say your obj files have different colours in them, do you mean they have per vertex coloring or are you talking about an mtl file with a diffuse texture? – Cinder Biscuits Nov 06 '17 at 14:25
  • Also, I don't see a camera controller anywhere in the code you've pasted. You'll need to give us more information where you have added the camera. – Cinder Biscuits Nov 06 '17 at 14:26
  • Well I am exporting a 3d model as obj from Mathematica. It is a set of cuboids of multiple colors and sizes. That is a visualisation of my solution. I can view the obj file through other obj file viewers and it shows in color. I do not know anything about 3d. I only know qt, C++ and Mathematica, and I need to show that obj in my application. This is the only code I am using. I merely changed few lines of code. Most of them are copy pasted. This is the only very small part of my application, that needs 3d. It looks like I am using QOrbitCameraController. – Neel Basu Nov 06 '17 at 15:23
  • Alright, from http://reference.wolfram.com/language/ref/format/OBJ.html we can surmise that the color is per-face. In this case you wouldn't use the QPhongMaterial class, you should use the QPerVertexColorMaterial class which reads color information from the vertex data. https://doc.qt.io/qt-5/qt3dextras-qpervertexcolormaterial.html The phong shader just does simple blinn-phong lighting. – Cinder Biscuits Nov 06 '17 at 15:31
  • 1
    replaced `QPhongMaterial` with `QPerVertexColorMaterial`, now it looks completely black, no light no shades. – Neel Basu Nov 06 '17 at 16:35
  • I overlooked, Whenever I export my model as `obj`, but it actually creates two files. one `model.obj` and another `model.obj.mtl`. I opened the `obj` in Text Editor. It looks like it is referring to the mil file for colour informations. – Neel Basu Nov 06 '17 at 17:13

1 Answers1

0

you need to look on the sceneloader function in Qt for such loading process. i mean with mtl fui

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 09 '22 at 06:33