I’m attempting to save an OpenGL created scene as a TBitmap. The problem I’m having is glReadPixels is returning all empty data (all 0’s). Thoughts on why this would occur? I’m attempting to capture the currentl OpenGL context. (display on screen is all working fine). This is with Delphi 7 under Windows.
var
pbuf: pointer;
y: integer;
bmp: TBitmap;
p1, p2: pointer;
begin
GetMem( pbuf, pnScene.Width * pnScene.Height * 4);
glReadPixels( 0, 0, pnScene.Width, pnScene.Height, GL_RGBA, GL_UNSIGNED_BYTE, pbuf);
//<------ pbuf now contains all 0's ---------->
bmp := TBitmap.Create;
bmp.PixelFormat := pf32bit;
bmp.Width := pnScene.Width;
bmp.Height := pnScene.height;
for y := 0 to (pnScene.Height -1) do
begin
p1 := bmp.ScanLine[y];
p2 := pointer( integer(pbuf)+ (y * bmp.Width * 4));
CopyMemory( p1, p2, bmp.Width * 4);
end;
bmp.SaveToFile( 'c:\test\temp.bmp');
bmp.Free;
FreeMem( pbuf);
end;
And suggestions/thoughts are greatly welcome!
Addendum:
Ultimately the plan was to place the glReadPixels() call right after the rendering calls for purposes of recording video. When I did that, it worked ok, so this ended up being something of a non-problem.
Make sure you are doing this from the same thread as where you created the GL context. If this is a callback function you supply to a third-party library then it may execute in another thread without you even realizing it. This happened to me once and all I got was zeroes just like you.