Qt 4.7 - glTexSubImage2D返回GL_INVALID_OPERATION

时间:2014-12-16 10:10:30

标签: c++ qt opengl

我在Qt 4.7.2中使用QGLWidget通过OpenGL绘制自己的图片。我使用OpenGL是因为我希望快速扩展图像并降低CPU使用率。

为此,我有三种方法。一个用于初始化OpenGL环境,一个用于从不同的线程更新我的图像数据,另一个用于绘制图像本身。

问题是在我的updateImage方法中,glTexSubImage2D()的调用返回GL_INVALID_OPERATION错误。它试图用gDEBugger调试它,但只得到这一个错误。如果我查看我的程序的上下文1,在调用glTexSubImage2D()后,我只看到大小为0x0的纹理;

初始化OpenGL

void Class::OpenGLInit(void){
   QGLContext* context = const_cast<QGLContext *>(QGLContext::currentContext());
   if (!context)
   {
    qDebug() << "Can not get current OpenGL context";
    return;
   }
   context->makeCurrent();
   glGenTextures(1,&texture);
   glBindTexture(GL_TEXTURE_2D, texture);
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_BASE_LEVEL,0);
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAX_LEVEL,0);
   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1024, 1024, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, NULL);
   glBindTexture(GL_TEXTURE_2D,0);
}

更新图片

void Class::updateImage(void){
   dataPointer = (u_int8_t*) malloc(1024*1024*4);
   memcpy(dataPointer,externalBuffer,1024*1024*4);

   glBindTexture(GL_TEXTURE_2D, texture);
   glTexSubImage2D(GL_TEXTURE_2D,0, 0,0,1024,1024,GL_RGBA,GL_UNSIGNED_INT_8_8_8_8,dataPointer);
   //This line returns a GL_INVALID_OPERATION 
}

绘制图片

void Class::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    painter.beginNativePainting();
    glClearColor(0.0f,0.0f,0.0f,0.0f);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    //Draw black screen
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);
    glBegin(GL_QUADS);
        //Draw Picture
        glTexCoord2f(0, 0);
        glVertex2f(0, 0);
        glTexCoord2f(1024, 0);
        glVertex2f(1024, 0);
        glTexCoord2f(1024, 1024);
        glVertex2f(1024,1024);
        glTexCoord2f(0, 1024);
        glVertex2f(0, 1024);

glEnd();
glDisable(GL_TEXTURE_2D);
painter.endNativePainting();
}

1 个答案:

答案 0 :(得分:0)

我只是注意到你提到&#34;从另一个线程更新图像&#34;,对吗?您的错误可能是由于调用&#39; updateImage&#39;的线程中缺少OGL上下文。请尝试在与OGL上下文相同的线程中调用它,并使其成为当前。