I'm using LWJGL in Java, which has identical function names to the C++ OpenGL. Note that I'm forced to use OpenGL's "old" Fixed Function Pipeline.
I'm currently successfully drawing a Framebuffer's RGB contents to PixelBufferObjects using the following code:
//the initialization method
public void init(int width, int height) {
//initializing the PBO
pbo_id = glGenBuffers();
// [...]
//initializing the Framebuffer
framebufferObject = glGenFramebuffers();
framebufferTexture = glGenTextures();
depthBuffer = glGenRenderbuffers();
framebufferFilter = GL_NEAREST;
glBindTexture(GL_TEXTURE_2D, framebufferTexture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (ByteBuffer)null);
glBindFramebuffer(GL_FRAMEBUFFER, framebufferObject);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, framebufferTexture, 0);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);
glBindTexture(GL_TEXTURE_2D, 0);
}
//this is called after the world was rendered to the framebuffer
public void capture() {
//binding the PBO
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo_id);
//binding the Framebuffer's texture
glBindTexture(GL_TEXTURE_2D, framebuffer_id);
//storing the Framebuffer's contents in the PBO
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
//unbinding the Framebuffer
glBindTexture(GL_TEXTURE_2D, 0);
//unbinding the PBO
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
}
How would I store the contents of the depth map (which I suppose is being initialized as depthBuffer) in the PBO instead of the "main" framebuffer contents?
UPDATE: This is the code which I'm using to read the depth map's contents to a PBO now:
public void captureDepthMap() {
//binding the PBO
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo_id);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
//binding the FBO
glBindFramebuffer(GL_FRAMEBUFFER, framebufferObject);
//storing the Depth Map's contents in the PBO
glReadPixels(0, 0, width, height, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT_24_8, 0);
//unbinding the FBO
glBindFramebuffer(GL_FRAMEBUFFER, 0);
//unbinding the PBO
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
}
The resulting image is just black however. What may cause this? Did I possibly mess up some GL formats?
Binding the "framebuffer's texture" does not make a whole lot of sense. This texture only represents a single (color buffer) attachment. You can have multiple textures attached to your framebuffer and the depth buffer can be stored as a texture too.
If you use a depth texture attachment instead of renderbuffer, you can read it back the same way you read the color attachment. The only difference would be the format and data type: GL_DEPTH_COMPONENT
and GL_UNSIGNED_INT_24_8
.
Alternatively, you can use glReadPixels (...)
to read from the attached renderbuffer. The API is a little more involved because you have to specify a rectangle to read.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.