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

时间:2016-01-13 08:51:32

标签: java opengl lwjgl framebuffer pbo

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

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

        $payer = new Payer();
    $payer->setPaymentMethod("paypal");

        $payment = new PPayment();
    $payment->setIntent("sale")
        ->setPayer($payer)
        ->setRedirectUrls($redirectUrls)
        ->setTransactions(array($transaction));

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

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

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

然而,生成的图像只是黑色。 可能是什么原因导致的我可能搞砸了一些GL格式吗?

1 个答案:

答案 0 :(得分:1)

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

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

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