6

多数の小さなウィンドウとコントロール (2D レンダリング) を含むアプリケーションを作成しており、各ウィンドウとコントロールを独自のビットマップにレンダリングしたいと考えています。これはこれまでのものです:

uses dglOpenGL;
...
var BMP: TBitmap;
    DC, RC: HDC;
...
function TMainForm.Init: Boolean;
begin
  Result := InitOpenGL;
  if Result = True then
  begin
    BMP := TBitmap.Create;
    BMP.PixelFormat := pf24bit;
    BMP.Width := 1280;
    BMP.Height := 1024;

    DC := (BMP.Canvas.Handle);
    RC := CreateRenderingContext(DC,
                                 [opGDI, opDoubleBuffered], // tried changing, didn't help
                                 24,
                                 24,
                                 0,
                                 0,
                                 0,
                                 0);
    ActivateRenderingContext(DC, RC);

    glClearColor(0.27, 0.4, 0.7, 0.0); // light blue
    glViewport(0, 0, 1280, 1024);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity;
    glOrtho(0, 1280, 0, 1024, -1, 10);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity;
  end;
end;

レンダリング手順:

  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);

  // red quad
  glColor3f(1, 0, 0);
  glBegin(GL_QUADS);
    glVertex2f(100, 100);
    glVertex2f(1280-100, 100);
    glVertex2f(1280-100, 1024-100);
    glVertex2f(100, 1024-100);
  glend;

  // swap
  SwapBuffers(DC);

しかし、出力はありません。
使用するMainForm.Canvas.Draw(0, 0, BMP);と、白い長方形が表示されます。

ビットマップを使って多くのことができるので(他の画像を描画したり、テキストをペイントしたり、ぼかしたり)、ビットマップでレンダリングをしたいのですが、オフスクリーンレンダリングを行う別の方法があれば大丈夫です...

では、オフスクリーン レンダリング用にアプリケーションを設定するにはどうすればよいでしょうか。

4

1 に答える 1

12

ターゲット デバイスのコンテキストに一致する OpenGL コンテキストを作成する必要があります。ウィンドウの場合、ビットマップとは異なる方法で作成されます。http://msdn.microsoft.com/en-us/library/windows/desktop/dd368826(v=vs.85).aspxdwFlagsを特に参照して ください。

PFD_DRAW_TO_WINDOW バッファーは、ウィンドウまたはデバイス サーフェスに描画できます。

PFD_DRAW_TO_BITMAP バッファはメモリ ビットマップに描画できます。

ただし、先に急いで、DIB DC のレンダリング コンテキストを作成する必要があります。なんで?DIB セクションの OpenGL レンダー コンテキストは、CPU で実行される OpenGL-1.1 のみをサポートするソフトウェア ラスタライザーを使用するため、非常に遅くなります。

代わりに、Framebuffer オブジェクトを作成し、カラー レンダバッファ アタッチメントをアタッチし、終了しglReadPixelsたら DIBSection に追加する必要があります。はるかに簡単に、はるかに高速に。

コメントリクエストがあったので更新

(StackOverflow が構文の色付けを正しく行わない理由がわかりません。つまり、どこにコメントがあり、どこにコメントがないかを判断します)

// flushes the OpenGL error queue and
// counts the total number of errors
int flushGLErrors(void)
{
    int i = 0;
    while( glGetError() != GL_NO_ERROR ) {
        i++;
    }

    return i;
}

// returns a HBITMAP or NULL.
// The HBITMAP must be freed using DeleteObject 
HBITMAP ReadPixelsToHBITMAP(
    int x,
    int y,
    int width,
    int height )
{
    void *pdata = NULL;

    /* Note that this values only makes sense if we know a target
     * output size like if we put the image to paper. */ 
    const int physical_resolution = 2835; /* 72 DPI */

    BITMAPINFOHEADER bmih = {
        /* .biSize          = */ sizeof(bmih),
        /* .biWidth         = */ width,
        /* .bi.Height       = */ height,
        /* .biPlanes        = */ 1,                   /* mandatory */
        /* .biBitCount      = */ 24,                  /* 8 bits per pixel */
        /* .biCompression   = */ BI_RGB,              /* uncompressed */
        /* .biSizeImage     = */ 0,                   /* implicit */
        /* .biXPelsPerMeter = */ physical_resolution, /* ignored */
        /* .biYPelsPerMeter = */ physical_resolution, /* ignored */
        /* .biClrUsed       = */ 0,                   /* no palette */
        /* .biClrImportant  = */ 0
    };

    HBITMAP hbm = CreateDIBSection(
        hdc, /* may be different than the DC used for OpenGL */
        (PBITMAPINFO)&bmih, /* can do this cast, because no palette is used */
        DIB_RGB_COLORS,
        &pdata,
        NULL,
        0
    );

    if( !hbm ) {
        return NULL;
    }

    flushGLErrors();

    glPixelStorei(GL_PACK_SWAP_BYTES,   GL_FALSE);
    glPixelStorei(GL_PACK_LSB_FIRST,    GL_TRUE);
    glPixelStorei(GL_PACK_ROW_LENGTH,   0);
    glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
    glPixelStorei(GL_PACK_SKIP_PIXELS,  0);
    glPixelStorei(GL_PACK_SKIP_ROWS,    0);
    glPixelStorei(GL_PACK_ALIGNMENT,    1);

    if( glGetError() != GL_NO_ERROR ) {
        DeleteObject(hbm);
        return NULL;
    }

    glReadPixels(x, y, width, height, GL_BGR, GL_UNSIGNED_BYTE, pdata);

    if( glGetError() != GL_NO_ERROR ) {
        DeleteObject(hbm);
        return NULL;
    }

    return hbm;
}
于 2013-02-16T11:46:46.230 に答える