unit rect1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,OpenGL;
type
  HGLRC = THandle;

type
  GLenum = Cardinal;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure FormResize(Sender: TObject);
  private
    openGLReady : Boolean;
    glDC : HDC;
    GLContext : HGLRC;
    errorCode : GLenum;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
  pfd : TPixelFormatDescriptor;
  FormatIndex : Integer;
begin
  fillchar(pfd,SizeOf(pfd),0);
  with pfd do
  begin
    nSize := SizeOf(pfd);
    nVersion := 1;
    dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL;
    iPixelType := PFD_TYPE_RGBA;
    cColorBits := 24;
    cDepthBits := 32;
    iLayerType := PFD_MAIN_PLANE;
  end;
  glDC := GetDC(Handle);
  FormatIndex := ChoosePixelFormat(glDC,@pfd);

  if FormatIndex=0 then
     raise Exception.Create('ChoosePixelFormat failed'+IntToStr(GetLastError));

  if not SetPixelFormat(glDC,FormatIndex,@pfd) then
     raise Exception.Create('SetPixelFormat failed'+IntToStr(GetLastError));

  GLContext := wglCreateContext(glDC);
  if GLContext=0 then
     raise Exception.Create('wglCreateContext failed'+IntToStr(GetLastError));


  if not wglMakeCurrent(glDC,GLContext) then
     raise Exception.Create('wglMakeCurrent failed'+IntToStr(GetLastError));

  openGLReady := true;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  openGLReady := False;
  wglMakeCurrent(glDC,0);
  wglDeleteContext(GLContext);
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  if not openGLReady then
     exit;

  glClearColor(0.0,0.4,0.0,0.0);
  glClear(GL_COLOR_BUFFER_BIT);

  errorCode := glGetError;
  if errorCode <> GL_NO_ERROR then
     raise Exception.Create('Error in paint'#13+
           gluErrorString(errorCode));
end;

procedure TForm1.FormResize(Sender: TObject);
begin
  if not openGLReady then
     exit;

  glViewPort(0,0,ClientWidth,ClientHeight);
  glOrtho(3.204,17.2,-11.0,1.45,5.025,41.0);

  errorCode := glGetError;
  if errorCode <>GL_NO_ERROR then
     raise Exception.Create('FormResize:'+gluErrorString(errorCode));
end;

end.
