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 fProjMatrix: TgluMatrix4f;
37 function GetProjMatrixPtr: Pointer;
38 function GetWidth: Single;
39 function GetHeight: Single;
40 function GetFOVAngle: Single;
41 function GetAspectRatio: Single;
42 procedure UpdateProjMatrix;
44 fIsOrthogonal: Boolean;
45 fTop, fBottom, fLeft, fRight, fNear, fFar: Single;
47 property Top: Single read fTop;
48 property Bottom: Single read fBottom;
49 property Left: Single read fLeft;
50 property Right: Single read fRight;
51 property Near: Single read fNear;
52 property Far: Single read fFar;
53 property Width: Single read GetWidth;
54 property Height: Single read GetHeight;
55 property FOVAngle: Single read GetFOVAngle;
56 property AspectRatio: Single read GetAspectRatio;
57 property IsOrthogonal: Boolean read fIsOrthogonal;
58 property ProjMatrix: TgluMatrix4f read fProjMatrix;
59 property ProjMatrixPtr: Pointer read GetProjMatrixPtr;
61 procedure Frustum(const aLeft, aRight, aBottom, aTop, aNear, aFar: Single);
62 procedure Perspective(const aFOVAngle, aAspectRatio, aNear, aFar: Single);
63 procedure Ortho(const aLeft, aRight, aBottom, aTop, aNear, aFar: Single);
72 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
73 TglcCamera = class(TglcFrustum)
75 fPosition: TgluMatrix4f;
76 function GetPositionPtr: Pointer;
78 property Position: TgluMatrix4f read fPosition write fPosition;
79 property PositionPtr: Pointer read GetPositionPtr;
81 procedure Move(const aVec: TgluVector3f);
82 procedure Tilt(const aAngle: Single);
83 procedure Turn(const aAngle: Single);
84 procedure Roll(const aAngle: Single);
86 function GetRay(const aPos: TgluVector2f): TgluRayf;
94 Math, {$IFNDEF OPENGL_ES}dglOpenGL{$ELSE}dglOpenGLES{$ENDIF};
97 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
98 //TglcFrustum///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
99 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
100 procedure TglcFrustum.UpdateProjMatrix;
102 if fIsOrthogonal then begin
103 fProjMatrix[maAxisX] := gluVector4f(
104 2 / (fRight - fLeft),
108 fProjMatrix[maAxisY] := gluVector4f(
110 2 / (fTop - fBottom),
113 fProjMatrix[maAxisZ] := gluVector4f(
118 fProjMatrix[maPos] := gluVector4f(
119 -(fRight + fLeft) / (fRight - fLeft),
120 -(fTop + fBottom) / (fTop - fBottom),
121 -(fFar + fNear) / (fFar - fNear),
124 fProjMatrix[maAxisX] := gluVector4f(
125 2 * fNear / (fRight - fLeft),
129 fProjMatrix[maAxisY] := gluVector4f(
131 2 * fNear / (fTop - fBottom),
134 fProjMatrix[maAxisZ] := gluVector4f(
135 (fRight + fLeft) / (fRight - fLeft),
136 (fTop + fBottom) / (fTop - fBottom),
137 -(fFar + fNear) / (fFar - fNear),
139 fProjMatrix[maPos] := gluVector4f(
142 -2 * fFar * fNear / (fFar - fNear),
147 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
148 function TglcFrustum.GetWidth: Single;
150 result := (fRight - fLeft);
153 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
154 function TglcFrustum.GetProjMatrixPtr: Pointer;
156 result := @fProjMatrix[0, 0];
159 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
160 function TglcFrustum.GetHeight: Single;
162 result := (fTop - fBottom);
165 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
166 function TglcFrustum.GetFOVAngle: Single;
168 result := arctan2(Height/2, fNear)/Pi*360;
171 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
172 function TglcFrustum.GetAspectRatio: Single;
174 result := Height / Width;
177 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
178 procedure TglcFrustum.Frustum(const aLeft, aRight, aBottom, aTop, aNear, aFar: Single);
180 fIsOrthogonal := false;
190 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
191 procedure TglcFrustum.Perspective(const aFOVAngle, aAspectRatio, aNear, aFar: Single);
193 fIsOrthogonal := false;
196 fTop := fNear * tan(aFOVAngle / 360 * Pi);
198 fRight := aAspectRatio * fTop;
203 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
204 procedure TglcFrustum.Ortho(const aLeft, aRight, aBottom, aTop, aNear, aFar: Single);
206 fIsOrthogonal := true;
216 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
217 procedure TglcFrustum.Activate;
219 glMatrixMode(GL_PROJECTION);
221 if fIsOrthogonal then
222 {$IFNDEF OPENGL_ES}glOrtho{$ELSE}glOrthof{$ENDIF}(fLeft, fRight, fBottom, fTop, fNear, fFar)
224 {$IFNDEF OPENGL_ES}glFrustum{$ELSE}glFrustumf{$ENDIF}(fLeft, fRight, fBottom, fTop, fNear, fFar);
225 glMatrixMode(GL_MODELVIEW);
228 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
230 procedure TglcFrustum.Render;
232 min, max: TgluVector2f;
234 min[0] := fLeft / fNear * fFar;
235 min[1] := fBottom / fNear * fFar;
236 max[0] := fRight / fNear * fFar;
237 max[1] := fTop / fNear * fFar;
239 glBegin(GL_LINE_LOOP);
240 glVertex3f(fLeft, fTop, -fNear);
241 glVertex3f(fLeft, fBottom, -fNear);
242 glVertex3f(fRight, fBottom, -fNear);
243 glVertex3f(fRight, fTop, -fNear);
246 glBegin(GL_LINE_LOOP);
247 glVertex3f(min[0], min[0], -fFar);
248 glVertex3f(min[0], max[0], -fFar);
249 glVertex3f(max[0], max[0], -fFar);
250 glVertex3f(max[0], min[0], -fFar);
254 glVertex3f(0, 0, 0); glVertex3f(min[0], min[0], -fFar);
255 glVertex3f(0, 0, 0); glVertex3f(min[0], max[0], -fFar);
256 glVertex3f(0, 0, 0); glVertex3f(max[0], max[0], -fFar);
257 glVertex3f(0, 0, 0); glVertex3f(max[0], min[0], -fFar);
262 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
263 constructor TglcFrustum.Create;
274 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
275 //TglcCamera////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
276 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
277 function TglcCamera.GetPositionPtr: Pointer;
279 result := @fPosition[0, 0];
282 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
283 procedure TglcCamera.Move(const aVec: TgluVector3f);
285 fPosition := gluMatrixMult(gluMatrixTranslate(aVec), fPosition);
288 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
289 procedure TglcCamera.Tilt(const aAngle: Single);
291 fPosition := gluMatrixMult(gluMatrixRotate(gluVector3f(1,0,0), aAngle), fPosition);
294 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
295 procedure TglcCamera.Turn(const aAngle: Single);
297 fPosition := gluMatrixMult(gluMatrixRotate(gluVector3f(0,1,0), aAngle), fPosition);
300 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
301 procedure TglcCamera.Roll(const aAngle: Single);
303 fPosition := gluMatrixMult(gluMatrixRotate(gluVector3f(0,0,1), aAngle), fPosition);
306 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
307 procedure TglcCamera.Activate;
310 glLoadMatrixf(@fPosition[0, 0]);
313 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
314 function TglcCamera.GetRay(const aPos: TgluVector2f): TgluRayf;
318 if (aPos[0] < 0) then
319 p[0] := -aPos[0] * fLeft
321 p[0] := aPos[0] * fRight;
322 if (aPos[1] < 0) then
323 p[1] := -aPos[1] * fBottom
325 p[1] := aPos[1] * fTop;
326 if (fIsOrthogonal) then begin
328 result.p := fPosition * p;
329 result.v := fPosition * gluVector3f(0, 0, -1);
332 result.p := gluVector3f(0, 0, 0);
333 result.v := fPosition * p;
335 result := gluRayNormalize(result);
338 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
339 constructor TglcCamera.Create;
342 fPosition := gluMatrixIdentity;