QGLBuffer :: map返回NULL?

时间:2011-12-09 21:58:10

标签: c++ qt opengl

我正在尝试使用QGLbuffer来显示图像 序列类似于:

initializeGL() {
  glbuffer= QGLBuffer(QGLBuffer::PixelUnpackBuffer);        
  glbuffer.create();
  glbuffer.bind();
  glbuffer.allocate(image_width*image_height*4); // RGBA
  glbuffer.release();
}

// Attempting to write an image directly the graphics memory.
// map() should map the texture into the address space and give me an address in the   
// to write directly to  but always returns NULL
unsigned char* dest = glbuffer.map(QGLBuffer::WriteOnly);  FAILS
MyGetImageFunction( dest );         
glbuffer.unmap();

paint() {
  glbuffer.bind();
    glBegin(GL_QUADS);
    glTexCoord2i(0,0); glVertex2i(0,height());
    glTexCoord2i(0,1); glVertex2i(0,0);
    glTexCoord2i(1,1); glVertex2i(width(),0);
    glTexCoord2i(1,0); glVertex2i(width(),height());
    glEnd();             
  glbuffer.release();
}

没有任何以这种方式使用GLBuffer的例子,这是非常新的

编辑---这里的搜索是工作解决方案-------

 // Where glbuffer is defined as  
 glbuffer= QGLBuffer(QGLBuffer::PixelUnpackBuffer); 

// sequence to get a pointer into a PBO, write data to it and copy it to a texture
glbuffer.bind(); // bind before doing anything
unsigned char *dest = (unsigned char*)glbuffer.map(QGLBuffer::WriteOnly);
MyGetImageFunction(dest);                           
glbuffer.unmap(); // need to unbind before the rest of openGL can access the PBO

glBindTexture(GL_TEXTURE_2D,texture);                       
// Note 'NULL' because memory is now onboard the card                                                                                   
glTexSubImage2D(GL_TEXTURE_2D, 0, 0,0, image_width, image_height, glFormatExt, glType, NULL);  
glbuffer.release();  // but don't release until finished the copy


// PaintGL function
glBindTexture(GL_TEXTURE_2D,textures);
glBegin(GL_QUADS);
    glTexCoord2i(0,0); glVertex2i(0,height());
    glTexCoord2i(0,1); glVertex2i(0,0);
    glTexCoord2i(1,1); glVertex2i(width(),0);
    glTexCoord2i(1,0); glVertex2i(width(),height());
glEnd();             

2 个答案:

答案 0 :(得分:3)

您应该在映射之前绑定缓冲区!

documentation for QGLBuffer::map

  

假设已在此缓冲区上调用create(),并且已将其绑定到当前上下文

除了VJovic的评论之外,我认为你遗漏了一些关于公益组织的观点:

像素解压缩缓冲区不会为您提供指向图形纹理的指针。它是在显卡上分配的一块独立内存,可以直接从CPU写入。

缓冲区可以通过glTexSubImage2D(.....,0)调用复制到纹理中,纹理也可以绑定,但是你没有这样做。 (0是像素缓冲区的偏移量)。部分需要复制,因为纹理的布局与线性像素缓冲区不同。

请参阅this page以获取有关PBO使用情况的详细解释(几周前我用它来执行异步纹理上传)。

答案 1 :(得分:0)

如果GL实现不支持缓冲区,或者没有当前的QGLContext,

create将返回false。

如果无法绑定,

bind将返回false,通常是因为此GL实现不支持type()。

您没有检查这两个功能是否通过。