Qt QGLWidget GLuint数组内存对齐问题

时间:2012-11-12 04:42:44

标签: qt4 texture-mapping memory-alignment qglwidget

我使用QGLWidget子类跟随Qt中的NeHe OGL tutorials。到目前为止,我还没有遇到任何问题,并且在纹理映射的6th tutorial上他引入了旋转框的纹理。我把所有东西都搞定了,然后试着把我自己的纹理添加到立方体的每一边。我使用QGLWidget :: bindTexture()函数加载我通过Qt资源系统存储的256x256 bmp文件,并将GLuint id保存在私有成员数组中。在paintGL()中,我使用GL_TEXTURE_2D和GLuint数组元素之一调用glBindTexture()作为参数,然后为方形的每个面调用glBegin(GL_QUADS)和glEnd()。它编译得很好,但是盒子要么呈现完全白色(好像它可能无法找到纹理),否则我会在旋转过程中产生疯狂的黑白瑕疵,并在屏幕上随机拍摄。

虽然我在设法通过在我的类声明的末尾添加另一个私有数据成员来解决问题,当事情神奇地开始工作时,并决定在类声明中将我的GLuint纹理数组的声明移动到更高的位置似乎工作。我对记忆调整一无所知,这似乎是导致事情发生的最明显原因,我希望能解释这里发生的事情。

如果重要的话,这是在Qt 4.6上,在GCC 4.4.0和MinGW 5.1.6中的GNU Make 3.8.1上编译的(我应该升级)。

代码: glwidget.hpp,在底部评论的违规声明

    #ifndef GLWIDGET_HPP
    #define GLWIDGET_HPP

    #include <QtOpenGL>

    class QTimer;

    class GLWidget : public QGLWidget
    {
    Q_OBJECT

    public:
        GLWidget( QWidget *parent = 0 );
        ~GLWidget();

    protected:
        void initializeGL();
        void paintGL();
        void resizeGL( int width, int height );

    private:
        void drawCube();
        void loadTextures();

        QTimer  *m_timer;
        GLuint  m_textures[6]; // doesn't cause rendering problems here
        GLfloat m_xrot, m_yrot, m_zrot;
    //  GLuint  m_textures[6]; // causes rendering problems if declared here
    };

    #endif

