2015-06-09 68 views
2

我想創建一個2d紋理顯示在我的Qt/OpenGL應用程序。然後我創建了一個填充屏幕來放置它的四合一,但我所有我得到的是一個沒有任何錯誤的白色屏幕。有沒有人看到我的代碼有什麼問題?試圖加載與glTexImage2D 2d紋理,但只是變得空白

#include "AppGLWidget.h" 

#include <iostream> 

AppGLWidget::AppGLWidget(QWidget *parent) : QGLWidget(parent), 
    mnRange(100), 
    mcDisplayedImage(512,512,QImage::Format_ARGB32) 
{ 
    mcDisplayedImage.fill(qRgb(100,0,0)); 

    mcDisplayedImage = QGLWidget::convertToGLFormat(mcDisplayedImage); 

    int value; 
    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value); 
} 

AppGLWidget::~AppGLWidget(void) 
{ 
} 

void AppGLWidget::initializeGL() { 
    glClearColor(0, 1, 0, 1); 
    glPushMatrix(); 
    glOrtho(0, size().width(), size().height(), 0, -1, 1); 
    glViewport(0, 0, (GLint)size().width(), (GLint)size().height()); 
    glDisable(GL_DEPTH_TEST); 
    glShadeModel(GL_FLAT); 

    glEnable(GL_TEXTURE_2D); 
    glGenTextures(1,&mcTextureObj); 
    glBindTexture(GL_TEXTURE_2D,mcTextureObj);  
    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_MAG_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);  
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mcDisplayedImage.width(), mcDisplayedImage.height(), 0, 
     GL_RGBA, GL_UNSIGNED_BYTE, NULL); 

    glDisable(GL_TEXTURE_2D); 

    // check OpenGL error 
    GLenum err; 
    while ((err = glGetError()) != GL_NO_ERROR) { 
    std::cerr << "OpenGL error: " << err << std::endl; 
    } 
} 

void AppGLWidget::resizeGL(int w, int h) 
{ 
    glPopMatrix(); 
    glPushMatrix(); 
    glOrtho(0, w, h, 0, -1, 1); 
    glViewport(0, 0, w, h); 
} 

void AppGLWidget::paintGL() { 

    glBindTexture(GL_TEXTURE_2D, mcTextureObj); 

    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, mcDisplayedImage.width(), mcDisplayedImage.height(), 
    GL_RGBA, GL_UNSIGNED_BYTE, mcDisplayedImage.bits()); 

    glBegin(GL_QUADS); 

    glTexCoord2i(0,0); glVertex2i(0,lnHeight); 
    glTexCoord2i(0,1); glVertex2i(0,0); 
    glTexCoord2i(1,1); glVertex2i(lnWidth,0); 
    glTexCoord2i(1,0); glVertex2i(lnWidth,lnHeight); 

    glEnd(); 

    // check OpenGL error 
    GLenum err; 
    while ((err = glGetError()) != GL_NO_ERROR) { 
    std::cerr << "OpenGL error: " << err << std::endl; 
    } 
} 

回答

2

GL_TEXTURE_2D需要是glEnable()倒是進行渲染。

現在你是glDisable()它在initializeGL()並離開它。

+0

謝謝,我愛你。 – CptanPanic