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  · 

Applying Transformations and Materials

In this tutorial we will apply transformations and materials to a simple cube object to demonstrate how to modify the QGLPainter state to achieve different effects.

Cube in QML

It is quite simple to achieve these effects in Qt3D:

 import QtQuick 2.0
 import Qt3D 1.0
 import Qt3D.Shapes 1.0

 Viewport {
     width: parent.width; height: parent.height
     fillColor: "darkblue"
     renderMode: "DirectRender"
     objectName: "cube viewport"

     Cube {
         scale: 1.5
         objectName: "cube"

         transform: Rotation3D {
             angle: 45
             axis: Qt.vector3d(1, 1, 1)
         }

         effect: Effect {
             color: "#aaca00"
             texture: "qtlogo.png"
             decal: true
         }
     }
 }

Cube in C++

In the C++ side of Qt3D we start by declaring a view class to show our cube on the screen:

 class CubeView : public QGLView
 {
     Q_OBJECT
 public:
     CubeView(QWindow *parent = 0);
     ~CubeView();

 protected:
     void paintGL(QGLPainter *painter);

 private:
     QGLSceneNode *cube;
 };

QGLView provides most of the logic for initializing the view, setting the camera position, and handling navigation via keyboard and mouse events. In the constructor we use QGLBuilder to create the geometry for the cube object that we will be using in later steps:

 CubeView::CubeView(QWindow *parent)
     : QGLView(parent)
 {
     QGLBuilder builder;
     builder << QGL::Faceted;
     builder << QGLCube();

     cube = builder.finalizedSceneNode();
 }

Then it is a simple matter to draw the object in our paintGL() method:

 void CubeView::paintGL(QGLPainter *painter)
 {
     cube->draw(painter);
 }

If we run the program now, we get the following output, which isn't very cube-like:

The problem is that we are looking at the cube straight onto its front face. So the rest of the cube is hidden from our view and it looks like a square. Let's modify the modelview transformation matrix a little bit to apply a 45 degree rotation around the axis (1, 1, 1):

 void CubeView::paintGL(QGLPainter *painter)
 {
     painter->modelViewMatrix().rotate(45.0f, 1.0f, 1.0f, 1.0f);
     cube->draw(painter);
 }

Now the results are a little better:

The cube is still a little odd-looking however. This is because up until now we have been using the default flat color effect in QGLPainter that colors the faces with a uniform color (white in this case). So let's change to a lit material effect with a nice green color:

 void CubeView::paintGL(QGLPainter *painter)
 {
     painter->setStandardEffect(QGL::LitMaterial);
     painter->setFaceColor(QGL::AllFaces, QColor(170, 202, 0));

     painter->modelViewMatrix().rotate(45.0f, 1.0f, 1.0f, 1.0f);
     cube->draw(painter);
 }

That's much better; now it looks like a cube:

To complete this tutorial, let's make the cube a little more interesting by adding a texture to the side:

 class CubeView : public QGLView
 {
     ...
 private:
     QGLSceneNode *cube;
     QGLTexture2D logo;
 };
 CubeView::CubeView(QWindow *parent)
     : QGLView(parent)
 {
     ...
     logo.setImage(QImage(QLatin1String(":/qtlogo.png")));
 }
 void CubeView::paintGL(QGLPainter *painter)
 {
     painter->setStandardEffect(QGL::LitDecalTexture2D);
     painter->setFaceColor(QGL::AllFaces, QColor(170, 202, 0));
     logo.bind();

     painter->modelViewMatrix().rotate(45.0f, 1.0f, 1.0f, 1.0f);
     cube->draw(painter);
 }

Return to the main Tutorials page.

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