3

Delphi XE2 で、OpenGL の ViewPort のレンダリングされたコンテンツをビットマップ ファイルに保存する方法について、助けが必要です。

基本的に私がやりたいことは、いくつかのレンダリングが行われた後、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をプロセッサーメモリに読み込むためだと思います(メインメモリではなく、L2 キャッシュである必要があり、これはイメージ全体を内部に収めることができません。これは事実ですか?もしそうなら、FrameBufferをメインメモリに読み込む方法について何か考えがありますか?

2) 1) のエラーを回避するために、小さいビューポート (25x25) でテストすると、「ピクセル」配列に格納されている値にアクセスしようとすると、アクセス違反エラーが発生します。これは、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);

2番目:ピクセルは動的配列であり、本質的にその配列へのポインターです。最初のバイトへのポインタを取得するには、glReadPixelsで@pixels [0]を使用し、メモリストリームに書き込みます。

于 2013-01-15T19:04:08.003 に答える