- #include <QtGui> 
- #include <QtOpenGL> 
- #include <QGLWidget> 
-   
- #include <math.h> 
-   
- #include "glwidget.h" 
-   
-   
- GLWidget ::GLWidget(QWidget *- parent )
- { 
- 	clearColor = Qt::red; 
-     yRot = 0; 
-     connect(timer, SIGNAL(timeout()), this, SLOT(animate())); 
-     timer->start(5); 
- } 
-   
- GLWidget::~GLWidget() 
- { 
-         makeCurrent(); 
-         glDeleteLists(object, 1); 
- } 
-   
- QSize-  GLWidget ::minimumSizeHint() const
 
- { 
- } 
-   
- QSize-  GLWidget ::sizeHint() const
 
- { 
- } 
-   
-   
-   
- void-  GLWidget ::setClearColor(const QColor &- color )
 
- { 
-     clearColor = color; 
-     updateGL(); 
- } 
-   
- void GLWidget::initializeGL() 
- { 
-   
-     object = makeObject(); 
-     glEnable(GL_DEPTH_TEST); 
-     glEnable(GL_CULL_FACE); 
-     glEnable(GL_TEXTURE_2D); 
- } 
-   
- void GLWidget::paintGL() 
- { 
-     qglClearColor(clearColor); 
-     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
-     glLoadIdentity(); 
-     glTranslated(0.0, 0.0, -12.0); 
-     glRotated(yRot, 0.0, 1.0, 0.0); 
-     glCallList(object); 
- } 
-   
- void GLWidget::resizeGL(int width, int height) 
- { 
-     glViewport(0, 0, width, height); 
-   
- 	glMatrixMode(GL_PROJECTION); 
-     glLoadIdentity(); 
- 	glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0); 
-     glMatrixMode(GL_MODELVIEW); 
-     glLoadIdentity(); 
- } 
-   
-   
-   
- GLuint GLWidget::makeObject() 
- { 
-     static const int coords[4][4][3] = { 
-         { { +1, -1, -1 }, { -1, -1, -1 }, { -1, +1, -1 }, { +1, +1, -1 } }, 
-         { { +1, -1, +1 }, { +1, -1, -1 }, { +1, +1, -1 }, { +1, +1, +1 } }, 
-         { { -1, -1, -1 }, { -1, -1, +1 }, { -1, +1, +1 }, { -1, +1, -1 } }, 
-         { { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } } 
-     }; 
-   
-   
-     GLuint textures[4]; 
-     for (int j=0; j < 4; ++j) 
-         textures [- j ] =-  bindTexture (QPixmap(QString(":/images/triple%1.png")- . arg(- j  + 1))- , 
-                                   GL_TEXTURE_2D); 
-   
-     GLuint list = glGenLists(1); 
-     glNewList(list, GL_COMPILE); 
-     for (int i = 0; i < 4; ++i) { 
-         glBindTexture(GL_TEXTURE_2D, textures[i]); 
-         glBegin(GL_QUADS); 
-         for (int j = 0; j < 4; ++j) { 
-             glTexCoord2d(j == 0 || j == 3, j == 0 || j == 1); 
-             glVertex3d(0.2 * coords[i][j][0], 0.2 * coords[i][j][1], 
-                        0.2 * coords[i][j][2]); 
-         } 
-         glEnd(); 
-     } 
-   
-     glEndList(); 
-     return list; 
- } 
-   
- void GLWidget::animate() 
- { 
-   
-    yRot += 1.0f; 
-    updateGL(); 
- } 
        #include <QtGui>
#include <QtOpenGL>
#include <QGLWidget>
#include <math.h>
#include "glwidget.h"
GLWidget::GLWidget(QWidget *parent)
    : QGLWidget(parent)
{
	clearColor = Qt::red;
    yRot = 0;
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(animate()));
    timer->start(5);
}
GLWidget::~GLWidget()
{
        makeCurrent();
        glDeleteLists(object, 1);
}
QSize GLWidget::minimumSizeHint() const
{
    return QSize(50, 50);
}
QSize GLWidget::sizeHint() const
{
    return QSize(200, 200);
}
void GLWidget::setClearColor(const QColor &color)
{
    clearColor = color;
    updateGL();
}
void GLWidget::initializeGL()
{
    object = makeObject();
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glEnable(GL_TEXTURE_2D);
}
void GLWidget::paintGL()
{
    qglClearColor(clearColor);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslated(0.0, 0.0, -12.0);
    glRotated(yRot, 0.0, 1.0, 0.0);
    glCallList(object);
}
void GLWidget::resizeGL(int width, int height)
{
    glViewport(0, 0, width, height);
	glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
	glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
GLuint GLWidget::makeObject()
{
    static const int coords[4][4][3] = {
        { { +1, -1, -1 }, { -1, -1, -1 }, { -1, +1, -1 }, { +1, +1, -1 } },
        { { +1, -1, +1 }, { +1, -1, -1 }, { +1, +1, -1 }, { +1, +1, +1 } },
        { { -1, -1, -1 }, { -1, -1, +1 }, { -1, +1, +1 }, { -1, +1, -1 } },
        { { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } }
    };
    GLuint textures[4];
    for (int j=0; j < 4; ++j)
        textures[j] = bindTexture(QPixmap(QString(":/images/triple%1.png").arg(j + 1)),
                                  GL_TEXTURE_2D);
    GLuint list = glGenLists(1);
    glNewList(list, GL_COMPILE);
    for (int i = 0; i < 4; ++i) {
        glBindTexture(GL_TEXTURE_2D, textures[i]);
        glBegin(GL_QUADS);
        for (int j = 0; j < 4; ++j) {
            glTexCoord2d(j == 0 || j == 3, j == 0 || j == 1);
            glVertex3d(0.2 * coords[i][j][0], 0.2 * coords[i][j][1],
                       0.2 * coords[i][j][2]);
        }
        glEnd();
    }
    glEndList();
    return list;
}
void GLWidget::animate()
{
   yRot += 1.0f;
   updateGL();
}
To copy to clipboard, switch view to plain text mode 
  
Bookmarks