Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

Loading a 3DS model with Qt3D

This 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.

Return to Tutorials.

Files:

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 5.0-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web