4 Prefix: glc - OpenGL Core
5 Beschreibung: diese Unit enthält eine Klassen-Kapselung für OpenGL Frustum und Kamera
10 camera := TglcCamera.Create;
12 camera.Perspective(45, 0.01, 100, 800/600); // define perspective view
13 camera.Move(gluVector(2, 3, -5)); // move 2 right, 3 up and 5 back
14 camera.Tilt(-25); // turn 25 degrees down
15 camera.Turn(-10); // turn 10 degrees left
16 camera.Activate; // activate camera
18 // do normal rendering
30 ugluVector, ugluMatrix;
33 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
34 TglcFrustum = class(TObject)
36 function GetWidth: Single;
37 function GetHeight: Single;
38 function GetFOVAngle: Single;
39 function GetAspectRatio: Single;
41 fIsOrthogonal: Boolean;
42 fTop, fBottom, fLeft, fRight, fNear, fFar: Single;
44 property Top : Single read fTop;
45 property Bottom : Single read fBottom;
46 property Left : Single read fLeft;
47 property Right : Single read fRight;
48 property Near : Single read fNear;
49 property Far : Single read fFar;
50 property Width : Single read GetWidth;
51 property Height : Single read GetHeight;
52 property FOVAngle : Single read GetFOVAngle;
53 property AspectRatio : Single read GetAspectRatio;
54 property IsOrthogonal: Boolean read fIsOrthogonal;
56 procedure Frustum(const aLeft, aRight, aBottom, aTop, aNear, aFar: Single);
57 procedure Perspective(const aFOVAngle, aAspectRatio, aNear, aFar: Single);
58 procedure Ortho(const aLeft, aRight, aBottom, aTop, aNear, aFar: Single);
67 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
68 TglcCamera = class(TglcFrustum)
70 fPosition: TgluMatrix4f;
72 property Position: TgluMatrix4f read fPosition write fPosition;
74 procedure Move(const aVec: TgluVector3f);
75 procedure Tilt(const aAngle: Single);
76 procedure Turn(const aAngle: Single);
77 procedure Roll(const aAngle: Single);
79 function GetRay(const aPos: TgluVector2f): TgluRayf;
87 Math, {$IFNDEF OPENGL_ES}dglOpenGL{$ELSE}dglOpenGLES{$ENDIF};
89 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
90 //TglcFrustum///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
91 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
92 function TglcFrustum.GetWidth: Single;
94 result := (fRight - fLeft);
97 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
98 function TglcFrustum.GetHeight: Single;
100 result := (fTop - fBottom);
103 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
104 function TglcFrustum.GetFOVAngle: Single;
106 result := arctan2(Height/2, fNear)/Pi*360;
109 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
110 function TglcFrustum.GetAspectRatio: Single;
112 result := Height / Width;
115 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
116 procedure TglcFrustum.Frustum(const aLeft, aRight, aBottom, aTop, aNear, aFar: Single);
118 fIsOrthogonal := false;
127 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
128 procedure TglcFrustum.Perspective(const aFOVAngle, aAspectRatio, aNear, aFar: Single);
130 fIsOrthogonal := false;
133 fTop := fNear * tan(aFOVAngle / 360 * Pi);
135 fRight := aAspectRatio * fTop;
139 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
140 procedure TglcFrustum.Ortho(const aLeft, aRight, aBottom, aTop, aNear, aFar: Single);
142 fIsOrthogonal := true;
151 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
152 procedure TglcFrustum.Activate;
154 glMatrixMode(GL_PROJECTION);
156 if fIsOrthogonal then
157 {$IFNDEF OPENGL_ES}glOrtho{$ELSE}glOrthof{$ENDIF}(fLeft, fRight, fBottom, fTop, fNear, fFar)
159 {$IFNDEF OPENGL_ES}glFrustum{$ELSE}glFrustumf{$ENDIF}(fLeft, fRight, fBottom, fTop, fNear, fFar);
160 glMatrixMode(GL_MODELVIEW);
163 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
165 procedure TglcFrustum.Render;
167 min, max: TgluVector2f;
169 min[0] := fLeft / fNear * fFar;
170 min[1] := fBottom / fNear * fFar;
171 max[0] := fRight / fNear * fFar;
172 max[1] := fTop / fNear * fFar;
174 glBegin(GL_LINE_LOOP);
175 glVertex3f(fLeft, fTop, -fNear);
176 glVertex3f(fLeft, fBottom, -fNear);
177 glVertex3f(fRight, fBottom, -fNear);
178 glVertex3f(fRight, fTop, -fNear);
181 glBegin(GL_LINE_LOOP);
182 glVertex3f(min[0], min[0], -fFar);
183 glVertex3f(min[0], max[0], -fFar);
184 glVertex3f(max[0], max[0], -fFar);
185 glVertex3f(max[0], min[0], -fFar);
189 glVertex3f(0, 0, 0); glVertex3f(min[0], min[0], -fFar);
190 glVertex3f(0, 0, 0); glVertex3f(min[0], max[0], -fFar);
191 glVertex3f(0, 0, 0); glVertex3f(max[0], max[0], -fFar);
192 glVertex3f(0, 0, 0); glVertex3f(max[0], min[0], -fFar);
197 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
198 constructor TglcFrustum.Create;
209 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
210 //TglcCamera////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
211 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
212 procedure TglcCamera.Move(const aVec: TgluVector3f);
214 fPosition := gluMatrixMult(gluMatrixTranslate(aVec), fPosition);
217 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
218 procedure TglcCamera.Tilt(const aAngle: Single);
220 fPosition := gluMatrixMult(gluMatrixRotate(gluVector3f(1,0,0), aAngle), fPosition);
223 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
224 procedure TglcCamera.Turn(const aAngle: Single);
226 fPosition := gluMatrixMult(gluMatrixRotate(gluVector3f(0,1,0), aAngle), fPosition);
229 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
230 procedure TglcCamera.Roll(const aAngle: Single);
232 fPosition := gluMatrixMult(gluMatrixRotate(gluVector3f(0,0,1), aAngle), fPosition);
235 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
236 procedure TglcCamera.Activate;
239 glLoadMatrixf(@fPosition[0, 0]);
242 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
243 function TglcCamera.GetRay(const aPos: TgluVector2f): TgluRayf;
247 if (aPos[0] < 0) then
248 p[0] := -aPos[0] * fLeft
250 p[0] := aPos[0] * fRight;
251 if (aPos[1] < 0) then
252 p[1] := -aPos[1] * fBottom
254 p[1] := aPos[1] * fTop;
255 if (fIsOrthogonal) then begin
257 result.p := fPosition * p;
258 result.v := fPosition * gluVector3f(0, 0, -1);
261 result.p := gluVector3f(0, 0, 0);
262 result.v := fPosition * p;
264 result := gluRayNormalize(result);
267 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
268 constructor TglcCamera.Create;
271 fPosition := gluMatrixIdentity;