This file is indexed.

/usr/src/castle-game-engine-6.4/base/castleprojection.pas is in castle-game-engine-src 6.4+dfsg1-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
{
  Copyright 2003-2017 Michalis Kamburelis.

  This file is part of "Castle Game Engine".

  "Castle Game Engine" is free software; see the file COPYING.txt,
  included in this distribution, for details about the copyright.

  "Castle Game Engine" is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  ----------------------------------------------------------------------------
}

{ Projection parameters (TProjection). }
unit CastleProjection;

{$I castleconf.inc}

interface

uses CastleVectors, CastleRectangles;

type
  { Projection type, used by @link(TProjection.ProjectionType). }
  TProjectionType = (ptOrthographic, ptPerspective, ptFrustum);

  { Projection determines how does the 3D world map onto 2D.
    To change the currently displayed projection,
    you usually want to override the @link(TCastleAbstractViewport.CalculateProjection). }
  TProjection = record
    ProjectionType: TProjectionType;

    { If ProjectionType is ptPerspective, this property specifies
      angles of view (horizontal and vertical), in degrees.

      Note that when overriding the @link(TCastleAbstractViewport.CalculateProjection),
      you are expected to provide both angles calculated, even though some routines
      for now will only use the vertical angle (and automatically adjust the other
      to the aspect ratio).
      Use the AdjustViewAngleDegToAspectRatio to calculate the angles as necessary.
    }
    PerspectiveAngles: TVector2;

    { If ProjectionType is ptOrthographic or ptFrustum, this property specifies
      dimensions of the visible window. }
    Dimensions: TFloatRectangle;

    { Near clipping distance.
      Everything closer to this distance is clipped (invisible). }
    ProjectionNear: Single;

    { Far clipping distance.
      Everything further than this distance is clipped (invisible).
      Note that it have a special value ZFarInfinity, which means that
      no far clipping plane is used. E.g. shadow volumes require this. }
    ProjectionFar: Single;

    { Far clipping distance to be used in cases when it cannot be infinite.
      Unlike ProjectionFar, this property cannot have a magical value ZFarInfinity.
      It should be calculated just like ProjectionFar,
      except it's never changed to be ZFarInfinity. }
    ProjectionFarFinite: Single;

    { Projection matrix, adjusted to given viewport aspect ratio (width/height). }
    function Matrix(const AspectRatio: Single): TMatrix4;
  end;

{ Calculate second viewing angle for perspective projection.
  Given one viewing angle of the camera (FirstViewAngleDeg) and
  aspect ratio of your window sizes (SecondToFirstRatio),
  calculate second viewing angle of the camera.

  The intention is that when projecting camera view (with given view angles)
  on a screen with given aspect ratio), the image will not be distorted
  (squeezed horizontally or vertically).

  For the "Deg" version both angles (given and returned) are in degress,
  for the "Rad" version both angles and in radians.

  @groupBegin }
function AdjustViewAngleDegToAspectRatio(const FirstViewAngleDeg,
  SecondToFirstRatio: Single): Single;
function AdjustViewAngleRadToAspectRatio(const FirstViewAngleRad,
  SecondToFirstRatio: Single): Single;
{ @groupEnd }

const
  { Special value that you can pass to various perspective-projection functions
    with the intention to set far plane at infinity.
    These functions include
    like @link(FrustumProjectionMatrix), @link(PerspectiveProjectionMatrixDeg),
    @link(PerspectiveProjectionMatrixRad), @link(PerspectiveProjection).
    You can pass ZFarInfinity as their @code(ZFar) parameter value.

    @italic(Implementation note:)
    It would be "cooler" to define this constant as Math.Infinity,
    but operating on Math.Infinity requires unnecessary turning
    off of some compiler checks. The point was only to have some special ZFar
    value, so 0 is as good as Infinity. }
  ZFarInfinity = 0.0;

{ Functions to create 4x4 matrices used in 3D graphics for projection.
  That are equivalent to OpenGL glOrtho, glFrustum, gluPerspective.

  The frustum and pespective generation
  (routines FrustumProjectionMatrix, PerspectiveProjectionMatrixDeg, PerspectiveProjectionMatrixRad)
  accept also a special value ZFarInfinity as the ZFar parameter.
  Then you get perspective projection matrix withour far clipping plane,
  which is very useful for the z-fail shadow volumes technique.
  @groupBegin }
function OrthoProjectionMatrix(const Dimensions: TFloatRectangle; const ZNear, ZFar: Single): TMatrix4; overload;
function OrthoProjectionMatrix(const left, right, bottom, top, ZNear, ZFar: Single): TMatrix4; overload; deprecated 'use the overloaded version that takes Dimensions as TFloatRectangle';

