- unit FormMain;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls, ExtCtrls, Menus, OpenGL;
- type
- TBmpHeader = Packed Record
- bfType1 : Byte;
- bfType2 : Byte;
- bfSize : LongInt;
- bfReserved1 : Word;
- bfReserved2 : Word;
- bfOffBits : LongInt;
- biSize : LongInt;
- biWidth : LongInt;
- biHeight : LongInt;
- biPlanes : Word;
- biBitCount : Word;
- biCompression : LongInt;
- biSizeImage : LongInt;
- biXPelsPerMeter : LongInt;
- biYPelsPerMeter : LongInt;
- biClrUsed : LongInt;
- biClrImportant : LongInt;
- end;
- Tform_main = class(TForm)
- ...
- private
- { Private-Deklarationen }
- oglDC :hDC;
- oglRC :hGLRC;
- oglPalette :hPALETTE;
- hPic :hBITMAP;
- procedure SetupPixelFormat;
- public
- { Public-Deklarationen }
- procedure render;
- end;
- var
- form_main: Tform_main;
- implementation
- {$R *.dfm}
- { TForm1 }
- procedure Tform_main.FormCreate(Sender: TObject);
- begin
- oglDC := CreateCompatibleDC(panel_ogl.Handle);
- hPic := CreateCompatibleBitmap(oglDC,panel_ogl.Width,panel_ogl.Height);
- // SelectObject(oglDC,hPic);
- SetupPixelFormat;
- oglRC := wglCreateContext(oglDC);
- wglMakeCurrent(oglDC, oglRC);
- glEnable(GL_DEPTH_TEST);
- glLoadIdentity;
- end;
- procedure Tform_main.SetupPixelFormat;
- var
- hHeap :THandle;
- nColors :integer;
- i :integer;
- lpPalette :PLogPalette;
- byRedMask,
- byGreenMask,
- byBlueMask :Byte;
- nPixelFormat :Integer;
- pfd :TPixelFormatDescriptor;
- begin
- FillChar(pfd, SizeOf(pfd), 0);
- with pfd do
- begin
- nSize := sizeOf(pfd);
- nVersion := 1;
- dwFlags := PFD_DRAW_TO_BITMAP or PFD_SUPPORT_OPENGL;
- iPixelType := PFD_TYPE_RGBA;
- cColorBits := 24;
- cRedBits := 8;
- cGreenBits := 8;
- cBlueBits := 8;
- cDepthBits := 16;
- iLayerType := PFD_MAIN_PLANE;
- end;
- nPixelFormat := ChoosePixelFormat(oglDC, @pfd);
- SetPixelFormat(oglDC, nPixelFormat, @pfd);
- DescribePixelFormat(oglDC, nPixelFormat, sizeOf(TPixelFormatDescriptor), pfd);
- 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 pred(nColors) 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;
- oglPalette := CreatePalette(lpPalette^);
- HeapFree(hHeap, 0, lpPalette);
- if (oglPalette <> 0) then
- begin
- SelectPalette(oglDC, oglPalette, false);
- RealizePalette(oglDC);
- end;
- end;
- end;
- procedure Tform_main.render;
- begin
- glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
- glLoadIdentity;
- glColor3f(random,random,random);
- glBegin(gl_quads);
- glVertex3f(1,1,-6);
- glVertex3f(-1,1,-6);
- glVertex3f(-1,-1,-6);
- glVertex3f(1,-1,-6);
- glEnd;
- glFinish;
- end;
- procedure Tform_main.bt_initClick(Sender: TObject);
- begin
- glViewPort(0,0,panel_ogl.Width,panel_ogl.Height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45.0, panel_ogl.Width/panel_ogl.Height, 1.0, 100.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity;
- end;
- procedure Tform_main.bt_offscreenClick(Sender: TObject);
- var
- pic :TBitmap;
- x,y :integer;
- w,h :integer;
- pba :pByteArray;
- buf :^Byte;
- PByte :^Byte;
- begin
- screen.Cursor := crHourGlass;
- pic := TBitmap.Create;
- pic.Width := panel_ogl.Width;
- pic.Height := panel_ogl.Height;
- pic.PixelFormat := pf24Bit;
- w := panel_ogl.Width;
- h := panel_ogl.Height;
- GetMem(Buf,3 * w * h);
- glReadPixels(0,0,w,h,GL_RGB,GL_UNSIGNED_BYTE,buf);
- PByte := @(buf^);
- for y := pred(h) downto 0 do
- begin
- pba := pic.ScanLine[y];
- for x := 0 to pred(w) do
- begin
- pba[x*3+2] := PByte^;
- inc(PByte);
- pba[x*3+1] := PByte^;
- inc(PByte);
- pba[x*3] := PByte^;
- inc(PByte);
- end;
- for x := 1 to w mod 4 do
- inc(PByte);
- end;
- FreeMem(buf);
- img_ogl.Picture.Bitmap.Assign(pic);
- pic.Free;
- screen.Cursor := crDefault;
- end;
- end.