我正在尝试使用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();

最佳答案

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

documentation for QGLBuffer::map中:


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


除了VJovic的评论外,我认为您还缺少有关PBO的几点:

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

可以通过glTexSubImage2D(.....,0)调用将缓冲区复制到纹理中,并且绑定了纹理,而您不需要这样做。 (0是像素缓冲区的偏移量)。需要复制的部分原因是纹理的布局与线性像素缓冲区的布局不同。

有关PBO用法的详细说明,请参见this page(我几周前曾使用它进行异步纹理上传)。

关于c++ - QGLBuffer::map返回NULL吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8452214/

10-11 16:44