我想将帧缓冲区的数据写入PBO,然后再将其写回纹理。
基本上,这是一个测试练习。
我创建了一个Framebuffer,并附加了一个纹理和渲染缓冲区附件。
我可以创建帧缓冲区的纹理,并将其映射到一个矩形。
接下来,我尝试将帧缓冲区的像素读取到PBO,然后尝试创建一个纹理对象。
创建PBO
// create a color attachment texture
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE,
NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int readIndex = 0;
int writeIndex = 1;
GLuint pbo[2];
// create PBOs to hold the data. this allocates memory for them too
glGenBuffers(2, pbo);
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo[0]);
glBufferData(GL_PIXEL_PACK_BUFFER, SCR_WIDTH *SCR_HEIGHT * 3, 0, GL_STREAM_READ);
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo[1]);
glBufferData(GL_PIXEL_PACK_BUFFER, SCR_WIDTH *SCR_HEIGHT * 3, 0, GL_STREAM_READ);
// unbind buffers for now
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);在渲染循环中
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); // Bind the framebuffer
// Draw Elements
/* */
glReadBuffer(framebuffer);
writeIndex = (writeIndex + 1) % 2;
readIndex = (writeIndex + 1) % 2;
// bind PBO to read pixels. This buffer is being copied from GPU to CPU memory
glBindTexture(GL_TEXTURE_2D, texture);
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo[writeIndex]);
// copy from framebuffer to PBO asynchronously. it will be ready in the NEXT frame
glReadPixels(0, 0, SCR_WIDTH, SCR_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
// now read other PBO which should be already in CPU memory
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo[readIndex]);
// map buffer so we can access it
void* downsampleData = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER,GL_READ_ONLY);
if (downsampleData)
{
glTexSubImage2D(GL_TEXTURE_2D, 0, GL_RGB, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, downsampleData);
// clear all relevant buffers
downsampleData = nullptr;
glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
}
// now bind back to default framebuffer and draw a quad plane with the attached texture updated from the PBO.
// Now render in NDC with a Quad covering full screen.
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDisable(GL_DEPTH_TEST); // disable depth test so screen-space quad isn't discarded due to depth test.
// clear all relevant buffers
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // set clear color to white (not really necessery actually, since we won't be able to see behind the quad anyways)
glClear(GL_COLOR_BUFFER_BIT);
screenShader.use();
glBindVertexArray(quadVAO);
glBindTexture(GL_TEXTURE_2D, texture);
glDrawArrays(GL_TRIANGLES, 0, 6);生成的纹理为黑色。
发布于 2019-10-24 13:09:20
在进行了以下更改后,此代码现在可以正常工作
if (downsampleData)
{
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo[writeIndex]); // Needed to add this line.
glTexSubImage2D(GL_TEXTURE_2D, 0,0,0, SCR_WIDTH, SCR_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, 0); // changed from the pointer name to 0.
// clear all relevant buffers
downsampleData = nullptr;
glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
}https://stackoverflow.com/questions/58534252
复制相似问题