/* CGIL Styles definitions for color coding */

Monday, January 08, 2007


Voici mon premier exemple de programme opengl en c++ en utilisant QT
pour le compiler il faut sauver les deux fichiers ci-dessous 01_2dsquare.pro et 01_2dsquare.cpp
dans un répertoire ensuite il faut lancer en ligne de commande :
qmake 01_2dsquare.pro
make
./01_2dsquare
ce programme est trivial dans le sens ou il ne fait qu'afficher un carré bleu centré sur la fenêtre,
mais il permet d'illustrer l'utilisation du widget QGLWidget pour utiliser du opengl avec la librairie QT
pratiquement il suffit d'implémenter une classe qui hérite de QGLWidget et dans cette sous-classe il faut implémenter les trois fonctions virtuelles de la classe parent :
initializeGL() qui permet d'initialiser le contexte opengl
resizeGL(int width, int height) qui est appelée à chaque redimensionnment de la fenêtre
paintGL() ou l'on dit ce que l'on veut dessiner et qui est appelée chaque fois que le widget qt doit être affiché
*******************************************************************************************
# Filename : 01_2dsquare.pro
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
QT += opengl
# Input
SOURCES += 01_2dsquare.cpp

/***********************************************************************
* QT OpenGL tutorial    : 01_2dsquare.cpp
* Autor : cgil
* Date : 20070107
* Very simple example to draw a square with opengl using Qt widget QGLWidget
******************************************************************/
#include <QApplication>
#include <QGLWidget>

// CgQtOpenGl is my simple subclass from QGLWidget
class CgQtOpenGl : public QGLWidget
{
public:
    CgQtOpenGl(QWidget *parent)  : QGLWidget(parent)  {        
    }
    
    CgQtOpenGl()  : QGLWidget()  {         
    }
       
    QSize sizeHint() const {
        return QSize(200, 200);
    }

protected:
    QColor cgColorBG;
    QColor cgColorFG;
    
    // initializeGL : Sets up the OpenGL rendering context & other init stuff like display lists
    //                      its  called once before the first time resizeGL() or paintGL() is called.
    void initializeGL()  {         
        cgColorBG = QColor::fromRgb(0,0,0,0) ;  // for BackGround Color
        cgColorFG = QColor::fromRgb(0,0,255,0) ; // for ForeGround Color
        qglClearColor(cgColorBG);       // note here that we use the QT qglClearColor instead of glClearColor to be able to use QColor object
        glEnable(GL_DEPTH_TEST);
        glShadeModel(GL_FLAT);         
    }
    
    // resizeGL : Sets up the OpenGL viewport, projection, etc.
    //                 its called whenever the the widget has been resized & when widget is first shown (auto resize event)
    void resizeGL(int width, int height)  {         
        int side = qMin(width, height);
        glViewport((width - side) / 2, (height - side) / 2, side, side);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0.0, 1.0, 0.0,1.0, -1.0, 15.0);
        glMatrixMode(GL_MODELVIEW);         
    }

    // paintGL : Renders the OpenGL scene
    //
    void paintGL() {
         // draw the scene:
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();
        
        qglColor(cgColorFG);        
        glBegin(GL_POLYGON);
            glVertex3f (0.25, 0.25, 0.0);
            glVertex3f (0.75, 0.25, 0.0);
            glVertex3f (0.75, 0.75, 0.0);
            glVertex3f (0.25, 0.75, 0.0);
        glEnd();
        glFlush ();
    }
};

int main(int argc, char *argv[])
{
        
    QApplication app(argc, argv);
    CgQtOpenGl glWidget ;
       
    glWidget.setWindowTitle("Hello OpenGl");
    
    glWidget.show();
    return app.exec();
}

No comments: