给你一个最简单的例子
unit OPENGL1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,OpenGL;
type
TForm1 = class(TForm)
procedure FormDestroy(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure FormShow(Sender: TObject);
private
DC:HDC;
RC:HGLRC;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormDestroy(Sender: TObject);
begin
wglMakeCurrent(0,0);
wglDeleteContext(RC);
DeleteObject(DC);
end;
procedure TForm1.FormPaint(Sender: TObject);
begin
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3b(127,0,0);
glVertex3f(0.5,0.5,0);
glColor3b(0,127,0);
glVertex3f(-0.5,0.5,0);
glColor3b(0,0,127);
glVertex3f(-0.5,-0.5,0);
glColor3b(127,127,127);
glVertex3f(0.5,-0.5,0);
glEnd();
SwapBuffers(DC);
end;
procedure TForm1.FormShow(Sender: TObject);
var
PFD:TPixelFormatDescriptor;
ChosenPixelFormat:integer;
begin
DC:=GetDc(self.Handle);
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:=24;
cDepthbits:=32;
iLayerType:=PFD_MAIN_PLANE;
end;
ChosenPixelFormat:=ChoosePixelFormat(DC,@PFD);
if ChosenPixelFormat=0 then Raise Exception.Create('ChoosePixelFormat failed!');
SetPixelFormat(DC,ChosenPixelFormat,@PFD);
RC:=wglCreateContext(DC);
wglMakeCurrent(DC,RC);
end;
end.