- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, OpenGL12, Geometry, AppEvnts, Textures;
- const
- FarClipping = 5000.0;
- NearClipping = 1.0;
- type
- TForm1 = class(TForm)
- procedure FormCreate(Sender: TObject);
- procedure FormShow(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- procedure FormResize(Sender: TObject);
- procedure ApplicationEventsIdle(Sender: TObject; var Done: Boolean);
- procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- private
- OpenGLInitialized : Boolean;
- RC : HGLRC; //OpenGL Rendering Context
- SkyBoxTexturen : Array[0..5] of TGlUInt;
- StartTick : Cardinal;
- Frames : LongInt;
- XRotation, YRotation : Single;
- MouseX, MouseY : LongInt;
- { Private-Deklarationen }
- public
- { Public-Deklarationen }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- //OpenGL ist noch nicht bereit
- OpenGLInitialized := False;
- //Kontrolle ob die Bilbliiotheken geladen sind
- if not LoadOpenGL then
- Halt(100)
- end;
- procedure TForm1.FormShow(Sender: TObject);
- procedure SetPixelFormat;
- var
- PixelFormat : TGLuint;
- PFD : pixelformatdescriptor;
- begin
- with pfd do
- begin
- nSize:= SizeOf( PIXELFORMATDESCRIPTOR );
- nVersion:= 1;
- dwFlags:= PFD_DRAW_TO_WINDOW
- or PFD_SUPPORT_OPENGL
- or PFD_DOUBLEBUFFER;
- iPixelType:= PFD_TYPE_RGBA;
- cColorBits:= 16;
- cRedBits:= 0;
- cRedShift:= 0;
- cGreenBits:= 0;
- cBlueBits:= 0;
- cBlueShift:= 0;
- cAlphaBits:= 0;
- cAlphaShift:= 0;
- cAccumBits:= 0;
- cAccumRedBits:= 0;
- cAccumGreenBits:= 0;
- cAccumBlueBits:= 0;
- cAccumAlphaBits:= 0;
- cDepthBits:= 16;
- cStencilBits:= 0;
- cAuxBuffers:= 0;
- iLayerType:= PFD_MAIN_PLANE;
- bReserved:= 0;
- dwLayerMask:= 0;
- dwVisibleMask:= 0;
- dwDamageMask:= 0
- end;
- PixelFormat := ChoosePixelFormat(Canvas.Handle, @pfd);
- if (PixelFormat=0) then
- MessageDlg('Can''t find a suitable PixelFormat.', mtError, [mbOk], 0);
- if (not Windows.SetPixelFormat(Canvas.Handle,PixelFormat,@pfd)) then
- MessageDlg('Can''t set PixelFormat.', mtError, [mbOk], 0)
- end; (*SetPixelFormat*)
- procedure StartGl;
- begin
- RC := wglCreateContext(Canvas.Handle);
- if (RC=0) then
- begin
- MessageDlg('Can''t create RC', mtError, [mbOk], 0);
- Halt(100)
- end;
- if (not wglMakeCurrent(Canvas.Handle, RC)) then
- begin
- MessageDlg('Can''t activate RC', mtError, [mbOk], 0);
- Halt(100)
- end
- end; (*StartGl*)
- procedure SetupGL;
- begin
- glClearColor(0.0, 0.0, 0.0, 0.0); //Hintergrundfarbe
- glEnable(GL_DEPTH_TEST);
- glEnable(gl_Cull_Face);
- glEnable(GL_TEXTURE_2D);
- glShadeModel(GL_SMOOTH)
- end; (*SetupGL*)
- procedure LoadTextures;
- const
- SkyTexturesName : Array[0..5] of String = ('north.jpg', 'east.jpg', 'south.jpg',
- 'west.jpg', 'top.jpg', 'bottom.jpg');
- var
- I : Integer;
- begin
- //Bilder für SkyBox laden
- for I := 0 to 5 do
- LoadTexture(SkyTexturesName[I], SkyBoxTexturen[I], False);
- end;(*LoadTextures*)
- begin
- RC := 0;
- SetPixelFormat;
- StartGL;
- //Extensions auslesen
- ClearExtensions;
- ReadExtensions;
- //GLStatus Variablen einstellen
- SetupGL;
- LoadTextures;
- Frames := 0;
- StartTick := GetTickCount;
- OpenGLInitialized := True
- end;
- procedure TForm1.FormDestroy(Sender: TObject);
- begin
- OpenGLInitialized := False;
- if RC<>0 then
- begin
- if (not wglMakeCurrent(Canvas.Handle,0)) then
- MessageBox(0,'Release of DC and RC failed.',' Shutdown Error',MB_OK or MB_ICONERROR);
- if (not wglDeleteContext(RC)) then
- begin
- MessageBox(0,'Release of Rendering Context failed.',' Shutdown Error',MB_OK or MB_ICONERROR);
- end
- end;
- RC:=0
- end;
- procedure TForm1.FormResize(Sender: TObject);
- begin
- glViewport(0, 0, ClientWidth, ClientHeight);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60.0,ClientWidth/ClientHeight,NearClipping,FarClipping);
- // hier wird das Verhätlnis zwischen errechnet Höhe und Breite der Oberfläche errechnet.
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity
- end;
- procedure TForm1.ApplicationEventsIdle(Sender: TObject;
- var Done: Boolean);
- procedure PaintSkyBox;
- const
- QuadPosition : Array[0..5] of Array[0..3] of Array[0..2] of Single =
- (((2.005,2.005,-1.995),(2.005,-2.005,-1.995),(-2.005,-2.005,-1.995),(-2.005,2.005,-1.995)), //Nordseite
- ((1.995,2.005,2),(1.995,-2.005,2),(1.995,-2.005,-2),(1.995,2.005,-2)), //Ostseite
- ((-2.005,2.005,1.995),(-2.005,-2.005, 1.995),(2.005,-2.005,1.995),(2.005,2.005,1.995)), //Südseite
- ((-1.995,2.005,-2),(-1.995,-2.005,-2),(-1.995,-2.005,2),(-1.995,2.005,2)), //Westseite
- ((-2,2,-2),(-2,2,2),(2,2,2), (2,2,-2)),
- ((2,-2,-2),(2,-2,2),(-2,-2,2),(-2,-2,-2)));
- TexturePos : Array[0..3] of Array[0..1] of Single =
- ((1,1),(1,0),(0, 0),(0, 1));
- var
- Side, Vertex : Integer;
- begin
- for Side := 0 to 5 do
- begin
- //Textur aktivieren
- glBindTexture(GL_TEXTURE_2D, SkyBoxTexturen[Side]);
- glBegin(GL_QUADS);
- //Vertieces und Tex Coords übergeben
- for Vertex := 3 downto 0 do
- begin
- glTexCoord2fv(@TexturePos[Vertex][0]);
- glVertex3fv(@QuadPosition[Side][Vertex][0])
- end;
- glEnd()
- end
- end; (*PaintSkyBox*)
- var
- Error : LongInt;
- begin
- Done := True;
- if not OpenGLInitialized then Exit;
- Done := False;
- glClear(GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT);
- glLoadIdentity;
- glRotatef(XRotation, 1, 0,0);
- glRotatef(YRotation, 0, 1,0);
- PaintSkyBox;
- //Error Handler
- Error := glgetError;
- if Error <> GL_NO_ERROR then
- begin
- MessageBeep(65535);
- Caption := gluErrorString(Error)
- end;
- //Frame Counter
- Inc(Frames);
- if GetTickCount - StartTick >=1000 then
- begin
- Caption := Format('Sky Box Demo FPS: %f', [Frames/(GetTickCount - StartTick)*1000]);
- Frames := 0;
- StartTick := GetTickCount
- end;
- SwapBuffers(Canvas.Handle)
- end;
- procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- begin
- MouseCapture := True;
- MouseX := X;
- MouseY := Y;
- end;
- procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- begin
- MouseCapture := False
- end;
- end.