gpt4 book ai didi

java - OpenGL:读取帧缓冲区的深度纹理

转载 作者:行者123 更新时间:2023-12-01 10:35:35 25 4
gpt4 key购买 nike

我在 Java 中使用 LWJGL,它与 C++ OpenGL 具有相同的函数名称。请注意,我被迫使用 OpenGL 的“旧”固定函数管道。

我目前正在使用以下代码成功地将 Framebuffer 的 RGB 内容绘制到 PixelBufferObjects:

//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);
}

我如何将深度图的内容(我认为它被初始化为深度缓冲区)存储在 PBO 中,而不是“主”帧缓冲区内容?

更新:这是我现在用来将深度图内容读取到 PBO 的代码:

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);
}

但是生成的图像只是黑色。可能是什么原因造成的?我是否可能弄乱了某些 GL 格式?

最佳答案

绑定(bind)“帧缓冲区的纹理”没有多大意义。该纹理仅代表单个(颜色缓冲区)附件。您可以将多个纹理附加到帧缓冲区,并且深度缓冲区也可以存储为纹理。

如果您使用深度纹理附件而不是渲染缓冲区,则可以像读取颜色附件一样读取它。唯一的区别是格式和数据类型:GL_DEPTH_COMPONENTGL_UNSIGNED_INT_24_8

或者,您可以使用 glReadPixels (...) 从附加的渲染缓冲区中读取。 API 稍微复杂一些,因为您必须指定要读取的矩形。

关于java - OpenGL:读取帧缓冲区的深度纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34762107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com