Loading a 3DS model with Qt3DThis tutorial shows how Qt3D can be used to load a simple model object in 3D Studio Max (3DS) format with a perspective camera view. We start by defining a class that inherits from QGLView, which provides some basic scene setup logic and 3D camera navigation: #include "qglview.h" class QGLAbstractScene; class QGLSceneNode; class ModelView : public QGLView { Q_OBJECT public: ModelView(QWindow *parent = 0) : QGLView(parent) {} ~ModelView(); protected: void initializeGL(QGLPainter *painter); void paintGL(QGLPainter *painter); private: QGLAbstractScene *m_scene; }; Refer first to the Teapot Example for the basics of using the QGLView class, lighting and so on. When the application starts up, we load the scene from its resource file, and store the result in a member variable, so we can refer to it in the paint function: void ModelView::initializeGL(QGLPainter *painter) { Q_UNUSED(painter); m_scene = QGLAbstractScene::loadScene(QLatin1String(":/penguin.3ds")); } In the teapot example we had to specify appropriate effects but the model loader sets appropriate effects on the scene for us. void ModelView::paintGL(QGLPainter *painter) { QGLSceneNode *o = m_scene->mainNode(); o->draw(painter); } Here in the paint function we call the draw() function of the scene's main object, in order to display the fully loaded model. This was really just two lines of code: one to load the model (once off, when the application initialized) and another line to display the model (every time the paint function is called). The result is pretty good for two lines of code, but it could stand some improvements. Here we are looking down onto the top of our penguin's head, and even when we stand him on his feet, he is too close to the camera to all fit in the frame. Let's make a few changes to have our penguin display nicely when the application opens. First of all, let move the camera away from the penguin and up so he fits in the frame and we can get a better angle on him, when the application loads. If we use dragging and so on in the QGLView this will change the camera from these intial settings, but this setup means the camera will be well positioned at the moment the application opens. We don't want to position the camera in the paintGL function, because that would defeat QGLViews camera dragging features and we would not be able to interact with the view properly. We'll also save the main object away in a member variable so that the overhead of searching the scene is not incurred every paint. Finally a pose for our penguin is calculated - its a turn around the x axis, so he is standing up on his feet; and a turn around the y axis, so he shows a bit more of his profile. The pose is calculated and stored as a quaternion - we want the x twist first so that goes last in the product of the two quaternions. Now all that remains in the updated paint function is to apply the new pose, and then paint the penguin. Files: |