function Ortho2DProjectionMatrix(const Dimensions: TFloatRectangle): TMatrix4; overload; deprecated 'just use OrthoProjectionMatrix, the 2D optimization is not really worth the maintenance';
function Ortho2DProjectionMatrix(const left, right, bottom, top: Single): TMatrix4; overload; deprecated 'just use OrthoProjectionMatrix, the 2D optimization is not really worth the maintenance';

function FrustumProjectionMatrix(const Dimensions: TFloatRectangle; const ZNear, ZFar: Single): TMatrix4; overload;
function FrustumProjectionMatrix(const left, right, bottom, top, ZNear, ZFar: Single): TMatrix4; overload; deprecated 'use the overloaded version that takes Dimensions as TFloatRectangle';

function PerspectiveProjectionMatrixDeg(const fovyDeg, aspect, ZNear, ZFar: Single): TMatrix4;
function PerspectiveProjectionMatrixRad(const fovyRad, aspect, ZNear, ZFar: Single): TMatrix4;
{ @groupEnd }

implementation

uses Math,
  CastleUtils, CastleLog;

{ TProjection ---------------------------------------------------------------- }

function TProjection.Matrix(const AspectRatio: Single): TMatrix4;
begin
  case ProjectionType of
    ptPerspective:
      Result := PerspectiveProjectionMatrixDeg(
        PerspectiveAngles.Data[1],
        AspectRatio,
        ProjectionNear,
        ProjectionFar);
    ptOrthographic:
      Result := OrthoProjectionMatrix(
        Dimensions,
        ProjectionNear,
        ProjectionFarFinite);
    ptFrustum:
      Result := FrustumProjectionMatrix(
        Dimensions,
        ProjectionNear,
        ProjectionFar);
    else raise EInternalError.Create('TCastleAbstractViewport.ApplyProjection:ProjectionType?');
  end;
end;

{ global routines ------------------------------------------------------------ }

function AdjustViewAngleRadToAspectRatio(const FirstViewAngleRad, SecondToFirstRatio: Single): Single;
begin
  { Ratio on window sizes is the ratio of angle tangeses.
    For two angles (a, b), and window sizes (x, y) we have
      Tan(a/2) = (x/2)/d
      Tan(b/2) = (y/2)/d
    where D is the distance to projection plane. So
      Tan(a/2) / Tan(b/2) = x/y }

  Result := ArcTan( Tan(FirstViewAngleRad/2) * SecondToFirstRatio) * 2;
end;

function AdjustViewAngleDegToAspectRatio(const FirstViewAngleDeg, SecondToFirstRatio: Single): Single;
begin
  Result := RadToDeg( AdjustViewAngleRadToAspectRatio( DegToRad(FirstViewAngleDeg),
    SecondToFirstRatio));
end;

function OrthoProjectionMatrix(const Left, Right, Bottom, Top, ZNear, ZFar: Single): TMatrix4;
var
  Dimensions: TFloatRectangle;
begin
  Dimensions.Left := Left;
  Dimensions.Width := Right - Left;
  Dimensions.Bottom := Bottom;
  Dimensions.Height := Top - Bottom;
  Result := OrthoProjectionMatrix(Dimensions, ZNear, ZFar);
end;

function OrthoProjectionMatrix(const Dimensions: TFloatRectangle; const ZNear, ZFar: Single): TMatrix4;
var
  Depth: Single;
begin
  Depth := ZFar - ZNear;

  Result := TMatrix4.Zero;
  Result.Data[0, 0] := 2 / Dimensions.Width;
  Result.Data[1, 1] := 2 / Dimensions.Height;
  Result.Data[2, 2] := - 2 / Depth; { negate, because our Z-y are negative when going "deeper inside the screen" }
  Result.Data[3, 0] := - (Dimensions.Right + Dimensions.Left  ) / Dimensions.Width;
  Result.Data[3, 1] := - (Dimensions.Top   + Dimensions.Bottom) / Dimensions.Height;
  Result.Data[3, 2] := - (ZFar + ZNear) / Depth;
  Result.Data[3, 3] := 1;
end;

function Ortho2DProjectionMatrix(const Left, Right, Bottom, Top: Single): TMatrix4;
var
  Dimensions: TFloatRectangle;
