| 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 |
{ Calculating 3D rays that correspond to the given points on 2D screen. |
| 17 |
This is used by ray-tracing (casting a ray for each image pixel) |
| 18 |
or when picking objects (what 3D object/point is indicated by |
| 19 |
the current mouse position). } |
| 20 |
unit CastleInternalRays; |
| 21 |
|
| 22 |
{$I castleconf.inc} |
| 23 |
|
| 24 |
interface |
| 25 |
|
| 26 |
uses CastleVectors, CastleProjection, CastleRectangles; |
| 27 |
|
| 28 |
type |
| 29 |
{ Calculate primary rays for given camera settings and screen size. } |
| 30 |
TRaysWindow = class |
| 31 |
private |
| 32 |
FCamPosition, FCamDirection, FCamUp: TVector3; |
| 33 |
CamSide: TVector3; |
| 34 |
public |
| 35 |
{ Camera vectors. Initialized in the constructor. |
| 36 |
Must be given already normalized. |
| 37 |
Note that CamUp may be changed in constructor, to be always perfectly |
| 38 |
orthogonal to CamDirection. |
| 39 |
|
| 40 |
@groupBegin } |
| 41 |
property CamPosition: TVector3 read FCamPosition; |
| 42 |
property CamDirection: TVector3 read FCamDirection; |
| 43 |
property CamUp: TVector3 read FCamUp; |
| 44 |
{ @groupEnd } |
| 45 |
|
| 46 |
{ Constructor. Calculates some values based on camera settings, |
| 47 |
this way many calls to PrimaryRay for the same camera settings |
| 48 |
are fast (useful for ray-tracers). } |
| 49 |
constructor Create(const ACamPosition, ACamDirection, ACamUp: TVector3); |
| 50 |
|
| 51 |
{ Create appropriate TRaysWindow instance. |
| 52 |
Constructs non-abstract descendant (TPerspectiveRaysWindow or |
| 53 |
TOrthographicRaysWindow, depending on Projection.ProjectionType). } |
| 54 |
class function CreateDescendant( |
| 55 |
const ACamPosition, ACamDirection, ACamUp: TVector3; |
| 56 |
const Projection: TProjection): TRaysWindow; |
| 57 |
|
| 58 |
{ Calculate position and direction of the primary ray cast from CamPosition, |
| 59 |
going through the pixel X, Y. |
| 60 |
|
| 61 |
X, Y coordinates start from (0, 0) if bottom left (like in typical 2D |
| 62 |
OpenGL). When they are integers and in the range of |
| 63 |
X = 0..ScreenWidth-1 (left..right), Y = 0..ScreenHeight-1 (bottom..top) |
| 64 |
it's guaranteed that resulting ray will go exactly through the middle |
| 65 |
of the appropriate pixel (on imaginary "rzutnia" = image positioned |
| 66 |
paraller to view direction). But you can provide non-integer X, Y, |
| 67 |
useful for multisampling (taking many samples within the pixel, |
| 68 |
like (X, Y) = (PixX + Random - 0.5, PixY + Random - 0.5)). |
| 69 |
|
| 70 |
Resulting RayDirection is guaranteed to be normalized |
| 71 |
(this is in practice not costly to us, and it often helps --- when ray |
| 72 |
direction is normalized, various distances from ray collisions are "real"). } |
| 73 |
procedure PrimaryRay(const x, y: Single; |
| 74 |
const ScreenWidth, ScreenHeight: Single; |
| 75 |
out RayOrigin, RayDirection: TVector3); virtual; abstract; |
| 76 |
end; |
| 77 |
|
| 78 |
TPerspectiveRaysWindow = class(TRaysWindow) |
| 79 |
private |
| 80 |
WindowWidth, WindowHeight: Single; |
| 81 |
PerspectiveAngles: TVector2; |
| 82 |
public |
| 83 |
constructor Create(const ACamPosition, ACamDirection, ACamUp: TVector3; |
| 84 |
const APerspectiveAngles: TVector2); |
| 85 |
|
| 86 |
procedure PrimaryRay(const x, y: Single; |
| 87 |
const ScreenWidth, ScreenHeight: Single; |
| 88 |
out RayOrigin, RayDirection: TVector3); override; |
| 89 |
end; |
| 90 |
|
| 91 |
TFrustumRaysWindow = class(TRaysWindow) |
| 92 |
private |
| 93 |
Dimensions: TFloatRectangle; |
| 94 |
public |
| 95 |
constructor Create(const ACamPosition, ACamDirection, ACamUp: TVector3; |
| 96 |
const ADimensions: TFloatRectangle); |
| 97 |
|
| 98 |
procedure PrimaryRay(const x, y: Single; |
| 99 |
const ScreenWidth, ScreenHeight: Single; |
| 100 |
out RayOrigin, RayDirection: TVector3); override; |
| 101 |
end; |
| 102 |
|
| 103 |
TOrthographicRaysWindow = class(TRaysWindow) |
| 104 |
private |
| 105 |
Dimensions: TFloatRectangle; |
| 106 |
public |
| 107 |
constructor Create(const ACamPosition, ACamDirection, ACamUp: TVector3; |
| 108 |
const ADimensions: TFloatRectangle); |
| 109 |
|
| 110 |
procedure PrimaryRay(const x, y: Single; |
| 111 |
const ScreenWidth, ScreenHeight: Single; |
| 112 |
out RayOrigin, RayDirection: TVector3); override; |
| 113 |
end; |
| 114 |
|
| 115 |
{ Calculate position and direction of the primary ray cast from CamPosition, |
| 116 |
going through the pixel X, Y. |
| 117 |
Takes into account camera 3D settings and screen sizes. |
| 118 |
RayDirection will always be normalized, just like from TRaysWindow.PrimaryRay. |
| 119 |
|
| 120 |
If you want to call this many times for the same camera settings, |
| 121 |
it may be more optimal to create TRaysWindow instance first |
| 122 |
and call it's TRaysWindow.PrimaryRay method. } |
| 123 |
procedure PrimaryRay(const x, y: Single; const ScreenWidth, ScreenHeight: Single; |
| 124 |
const CamPosition, CamDirection, CamUp: TVector3; |
| 125 |
const Projection: TProjection; |
| 126 |
out RayOrigin, RayDirection: TVector3); |
| 127 |
|
| 128 |
implementation |
| 129 |
|
| 130 |
uses Math, CastleUtils; |
| 131 |
|
| 132 |
{ TRaysWindow ------------------------------------------------------------ } |
| 133 |
|
| 134 |
constructor TRaysWindow.Create( |
| 135 |
const ACamPosition, ACamDirection, ACamUp: TVector3); |
| 136 |
begin |
| 137 |
inherited Create; |
| 138 |
|
| 139 |
FCamPosition := ACamPosition; |
| 140 |
FCamDirection := ACamDirection; |
| 141 |
FCamUp := ACamUp; |
| 142 |
|
| 143 |
{ fix CamUp } |
| 144 |
MakeVectorsOrthoOnTheirPlane(FCamUp, FCamDirection); |
| 145 |
|
| 146 |
{ CamSide will be normalized, if CamDirection and CamUp are normalized too. } |
| 147 |
CamSide := TVector3.CrossProduct(CamDirection, CamUp); |
| 148 |
|
| 149 |
Assert(SameValue(CamDirection.LengthSqr, 1.0, 0.01)); |
| 150 |
Assert(SameValue(CamUp.LengthSqr, 1.0, 0.01)); |
| 151 |
Assert(SameValue(CamSide.LengthSqr, 1.0, 0.01)); |
| 152 |
end; |
| 153 |
|
| 154 |
class function TRaysWindow.CreateDescendant( |
| 155 |
const ACamPosition, ACamDirection, ACamUp: TVector3; |
| 156 |
const Projection: TProjection): TRaysWindow; |
| 157 |
begin |
| 158 |
case Projection.ProjectionType of |
| 159 |
ptPerspective: |
| 160 |
Result := TPerspectiveRaysWindow.Create( |
| 161 |
ACamPosition, ACamDirection, ACamUp, Projection.PerspectiveAnglesRad); |
| 162 |
ptOrthographic: |
| 163 |
Result := TOrthographicRaysWindow.Create( |
| 164 |
ACamPosition, ACamDirection, ACamUp, Projection.Dimensions); |
| 165 |
ptFrustum: |
| 166 |
Result := TFrustumRaysWindow.Create( |
| 167 |
ACamPosition, ACamDirection, ACamUp, Projection.Dimensions); |
| 168 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 169 |
else raise EInternalError.Create('TRaysWindow.CreateDescendant:ProjectionType?'); |
| 170 |
{$endif} |
| 171 |
end; |
| 172 |
end; |
| 173 |
|
| 174 |
{ TPerspectiveRaysWindow ----------------------------------------------------- } |
| 175 |
|
| 176 |
constructor TPerspectiveRaysWindow.Create( |
| 177 |
const ACamPosition, ACamDirection, ACamUp: TVector3; |
| 178 |
const APerspectiveAngles: TVector2); |
| 179 |
begin |
| 180 |
inherited Create(ACamPosition, ACamDirection, ACamUp); |
| 181 |
|
| 182 |
PerspectiveAngles := APerspectiveAngles; |
| 183 |
|
| 184 |
{ calculate window parameters, ignoring camera settings. |
| 185 |
We assume distance to projection plane is 1 (this simplifies some calculations, |
| 186 |
and we can choose this distance arbitrarily --- it doesn't matter for user |
| 187 |
of this class). |
| 188 |
We know that WindowWidth / 2 = Tan(ViewAngleX / 2). |
| 189 |
From this, equations below follow. } |
| 190 |
WindowWidth := Tan(PerspectiveAngles.X / 2) * 2; |
| 191 |
WindowHeight := Tan(PerspectiveAngles.Y / 2) * 2; |
| 192 |
end; |
| 193 |
|
| 194 |
procedure TPerspectiveRaysWindow.PrimaryRay(const x, y: Single; |
| 195 |
const ScreenWidth, ScreenHeight: Single; |
| 196 |
out RayOrigin, RayDirection: TVector3); |
| 197 |
begin |
| 198 |
RayOrigin := CamPosition; |
| 199 |
|
| 200 |
{ Direction of ray, ignoring current camera settings |
| 201 |
(assume camera position = zero, direction = -Z, up = +Y). |
| 202 |
Integer X, Y values should result in a ray that goes |
| 203 |
right through the middle of the pixel area. } |
| 204 |
RayDirection.X := MapRange(x+0.5, 0, ScreenWidth , -WindowWidth /2, WindowWidth /2); |
| 205 |
RayDirection.Y := MapRange(y+0.5, 0, ScreenHeight, -WindowHeight/2, WindowHeight/2); |
| 206 |
RayDirection.Z := -1; |
| 207 |
|
| 208 |
{ Transform ray to take camera settings into acount. } |
| 209 |
RayDirection := TransformToCoords(RayDirection, CamSide, CamUp, -CamDirection); |
| 210 |
|
| 211 |
RayDirection := RayDirection.Normalize; |
| 212 |
end; |
| 213 |
|
| 214 |
{ TFrustumRaysWindow ---------------------------------------------------- } |
| 215 |
|
| 216 |
constructor TFrustumRaysWindow.Create( |
| 217 |
const ACamPosition, ACamDirection, ACamUp: TVector3; |
| 218 |
const ADimensions: TFloatRectangle); |
| 219 |
begin |
| 220 |
inherited Create(ACamPosition, ACamDirection, ACamUp); |
| 221 |
{ Workaround FPC bug on Darwin for AArch64 (not on other platforms): |
| 222 |
castlerays.pas(262,3) Fatal: Internal error 2014121702 |
| 223 |
Occurs with 3.0.4 and with 3.3.1 (r44333 from 2020/03/22). } |
| 224 |
{$if defined(DARWIN) and defined(CPUAARCH64)} |
| 225 |
Dimensions := FloatRectangle( |
| 226 |
ADimensions.Left, |
| 227 |
ADimensions.Bottom, |
| 228 |
ADimensions.Width, |
| 229 |
ADimensions.Height |
| 230 |
); |
| 231 |
{$else} |
| 232 |
Dimensions := ADimensions; |
| 233 |
{$endif} |
| 234 |
end; |
| 235 |
|
| 236 |
procedure TFrustumRaysWindow.PrimaryRay(const X, Y: Single; |
| 237 |
const ScreenWidth, ScreenHeight: Single; |
| 238 |
out RayOrigin, RayDirection: TVector3); |
| 239 |
begin |
| 240 |
{ this is quite similar to TPerspectiveRaysWindow } |
| 241 |
|
| 242 |
RayOrigin := CamPosition; |
| 243 |
|
| 244 |
{ Direction of ray, ignoring current camera settings |
| 245 |
(assume camera position = zero, direction = -Z, up = +Y). |
| 246 |
Integer X, Y values should result in a ray that goes |
| 247 |
right through the middle of the pixel area. } |
| 248 |
RayDirection.X := MapRange(X + 0.5, 0, ScreenWidth , Dimensions.Left, Dimensions.Right); |
| 249 |
RayDirection.Y := MapRange(Y + 0.5, 0, ScreenHeight, Dimensions.Bottom, Dimensions.Top); |
| 250 |
RayDirection.Z := -1; |
| 251 |
|
| 252 |
{ Transform ray to take camera settings into acount. } |
| 253 |
RayDirection := TransformToCoords(RayDirection, CamSide, CamUp, -CamDirection); |
| 254 |
|
| 255 |
RayDirection := RayDirection.Normalize; |
| 256 |
end; |
| 257 |
|
| 258 |
{ TOrthographicRaysWindow ---------------------------------------------------- } |
| 259 |
|
| 260 |
constructor TOrthographicRaysWindow.Create( |
| 261 |
const ACamPosition, ACamDirection, ACamUp: TVector3; |
| 262 |
const ADimensions: TFloatRectangle); |
| 263 |
begin |
| 264 |
inherited Create(ACamPosition, ACamDirection, ACamUp); |
| 265 |
{ Workaround FPC bug on Darwin for AArch64 (not on other platforms): |
| 266 |
castlerays.pas(262,3) Fatal: Internal error 2014121702 |
| 267 |
Occurs with 3.0.4 and with 3.3.1 (r44333 from 2020/03/22). } |
| 268 |
{$if defined(DARWIN) and defined(CPUAARCH64)} |
| 269 |
Dimensions := FloatRectangle( |
| 270 |
ADimensions.Left, |
| 271 |
ADimensions.Bottom, |
| 272 |
ADimensions.Width, |
| 273 |
ADimensions.Height |
| 274 |
); |
| 275 |
{$else} |
| 276 |
Dimensions := ADimensions; |
| 277 |
{$endif} |
| 278 |
end; |
| 279 |
|
| 280 |
procedure TOrthographicRaysWindow.PrimaryRay(const x, y: Single; |
| 281 |
const ScreenWidth, ScreenHeight: Single; |
| 282 |
out RayOrigin, RayDirection: TVector3); |
| 283 |
begin |
| 284 |
RayOrigin := CamPosition; |
| 285 |
RayOrigin := RayOrigin + CamSide * MapRange(X + 0.5, 0, ScreenWidth, |
| 286 |
Dimensions.Left, Dimensions.Right); |
| 287 |
RayOrigin := RayOrigin + CamUp * MapRange(Y + 0.5, 0, ScreenHeight, |
| 288 |
Dimensions.Bottom, Dimensions.Top); |
| 289 |
|
| 290 |
{ CamDirection must already be normalized, so RayDirection is normalized too } |
| 291 |
RayDirection := CamDirection; |
| 292 |
end; |
| 293 |
|
| 294 |
{ global functions ----------------------------------------------------------- } |
| 295 |
|
| 296 |
procedure PrimaryRay(const x, y: Single; const ScreenWidth, ScreenHeight: Single; |
| 297 |
const CamPosition, CamDirection, CamUp: TVector3; |
| 298 |
const Projection: TProjection; |
| 299 |
out RayOrigin, RayDirection: TVector3); |
| 300 |
var |
| 301 |
RaysWindow: TRaysWindow; |
| 302 |
begin |
| 303 |
RaysWindow := TRaysWindow.CreateDescendant(CamPosition, CamDirection, CamUp, |
| 304 |
Projection); |
| 305 |
try |
| 306 |
RaysWindow.PrimaryRay(x, y, ScreenWidth, ScreenHeight, RayOrigin, RayDirection); |
| 307 |
finally RaysWindow.Free end; |
| 308 |
end; |
| 309 |
|
| 310 |
end. |