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);
65 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
66 TglcCamera = class(TglcFrustum)
68 fPosition: TgluMatrix4f;
70 property Position: TgluMatrix4f read fPosition write fPosition;
72 procedure Move(const aVec: TgluVector3f);
73 procedure Tilt(const aAngle: Single);
74 procedure Turn(const aAngle: Single);
75 procedure Roll(const aAngle: Single);
77 function GetRay(const aPos: TgluVector2f): TgluRayf;
87 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
88 //TglcFrustum///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
89 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
90 function TglcFrustum.GetWidth: Single;
92 result := (fRight - fLeft);
95 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
96 function TglcFrustum.GetHeight: Single;
98 result := (fTop - fBottom);
101 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
102 function TglcFrustum.GetFOVAngle: Single;
104 result := arctan2(Height/2, fNear)/Pi*360;
107 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
108 function TglcFrustum.GetAspectRatio: Single;
110 result := Height / Width;
113 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
114 procedure TglcFrustum.Frustum(const aLeft, aRight, aBottom, aTop, aNear, aFar: Single);
116 fIsOrthogonal := false;
125 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
126 procedure TglcFrustum.Perspective(const aFOVAngle, aAspectRatio, aNear, aFar: Single);
128 fIsOrthogonal := false;
131 fTop := fNear * tan(aFOVAngle / 360 * Pi);
133 fRight := aAspectRatio * fTop;
137 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
138 procedure TglcFrustum.Ortho(const aLeft, aRight, aBottom, aTop, aNear, aFar: Single);
140 fIsOrthogonal := true;
149 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
150 procedure TglcFrustum.Activate;
152 glMatrixMode(GL_PROJECTION);
154 if fIsOrthogonal then
155 glOrtho(fLeft, fRight, fBottom, fTop, fNear, fFar)
157 glFrustum(fLeft, fRight, fBottom, fTop, fNear, fFar);
158 glMatrixMode(GL_MODELVIEW);
161 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
162 procedure TglcFrustum.Render;
164 min, max: TgluVector2f;
166 min[0] := fLeft / fNear * fFar;
167 min[1] := fBottom / fNear * fFar;
168 max[0] := fRight / fNear * fFar;
169 max[1] := fTop / fNear * fFar;
171 glBegin(GL_LINE_LOOP);
172 glVertex3f(fLeft, fTop, -fNear);
173 glVertex3f(fLeft, fBottom, -fNear);
174 glVertex3f(fRight, fBottom, -fNear);
175 glVertex3f(fRight, fTop, -fNear);
178 glBegin(GL_LINE_LOOP);
179 glVertex3f(min[0], min[0], -fFar);
180 glVertex3f(min[0], max[0], -fFar);
181 glVertex3f(max[0], max[0], -fFar);
182 glVertex3f(max[0], min[0], -fFar);
186 glVertex3f(0, 0, 0); glVertex3f(min[0], min[0], -fFar);
187 glVertex3f(0, 0, 0); glVertex3f(min[0], max[0], -fFar);
188 glVertex3f(0, 0, 0); glVertex3f(max[0], max[0], -fFar);
189 glVertex3f(0, 0, 0); glVertex3f(max[0], min[0], -fFar);
193 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
194 constructor TglcFrustum.Create;
205 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
206 //TglcCamera////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
207 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
208 procedure TglcCamera.Move(const aVec: TgluVector3f);
210 fPosition := gluMatrixMult(gluMatrixTranslate(aVec), fPosition);
213 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
214 procedure TglcCamera.Tilt(const aAngle: Single);
216 fPosition := gluMatrixMult(gluMatrixRotate(gluVector3f(1,0,0), aAngle), fPosition);
219 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
220 procedure TglcCamera.Turn(const aAngle: Single);
222 fPosition := gluMatrixMult(gluMatrixRotate(gluVector3f(0,1,0), aAngle), fPosition);
225 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
226 procedure TglcCamera.Roll(const aAngle: Single);
228 fPosition := gluMatrixMult(gluMatrixRotate(gluVector3f(0,0,1), aAngle), fPosition);
231 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
232 procedure TglcCamera.Activate;
235 glLoadMatrixf(@fPosition[0, 0]);
238 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
239 function TglcCamera.GetRay(const aPos: TgluVector2f): TgluRayf;
243 if (aPos[0] < 0) then
244 p[0] := -aPos[0] * fLeft
246 p[0] := aPos[0] * fRight;
247 if (aPos[1] < 0) then
248 p[1] := -aPos[1] * fBottom
250 p[1] := aPos[1] * fTop;
251 if (fIsOrthogonal) then begin
253 result.p := fPosition * p;
254 result.v := fPosition * gluVector3f(0, 0, -1);
257 result.p := gluVector3f(0, 0, 0);
258 result.v := fPosition * p;
260 result := gluRayNormalize(result);
263 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
264 constructor TglcCamera.Create;
267 fPosition := gluMatrixIdentity;