/usr/src/castle-game-engine-6.4/3d/castlerays.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 | {
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.
----------------------------------------------------------------------------
}
{ Calculating 3D rays that correspond to the given points on 2D screen.
This is used by ray-tracing (casting a ray for each image pixel)
or when picking objects (what 3D object/point is indicated by
the current mouse position). }
unit CastleRays;
{$I castleconf.inc}
interface
uses CastleVectors, CastleProjection, CastleRectangles;
type
{ Calculate primary rays for given camera settings and screen size. }
TRaysWindow = class
private
FCamPosition, FCamDirection, FCamUp: TVector3;
CamSide: TVector3;
public
{ Camera vectors. Initialized in the constructor.
Must be given already normalized.
Note that CamUp may be changed in constructor, to be always perfectly
orthogonal to CamDirection.
@groupBegin }
property CamPosition: TVector3 read FCamPosition;
property CamDirection: TVector3 read FCamDirection;
property CamUp: TVector3 read FCamUp;
{ @groupEnd }
{ Constructor. Calculates some values based on camera settings,
this way many calls to PrimaryRay for the same camera settings
are fast (useful for ray-tracers). }
constructor Create(const ACamPosition, ACamDirection, ACamUp: TVector3);
{ Create appropriate TRaysWindow instance.
Constructs non-abstract descendant (TPerspectiveRaysWindow or
TOrthographicRaysWindow, depending on Projection.ProjectionType). }
class function CreateDescendant(
const ACamPosition, ACamDirection, ACamUp: TVector3;
const Projection: TProjection): TRaysWindow;
{ Calculate position and direction of the primary ray cast from CamPosition,
going through the pixel X, Y.
X, Y coordinates start from (0, 0) if bottom left (like in typical 2D
OpenGL). When they are integers and in the range of
X = 0..ScreenWidth-1 (left..right), Y = 0..ScreenHeight-1 (bottom..top)
it's guaranteed that resulting ray will go exactly through the middle
of the appropriate pixel (on imaginary "rzutnia" = image positioned
paraller to view direction). But you can provide non-integer X, Y,
useful for multisampling (taking many samples within the pixel,
like (X, Y) = (PixX + Random - 0.5, PixY + Random - 0.5)).
Resulting RayDirection is guaranteed to be normalized
(this is in practice not costly to us, and it often helps --- when ray
direction is normalized, various distances from ray collisions are "real"). }
procedure PrimaryRay(const x, y: Single;
const ScreenWidth, ScreenHeight: Integer;
out RayOrigin, RayDirection: TVector3); virtual; abstract;
end;
TPerspectiveRaysWindow = class(TRaysWindow)
private
WindowWidth, WindowHeight: Single;
PerspectiveAngles: TVector2;
public
constructor Create(const ACamPosition, ACamDirection, ACamUp: TVector3;
const APerspectiveAngles: TVector2);
procedure PrimaryRay(const x, y: Single;
const ScreenWidth, ScreenHeight: Integer;
out RayOrigin, RayDirection: TVector3); override;
end;
TFrustumRaysWindow = class(TRaysWindow)
private
Dimensions: TFloatRectangle;
public
constructor Create(const ACamPosition, ACamDirection, ACamUp: TVector3;
const ADimensions: TFloatRectangle);
procedure PrimaryRay(const x, y: Single;
const ScreenWidth, ScreenHeight: Integer;
out RayOrigin, RayDirection: TVector3); override;
end;
TOrthographicRaysWindow = class(TRaysWindow)
private
Dimensions: TFloatRectangle;
public
constructor Create(const ACamPosition, ACamDirection, ACamUp: TVector3;
const ADimensions: TFloatRectangle);
procedure PrimaryRay(const x, y: Single;
const ScreenWidth, ScreenHeight: Integer;
out RayOrigin, RayDirection: TVector3); override;
end;
{ Calculate position and direction of the primary ray cast from CamPosition,
going through the pixel X, Y.
Takes into account camera 3D settings and screen sizes.
RayDirection will always be normalized, just like from TRaysWindow.PrimaryRay.
If you want to call this many times for the same camera settings,
it may be more optimal to create TRaysWindow instance first
and call it's TRaysWindow.PrimaryRay method. }
procedure PrimaryRay(const x, y: Single; const ScreenWidth, ScreenHeight: Integer;
const CamPosition, CamDirection, CamUp: TVector3;
const Projection: TProjection;
out RayOrigin, RayDirection: TVector3);
implementation
uses Math, CastleUtils;
{ TRaysWindow ------------------------------------------------------------ }
constructor TRaysWindow.Create(
const ACamPosition, ACamDirection, ACamUp: TVector3);
begin
inherited Create;
FCamPosition := ACamPosition;
FCamDirection := ACamDirection;
FCamUp := ACamUp;
{ fix CamUp }
MakeVectorsOrthoOnTheirPlane(FCamUp, FCamDirection);
{ CamSide will be normalized, if CamDirection and CamUp are normalized too. }
CamSide := TVector3.CrossProduct(CamDirection, CamUp);
Assert(SameValue(CamDirection.LengthSqr, 1.0, 0.01));
Assert(SameValue(CamUp.LengthSqr, 1.0, 0.01));
Assert(SameValue(CamSide.LengthSqr, 1.0, 0.01));
end;
class function TRaysWindow.CreateDescendant(
const ACamPosition, ACamDirection, ACamUp: TVector3;
const Projection: TProjection): TRaysWindow;
begin
case Projection.ProjectionType of
ptPerspective:
Result := TPerspectiveRaysWindow.Create(
ACamPosition, ACamDirection, ACamUp, Projection.PerspectiveAngles);
ptOrthographic:
Result := TOrthographicRaysWindow.Create(
ACamPosition, ACamDirection, ACamUp, Projection.Dimensions);
ptFrustum:
Result := TFrustumRaysWindow.Create(
ACamPosition, ACamDirection, ACamUp, Projection.Dimensions);
else raise EInternalError.Create('TRaysWindow.CreateDescendant:ProjectionType?');
end;
end;
{ TPerspectiveRaysWindow ----------------------------------------------------- }
constructor TPerspectiveRaysWindow.Create(
const ACamPosition, ACamDirection, ACamUp: TVector3;
const APerspectiveAngles: TVector2);
begin
inherited Create(ACamPosition, ACamDirection, ACamUp);
PerspectiveAngles := APerspectiveAngles;
{ calculate window parameters, ignoring camera settings.
We assume distance to projection plane is 1 (this simplifies some calculations,
and we can choose this distance arbitrarily --- it doesn't matter for user
of this class).
We know that WindowWidth / 2 = Tan(ViewAngleX / 2).
From this, equations below follow. }
WindowWidth := Tan(DegToRad(PerspectiveAngles.Data[0]) / 2) * 2;
WindowHeight := Tan(DegToRad(PerspectiveAngles.Data[1]) / 2) * 2;
end;
procedure TPerspectiveRaysWindow.PrimaryRay(const x, y: Single;
const ScreenWidth, ScreenHeight: Integer;
out RayOrigin, RayDirection: TVector3);
begin
RayOrigin := CamPosition;
{ Direction of ray, ignoring current camera settings
(assume camera position = zero, direction = -Z, up = +Y).
Integer X, Y values should result in a ray that goes
right through the middle of the pixel area. }
RayDirection.Data[0] := MapRange(x+0.5, 0, ScreenWidth , -WindowWidth /2, WindowWidth /2);
RayDirection.Data[1] := MapRange(y+0.5, 0, ScreenHeight, -WindowHeight/2, WindowHeight/2);
RayDirection.Data[2] := -1;
{ Transform ray to take camera settings into acount. }
RayDirection := TransformToCoords(RayDirection, CamSide, CamUp, -CamDirection);
RayDirection.NormalizeMe;
end;
{ TFrustumRaysWindow ---------------------------------------------------- }
constructor TFrustumRaysWindow.Create(
const ACamPosition, ACamDirection, ACamUp: TVector3;
const ADimensions: TFloatRectangle);
begin
inherited Create(ACamPosition, ACamDirection, ACamUp);
{ Workaround FPC 3.0.4 bug (internal error) on Darwin for AArch64
(not on other platforms):
castlerays.pas(262,3) Fatal: Internal error 2014121702
}
{$if defined(VER3_0) and defined(DARWIN) and defined(CPUAARCH64)}
Dimensions := FloatRectangle(
ADimensions.Left,
ADimensions.Bottom,
ADimensions.Width,
ADimensions.Height
);
{$else}
Dimensions := ADimensions;
{$endif}
end;
procedure TFrustumRaysWindow.PrimaryRay(const X, Y: Single;
const ScreenWidth, ScreenHeight: Integer;
out RayOrigin, RayDirection: TVector3);
begin
{ this is quite similar to TPerspectiveRaysWindow }
RayOrigin := CamPosition;
{ Direction of ray, ignoring current camera settings
(assume camera position = zero, direction = -Z, up = +Y).
Integer X, Y values should result in a ray that goes
right through the middle of the pixel area. }
RayDirection.Data[0] := MapRange(X + 0.5, 0, ScreenWidth , Dimensions.Left, Dimensions.Right);
RayDirection.Data[1] := MapRange(Y + 0.5, 0, ScreenHeight, Dimensions.Bottom, Dimensions.Top);
RayDirection.Data[2] := -1;
{ Transform ray to take camera settings into acount. }
RayDirection := TransformToCoords(RayDirection, CamSide, CamUp, -CamDirection);
RayDirection.NormalizeMe;
end;
{ TOrthographicRaysWindow ---------------------------------------------------- }
constructor TOrthographicRaysWindow.Create(
const ACamPosition, ACamDirection, ACamUp: TVector3;
const ADimensions: TFloatRectangle);
begin
inherited Create(ACamPosition, ACamDirection, ACamUp);
{ Workaround FPC 3.0.4 bug (internal error) on Darwin for AArch64
(not on other platforms):
castlerays.pas(262,3) Fatal: Internal error 2014121702
}
{$if defined(VER3_0) and defined(DARWIN) and defined(CPUAARCH64)}
Dimensions := FloatRectangle(
ADimensions.Left,
ADimensions.Bottom,
ADimensions.Width,
ADimensions.Height
);
{$else}
Dimensions := ADimensions;
{$endif}
end;
procedure TOrthographicRaysWindow.PrimaryRay(const x, y: Single;
const ScreenWidth, ScreenHeight: Integer;
out RayOrigin, RayDirection: TVector3);
begin
RayOrigin := CamPosition;
RayOrigin := RayOrigin + CamSide * MapRange(X + 0.5, 0, ScreenWidth,
Dimensions.Left, Dimensions.Right);
RayOrigin := RayOrigin + CamUp * MapRange(Y + 0.5, 0, ScreenHeight,
Dimensions.Bottom, Dimensions.Top);
{ CamDirection must already be normalized, so RayDirection is normalized too }
RayDirection := CamDirection;
end;
{ global functions ----------------------------------------------------------- }
procedure PrimaryRay(const x, y: Single; const ScreenWidth, ScreenHeight: Integer;
const CamPosition, CamDirection, CamUp: TVector3;
const Projection: TProjection;
out RayOrigin, RayDirection: TVector3);
var
RaysWindow: TRaysWindow;
begin
RaysWindow := TRaysWindow.CreateDescendant(CamPosition, CamDirection, CamUp,
Projection);
try
RaysWindow.PrimaryRay(x, y, ScreenWidth, ScreenHeight, RayOrigin, RayDirection);
finally RaysWindow.Free end;
end;
end.
|