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  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

glwidget.cpp Example File

opengl/framebufferobject2/glwidget.cpp
 /****************************************************************************
 **
 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
 ** All rights reserved.
 ** Contact: Nokia Corporation (qt-info@nokia.com)
 **
 ** This file is part of the examples of the Qt Toolkit.
 **
 ** $QT_BEGIN_LICENSE:BSD$
 ** You may use this file under the terms of the BSD license as follows:
 **
 ** "Redistribution and use in source and binary forms, with or without
 ** modification, are permitted provided that the following conditions are
 ** met:
 **   * Redistributions of source code must retain the above copyright
 **     notice, this list of conditions and the following disclaimer.
 **   * Redistributions in binary form must reproduce the above copyright
 **     notice, this list of conditions and the following disclaimer in
 **     the documentation and/or other materials provided with the
 **     distribution.
 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
 **     the names of its contributors may be used to endorse or promote
 **     products derived from this software without specific prior written
 **     permission.
 **
 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
 ** $QT_END_LICENSE$
 **
 ****************************************************************************/

 #include "glwidget.h"
 #include <QtGui/QImage>

 #include <math.h>

 static GLint cubeArray[][3] = {
     {0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0},
     {0, 0, 1}, {1, 0, 1}, {1, 1, 1}, {0, 1, 1},
     {0, 0, 0}, {1, 0, 0}, {1, 0, 1}, {0, 0, 1},
     {0, 1, 0}, {0, 1, 1}, {1, 1, 1}, {1, 1, 0},
     {0, 1, 0}, {0, 0, 0}, {0, 0, 1}, {0, 1, 1},
     {1, 0, 0}, {1, 1, 0}, {1, 1, 1}, {1, 0, 1}
 };

 static GLint cubeTextureArray[][2] = {
     {0, 0}, {1, 0}, {1, 1}, {0, 1},
     {0, 0}, {0, 1}, {1, 1}, {1, 0},
     {0, 0}, {1, 0}, {1, 1}, {0, 1},
     {1, 0}, {0, 0}, {0, 1}, {1, 1},
     {0, 0}, {1, 0}, {1, 1}, {0, 1},
     {1, 0}, {0, 0}, {0, 1}, {1, 1}
 };

 static GLint faceArray[][2] = {
     {1, -1}, {1, 1}, {-1, 1}, {-1, -1}
 };

 static GLubyte colorArray[][4] = {
     {102, 176, 54, 255},
     {81, 141, 41, 255},
     {62, 108, 32, 255},
     {45, 79, 23, 255}
 };

 GLWidget::GLWidget(QWidget *parent)
   : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
 {
     // create the framebuffer object - make sure to have a current
     // context before creating it
     makeCurrent();
     fbo = new QGLFramebufferObject(512, 512);
     timerId = startTimer(20);
     setWindowTitle(tr("OpenGL framebuffer objects 2"));
 }

 GLWidget::~GLWidget()
 {
     glDeleteLists(pbufferList, 1);
     delete fbo;
 }

 void GLWidget::initializeGL()
 {
     glMatrixMode(GL_MODELVIEW);

     glEnable(GL_CULL_FACE);

     glEnableClientState(GL_VERTEX_ARRAY);
     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
     glVertexPointer(3, GL_INT, 0, cubeArray);
     glTexCoordPointer(2, GL_INT, 0, cubeTextureArray);
     glColorPointer(4, GL_UNSIGNED_BYTE, 0, colorArray);

     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     glEnable(GL_BLEND);
     glEnable(GL_TEXTURE_2D);
     glEnable(GL_DEPTH_TEST);

     glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
     pbufferList = glGenLists(1);
     glNewList(pbufferList, GL_COMPILE);
     {
         glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

         // draw cube background
         glPushMatrix();
         glLoadIdentity();
         glTranslatef(0.5f, 0.5f, -2.0f);
         glDisable(GL_TEXTURE_2D);
         glEnableClientState(GL_COLOR_ARRAY);
         glVertexPointer(2, GL_INT, 0, faceArray);
         glDrawArrays(GL_QUADS, 0, 4);
         glVertexPointer(3, GL_INT, 0, cubeArray);
         glDisableClientState(GL_COLOR_ARRAY);
         glEnable(GL_TEXTURE_2D);
         glPopMatrix();

         // draw cube
         glTranslatef(0.5f, 0.5f, 0.5f);
         glRotatef(3.0f, 1.0f, 1.0f, 1.0f);
         glTranslatef(-0.5f, -0.5f, -0.5f);
         glColor4f(0.9f, 0.9f, 0.9f, 1.0f);
         glDrawArrays(GL_QUADS, 0, 24);

         glPushMatrix(); // this state is popped back in the paintGL() function
     }
     glEndList();

     for (int i = 0; i < 3; ++i) {
         yOffs[i] = 0.0f;
         xInc[i] = 0.005f;
         rot[i] = 0.0f;
     }
     xOffs[0]= 0.0f;
     xOffs[1]= 0.5f;
     xOffs[2]= 1.0f;

     cubeTexture = bindTexture(QImage(":res/cubelogo.png"));

     glPushMatrix(); // push to avoid stack underflow in the first paintGL() call
 }

 void GLWidget::resizeGL(int w, int h)
 {
     glViewport(0, 0, w, h);
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     float aspect = w/(float)(h ? h : 1);
     glFrustum(-aspect, aspect, -1, 1, 10, 100);
     glTranslatef(-0.5f, -0.5f, -0.5f);
     glTranslatef(0.0f, 0.0f, -15.0f);
 }

 void GLWidget::paintGL()
 {
     glPopMatrix(); // pop the matrix pushed in the pbuffer list

     // push the projection matrix and the entire GL state before
     // doing any rendering into our framebuffer object
     glPushAttrib(GL_ALL_ATTRIB_BITS);
     glMatrixMode(GL_PROJECTION);
     glPushMatrix();

     glViewport(0, 0, fbo->size().width(), fbo->size().height());
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     glOrtho(-1, 1, -1, 1, -99, 99);
     glTranslatef(-0.5f, -0.5f, 0.0f);
     glMatrixMode(GL_MODELVIEW);

     // render to the framebuffer object
     fbo->bind();
     glBindTexture(GL_TEXTURE_2D, cubeTexture);
     glCallList(pbufferList);
     fbo->release();

     // pop the projection matrix and GL state back for rendering
     // to the actual widget
     glPopAttrib();
     glMatrixMode(GL_PROJECTION);
     glPopMatrix();

     glBindTexture(GL_TEXTURE_2D, fbo->texture());
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

     // draw the background
     glMatrixMode(GL_MODELVIEW);
     glPushMatrix();
     glLoadIdentity();
     glMatrixMode(GL_PROJECTION);
     glPushMatrix();
     glLoadIdentity();

     glVertexPointer(2, GL_INT, 0, faceArray);
     glTranslatef(-1.2f, -0.8f, 0.0f);
     glScalef(0.2f, 0.2f, 0.2f);
     for (int y = 0; y < 5; ++y) {
         for (int x = 0; x < 5; ++x) {
             glTranslatef(2.0f, 0, 0);
             glColor4f(0.8f, 0.8f, 0.8f, 1.0f);
             glDrawArrays(GL_QUADS, 0, 4);
         }
         glTranslatef(-10.0f, 2.0f, 0);
     }
     glVertexPointer(3, GL_INT, 0, cubeArray);

     glPopMatrix();
     glMatrixMode(GL_MODELVIEW);

     // draw the bouncing cubes
     drawCube(0, 0.0f, 1.5f, 2.5f, 1.5f);
     drawCube(1, 1.0f, 2.0f, 2.5f, 2.0f);
     drawCube(2, 2.0f, 3.5f, 2.5f, 2.5f);
     glPopMatrix();
 }

 void GLWidget::drawCube(int i, GLfloat z, GLfloat rotation, GLfloat jmp, GLfloat amp)
 {
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
     glTranslatef(xOffs[i], yOffs[i], z);
     glTranslatef(0.5f, 0.5f, 0.5f);
     GLfloat scale = 0.75 + i*(0.25f/2);
     glScalef(scale, scale, scale);
     glRotatef(rot[i], 1.0f, 1.0f, 1.0f);
     glTranslatef(-0.5f, -0.5f, -0.5f);

     glColor4f(1.0f, 1.0f, 1.0f, 0.8f);
     glDrawArrays(GL_QUADS, 0, 24);

     if (xOffs[i] > 1.0f || xOffs[i] < -1.0f) {
         xInc[i] = -xInc[i];
         xOffs[i] = xOffs[i] > 1.0f ? 1.0f : -1.0f;
     }
     xOffs[i] += xInc[i];
     yOffs[i] = qAbs(cos((-3.141592f * jmp) * xOffs[i]) * amp) - 1;
     rot[i] += rotation;
 }
Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. Les développeurs détestent-ils les antivirus ? Un programmeur manifeste sa haine envers ces solutions de sécurité 26
  2. «Le projet de loi des droits du développeur» : quelles conditions doivent remplir les entreprises pour que le développeur puisse réussir ? 61
  3. Une nouvelle ère d'IHM 3D pour les automobiles, un concept proposé par Digia et implémenté avec Qt 3
  4. Qt Creator 2.5 est sorti en beta, l'EDI supporte maintenant plus de fonctionnalités de C++11 2
  5. PySide devient un add-on Qt et rejoint le Qt Project et le modèle d'open gouvernance 1
  6. Vingt sociétés montrent leurs décodeurs basés sur Qt au IPTV World Forum, en en exploitant diverses facettes (déclaratif, Web, widgets) 0
  7. Thread travailleur avec Qt en utilisant les signaux et les slots, un article de Christophe Dumez traduit par Thibaut Cuvelier 1
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 99
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Pourquoi les programmeurs sont-ils moins payés que les gestionnaires de programmes ? Manquent-ils de pouvoir de négociation ? 50
  4. Les développeurs détestent-ils les antivirus ? Un programmeur manifeste sa haine envers ces solutions de sécurité 26
  5. «Le projet de loi des droits du développeur» : quelles conditions doivent remplir les entreprises pour que le développeur puisse réussir ? 61
  6. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  7. Qt Commercial : Digia organise un webinar gratuit le 27 mars sur la conception d'interfaces utilisateur et d'applications avec le framework 0
Page suivante

Le Qt Developer Network au hasard

Logo

Combiner licence, à propos et fermer d'une dernière manière

Le Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

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 4.7
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