unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, dglOpenGL;
Type
TVector2f =
Array [0..1]
of Single;
TVector3f =
Array [0..2]
of Single;
type
TForm1 =
class(TForm)
Panel1: TPanel;
Timer1: TTimer;
Timer2: TTimer;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Timer2Timer(Sender: TObject);
private
{ Private-Deklarationen }
FrameCount : Cardinal;
//FrameCounter
procedure SetupGL;
procedure Render;
procedure ErrorHandler;
procedure Cube(x,y,z:Double;Color:Integer);
public
{ Public-Deklarationen }
DC : HDC;
//Handle auf Zeichenfläche
RC : HGLRC;
//Rendering Context
end;
var
Form1: TForm1;
const
NearClipping = 1;
FarClipping = 1000;
var
RotateX : single;
// Rotation um die X-Achse
RotateY : single;
// Rotation um die Y-Achse
RotateZ : single;
// Rotation um die Z-Achse
PosVect : TVector3f;
// Vector mit der Position des Objektes.
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
DC:= GetDC(Panel1.Handle);
if not InitOpenGL
then Application.Terminate;
RC:= CreateRenderingContext(
DC,
[opDoubleBuffered],
32,
24,
0,0,0,
0);
ActivateRenderingContext(
DC, RC);
SetupGL;
end;
procedure TForm1.SetupGL;
begin
glClearColor(0.0, 0.0, 0.0, 0.0);
//Hintergrundfarbe:
glEnable(GL_DEPTH_TEST);
//Tiefentest aktivieren
glEnable(GL_CULL_FACE);
//Backface Culling aktivieren
RotateX := 0;
RotateY := 0;
RotateZ := 0;
PosVect[0] := 0;
PosVect[1] := 0;
PosVect[2] := -5;
end;
procedure TForm1.Cube(x,y,z:Double;Color:Integer);
begin
if Color <> 0
then glColor4f(0.5, 0.5, 1.0, 0.8)
// Set The Color
else glColor4f(0.3, 0.3, 0.3, 0.4);
glVertex3f( 1.0+x, 1.0+y,-1.0+z);
// Top Right Of The Quad (Top)
glVertex3f(-1.0+x, 1.0+y,-1.0+z);
// Top Left Of The Quad (Top)
glVertex3f(-1.0+x, 1.0+y, 1.0+z);
// Bottom Left Of The Quad (Top)
glVertex3f( 1.0+x, 1.0+y, 1.0+z);
// Bottom Right Of The Quad (Top)
if Color <> 0
then glColor4f(0.5, 0.5, 1.0, 0.8)
// Set The Color
else glColor4f(0.3, 0.3, 0.3, 0.4);
glVertex3f( 1.0+x,-1.0+y, 1.0+z);
// Top Right Of The Quad (Bottom)
glVertex3f(-1.0+x,-1.0+y, 1.0+z);
// Top Left Of The Quad (Bottom)
glVertex3f(-1.0+x,-1.0+y,-1.0+z);
// Bottom Left Of The Quad (Bottom)
glVertex3f( 1.0+x,-1.0+y,-1.0+z);
// Bottom Right Of The Quad (Bottom)
if Color <> 0
then glColor4f(0.5, 0.5, 1.0, 0.8)
// Set The Color
else glColor4f(0.3, 0.3, 0.3, 0.4);
glVertex3f( 1.0+x, 1.0+y, 1.0+z);
// Top Right Of The Quad (Front)
glVertex3f(-1.0+x, 1.0+y, 1.0+z);
// Top Left Of The Quad (Front)
glVertex3f(-1.0+x,-1.0+y, 1.0+z);
// Bottom Left Of The Quad (Front)
glVertex3f( 1.0+x,-1.0+y, 1.0+z);
// Bottom Right Of The Quad (Front)
if Color <> 0
then glColor4f(0.5, 0.5, 1.0, 0.8)
// Set The Color
else glColor4f(0.3, 0.3, 0.3, 0.4);
glVertex3f( 1.0+x,-1.0+y,-1.0+z);
// Bottom Left Of The Quad (Back)
glVertex3f(-1.0+x,-1.0+y,-1.0+z);
// Bottom Right Of The Quad (Back)
glVertex3f(-1.0+x, 1.0+y,-1.0+z);
// Top Right Of The Quad (Back)
glVertex3f( 1.0+x, 1.0+y,-1.0+z);
// Top Left Of The Quad (Back)
if Color <> 0
then glColor4f(0.5, 0.5, 1.0, 0.8)
// Set The Color
else glColor4f(0.3, 0.3, 0.3, 0.4);
glVertex3f(-1.0+x, 1.0+y, 1.0+z);
// Top Right Of The Quad (Left)
glVertex3f(-1.0+x, 1.0+y,-1.0+z);
// Top Left Of The Quad (Left)
glVertex3f(-1.0+x,-1.0+y,-1.0+z);
// Bottom Left Of The Quad (Left)
glVertex3f(-1.0+x,-1.0+y, 1.0+z);
// Bottom Right Of The Quad (Left)
if Color <> 0
then glColor4f(0.5, 0.5, 1.0, 0.8)
// Set The Color
else glColor4f(0.3, 0.3, 0.3, 0.4);
glVertex3f( 1.0+x, 1.0+y,-1.0+z);
// Top Right Of The Quad (Right)
glVertex3f( 1.0+x, 1.0+y, 1.0+z);
// Top Left Of The Quad (Right)
glVertex3f( 1.0+x,-1.0+y, 1.0+z);
// Bottom Left Of The Quad (Right)
glVertex3f( 1.0+x,-1.0+y,-1.0+z);
// Bottom Right Of The Quad (Right)
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
DeactivateRenderingContext;
DestroyRenderingContext(RC);
ReleaseDC(
Handle,
DC);
end;
procedure TForm1.Render;
Const voffset : TVector3f = (0.0, 0.0, 0.0);
toffset : TVector2f = (0.0, 0.0);
tscale : TVector2f = (1.0, 1.0);
var x,y,z:Integer;
begin
glClear(GL_COLOR_BUFFER_BIT
or GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
gluPerspective(150.0, Panel1.ClientWidth/Panel1.ClientHeight, NearClipping, FarClipping);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
glTranslatef(0, 0, 0);
// Das Objekt auf seine aktuelle Position setzen.
glTranslatef( PosVect[0], PosVect[1], PosVect[2]);
{ Das Objekt noch drehen, damit denn wir haben bis jetzt ja nur den }
{ Vector gedreht. }
glRotatef(RotateX, 1, 0, 0);
glRotatef(RotateY, 0, 1, 0);
glRotatef(RotateZ, 0, 0, 1);
glBegin(GL_QUADS);
for x := 0
to 4
do
for y := 0
to 4
do
for z := 0
to 4
do
Cube(x*3,y*3,z*3,0);
glEnd;
SwapBuffers(
DC);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
inc(FrameCount);
Render;
If FrameCount = 20
then
begin
ErrorHandler;
FrameCount := 0;
end;
end;
procedure TForm1.Timer2Timer(Sender: TObject);
begin
RotateY := RotateY + 0.5;
// if RotateY <= 0 then RotateY := RotateY + 360;
// RotateX := RotateX + 0.5;
// if RotateX <= 0 then RotateX := RotateX + 360;
// RotateZ := RotateY + 0.5;
end;
procedure TForm1.ErrorHandler;
begin
Form1.Caption :=
string(gluErrorString(glGetError));
end;
end.