陈
陈晨
Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,opengl;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormShow(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
DC:HDC;
RC:HGLRC;
implementation
{$R *.dfm}
function SetupPixelFormat(var dc:HDC):Boolean;
var
ppfdPIXELFORMATDESCRIPTOR;
npixelformat:Integer;
begin
New(ppfd);
ppfd^.nSize:=sizeof(PIXELFORMATDESCRIPTOR);
ppfd^.nVersion:=1;
ppfd^.dwFlags:=PFD_DRAW_TO_WINDOW
or PFD_SUPPORT_OPENGL or
PFD_DOUBLEBUFFER;
ppfd^.dwLayerMask:=PFD_MAIN_PLANE;
ppfd^.iPixelType:=PFD_TYPE_COLORINDEX;
ppfd^.cColorBits:=8;
ppfd^.cDepthBits:=16;
ppfd^.cAccumBits:=0;
ppfd^.cStencilBits:=0;
npixelformat:=ChoosePixelFormat(dc, ppfd);
if (nPixelformat=0) then
begin
MessageBox(NULL, 'choosePixelFormat failed',
'Error', MB_OK);
Result:=False;
Exit;
end;
if (SetPixelFormat(dc, npixelformat, ppfd)= FALSE) then
begin
MessageBox(NULL, 'SetPixelFormat failed',
'Error', MB_OK);
Result:=False;
Exit;
end;
Result:=True;
Dispose(ppfd);
end;
procedure TForm1.FormShow(Sender: TObject);
begin
DC:=Canvas.Handle;
SetupPixelFormat(dc);
RC:=wglCreateContext(DC);
wglMakeCurrent(DC,RC);
glclearcolor(0.5,0.7,0.9,1.0);
glclear(GL_COLOR_BUFFER_BIT);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
wglMakeCurrent(0,0);
if RC<>null then
wglDeleteContext(RC);
if DC<>null then
ReleaseDC(Handle,DC);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
glbegin(GL_LINES);
glcolor3i(0,255,0);
glVertex2i(50,50);
glcolor3i(255,0,0);
glVertex2i(300,300);
glend();
//swapbuffers(dc);
gluLookAt(2, 4, 6, 0, 0, 0, 0, 1, 0);
SwapBuffers(DC);
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,opengl;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormShow(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
DC:HDC;
RC:HGLRC;
implementation
{$R *.dfm}
function SetupPixelFormat(var dc:HDC):Boolean;
var
ppfdPIXELFORMATDESCRIPTOR;
npixelformat:Integer;
begin
New(ppfd);
ppfd^.nSize:=sizeof(PIXELFORMATDESCRIPTOR);
ppfd^.nVersion:=1;
ppfd^.dwFlags:=PFD_DRAW_TO_WINDOW
or PFD_SUPPORT_OPENGL or
PFD_DOUBLEBUFFER;
ppfd^.dwLayerMask:=PFD_MAIN_PLANE;
ppfd^.iPixelType:=PFD_TYPE_COLORINDEX;
ppfd^.cColorBits:=8;
ppfd^.cDepthBits:=16;
ppfd^.cAccumBits:=0;
ppfd^.cStencilBits:=0;
npixelformat:=ChoosePixelFormat(dc, ppfd);
if (nPixelformat=0) then
begin
MessageBox(NULL, 'choosePixelFormat failed',
'Error', MB_OK);
Result:=False;
Exit;
end;
if (SetPixelFormat(dc, npixelformat, ppfd)= FALSE) then
begin
MessageBox(NULL, 'SetPixelFormat failed',
'Error', MB_OK);
Result:=False;
Exit;
end;
Result:=True;
Dispose(ppfd);
end;
procedure TForm1.FormShow(Sender: TObject);
begin
DC:=Canvas.Handle;
SetupPixelFormat(dc);
RC:=wglCreateContext(DC);
wglMakeCurrent(DC,RC);
glclearcolor(0.5,0.7,0.9,1.0);
glclear(GL_COLOR_BUFFER_BIT);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
wglMakeCurrent(0,0);
if RC<>null then
wglDeleteContext(RC);
if DC<>null then
ReleaseDC(Handle,DC);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
glbegin(GL_LINES);
glcolor3i(0,255,0);
glVertex2i(50,50);
glcolor3i(255,0,0);
glVertex2i(300,300);
glend();
//swapbuffers(dc);
gluLookAt(2, 4, 6, 0, 0, 0, 0, 1, 0);
SwapBuffers(DC);
end;
end.