unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, dglOpenGl, glBitmap, ExtCtrls;
type
TglPanel =
class(TPanel)
private
FOnPaintEvent:TNotifyEvent;
protected
procedure WMPaint(
var Message: TWMPaint);
message WM_PAINT;
procedure WMEraseBkgnd(
var Message: TWmEraseBkgnd);
message WM_ERASEBKGND;
published
property OnPaint:TNotifyEvent
read FOnPaintEvent
write FOnPaintEvent;
end;
TfrmMain =
class(TForm)
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure FormKeyDown(Sender: TObject;
var Key: Word;
Shift: TShiftState);
procedure FormResize(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
myRc, myDc : Cardinal;
GL_Texture:TglBitmap2D;
Panel: TglPanel;
ControlPanel: TPanel;
procedure InitGl();
procedure DeInitGl();
procedure glRender();
end;
var
frmMain: TfrmMain;
e: TWinControl;
Angle: Integer;
implementation
{$R *.dfm}
procedure TfrmMain.DeInitGl;
begin
DeactivateRenderingContext();
DestroyRenderingContext(myRc);
ReleaseDC(Panel.Handle, myDc);
end;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
OnResize :=
nil;
Angle := 0;
Panel := TglPanel.Create(Self);
with Panel
do
begin
Caption := '
';
Align := alClient;
OnPaint := FormPaint;
Parent := frmMain;
end;
InitGl();
Caption := '
OpenGL VCL Template by sniper (haris_201@hotmail.com)';
KeyPreview := True;
GL_Texture := TglBitmap2D.Create();
{//GL_Texture.BuildMipMaps := False;
GL_Texture.LoadFromFile('../Images/back.bmp');
GL_Texture.FreeDataAfterGenTexture := True;
//GL_Texture.GenTexture();}
OnResize := FormResize;
Resize();
end;
procedure TfrmMain.FormDestroy(Sender: TObject);
begin
GL_Texture.Free();
DeInitGl();
end;
procedure TfrmMain.InitGl;
begin
InitOpenGL();
myDc := GetDC(Panel.Handle);
myRc := CreateRenderingContext(myDc, [opDoubleBuffered], 24, 16, 8, 0, 0, 0);
ActivateRenderingContext(myDc,myRc);
glClearColor(0, 0, 0, 0);
glShadeModel(GL_SMOOTH);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc (GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_TEXTURE_2D);
end;
procedure TfrmMain.FormPaint(Sender: TObject);
begin
glClear(GL_COLOR_BUFFER_BIT
or GL_DEPTH_BUFFER_BIT);
glRender();
SwapBuffers(myDc);
end;
procedure TfrmMain.FormKeyDown(Sender: TObject;
var Key: Word;
Shift: TShiftState);
begin
if Key=27
then Close();
end;
procedure TfrmMain.FormResize(Sender: TObject);
begin//
if (ClientHeight=0)
then
begin
ClientHeight := 1;
end;
glViewport(0, 0, ClientWidth, ClientHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(120.0, ClientWidth/ClientHeight, 1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
FormPaint(
nil);
end;
procedure TfrmMain.glRender();
begin
glClear(GL_COLOR_BUFFER_BIT
or GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
gluPerspective(120.0, ClientWidth/ClientHeight, 1, 1000);
glTranslatef(0, 0, -5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
glRotated(Angle, 0, 1, 0);
glBegin(GL_LINES);
glColor3f(255, 0, 0);
glVertex3f(-8, 0, 0);
glVertex3f( 8, 0, 0);
glColor3f(0, 255, 0);
glVertex3f( 0, 8, 0);
glVertex3f( 0, -8, 0);
glEnd;
SwapBuffers(myDC);
end;
{ TglPanel }
procedure TglPanel.WMEraseBkgnd(
var Message: TWmEraseBkgnd);
begin
Message.Result := 1;
end;
procedure TglPanel.WMPaint(
var Message: TWMPaint);
var
PS: TPaintStruct;
begin
BeginPaint(
Handle, PS);
if Assigned(FOnPaintEvent)
then
FOnPaintEvent(Self);
EndPaint(
Handle, PS);
Message.Result := 0;
end;
procedure TfrmMain.Timer1Timer(Sender: TObject);
begin
if Angle >= 360
then
Angle := 0
else
begin
Inc(Angle);
Inc(Angle);
Inc(Angle);
Inc(Angle);
end;
glRender;
end;
end.