我需要关于在Delphi XE2中保存OpenGL ViewPort渲染内容到位图文件的帮助。
基本上我想要做的事情是,在进行一些渲染后,将FrameBuffer的内容转储到位图(全彩色格式)文件中。以下是应该实现此操作的代码摘录。
我遇到的主要问题是:
1)如果视口大小太大(尝试保存256 * 256大小的图像时),会出现堆栈溢出错误。我认为这可能是因为“glReadPixels”函数将FrameBuffer读取到处理器内存(我假设这是L2缓存),而不是主内存,而且处理器内存无法容纳整个图像。是否是这种情况?如果是,您有没有任何关于如何将FrameBuffer读取到主内存的想法?
2)为了避免1)中的错误,在较小的视口上进行测试(25×25),当我尝试访问“pixels”数组中存储的任何值时,会导致访问冲突错误。这意味着glReadPixels未正确从缓冲区读取,我认为原因是我传递给函数的参数存在一些不一致。
基本上我想要做的事情是,在进行一些渲染后,将FrameBuffer的内容转储到位图(全彩色格式)文件中。以下是应该实现此操作的代码摘录。
procedure TForm1.saveBtnClick(Sender: TObject);
var
//TBitmap object holding the newly created Bitmap.
srcBitmap: TBitmap;
// the array to hold pixels value while reading from the FrameBuffer
pixels: Array of GLUbyte;
dimensions: Array [0 .. 3] of Integer;
//Stream between the memory location of pixels and my bitmap.
MS: TMemoryStream;
I: Integer;
begin
if SaveDialog1.Execute then
begin
//create the bitmap and set it to Full Color Format; Open the Memory Stream
srcBitmap := TBitmap.Create;
srcBitmap.PixelFormat:=pf24bit;
MS:= TMemoryStream.Create;
//get the dimensions info for the current ViewPort
glGetIntegerv(GL_VIEWPORT, @dimensions);
srcBitmap.Width := dimensions[2];
srcBitmap.Height :=dimensions[3];
//allocate enough memory for pixels;
SetLength(pixels, dimensions[2] * dimensions[3] * 3);
//this is the function that is supposed to read the contents from the Frame
// Buffer and write them to pixels
glReadPixels(0, 0, dimensions[2], dimensions[3], GL_RGB,
GL_UNSIGNED_BYTE, @pixels);
//Do something if an error occured
ErrorHandler;
// Below I attempt to create a bitmap file from the read in pixels
MS.Read(pixels,dimensions[2] * dimensions[3] * 3) ;
srcBitmap.LoadFromStream(MS);
Edit2.Text := SaveDialog1.FileName;
srcBitmap.SaveToFile(Edit2.Text);
MS.Free;
srcBitmap.Free;
end;
end;
我遇到的主要问题是:
1)如果视口大小太大(尝试保存256 * 256大小的图像时),会出现堆栈溢出错误。我认为这可能是因为“glReadPixels”函数将FrameBuffer读取到处理器内存(我假设这是L2缓存),而不是主内存,而且处理器内存无法容纳整个图像。是否是这种情况?如果是,您有没有任何关于如何将FrameBuffer读取到主内存的想法?
2)为了避免1)中的错误,在较小的视口上进行测试(25×25),当我尝试访问“pixels”数组中存储的任何值时,会导致访问冲突错误。这意味着glReadPixels未正确从缓冲区读取,我认为原因是我传递给函数的参数存在一些不一致。
glReadPixels(0,0,dimensions[2],dimensions[3],GL_RGB,GL_UNSIGNED_BYTE,@pixels)
。