Debian Codegraph @

src/base/castleprojection.pas

raw
1 {
2 Copyright 2003-2022 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Projection parameters (TProjection). }
17 unit CastleProjection;
18
19 {$I castleconf.inc}
20
21 interface
22
23 uses CastleVectors, CastleRectangles;
24
25 type
26 { Projection type, used by @link(TProjection.ProjectionType). }
27 TProjectionTypeCore = (ptOrthographic, ptPerspective, ptFrustum);
28 TProjectionType = ptOrthographic .. ptPerspective;
29
30 { Projection determines how does the 3D world map onto 2D screen.
31 To change the currently used projection, change the TCastleCamera parameters. }
32 TProjection = record
33 ProjectionType: TProjectionTypeCore;
34
35 { If ProjectionType is ptPerspective, this property specifies
36 angles of view (horizontal and vertical), in radians.
37
38 Note that when overriding the @link(TCastleViewport.CalculateProjection),
39 you are expected to provide both angles calculated, even though some routines
40 for now will only use the vertical angle (and automatically adjust the other
41 to the aspect ratio).
42 Use the AdjustViewAngleRadToAspectRatio to calculate the angles as necessary.
43 }
44 PerspectiveAnglesRad: TVector2;
45
46 { If ProjectionType is ptOrthographic or ptFrustum, this property specifies
47 dimensions of the visible window. }
48 Dimensions: TFloatRectangle;
49
50 { Near clipping distance.
51 Everything closer to this distance is clipped (invisible). }
52 ProjectionNear: Single;
53
54 { Far clipping distance.
55 Everything further than this distance is clipped (invisible).
56 Note that it have a special value ZFarInfinity, which means that
57 no far clipping plane is used. E.g. shadow volumes require this. }
58 ProjectionFar: Single;
59
60 { Projection matrix, adjusted to given viewport aspect ratio (width/height). }
61 function Matrix(const AspectRatio: Single): TMatrix4;
62
63 { Detect whether any sensible projection values are initialized. }
64 function Initialized: Boolean; deprecated 'this should not be necessary anymore';
65 end;
66
67 { Calculate second viewing angle for perspective projection.
68 Given one viewing angle of the camera (FirstViewAngleDeg) and
69 aspect ratio of your window sizes (SecondToFirstRatio),
70 calculate second viewing angle of the camera.
71
72 The intention is that when projecting camera view (with given view angles)
73 on a screen with given aspect ratio), the image will not be distorted
74 (squeezed horizontally or vertically).
75
76 For the "Deg" version both angles (given and returned) are in degress,
77 for the "Rad" version both angles and in radians.
78
79 @groupBegin }
80 function AdjustViewAngleDegToAspectRatio(const FirstViewAngleDeg,
81 SecondToFirstRatio: Single): Single; deprecated 'use radians for everything throughout CGE';
82 function AdjustViewAngleRadToAspectRatio(const FirstViewAngleRad,
83 SecondToFirstRatio: Single): Single;
84 { @groupEnd }
85
86 const
87 { Special value that you can pass to various perspective-projection functions
88 with the intention to set far plane at infinity.
89 These functions include
90 like @link(FrustumProjectionMatrix), @link(PerspectiveProjectionMatrixDeg),
91 @link(PerspectiveProjectionMatrixRad), @link(PerspectiveProjection).
92 You can pass ZFarInfinity as their @code(ZFar) parameter value.
93
94 @italic(Implementation note:)
95 It would be "cooler" to define this constant as Math.Infinity,
96 but operating on Math.Infinity requires unnecessary turning
97 off of some compiler checks. The point was only to have some special ZFar
98 value, so 0 is as good as Infinity. }
99 ZFarInfinity = 0.0;
100
101 { Functions to create 4x4 matrices used in 3D graphics for projection.
102 That are equivalent to OpenGL glOrtho, glFrustum, gluPerspective.
103
104 The frustum and pespective generation
105 (routines FrustumProjectionMatrix, PerspectiveProjectionMatrixDeg, PerspectiveProjectionMatrixRad)
106 accept also a special value ZFarInfinity as the ZFar parameter.
107 Then you get perspective projection matrix withour far clipping plane,
108 which is very useful for the z-fail shadow volumes technique.
109 @groupBegin }
110 function OrthoProjectionMatrix(const Dimensions: TFloatRectangle; const ZNear, ZFar: Single): TMatrix4; overload;
111 function OrthoProjectionMatrix(const left, right, bottom, top, ZNear, ZFar: Single): TMatrix4; overload; deprecated 'use the overloaded version that takes Dimensions as TFloatRectangle';
112
113 function Ortho2DProjectionMatrix(const Dimensions: TFloatRectangle): TMatrix4; overload; deprecated 'just use OrthoProjectionMatrix, the 2D optimization is not really worth the maintenance';
114 function Ortho2DProjectionMatrix(const left, right, bottom, top: Single): TMatrix4; overload; deprecated 'just use OrthoProjectionMatrix, the 2D optimization is not really worth the maintenance';
115
116 function FrustumProjectionMatrix(const Dimensions: TFloatRectangle; const ZNear, ZFar: Single): TMatrix4; overload;
117 function FrustumProjectionMatrix(const left, right, bottom, top, ZNear, ZFar: Single): TMatrix4; overload; deprecated 'use the overloaded version that takes Dimensions as TFloatRectangle';
118
119 function PerspectiveProjectionMatrixDeg(const fovyDeg, aspect, ZNear, ZFar: Single): TMatrix4;
120 function PerspectiveProjectionMatrixRad(const fovyRad, aspect, ZNear, ZFar: Single): TMatrix4;
121 { @groupEnd }
122
123 function ProjectionTypeToStr(const ProjectionType: TProjectionTypeCore): String;
124
125 implementation
126
127 uses Math,
128 CastleUtils, CastleLog;
129
130 { TProjection ---------------------------------------------------------------- }
131
132 function TProjection.Matrix(const AspectRatio: Single): TMatrix4;
133 begin
134 case ProjectionType of
135 ptPerspective:
136 Result := PerspectiveProjectionMatrixRad(
137 PerspectiveAnglesRad.Y,
138 AspectRatio,
139 ProjectionNear,
140 ProjectionFar);
141 ptOrthographic:
142 Result := OrthoProjectionMatrix(
143 Dimensions,
144 ProjectionNear,
145 ProjectionFar);
146 ptFrustum:
147 Result := FrustumProjectionMatrix(
148 Dimensions,
149 ProjectionNear,
150 ProjectionFar);
151 {$ifndef COMPILER_CASE_ANALYSIS}
152 else raise EInternalError.Create(2018081901);
153 {$endif}
154 end;
155 end;
156
157 function TProjection.Initialized: Boolean;
158 begin
159 Result :=
160 { ProjectionNear may remain = 0 in case of orthographic projection,
161 see TCastleViewport.CalculateProjection .
162 Testcase: glTF-Sample-Models/2.0/Cameras/glTF/Cameras.gltf , switch to ortho viewpoint. }
163 // (ProjectionNear <> 0) and
164 { ProjectionFar may remain = 0 = ZFarInfinity. }
165 // (ProjectionFar <> 0) and
166 (Dimensions.Width <> 0) and
167 (Dimensions.Height <> 0);
168 end;
169
170 { global routines ------------------------------------------------------------ }
171
172 function AdjustViewAngleRadToAspectRatio(const FirstViewAngleRad, SecondToFirstRatio: Single): Single;
173 begin
174 { Ratio on window sizes is the ratio of angle tangeses.
175 For two angles (a, b), and window sizes (x, y) we have
176 Tan(a/2) = (x/2)/d
177 Tan(b/2) = (y/2)/d
178 where D is the distance to projection plane. So
179 Tan(a/2) / Tan(b/2) = x/y }
180
181 Result := ArcTan( Tan(FirstViewAngleRad/2) * SecondToFirstRatio) * 2;
182 end;
183
184 function AdjustViewAngleDegToAspectRatio(const FirstViewAngleDeg, SecondToFirstRatio: Single): Single;
185 begin
186 Result := RadToDeg( AdjustViewAngleRadToAspectRatio( DegToRad(FirstViewAngleDeg),
187 SecondToFirstRatio));
188 end;
189
190 function OrthoProjectionMatrix(const Left, Right, Bottom, Top, ZNear, ZFar: Single): TMatrix4;
191 var
192 Dimensions: TFloatRectangle;
193 begin
194 Dimensions.Left := Left;
195 Dimensions.Width := Right - Left;
196 Dimensions.Bottom := Bottom;
197 Dimensions.Height := Top - Bottom;
198 Result := OrthoProjectionMatrix(Dimensions, ZNear, ZFar);
199 end;
200
201 function OrthoProjectionMatrix(const Dimensions: TFloatRectangle; const ZNear, ZFar: Single): TMatrix4;
202 var
203 Depth: Single;
204 begin
205 Depth := ZFar - ZNear;
206
207 Result := TMatrix4.Zero;
208 Result.Data[0, 0] := 2 / Dimensions.Width;
209 Result.Data[1, 1] := 2 / Dimensions.Height;
210 Result.Data[2, 2] := - 2 / Depth; { negate, because our Z-y are negative when going "deeper inside the screen" }
211 Result.Data[3, 0] := - (Dimensions.Right + Dimensions.Left ) / Dimensions.Width;
212 Result.Data[3, 1] := - (Dimensions.Top + Dimensions.Bottom) / Dimensions.Height;
213 Result.Data[3, 2] := - (ZFar + ZNear) / Depth;
214 Result.Data[3, 3] := 1;
215 end;
216
217 function Ortho2DProjectionMatrix(const Left, Right, Bottom, Top: Single): TMatrix4;
218 var
219 Dimensions: TFloatRectangle;
220 begin
221 Dimensions.Left := Left;
222 Dimensions.Width := Right - Left;
223 Dimensions.Bottom := Bottom;
224 Dimensions.Height := Top - Bottom;
225 {$warnings off}
226 // consciously using deprecated function in another deprecated function
227 Result := Ortho2DProjectionMatrix(Dimensions);
228 {$warnings on}
229 end;
230
231 function Ortho2DProjectionMatrix(const Dimensions: TFloatRectangle): TMatrix4;
232 begin
233 { simple version: Result := OrthoProjectionMatrix(Dimensions, -1, 1); }
234 { optimized version below: }
235
236 Result := TMatrix4.Zero;
237 Result.Data[0, 0] := 2 / Dimensions.Width;
238 Result.Data[1, 1] := 2 / Dimensions.Height;
239 Result.Data[2, 2] := -1;
240 Result.Data[3, 0] := - (Dimensions.Right + Dimensions.Left ) / Dimensions.Width;
241 Result.Data[3, 1] := - (Dimensions.Top + Dimensions.Bottom) / Dimensions.Height;
242 Result.Data[3, 3] := 1;
243 end;
244
245 function FrustumProjectionMatrix(const Left, Right, Bottom, Top, ZNear, ZFar: Single): TMatrix4;
246 var
247 Dimensions: TFloatRectangle;
248 begin
249 Dimensions.Left := Left;
250 Dimensions.Width := Right - Left;
251 Dimensions.Bottom := Bottom;
252 Dimensions.Height := Top - Bottom;
253 Result := FrustumProjectionMatrix(Dimensions, ZNear, ZFar);
254 end;
255
256 function FrustumProjectionMatrix(const Dimensions: TFloatRectangle; const ZNear, ZFar: Single): TMatrix4;
257
258 { This is based on "OpenGL Programming Guide",
259 Appendix G "... and Transformation Matrices".
260 ZFarInfinity version based on various sources, pretty much every
261 article about shadow volumes mentions z-fail and this trick. }
262
263 var
264 Depth, ZNear2: Single;
265 begin
266 ZNear2 := ZNear * 2;
267
268 Result := TMatrix4.Zero;
269 Result.Data[0, 0] := ZNear2 / Dimensions.Width;
270 Result.Data[2, 0] := (Dimensions.Right + Dimensions.Left) / Dimensions.Width;
271 Result.Data[1, 1] := ZNear2 / Dimensions.Height;
272 Result.Data[2, 1] := (Dimensions.Top + Dimensions.Bottom) / Dimensions.Height;
273 if ZFar <> ZFarInfinity then
274 begin
275 Depth := ZFar - ZNear;
276 Result.Data[2, 2] := - (ZFar + ZNear) / Depth;
277 Result.Data[3, 2] := - ZNear2 * ZFar / Depth;
278 end else
279 begin
280 Result.Data[2, 2] := -1;
281 Result.Data[3, 2] := -ZNear2;
282 end;
283 Result.Data[2, 3] := -1;
284 end;
285
286 function PerspectiveProjectionMatrixDeg(const FovyDeg, Aspect, ZNear, ZFar: Single): TMatrix4;
287 begin
288 Result := PerspectiveProjectionMatrixRad(DegToRad(FovyDeg), Aspect, ZNear, ZFar);
289 end;
290
291 function PerspectiveProjectionMatrixRad(const FovyRad, Aspect, ZNear, ZFar: Single): TMatrix4;
292 { Based on various sources, e.g. sample implementation of
293 glu by SGI in Mesa3d sources. }
294 var
295 Depth, ZNear2, Cotangent: Single;
296 // R: TFloatRectangle;
297 // W, H: Single;
298 begin
299 { You can express PerspectiveProjectionMatrixRad using FrustumProjectionMatrix.
300 Adapted from https://stackoverflow.com/a/12943456 }
301 // H := Tan(FovyRad / 2) * ZNear;
302 // W := H * Aspect;
303 // R := FloatRectangle(-W, -H, 2 * W, 2 * H);
304 // Result := FrustumProjectionMatrix(R, ZNear, ZFar);
305 // WritelnLog('Converted PerspectiveProjectionMatrixRad to a FrustumProjectionMatrix with rectangle %s',
306 // [R.ToString]);
307
308 ZNear2 := ZNear * 2;
309 Cotangent := CastleCoTan(FovyRad / 2);
310
311 Result := TMatrix4.Zero;
312 Result.Data[0, 0] := Cotangent / Aspect;
313 Result.Data[1, 1] := Cotangent;
314 if ZFar <> ZFarInfinity then
315 begin
316 Depth := ZFar - ZNear;
317 Result.Data[2, 2] := - (ZFar + ZNear) / Depth;
318 Result.Data[3, 2] := - ZNear2 * ZFar / Depth;
319 end else
320 begin
321 Result.Data[2, 2] := -1;
322 Result.Data[3, 2] := -ZNear2;
323 end;
324
325 Result.Data[2, 3] := -1;
326 end;
327
328 function ProjectionTypeToStr(const ProjectionType: TProjectionTypeCore): String;
329 const
330 Names: array[TProjectionTypeCore] of String = ('Orthographic', 'Perspective', 'Frustum');
331 begin
332 Result := Names[ProjectionType];
333 end;
334
335 end.