begin
  Dimensions.Left   := Left;
  Dimensions.Width  := Right - Left;
  Dimensions.Bottom := Bottom;
  Dimensions.Height := Top - Bottom;
  {$warnings off}
  // consciously using deprecated function in another deprecated function
  Result := Ortho2DProjectionMatrix(Dimensions);
  {$warnings on}
end;

function Ortho2DProjectionMatrix(const Dimensions: TFloatRectangle): TMatrix4;
begin
  { simple version: Result := OrthoProjectionMatrix(Dimensions, -1, 1); }
  { optimized version below: }

  Result := TMatrix4.Zero;
  Result.Data[0, 0] := 2 / Dimensions.Width;
  Result.Data[1, 1] := 2 / Dimensions.Height;
  Result.Data[2, 2] := -1;
  Result.Data[3, 0] := - (Dimensions.Right + Dimensions.Left  ) / Dimensions.Width;
  Result.Data[3, 1] := - (Dimensions.Top   + Dimensions.Bottom) / Dimensions.Height;
  Result.Data[3, 3] := 1;
end;

function FrustumProjectionMatrix(const Left, Right, Bottom, Top, ZNear, ZFar: Single): TMatrix4;
var
  Dimensions: TFloatRectangle;
begin
  Dimensions.Left := Left;
  Dimensions.Width := Right - Left;
  Dimensions.Bottom := Bottom;
  Dimensions.Height := Top - Bottom;
  Result := FrustumProjectionMatrix(Dimensions, ZNear, ZFar);
end;

function FrustumProjectionMatrix(const Dimensions: TFloatRectangle; const ZNear, ZFar: Single): TMatrix4;

{ This is based on "OpenGL Programming Guide",
  Appendix G "... and Transformation Matrices".
  ZFarInfinity version based on various sources, pretty much every
  article about shadow volumes mentions z-fail and this trick. }

var
  Depth, ZNear2: Single;
begin
  ZNear2 := ZNear * 2;

  Result := TMatrix4.Zero;
  Result.Data[0, 0] := ZNear2                               / Dimensions.Width;
  Result.Data[2, 0] := (Dimensions.Right + Dimensions.Left) / Dimensions.Width;
  Result.Data[1, 1] := ZNear2                               / Dimensions.Height;
  Result.Data[2, 1] := (Dimensions.Top + Dimensions.Bottom) / Dimensions.Height;
  if ZFar <> ZFarInfinity then
  begin
    Depth := ZFar - ZNear;
    Result.Data[2, 2] := - (ZFar + ZNear) / Depth;
    Result.Data[3, 2] := - ZNear2 * ZFar  / Depth;
  end else
  begin
    Result.Data[2, 2] := -1;
    Result.Data[3, 2] := -ZNear2;
  end;
  Result.Data[2, 3] := -1;
end;

function PerspectiveProjectionMatrixDeg(const FovyDeg, Aspect, ZNear, ZFar: Single): TMatrix4;
begin
  Result := PerspectiveProjectionMatrixRad(DegToRad(FovyDeg), Aspect, ZNear, ZFar);
end;

function PerspectiveProjectionMatrixRad(const FovyRad, Aspect, ZNear, ZFar: Single): TMatrix4;
{ Based on various sources, e.g. sample implementation of
  glu by SGI in Mesa3d sources. }
var
  Depth, ZNear2, Cotangent: Single;
  // R: TFloatRectangle;
  // W, H: Single;
begin
  { You can express PerspectiveProjectionMatrixRad using FrustumProjectionMatrix.
    Adapted from https://stackoverflow.com/a/12943456 }
  // H := Tan(FovyRad / 2) * ZNear;
  // W := H * Aspect;
  // R := FloatRectangle(-W, -H, 2 * W, 2 * H);
  // Result := FrustumProjectionMatrix(R, ZNear, ZFar);
  // WritelnLog('Converted PerspectiveProjectionMatrixRad to a FrustumProjectionMatrix with rectangle %s',
  //   [R.ToString]);

  ZNear2 := ZNear * 2;
  Cotangent := CastleCoTan(FovyRad / 2);

  Result := TMatrix4.Zero;
  Result.Data[0, 0] := Cotangent / Aspect;
  Result.Data[1, 1] := Cotangent;
  if ZFar <> ZFarInfinity then
  begin
    Depth := ZFar - ZNear;
    Result.Data[2, 2] := - (ZFar + ZNear) / Depth;
    Result.Data[3, 2] := - ZNear2 * ZFar  / Depth;
  end else
  begin
    Result.Data[2, 2] := -1;
    Result.Data[3, 2] := -ZNear2;
  end;

  Result.Data[2, 3] := -1;
end;

end.