0

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

我如何将深度图的内容(我想它被初始化为 depthBuffer)而不是“主”帧缓冲区内容存储在 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 格式吗?

4

1 回答 1

1

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

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

或者,您可以使用glReadPixels (...)从附加的渲染缓冲区中读取。API 涉及更多一点,因为您必须指定要读取的矩形。

于 2016-01-13T11:11:49.550 回答