glwidget.cpp

    #include "glwidget.hpp"

    #include <QTimer>

    GLWidget::GLWidget( QWidget *parent )
        : QGLWidget( QGLFormat(QGL::SampleBuffers), parent )
    {
        m_timer = new QTimer( this );
        m_timer->setSingleShot( false );
        connect( m_timer, SIGNAL(timeout()), this, SLOT(updateGL()) );
        m_timer->start( 7 );

        setWindowTitle( "NeHe Lesson 6" );
    }

    GLWidget::~GLWidget()
    {
        glDeleteTextures( 6, m_textures );
    }

    void GLWidget::initializeGL()
    {
        glShadeModel( GL_SMOOTH );  // Enable smooth shading
        qglClearColor( Qt::black );
        glClearDepth( 1.0f );   // Depth buffer setup
        glEnable( GL_DEPTH_TEST );
        glEnable( GL_TEXTURE_2D );
        glDepthFunc( GL_LEQUAL );
        glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );

        loadTextures();
    }

    void GLWidget::loadTextures()
    {
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

        m_textures[0] = bindTexture( QPixmap(":/img/scifi.bmp") );
        m_textures[1] = bindTexture( QPixmap(":/img/scifi2.bmp") );
        m_textures[2] = bindTexture( QPixmap(":/img/scifi3.bmp") );
        m_textures[3] = bindTexture( QPixmap(":/img/scifi4.bmp") );
        m_textures[4] = bindTexture( QPixmap(":/img/scifi5.bmp") );
        m_textures[5] = bindTexture( QPixmap(":/img/scifi6.bmp") );
    }

    void GLWidget::drawCube()
    {
        // Front face
        glBindTexture( GL_TEXTURE_2D, m_textures[0] );
        glBegin( GL_QUADS );
            glNormal3f( 0.0f, 0.0f, 1.0f );
            glTexCoord2f( 0.0f, 0.0f ); glVertex3f(-1.0f,-1.0f, 1.0f ); // Bottom left
            glTexCoord2f( 1.0f, 0.0f ); glVertex3f( 1.0f,-1.0f, 1.0f ); // Bottom right
            glTexCoord2f( 1.0f, 1.0f ); glVertex3f( 1.0f, 1.0f, 1.0f ); // Top right
            glTexCoord2f( 0.0f, 1.0f ); glVertex3f(-1.0f, 1.0f, 1.0f ); // Top left
        glEnd();

        // Back face
        glBindTexture( GL_TEXTURE_2D, m_textures[1] );
        glBegin( GL_QUADS );
            glNormal3f( 0.0f, 0.0f, -1.0f );
            glTexCoord2f( 1.0f, 0.0f ); glVertex3f(-1.0f,-1.0f,-1.0f ); // Bottom right
            glTexCoord2f( 1.0f, 1.0f ); glVertex3f(-1.0f, 1.0f,-1.0f ); // Top right
            glTexCoord2f( 0.0f, 1.0f ); glVertex3f( 1.0f, 1.0f,-1.0f ); // Top left
            glTexCoord2f( 0.0f, 0.0f ); glVertex3f( 1.0f,-1.0f,-1.0f ); // Bottom left
        glEnd();

        // Top Face
        glBindTexture( GL_TEXTURE_2D, m_textures[2] );
        glBegin( GL_QUADS );
            glNormal3f( 0.0f, 1.0f, 0.0f );
            glTexCoord2f( 0.0f, 1.0f ); glVertex3f(-1.0f, 1.0f,-1.0f ); // Top left
            glTexCoord2f( 0.0f, 0.0f ); glVertex3f(-1.0f, 1.0f, 1.0f ); // Bottom left
            glTexCoord2f( 1.0f, 0.0f ); glVertex3f( 1.0f, 1.0f, 1.0f ); // Bottom right
            glTexCoord2f( 1.0f, 1.0f ); glVertex3f( 1.0f, 1.0f,-1.0f ); // Top right
        glEnd();

        // Bottom Face
        glBindTexture( GL_TEXTURE_2D, m_textures[3] );
        glBegin( GL_QUADS );
            glNormal3f( 0.0f, -1.0f, 0.0f );
            glTexCoord2f( 1.0f, 1.0f ); glVertex3f(-1.0f,-1.0f,-1.0f ); // Top right
            glTexCoord2f( 0.0f, 1.0f ); glVertex3f( 1.0f,-1.0f,-1.0f ); // Top left
            glTexCoord2f( 0.0f, 0.0f ); glVertex3f( 1.0f,-1.0f, 1.0f ); // Bottom left
            glTexCoord2f( 1.0f, 0.0f ); glVertex3f(-1.0f,-1.0f, 1.0f ); // Bottom right
        glEnd();

        // Right face
        glBindTexture( GL_TEXTURE_2D, m_textures[4] );
        glBegin( GL_QUADS );
            glNormal3f( 1.0f, 0.0f, 0.0f );
            glTexCoord2f( 1.0f, 0.0f ); glVertex3f( 1.0f,-1.0f,-1.0f ); // Bottom right
            glTexCoord2f( 1.0f, 1.0f ); glVertex3f( 1.0f, 1.0f,-1.0f ); // Top right
            glTexCoord2f( 0.0f, 1.0f ); glVertex3f( 1.0f, 1.0f, 1.0f ); // Top left
            glTexCoord2f( 0.0f, 0.0f ); glVertex3f( 1.0f,-1.0f, 1.0f ); // Bottom left
        glEnd();

        // Left Face
        glBindTexture( GL_TEXTURE_2D, m_textures[5] );
        glBegin( GL_QUADS );
            glNormal3f( -1.0f, 0.0f, 0.0f );
            glTexCoord2f( 0.0f, 0.0f ); glVertex3f(-1.0f,-1.0f,-1.0f ); // Bottom left
            glTexCoord2f( 1.0f, 0.0f ); glVertex3f(-1.0f,-1.0f, 1.0f ); // Bottom right
            glTexCoord2f( 1.0f, 1.0f ); glVertex3f(-1.0f, 1.0f, 1.0f ); // Top right
            glTexCoord2f( 0.0f, 1.0f ); glVertex3f(-1.0f, 1.0f,-1.0f ); // Top left
        glEnd();
    }

    void GLWidget::paintGL()
    {
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        glLoadIdentity();
        glTranslatef( 0.0f, 0.0f, -5.0f );
        glRotatef( m_xrot, 1.0f, 0.0f, 0.0f );
        glRotatef( m_yrot, 0.0f, 1.0f, 0.0f );
        glRotatef( m_zrot, 0.0f, 0.0f, 1.0f );
        drawCube();

        m_xrot += 0.3f;
        m_yrot += 0.2f;
        m_zrot += 0.4f;
    }

    void GLWidget::resizeGL( int width, int height )
    {
        if( height == 0 )
            height = 1;

        glViewport( 0, 0, width, height );
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        gluPerspective( 45.0f, static_cast<GLfloat>(width)/height, 0.1f, 100.0f );
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
    }

0 个答案:

没有答案