3

我需要一些关于将 OpenGL 的 ViewPort 的渲染内容保存到 Delphi XE2 中的位图文件的帮助。

基本上我想做的事情是在完成一些渲染之后,将 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)如果 ViewPort 尺寸太大,堆栈溢出错误(我在尝试保存尺寸为 256*256 的图像时遇到 SO 错误)。我认为这可能是因为“glReadPixels”函数将 FrameBuffer 读取到 PROCESSOR MEMORY(我假设这个是L2 Cache),而不是主存,这个不能容纳整个图像。是这样吗?如果是这样,您对如何将 FrameBuffer 读入主存储器有任何想法吗?

2) 在较小的视口 (25x25) 上进行测试,以避免 1) 中的错误,当我尝试访问存储在“像素”数组中的任何值时,会出现访问冲突错误。这意味着 glReadPixels 没有正确地从缓冲区中读取,我认为原因是与我传递给函数的参数之间存在一些不一致glReadPixels(0, 0, dimensions[2], dimensions[3], GL_RGB,GL_UNSIGNED_BYTE, @pixels)

4

2 回答 2

2
Procedure GetOGL_BMP(var BMP: TBitmap);
var
  Dimensions: array [0 .. 3] of Integer;
  RGBBits: PRGBQuad;
  Pixel: PRGBQuad;
  Header: PBitmapInfo;
  x, y: Integer;
  Temp: Byte;
begin
  glGetIntegerv(GL_VIEWPORT, @Dimensions);
  GetMem(RGBBits, Dimensions[2] * Dimensions[3] * 4);
  glFinish;
  glPixelStorei(GL_PACK_ALIGNMENT, 4);
  glPixelStorei(GL_PACK_ROW_LENGTH, 0);
  glPixelStorei(GL_PACK_SKIP_ROWS, 0);
  glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
  glReadPixels(0, 0, Dimensions[2], Dimensions[3], GL_RGBA, GL_UNSIGNED_BYTE,
    RGBBits);
  if not Assigned(BMP) then
      BMP := TBitmap.Create;
  BMP.PixelFormat := pf32Bit;
  BMP.Width := Dimensions[2];
  BMP.Height := Dimensions[3];
  GetMem(Header, SizeOf(TBitmapInfoHeader));
  with Header^.bmiHeader do
  begin
    biSize := SizeOf(TBitmapInfoHeader);
    biWidth := Dimensions[2];
    biHeight := Dimensions[3];
    biPlanes := 1;
    biBitCount := 32;
    biCompression := BI_RGB;
    biSizeImage := Dimensions[2] * Dimensions[3] * 4;
  end;
  // Rot und Blau vertauschen
  Pixel := RGBBits;
  for x := 0 to Dimensions[2] - 1 do
    for y := 0 to Dimensions[3] - 1 do
    begin
      Temp := Pixel.rgbRed;
      Pixel.rgbRed := Pixel.rgbBlue;
      Pixel.rgbBlue := Temp;
      inc(Pixel);
    end;
  SetDIBits(BMP.Canvas.Handle, BMP.Handle, 0, Dimensions[3], RGBBits,
    TBitmapInfo(Header^), DIB_RGB_COLORS);

  FreeMem(Header);
  FreeMem(RGBBits);
end;
于 2013-01-15T18:56:29.733 回答
0

首先:你从流中读取而不是写入它:MS。读取(像素,尺寸[2] * 尺寸[3] * 3);

第二:像素是一个动态数组,本质上是一个指向该数组的指针。要获取指向第一个字节的指针,请使用 glReadPixels 中的 @pixels[0] 并写入内存流。

于 2013-01-15T19:04:08.003 回答