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  · 

Teapot Example

The Teapot example shows how Qt3D can be used to draw a simple teapot object with a perspective camera view in C++. There is also a QML version of the teapot example.

We start by defining a class that inherits from QGLView, which provides some basic scene setup logic and 3D camera navigation:

 #include "qglview.h"
 #include "qglteapot.h"

 class QGLSceneNode;

 class TeapotView : public QGLView
 {
     Q_OBJECT
 public:
     TeapotView(QWindow *parent = 0) : QGLView(parent), teapot(0) {}
     ~TeapotView();

 protected:
     void initializeGL(QGLPainter *painter);
     void paintGL(QGLPainter *painter);

 private:
     QGLSceneNode *teapot;
 };
     ...
 int main(int argc, char *argv[])
 {
     QGuiApplication app(argc, argv);
     TeapotView view;
     return app.exec();
 }

When the application starts up, we set up some scene parameters in the initializeGL() function:

 void TeapotView::initializeGL(QGLPainter *painter)
 {
     painter->setStandardEffect(QGL::LitMaterial);

The first line of the function selects a standard rendering effect that lights material colors with the default OpenGL two-sided lighting algorithm and the default light.

The teapot member variable is an instance of QGLSceneNode, which we create using QGLBuilder during initializeGL():

     QGLBuilder builder;
     builder << QGLTeapot();
     teapot = builder.finalizedSceneNode();
 }

The QGLTeapot class represents the geometry for the teapot, which is added to the builder with the << operator.

We then call QGLBuilder::finalizedSceneNode() to finalize the object, prepare it to be uploaded to the GL server as a vertex buffer, and hand over ownership of the scene node.

We have to take care to clean up our scene after we're done with it and here that is done in the destructor:

 TeapotView::~TeapotView()
 {
     delete teapot;
 }

Finally, we paint the teapot every time the window is refreshed:

 void TeapotView::paintGL(QGLPainter *painter)
 {
     teapot->draw(painter);
 }

The QGLView class has in-built support for camera navigation using the mouse and keyboard. By clicking and dragging the mouse, the teapot can be rotated into any position. The image on the left shows the view in its startup default position, and the image on the right shows the view after rotation using the mouse:

Return to Examples.

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