开发者

OpenGL: how to limit to an image component?

I am busy climbing the learning curve for OpenGL, using Delphi (pascal); I am using an excellent text, but every example in the book draws to the entire Form. I want to place an开发者_C百科 image component on the from, and draw to that. I tried assigning the Device context handle (GDC) to the handle of the image control's canvas, rather than to the handle of the form, but that returns an error when ChoosePixelFormat is invoked.

So, if anyone knows how to get this to occur, I'd appreciate any suggestions.

Thanks in advance for any help.

jrDoner


I always use the following code to setup the window HWND for OpenGL output:

procedure rglSetupGL(Handle: HWnd);
var
  DC: HDC;
  PixelFormat: integer;
const
  PFD: TPixelFormatDescriptor = (
         nSize: sizeOf(TPixelFormatDescriptor);
         nVersion: 1;
         dwFlags: PFD_SUPPORT_OPENGL or PFD_DRAW_TO_WINDOW or PFD_DOUBLEBUFFER;
         iPixelType: PFD_TYPE_RGBA;
         cColorBits: 24;
         cRedBits: 0;
         cRedShift: 0;
         cGreenBits: 0;
         cGreenShift: 0;
         cBlueBits: 0;
         cBlueShift: 0;
         cAlphaBits: 24;
         cAlphaShift: 0;
         cAccumBits: 0;
         cAccumRedBits: 0;
         cAccumGreenBits: 0;
         cAccumBlueBits: 0;
         cAccumAlphaBits: 0;
         cDepthBits: 16;
         cStencilBits: 0;
         cAuxBuffers: 0;
         iLayerType: PFD_MAIN_PLANE;
         bReserved: 0;
         dwLayerMask: 0;
         dwVisibleMask: 0;
         dwDamageMask: 0);
begin
  DC := GetDC(Handle);
  PixelFormat := ChoosePixelFormat(DC, @PFD);
  SetPixelFormat(DC, PixelFormat, @PFD);
  RC := wglCreateContext(DC);
  wglMakeCurrent(DC, RC);
end;

As you know (?), there is a huge difference between window handles (HWNDs) and device contexts (DCs). Every window has a HWND, and every window that you can draw to has a HDC. Given a form, Handle is its HWND, and Canvas.Handle is its HDC.

To get the DC associated with a window, you can use GetDC(HWND).

You have to setup OpenGL on a window, that is, on a HWND. So you cannot render OpenGL on a control without a window handle, such as a TImage. Use a TPanel or some other decendant of TWinControl.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