I am having problems reading using glReadPixels on Intel Hardware (see separate post) and thought I would give mapping a try.
I have attempted to map the Color buffer of my framebuffer but it always returns zeros (even if I clear with buffer with 1).
Can anyone spot my error in code below?? I do have a valid FrameBuffer and Color Buffers and the MapData structure is an Array of Array of vec4 as I have an RGBA color buffer. Is it my interpretation of the data pointer returned??
The code fails on Intel, NVIDEA and AMD hardware so I think not a Hardware issue.
Procedure ReadAtLocation (X,Y:Integer);
glBindFramebuffer(GL_READ_FRAMEBUFFER, fFBOHandle);
glReadBuffer(GL_COLOR_ATTACHMENT0);
glBindBuffer(GL_PIXEL_PACK_BUFFER, fColorBuffer[0]);
MapData:=glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
if MapData<>Nil then
Begin
PixelID[0]:=MapData[X][Y][0];
PixelID[1]:=MapData[X][Y][1];
PixelID[2]:=MapData[X][Y][2];
End;
glunMapBuffer(GL_PIXEL_PACK_BUFFER);
glBindBuffer(GL_PIXEL_PACK_BUFFER,0);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
If a buffer object is bound as pixel pack buffer, it is used as the destination for glReadPixels, the pointer parameter becoming an offset into the buffer. This is useful when you want to do asynchronous pixel reads, i.e. not having the glReadPixels call block until the GPU has finished rendering.
Binding a pixel pack buffer does not magically transfer contents from the framebuffer to it, you have to explicitly call glReadPixels. Also, you can’t bind anything other than a buffer object (your fColorBuffer[0] looks suspiciously like it could be a handle to the renderbuffer or texture you’re using as the first FBO attachment).
I think I now get it. See below for my code. I need another buffer to use as the PACK PIXEL target and then I can read pixels into it then extract from it using the glMap…
// Create the FBO
glGenFramebuffers(1, @fFBOHandle);
glBindFramebuffer(GL_FRAMEBUFFER, fFBOHandle);
// Create the texture object for the primitive information buffer
glGenRenderBuffers(fColBufSize, @fColorBuffer[0]);
for I := 0 to fColBufSize-1 do
Begin
glBindRenderBuffer(GL_RENDERBUFFER, fColorBuffer[i]);
glRenderBufferStorage( GL_RENDERBUFFER, GL_RGBA32F, SizeX, SizeY );
glFramebufferRenderBuffer (GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + I, GL_RENDERBUFFER, fColorTexture[i] );
fBuffer[i] := GL_COLOR_ATTACHMENT0 + I;
end;
// depth
if fIncDepthBuffer then
Begin
glGenRenderBuffers(1, @fDepthBuff);
glBindRenderBuffer(GL_RENDERBUFFER, fDepthBuff);
glRenderBufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, SizeX, SizeY );
glFramebufferRenderBuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fDepthBuff);
end;
glDrawBuffers(fColBufSize, @fBuffer[0]);
// Verify that the FBO is correct
aStatus := glCheckFramebufferStatus(GL_FRAMEBUFFER);
CheckForGLErrors('Float Buffer Set Size');
// Restore the default framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);