DOC

OPENGL UNDER DELPHI

By Sue Stephens,2014-03-11 05:17
8 views 0
OPENGL UNDER DELPHI

opengl under delphi

    发信人: babypig ())猪), 信区: Delphi

     : opengl under delphi

    发信站: BBS 水木清华站 (Mon Dec 20 10:52:01 1999)

    is very simple

    but first you should set you window's pixel to made it compatible to

    opengl pixel format

    you can do like this.

    procedure PreparePixelFormat(var DevC: HDC; var Palette: HPALETTE);

    var

    THandle; hHeap:

     nColors, i: Integer;

     lpPalette: PLogPalette;

     byRedMask, byGreenMask, byBlueMask: Byte;

     PFD : TPixelFormatDescriptor;

     ChosenPixelFormat : Integer;

    begin

     FillChar(PFD, SizeOf(TPixelFormatDescriptor), 0);

     with PFD do

     begin

     nSize := SizeOf(TPixelFormatDescriptor);

     nVersion := 1;

     dwFlags := PFD_DRAW_TO_WINDOW or

     PFD_SUPPORT_OPENGL or

     PFD_DOUBLEBUFFER;

     iPixelType := PFD_TYPE_RGBA;

     cColorBits := 16;

     cDepthBits:= 32; // 32-bit depth buffer

     iLayerType:= PFD_MAIN_PLANE;

     { Should be 24, but we must allow for the clunky WKU boxes }

     end;

     ChosenPixelFormat := ChoosePixelFormat(DevC, @PFD);

     if ChosenPixelFormat = 0 then

     Raise Exception.Create('ChoosePixelFormat failed!');

     SetPixelFormat(DevC, ChosenPixelFormat, @PFD);

     { Now check to make sure that the chosen format supports the

     features we really need. }

     DescribePixelFormat(

     DevC,

     ChosenPixelFormat,

     SizeOf(TPixelFormatDescriptor),

     PFD);

     with PFD do

     if ((dwFlags and PFD_DRAW_TO_WINDOW) = 0) or

     ((dwFlags and PFD_SUPPORT_OPENGL) = 0) or

     ((dwFlags and PFD_DOUBLEBUFFER) = 0) or

     (iPixelType <> PFD_TYPE_RGBA) or

     (cColorBits < 16) then

     Raise Exception.Create('Inappropriate Pixel Format chosen.');

     //SetPixelFormat(DevC, ChosenPixelFormat, @PFD);

     {realize the color palette}

     if ((pfd.dwFlags and PFD_NEED_PALETTE) <> 0) then begin

     nColors := 1 shl pfd.cColorBits;

     hHeap := GetProcessHeap;

    lpPalette := HeapAlloc(hHeap, 0, sizeof(TLogPalette) +

     (nColors * sizeof(TPaletteEntry)));

     lpPalette^.palVersion := $300;

     lpPalette^.palNumEntries := nColors;

     byRedMask := (1 shl pfd.cRedBits) - 1;

     byGreenMask := (1 shl pfd.cGreenBits) - 1;

     byBlueMask := (1 shl pfd.cBlueBits) - 1;

     for i := 0 to nColors - 1 do begin

     lpPalette^.palPalEntry[i].peRed := (((i shr pfd.cRedShift)

     and byRedMask) * 255) DIV byRedMask;

     lpPalette^.palPalEntry[i].peGreen := (((i shr pfd.cGreenShift)

     and byGreenMask) * 255) DIV byGreenMask;

     lpPalette^.palPalEntry[i].peBlue := (((i shr pfd.cBlueShift)

     and byBlueMask) * 255) DIV byBlueMask;

     lpPalette^.palPalEntry[i].peFlags := 0;

     end;

     Palette := CreatePalette(lpPalette^);

     HeapFree(hHeap, 0, lpPalette);

     if (Palette <> 0) then begin

     SelectPalette(DevC, Palette, False);

     RealizePalette(DevC);

     end;

     end;

    end;

    in your formshow procedure or other place

    you should do like this

    var DC : HDC;

     RC : HGLRC;

     Palette: HPALETTE;

    begin

     ........

     DC := GetDC(Handle);

     PreparePixelFormat(DC, Palette);

     RC := wglCreateContext(DC);

     wglMakeCurrent(DC, RC);

     glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);

     glMatrixMode(GL_PROJECTION);

     glLoadIdentity;

     ............

    end;

    then you can use opengl freely like other language --

    :?BBS 水木清华站 smth.org?[FROM: 210.45.232.6] ※来源

Report this document

For any questions or suggestions please email
cust-service@docsford.com