LWJGL Forum

Programming => OpenGL => Topic started by: MattCa on June 15, 2012, 23:21:32

Title: Depth Buffer -> Frame Buffer -> Float Buffer
Post by: MattCa on June 15, 2012, 23:21:32
Hi guys!
I've been trying all night, but I've yet to find a solution. I've hooked up a Frame Buffer that fetches the Depth component to a texture, like so:

public void setup() {
frameBufferID = ARBFramebufferObject.glGenFramebuffers();

renderTextureID = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, renderTextureID);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);

GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP);
GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_DEPTH_COMPONENT, 800, 600, 0,
        GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, (FloatBuffer)null);

begin();
// Setup Frame Buffer
ARBFramebufferObject.glFramebufferTexture2D(ARBFramebufferObject.GL_FRAMEBUFFER,
ARBFramebufferObject.GL_DEPTH_ATTACHMENT, GL11.GL_TEXTURE_2D, renderTextureID, 0);
end();

}

public void begin() {
GL11.glBindTexture(GL11.GL_TEXTURE_2D, renderTextureID);
ARBFramebufferObject.glBindFramebuffer(ARBFramebufferObject.GL_FRAMEBUFFER, frameBufferID);
}

public void draw() {

}

public void end() {
ARBFramebufferObject.glBindFramebuffer(ARBFramebufferObject.GL_FRAMEBUFFER, 0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
}


However, my problem is how do I get the data from within the texture to a FloatBuffer?

I've tried setting the FloatBuffer as the source in glTexImage2D, but that still only contains 0.0f even after rendering.

Any ideas?
Thanks!
Matt.

Edit: Just to confirm, I'm rendering two quads to the screen in the main draw loop.
Title: Re: Depth Buffer -> Frame Buffer -> Float Buffer
Post by: broumbroum on June 23, 2012, 15:39:51
You may rather try with the PBO's. Either copy the depth buffer into your texture directly or use a PBO to process the copy asynchronously first to the PBO then to any texture, as of what the PBO are intended for. see some tutorials about PBO for that, they are thoughtful.
Title: Re: Depth Buffer -> Frame Buffer -> Float Buffer
Post by: sasmaster on July 26, 2012, 07:32:21
PBO is so old school ! ;)) 

Basically what you need is to read the pixels from the texture attachment so you call glReadBuffer()
then glReadPixels like this :

glReadPixels(0, 0,width, height , format,    GL_UNSIGNED_BYTE, pixels); where pixels is the ByteBuffer but you can swap to GL_FLOAT  format and use FloatBuffer. So this way you will get your pixels filled with the data from the texture attachment .
Title: Re: Depth Buffer -> Frame Buffer -> Float Buffer
Post by: broumbroum on March 10, 2013, 11:25:06
Quote from: MattCa on June 15, 2012, 23:21:32


However, my problem is how do I get the data from within the texture to a FloatBuffer?

I'v..
I currently use PBO to read from a Texture that stores a Stencil Buffer Snapshot .
like :

private void PBOgetTex(Item pbo) {
            /**
             * read (pack) into the PBO to make a texture update possible the pbo
             * contents will be sent back to the texture after it has been modified
             */
            ARBPixelBufferObject.glBindBufferARB(ARBPixelBufferObject.GL_PIXEL_PACK_BUFFER_ARB, pbo.name);
            GL11.glEnable(GL11.GL_TEXTURE_2D);
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureName);
            GL11.glGetTexImage(GL11.GL_TEXTURE_2D, 0, GL11.GL_LUMINANCE, GL11.GL_FLOAT, 0); //I use Luminance because of its 1 component spec compatible with a Stencil Value(.0f to 1.0f)
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);   
            ARBPixelBufferObject.glBindBufferARB(ARBPixelBufferObject.GL_PIXEL_PACK_BUFFER_ARB, 0);
            Util.checkGLError();
        }

cu
EDIT : simply map the PBO and download to a new local FloatBuffer that you can use for whatever purpose.