0

下面的 QT 示例很简单:我试图找出为什么只有在应用程序被 MessageBox 计时时才会显示纹理。我正在使用 QT4.8.1 并在 linux 和 VS2008 下都尝试过。

#include "ui_qtgltest.h"
#include <QtGui/QApplication>
#include <QtGui/QMainWindow>
#include <QGraphicsPixmapItem>
#include <QGraphicsScene>
#include <QGLWidget>
#include <QMessageBox>
#include <QDebug>
#include <QTimer>

class GLItem : public QGraphicsPixmapItem  // an item that issue opengl commands
{
    GLuint texture[1] ;
    GLsizei W, H;
public:

    GLItem(QGraphicsItem *parent = NULL) : QGraphicsPixmapItem(parent){
            // when the following line is commented out, the texture doesn't show up.
        QMessageBox::warning(NULL,QString("???"),QString("hold on a second") );

        QImage img( "im0.png" ) ;
        QImage GL_formatted_image;
        GL_formatted_image = QGLWidget::convertToGLFormat(img);
        W = GL_formatted_image.width();
        H = GL_formatted_image.height();

        glGenTextures(1, texture);
        glBindTexture(GL_TEXTURE_2D, texture[0]);           
        glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA,
            GL_formatted_image.width(), GL_formatted_image.height(),
            0, GL_RGBA, GL_UNSIGNED_BYTE, GL_formatted_image.bits() );


        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); 
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
        glBindTexture(GL_TEXTURE_2D, 0);

    }

    ~GLItem(void){}

    void paint(QPainter *paint, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        paint->setRenderHint(QPainter::Antialiasing);
        paint->beginNativePainting();

        glClearColor( 0.5f, 0.5f, 0.5f, 0.0f);
        glClear( GL_COLOR_BUFFER_BIT );

        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
        glEnable    ( GL_BLEND ) ;
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, texture[0]);

        glBegin(GL_QUADS);
        glTexCoord2i( 1, 0 );    glColor3f(1,0,0); glVertex2f( W,    0.0f);
        glTexCoord2i( 1, 1 );    glColor3f(0,1,0); glVertex2f( W,    H);
        glTexCoord2i( 0, 1 );    glColor3f(0,0,1); glVertex2f( 0.0f, H);
        glTexCoord2i( 0, 0 );    glColor3f(1,0,1); glVertex2f( 0.0f, 0.0f);
        glEnd();

        paint->endNativePainting();
    }
};

class QTGL: public QMainWindow
{
public: 
    QTGL(QWidget *parent =0, Qt::WFlags flags=0) : QMainWindow(parent, flags)
    {
        ui.setupUi(this);
    }

    void setup() 
    {           
        QGLWidget   * glw   = new QGLWidget(QGLFormat(QGL::SampleBuffers)); 
        QGraphicsScene  * s = new QGraphicsScene();
        QGraphicsView   * g =  ui.graphicsView;
        g->setViewport(glw); 
        g->setScene(s);

        GLItem * gi = new GLItem();
        s->addItem( gi ); 


        QTimer  * timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), s, SLOT(update()));
        timer->start(10); 
    }   

    ~QTGL() {}

private:
    Ui::mainWindow ui;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTGL w;
    w.show();
    w.setup();
    return a.exec();
}

关于如何调试这个并弄清楚里面发生了什么的任何想法?

4

0 回答 0