unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, dglOpenGL, glBitmap, ComCtrls;
type
TForm1 = class(TForm)
StatusBar1: TStatusBar;
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormCreate(Sender: TObject);
procedure IdleHandler(Sender: TObject; var Done: Boolean);
procedure FormResize(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Render;
private { Private-Deklarationen }
StartTime, TimeCount, FrameCount : Cardinal; //FrameCounter
Frames, DrawTime : Cardinal; //& Timebased Movement
fTexture: TglBitmap2D; // Instanz unserer Textur
procedure SetupGL;
procedure ErrorHandler;
public { Public-Deklarationen }
DC : HDC; //
Handle auf Zeichenfläche
RC : HGLRC;//Rendering Context
end;
var
Form1: TForm1;
xs, ys: Integer;
const
NearClipping = 1;
FarClipping = 1000;
Figur = 0;
namen: array[-1..0] of string = ('nichts','Figur');
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
DC:= GetDC(
Handle);
if not InitOpenGL then Application.Terminate;
RC:= CreateRenderingContext(
DC,
[opDoubleBuffered],
32,
24,
0,0,0,
0);
ActivateRenderingContext(
DC, RC);
{fTexture := TglBitmap2D.Create; // Instanz der Klasse erstellen
fTexture.LoadFromFile('thelfer04.bmp'); // Datei laden
fTexture.GenTexture; // geladene Textur an
OpenGL übergeben }
SetupGL;
end;
procedure TForm1.SetupGL;
begin
glClearColor(0, 0, 0, 0.0); //Hintergrundfarbe: Hier ein leichtes Blau
glEnable(GL_DEPTH_TEST); //Tiefentest aktivieren
glEnable(GL_CULL_FACE); //Backface Culling aktivieren
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
DeactivateRenderingContext;
DestroyRenderingContext(RC);
ReleaseDC(
Handle,
DC);
fTexture.Free;
end;
procedure TForm1.FormResize(Sender: TObject);
var tmpBool : Boolean;
begin
glViewport(0, 0, ClientWidth, ClientHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
gluPerspective(45.0, ClientWidth/ClientHeight, NearClipping, FarClipping);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
IdleHandler(Sender, tmpBool);
end;
function Selection : integer;
var
Puffer : array[0..256] of GLUInt;
Viewport : TGLVectori4;
Treffer,i : Integer;
Z_Wert : GLUInt;
Getroffen : GLUInt;
begin
glGetIntegerv(GL_VIEWPORT, @viewport); //Die Sicht speichern
glSelectBuffer(256, @Puffer); //Den Puffer zuordnen
glMatrixMode(GL_PROJECTION); //In den Projektionsmodus
glRenderMode(GL_SELECT); //In den Selectionsmodus schalten
glPushMatrix; //Um unsere Matrix zu sichern
glLoadIdentity; //Und dieselbige wieder zurückzusetzen
gluPickMatrix(xs, viewport[3]-ys, 1.0, 1.0, viewport);
gluPerspective(60.0, Viewport[2]/Viewport[3], 1, 256);
glMatrixMode(GL_PROJECTION); //Wieder in den Projektionsmodus
glPopMatrix; //und unsere alte Matrix wiederherzustellen
treffer := glRenderMode(GL_RENDER); //Anzahl der Treffer auslesen
Getroffen := High(GLUInt); //Höchsten möglichen Wert annehmen
Z_Wert := High(GLUInt); //Höchsten Z - Wert
for i := 0 to Treffer-1 do
if Puffer[(i*4)+1] < Z_Wert then
begin
getroffen := Puffer[(i*4)+3];
Z_Wert := Puffer[(i*4)+1];
end;
if getroffen=High(GLUInt)
then Result := -1
else Result := getroffen;
end;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
xs := x;
ys := y;
statusbar1.SimpleText := 'Sie haben auf ' + namen[selection] + ' geklickt!' + 'Maus koords x:'+inttostr(xs)+ 'Maus koords y:'+inttostr(ys);
end;
procedure TForm1.Render;
begin
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
gluPerspective(45.0, ClientWidth/ClientHeight, NearClipping, FarClipping);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
glTranslatef(0, 0, -5);
glLoadName(Figur);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(-0.4,0.4,0); //lo
glTexCoord2f(0,1); glVertex3f(-0.4,-0.4,0); //lu
glTexCoord2f(1,1); glVertex3f(0.4,-0.4,0); //ru
glTexCoord2f(1,0); glVertex3f(0.4,0.4,0); //ro
glEnd;
SwapBuffers(
DC);
end;
procedure TForm1.IdleHandler(Sender: TObject; var Done: Boolean);
begin
StartTime:= GetTickCount;
Render;
DrawTime:= GetTickCount - StartTime;
Inc(TimeCount, DrawTime);
Inc(FrameCount);
if TimeCount >= 1000 then begin
Frames:= FrameCount;
TimeCount:= TimeCount - 1000;
FrameCount:= 0;
Caption:= InttoStr(Frames) + 'FPS';
ErrorHandler;
end;
Done:= false;
end;
procedure TForm1.ErrorHandler;
begin
Form1.Caption := gluErrorString(glGetError);
end;
end.