| 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 |
{ Random sampling of points (directions) on a sphere and hemisphere. |
| 17 |
Useful e.g. for ray-tracers. |
| 18 |
|
| 19 |
Most of the implementation based on "Global Illumination Compendium" IV.B |
| 20 |
[http://www.cs.kuleuven.ac.be/~phil/GI/]. |
| 21 |
See also images there, they help to illustrate the meaning of some |
| 22 |
functions here. |
| 23 |
|
| 24 |
We use two ways to represent points on hemisphere: |
| 25 |
|
| 26 |
@orderedList( |
| 27 |
@item(Functions @italic(without "XYZ" suffix) return vector |
| 28 |
of 2 floats = two angles, called phi and theta (in this order). |
| 29 |
Phi (in [0, 2*Pi]) is an angle from some chosen meridian. |
| 30 |
Theta (in [0, Pi/2] for hemisphere and [0, Pi] for sphere) |
| 31 |
is an angle from chosen vector pointing outward from the (hemi)sphere. |
| 32 |
See images in "Global Illumination Compendium", |
| 33 |
they are probably much easier to understand than this definition.) |
| 34 |
|
| 35 |
@item(Functions @italic(with "XYZ" suffix) return |
| 36 |
3D point x, y, z. |
| 37 |
|
| 38 |
@unorderedList( |
| 39 |
@item (0, 0, 0) is the center of (hemi)sphere, |
| 40 |
@item (0, 0, 1) is the chosen outward vector (i.e. Theta = 0 there), |
| 41 |
@item (1, 0, 0) is the direction where Phi = 0 and Theta = Pi/2, |
| 42 |
@item (0, 1, 0) is the direction where Phi = Pi/2 and Theta = Pi/2. |
| 43 |
) |
| 44 |
|
| 45 |
This is matching conventions in "Global Illumination Compendium", |
| 46 |
see there (point 21).) |
| 47 |
) |
| 48 |
|
| 49 |
Functions with Density <> Const return PdfValue for returned point, |
| 50 |
i.e. for density p(Theta) it's PfdValue = p(Result.Y). |
| 51 |
These functions try to calculate PdfValue smartly (often calculating |
| 52 |
PfdValue and calculating Result uses the same intermediate calculation, |
| 53 |
so we can save some computation). PdfValue is needed for importance sampling. |
| 54 |
} |
| 55 |
|
| 56 |
unit CastleInternalSphereSampling; |
| 57 |
|
| 58 |
{$I castleconf.inc} |
| 59 |
|
| 60 |
interface |
| 61 |
|
| 62 |
uses CastleVectors, CastleUtils; |
| 63 |
|
| 64 |
{ Convert from PhiTheta representation of (hemi)sphere direction to |
| 65 |
XYZ representation. |
| 66 |
|
| 67 |
See the beginning of this unit's documentation, CastleInternalSphereSampling, |
| 68 |
for more precise description of XYZ representation. } |
| 69 |
function PhiThetaToXYZ(const PhiTheta: TVector2; const SphereRadius: Single) |
| 70 |
:TVector3; overload; |
| 71 |
|
| 72 |
{ Convert from PhiTheta representation of (hemi)sphere direction to |
| 73 |
XYZ representation. |
| 74 |
|
| 75 |
This is the more advanced version where you can freely specify which |
| 76 |
vector is the "main outside (hemi)sphere vector", SphereTheta0. |
| 77 |
Points with Theta = 0 are exactly on SphereTheta0. |
| 78 |
It is @italic(undefined) where point like (Phi = 0, Theta = Pi/2) |
| 79 |
(or any other point with Theta <> 0) will be placed, |
| 80 |
i.e. it's not defined where's the "chosen meridian" for Phi = 0. |
| 81 |
However @italic(it's defined that this meridian will be determined only by |
| 82 |
SphereTheta0), and this is usually sufficient (since this makes sure |
| 83 |
that sampling and then converting to XYZ multiple points with the same |
| 84 |
SphereTheta0 will preserve sampled density). |
| 85 |
|
| 86 |
Note that the length of SphereTheta0 determines also the sphere radius. } |
| 87 |
function PhiThetaToXYZ(const PhiTheta: TVector2; |
| 88 |
const SphereTheta0: TVector3): TVector3; overload; |
| 89 |
|
| 90 |
{ Convert from XYZ representation of (hemi)sphere direction to PhiTheta. } |
| 91 |
function XYZToPhiTheta(const XYZ: TVector3): TVector2; |
| 92 |
|
| 93 |
{ Random point (direction) on unit hemisphere, sampled with |
| 94 |
constant density (p(Theta) = 1/2*Pi). |
| 95 |
@groupBegin } |
| 96 |
function RandomHemispherePointConst: TVector2; |
| 97 |
function RandomHemispherePointConstXYZ: TVector3; |
| 98 |
{ @groupEnd } |
| 99 |
|
| 100 |
{ Random point (direction) on unit hemisphere, sampled with |
| 101 |
density p(Theta) = cos(Theta)/Pi. |
| 102 |
@groupBegin } |
| 103 |
function RandomHemispherePointCosTheta( |
| 104 |
out PdfValue: Single): TVector2; |
| 105 |
function RandomHemispherePointCosThetaXYZ( |
| 106 |
out PdfValue: Single): TVector3; |
| 107 |
{ @groupEnd } |
| 108 |
|
| 109 |
{ Random point (direction) on unit hemisphere, sampled with |
| 110 |
density p(Theta) = (n+1) * (cos(Theta))^n / 2*Pi. |
| 111 |
@groupBegin } |
| 112 |
function RandomHemispherePointCosThetaExp(const n: Single; |
| 113 |
out PdfValue: Single): TVector2; |
| 114 |
function RandomHemispherePointCosThetaExpXYZ(const n: Single; |
| 115 |
out PdfValue: Single): TVector3; |
| 116 |
{ @groupEnd } |
| 117 |
|
| 118 |
implementation |
| 119 |
|
| 120 |
{ Note: Math unit operates of Float type. We try to minimize in some routines |
| 121 |
below conversions Single <-> Float. } |
| 122 |
|
| 123 |
uses Math; |
| 124 |
|
| 125 |
function PhiThetaToXYZ(const PhiTheta: TVector2; const SphereRadius: Single): TVector3; |
| 126 |
var |
| 127 |
SinPhi, CosPhi, SinTheta, CosTheta: Float; |
| 128 |
begin |
| 129 |
SinCos(PhiTheta.X, SinPhi, CosPhi); |
| 130 |
SinCos(PhiTheta.Y, SinTheta, CosTheta); |
| 131 |
|
| 132 |
result.X := SphereRadius * CosPhi * SinTheta; |
| 133 |
result.Y := SphereRadius * SinPhi * SinTheta; |
| 134 |
result.Z := SphereRadius * CosTheta; |
| 135 |
end; |
| 136 |
|
| 137 |
function XYZToPhiTheta(const XYZ: TVector3): TVector2; |
| 138 |
begin |
| 139 |
Result.X := ArcTan2(XYZ.Y, XYZ.X); |
| 140 |
Result.Y := ArcTan2(Sqrt(Sqr(XYZ.X) + Sqr(XYZ.Y)), XYZ.Z); |
| 141 |
end; |
| 142 |
|
| 143 |
function PhiThetaToXYZ(const PhiTheta: TVector2; const SphereTheta0: TVector3): TVector3; |
| 144 |
var |
| 145 |
NewX, NewY: TVector3; |
| 146 |
SphereRadius, NewXLen, NewYLen: Single; |
| 147 |
begin |
| 148 |
result := PhiThetaToXYZ(PhiTheta, 1); |
| 149 |
|
| 150 |
{ make NewX anything orthogonal (but not zero) to SphereTheta0. } |
| 151 |
if IsZero(SphereTheta0.X) and IsZero(SphereTheta0.Y) then |
| 152 |
begin |
| 153 |
{ then we're sure that SphereTheta0.Z <> 0, so NewX will not be zero } |
| 154 |
NewX.X := 0; |
| 155 |
NewX.Y := -SphereTheta0.Z; |
| 156 |
NewX.Z := SphereTheta0.Y; |
| 157 |
end else |
| 158 |
begin |
| 159 |
NewX.X := -SphereTheta0.Y; |
| 160 |
NewX.Y := SphereTheta0.X; |
| 161 |
NewX.Z := 0; |
| 162 |
end; |
| 163 |
NewY := TVector3.CrossProduct(SphereTheta0, NewX); |
| 164 |
{ set correct lengths for NewX and NewY. We calculate NewYLen fast, without |
| 165 |
any Sqrt (which would happen inside VectorLen), because we know that |
| 166 |
NewY was calculated by TVector3.CrossProduct above and that NewX and SphereTheta0 |
| 167 |
are orthogonal. } |
| 168 |
SphereRadius := SphereTheta0.Length; |
| 169 |
NewXLen := NewX.Length; |
| 170 |
NewYLen := NewXLen * SphereRadius; |
| 171 |
|
| 172 |
NewX := NewX * SphereRadius/NewXLen; |
| 173 |
NewY := NewY * SphereRadius/NewYLen; |
| 174 |
|
| 175 |
{ TODO: create TMatrix4.MultPointVar to speed this a little bit? } |
| 176 |
Result := TransformToCoordsMatrix(TVector3.Zero, |
| 177 |
NewX, |
| 178 |
NewY, |
| 179 |
SphereTheta0).MultPoint(Result); |
| 180 |
end; |
| 181 |
|
| 182 |
function RandomHemispherePointConst: TVector2; |
| 183 |
begin |
| 184 |
Result.X := 2 * Pi * Random; |
| 185 |
Result.Y := ArcCos(Random); |
| 186 |
end; |
| 187 |
|
| 188 |
function RandomHemispherePointConstXYZ: TVector3; |
| 189 |
var |
| 190 |
R1, R2, SqRoot: Single; |
| 191 |
C, S: Single; |
| 192 |
begin |
| 193 |
R1 := Random; |
| 194 |
R2 := Random; |
| 195 |
SinCos(2 * Pi * R1, S, C); |
| 196 |
SqRoot := Sqrt(1 - Sqr(R2)); |
| 197 |
|
| 198 |
Result.X := C * SqRoot; |
| 199 |
Result.Y := S * SqRoot; |
| 200 |
Result.Z := R2; |
| 201 |
end; |
| 202 |
|
| 203 |
function RandomHemispherePointCosTheta( |
| 204 |
out PdfValue: Single): TVector2; |
| 205 |
var |
| 206 |
SqrtR2: Float; |
| 207 |
begin |
| 208 |
SqrtR2 := Sqrt(Random); |
| 209 |
|
| 210 |
Result.X := 2 * Pi * Random; |
| 211 |
Result.Y := ArcCos(SqrtR2); |
| 212 |
PdfValue := SqrtR2 / Pi; |
| 213 |
end; |
| 214 |
|
| 215 |
function RandomHemispherePointCosThetaXYZ( |
| 216 |
out PdfValue: Single): TVector3; |
| 217 |
var |
| 218 |
SqRoot, R1, R2: Single; |
| 219 |
SinR1, CosR1: Float; |
| 220 |
begin |
| 221 |
R1 := Random; |
| 222 |
R2 := Random; |
| 223 |
SinCos(2 * Pi * R1, SinR1, CosR1); |
| 224 |
SqRoot := Sqrt(1 - R2); |
| 225 |
|
| 226 |
Result.X := CosR1 * SqRoot; |
| 227 |
Result.Y := SinR1 * SqRoot; |
| 228 |
Result.Z := Sqrt(R2); |
| 229 |
PdfValue := Result.Z; |
| 230 |
end; |
| 231 |
|
| 232 |
function RandomHemispherePointCosThetaExp(const n: Single; |
| 233 |
out PdfValue: Single): TVector2; |
| 234 |
var |
| 235 |
R2: Float; |
| 236 |
begin |
| 237 |
R2 := Random; |
| 238 |
|
| 239 |
Result.X := 2 * Pi * Random; |
| 240 |
Result.Y := ArcCos(Power(R2, 1 / (n + 1))); |
| 241 |
PdfValue := (n + 1) * Power(R2, n / (n + 1)) / (2 * Pi); |
| 242 |
end; |
| 243 |
|
| 244 |
function RandomHemispherePointCosThetaExpXYZ(const n: Single; |
| 245 |
out PdfValue: Single): TVector3; |
| 246 |
var |
| 247 |
R1, R2, R2Power, R2Root: Single; |
| 248 |
SinR1, CosR1: Float; |
| 249 |
begin |
| 250 |
R1 := Random; |
| 251 |
R2 := Random; |
| 252 |
SinCos(2*Pi*R1, SinR1, CosR1); |
| 253 |
R2Power := Power(R2, 1 / (n + 1)); |
| 254 |
R2Root := Sqrt(1 - Sqr(R2Power)); |
| 255 |
|
| 256 |
Result.X := CosR1 * R2Root; |
| 257 |
Result.Y := SinR1 * R2Root; |
| 258 |
Result.Z := R2Power; |
| 259 |
PdfValue := (n + 1) * Power(R2, n / (n + 1)) / (2 * Pi); |
| 260 |
end; |
| 261 |
|
| 262 |
end. |