| 1 |
{ |
| 2 |
Copyright 2008-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 |
{ Spherical harmonic basis functions. } |
| 17 |
unit CastleInternalSphericalHarmonics; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses CastleVectors, CastleUtils, Math, CastleInternalCubeMaps; |
| 24 |
|
| 25 |
const |
| 26 |
{ How many basis can SHBasis calculate. LM for SHBasis must be within |
| 27 |
0 .. SHBasesCount - 1. } |
| 28 |
MaxSHBasis = 25; |
| 29 |
|
| 30 |
{ The first SH basis function is actually constant. |
| 31 |
This is sometimes useful. } |
| 32 |
SHBasis0 = 1 / (2 * {$ifdef FPC}Sqrt(Pi){$else}1.772453850905516{$endif}); |
| 33 |
|
| 34 |
{ Calculate spherical harmonic basis function for given arguments. |
| 35 |
|
| 36 |
LM indicates (L, M) that specify which SH basis to use. |
| 37 |
LM determines a pair (L, M) in the natural order: for each L, |
| 38 |
take for each M from -L to L. |
| 39 |
That is, LM = (0, 1, 2, 3, ...) indicate |
| 40 |
@preformatted( |
| 41 |
(L, M) = |
| 42 |
( (0, 0), |
| 43 |
(1, -1), (1, 0), (1, 1), |
| 44 |
(2, -2), (2, -1), (2, 0), (2, 1), (2, 2), |
| 45 |
... ) |
| 46 |
) ) } |
| 47 |
function SHBasis(const LM: Cardinal; const PhiTheta: TVector2): Float; |
| 48 |
|
| 49 |
procedure LMDecode(const LM: Cardinal; out L: Cardinal; out M: Integer); |
| 50 |
|
| 51 |
var |
| 52 |
(*For each SHBasis function (first index of the array is LM of this function), |
| 53 |
a precalculated results of basic spherical harmonic functions. |
| 54 |
Multiplied by solid angle of this cube map pixel, since this is what |
| 55 |
you usually need. |
| 56 |
|
| 57 |
For each side of the cube, and for each pixel on this side (pixels are |
| 58 |
arranged same as in TGrayscaleImage, that is row-by-row from |
| 59 |
lower to higher, from left to right) this gives the result of SHBasis |
| 60 |
for this direction. Multiplied by solid angle. |
| 61 |
|
| 62 |
You have to initialize this (once, like at the beginning of your |
| 63 |
program) by InitializeSHBasisMap. |
| 64 |
|
| 65 |
This is useful for calculating sh basis vector from given cube map: |
| 66 |
since you can just project any function on any basis, so if you have |
| 67 |
a particular cube map you can project it on each SH basis function. |
| 68 |
See SHVectorFromCubeMap implementation for code how to use SHBasisMap |
| 69 |
for this, and in simple cases you can just call SHVectorFromCubeMap. *) |
| 70 |
SHBasisMap: array [0..MaxSHBasis - 1] of TCubeMapFloat; |
| 71 |
|
| 72 |
procedure InitializeSHBasisMap; |
| 73 |
|
| 74 |
type |
| 75 |
TSHVectorSingle = array [0..MaxSHBasis - 1] of Single; |
| 76 |
PSHVectorSingle = ^TSHVectorSingle; |
| 77 |
|
| 78 |
{ Calculate SH basis coefficients that approximate function in Map. |
| 79 |
This uses SHBasisMap, so be sure to initialize it first. } |
| 80 |
procedure SHVectorFromCubeMap(var SHVector: array of Single; |
| 81 |
const Map: TCubeMapByte); |
| 82 |
|
| 83 |
implementation |
| 84 |
|
| 85 |
uses CastleInternalSphereSampling; |
| 86 |
|
| 87 |
function SHBasis(const LM: Cardinal; const PhiTheta: TVector2): Float; |
| 88 |
|
| 89 |
{ Taken from http://www.sjbrown.co.uk/2004/10/16/spherical-harmonic-basis/ } |
| 90 |
|
| 91 |
var |
| 92 |
SinPhi, CosPhi, SinTheta, CosTheta: Float; |
| 93 |
|
| 94 |
function Sin2Theta: Float; |
| 95 |
begin |
| 96 |
Result := Sqr(SinTheta); |
| 97 |
end; |
| 98 |
|
| 99 |
function Sin3Theta: Float; |
| 100 |
begin |
| 101 |
Result := Sqr(SinTheta)*SinTheta; |
| 102 |
end; |
| 103 |
|
| 104 |
function Sin4Theta: Float; |
| 105 |
begin |
| 106 |
Result := Sqr(Sqr(SinTheta)); |
| 107 |
end; |
| 108 |
|
| 109 |
function Cos2Theta: Float; |
| 110 |
begin |
| 111 |
Result := Sqr(CosTheta); |
| 112 |
end; |
| 113 |
|
| 114 |
function Cos3Theta: Float; |
| 115 |
begin |
| 116 |
Result := Sqr(CosTheta)*CosTheta; |
| 117 |
end; |
| 118 |
|
| 119 |
function Cos4Theta: Float; |
| 120 |
begin |
| 121 |
Result := Sqr(Sqr(CosTheta)); |
| 122 |
end; |
| 123 |
|
| 124 |
function Sin2Phi: Float; |
| 125 |
begin |
| 126 |
Result := Sqr(SinPhi); |
| 127 |
end; |
| 128 |
|
| 129 |
function Sin3Phi: Float; |
| 130 |
begin |
| 131 |
Result := Sqr(SinPhi)*SinPhi; |
| 132 |
end; |
| 133 |
|
| 134 |
function Sin4Phi: Float; |
| 135 |
begin |
| 136 |
Result := Sqr(Sqr(SinPhi)); |
| 137 |
end; |
| 138 |
|
| 139 |
function Cos2Phi: Float; |
| 140 |
begin |
| 141 |
Result := Sqr(CosPhi); |
| 142 |
end; |
| 143 |
|
| 144 |
function Cos3Phi: Float; |
| 145 |
begin |
| 146 |
Result := Sqr(CosPhi)*CosPhi; |
| 147 |
end; |
| 148 |
|
| 149 |
function Cos4Phi: Float; |
| 150 |
begin |
| 151 |
Result := Sqr(Sqr(CosPhi)); |
| 152 |
end; |
| 153 |
|
| 154 |
begin |
| 155 |
SinCos(PhiTheta[0], SinPhi, CosPhi); |
| 156 |
SinCos(PhiTheta[1], SinTheta, CosTheta); |
| 157 |
|
| 158 |
{ Fear not, this case should be converted to lookup table by FPC. } |
| 159 |
|
| 160 |
case LM of |
| 161 |
0: Result := SHBasis0; |
| 162 |
|
| 163 |
1: Result := Sqrt(3) / (2 * Sqrt(Pi)) * CosPhi * SinTheta; |
| 164 |
2: Result := Sqrt(3) / (2 * Sqrt(Pi)) * CosTheta; |
| 165 |
3: Result := Sqrt(3) / (2 * Sqrt(Pi)) * SinPhi * SinTheta; |
| 166 |
|
| 167 |
4: Result := Sqrt(15) / (2 * Sqrt(Pi)) * CosPhi * SinPhi * Sin2Theta; |
| 168 |
5: Result := Sqrt(15) / (2 * Sqrt(Pi)) * SinPhi * CosTheta * SinTheta; |
| 169 |
6: Result := Sqrt(15) / (4 * Sqrt(Pi)) * (3 * Cos2Theta - 1); |
| 170 |
7: Result := Sqrt(15) / (2 * Sqrt(Pi)) * CosPhi * CosTheta * SinTheta; |
| 171 |
8: Result := Sqrt(15) / (4 * Sqrt(Pi)) * (Cos2Phi - Sin2Phi) * Sin2Theta; |
| 172 |
|
| 173 |
9 : Result := Sqrt(35) / (4 * Sqrt(2*Pi) ) * (3 * Cos2Phi - Sin2Phi) * SinPhi * Sin3Theta; |
| 174 |
10: Result := Sqrt(105) / (2 * Sqrt(Pi ) ) * CosPhi * SinPhi * CosTheta * Sin2Theta; |
| 175 |
11: Result := Sqrt(21) / (4 * Sqrt(2*Pi) ) * (5 * Cos2Theta - 1) * SinPhi * SinTheta; |
| 176 |
12: Result := Sqrt(7) / (4 * Sqrt(Pi ) ) * (5 * Cos3Theta - 3 * CosTheta); |
| 177 |
13: Result := Sqrt(21) / (4 * Sqrt(2*Pi) ) * (5 * Cos2Theta - 1) * CosPhi * SinTheta; |
| 178 |
14: Result := Sqrt(105) / (4 * Sqrt(Pi ) ) * (Cos2Phi - Sin2Phi) * CosTheta * Sin2Theta; |
| 179 |
15: Result := Sqrt(35) / (4 * Sqrt(2*Pi) ) * (Cos2Phi - 3 * Sin2Phi) * CosPhi * Sin3Theta; |
| 180 |
|
| 181 |
16: Result := 3 * Sqrt(35) / (4 * Sqrt(Pi ) ) * ( Cos2Phi - Sin2Phi) * CosPhi * SinPhi * Sin4Theta; |
| 182 |
17: Result := 3 * Sqrt(35) / (4 * Sqrt(2*Pi) ) * (3 * Cos2Phi - Sin2Phi) * SinPhi * CosTheta * Sin3Theta; |
| 183 |
18: Result := 3 * Sqrt(5) / (4 * Sqrt(Pi ) ) * (7 * Cos2Theta - 1) * CosPhi * SinPhi * Sin2Theta; |
| 184 |
19: Result := 3 * Sqrt(5) / (4 * Sqrt(2*Pi) ) * (7 * Cos2Theta - 3) * SinPhi * CosTheta * SinTheta; |
| 185 |
20: Result := 3 / (16 * Sqrt(Pi ) ) * (3 - 30 * Cos2Theta + 35 * Cos4Theta); |
| 186 |
21: Result := 3 * Sqrt(5) / (4 * Sqrt(2*Pi) ) * (7 * Cos2Theta - 3) * CosPhi * CosTheta * SinTheta; |
| 187 |
22: Result := 3 * Sqrt(5) / (8 * Sqrt(Pi ) ) * (7 * Cos2Theta - 1) * (Cos2Phi - Sin2Phi) * Sin2Theta; |
| 188 |
23: Result := 3 * Sqrt(35) / (4 * Sqrt(2*Pi) ) * (Cos2Phi - 3 * Sin2Phi) * CosPhi * CosTheta * Sin3Theta; |
| 189 |
24: Result := 3 * Sqrt(35) / (16 * Sqrt(Pi ) ) * (Sin4Phi - 6 * Cos2Phi * Sin2Phi + Cos4Phi) * Sin4Theta; |
| 190 |
|
| 191 |
else raise EInternalError.Create('SHBasis LM out'); |
| 192 |
end |
| 193 |
end; |
| 194 |
|
| 195 |
procedure LMDecode(const LM: Cardinal; out L: Cardinal; out M: Integer); |
| 196 |
var |
| 197 |
ReachedLM: Integer; |
| 198 |
begin |
| 199 |
L := 0; |
| 200 |
ReachedLM := -1; |
| 201 |
repeat |
| 202 |
if ReachedLM + Integer(2*L+1) >= Integer(LM) then |
| 203 |
begin |
| 204 |
M := LM - ReachedLM - L - 1; |
| 205 |
Break; |
| 206 |
end; |
| 207 |
ReachedLM := ReachedLM + Integer(2*L+1); |
| 208 |
Inc(L); |
| 209 |
until false; |
| 210 |
end; |
| 211 |
|
| 212 |
var |
| 213 |
SHBasisMapInitialized: boolean = false; |
| 214 |
|
| 215 |
procedure InitializeSHBasisMap; |
| 216 |
var |
| 217 |
LM: Cardinal; |
| 218 |
Side: TCubeMapSide; |
| 219 |
Pixel: Cardinal; |
| 220 |
begin |
| 221 |
if SHBasisMapInitialized then Exit; |
| 222 |
|
| 223 |
for LM := 0 to MaxSHBasis - 1 do |
| 224 |
for Side := Low(Side) to High(Side) do |
| 225 |
for Pixel := 0 to Sqr(CubeMapSize) - 1 do |
| 226 |
begin |
| 227 |
SHBasisMap[LM][Side][Pixel] := |
| 228 |
SHBasis(LM, XYZToPhiTheta(CubeMapDirection(Side, Pixel))) * |
| 229 |
CubeMapSolidAngle(Side, Pixel); |
| 230 |
end; |
| 231 |
|
| 232 |
SHBasisMapInitialized := true; |
| 233 |
end; |
| 234 |
|
| 235 |
procedure SHVectorFromCubeMap(var SHVector: array of Single; |
| 236 |
const Map: TCubeMapByte); |
| 237 |
var |
| 238 |
LM: Cardinal; |
| 239 |
Side: TCubeMapSide; |
| 240 |
Pixel: Cardinal; |
| 241 |
begin |
| 242 |
Assert(SHBasisMapInitialized); |
| 243 |
|
| 244 |
for LM := 0 to High(SHVector) do |
| 245 |
begin |
| 246 |
SHVector[LM] := 0; |
| 247 |
for Side := Low(Side) to High(Side) do |
| 248 |
for Pixel := 0 to Sqr(CubeMapSize) - 1 do |
| 249 |
SHVector[LM] := SHVector[LM] + (Map[Side, Pixel]/255) * SHBasisMap[LM, Side, Pixel]; |
| 250 |
|
| 251 |
{ SHVector[LM] is now calculated for all sphere points. |
| 252 |
We want this to be integral over a sphere, so normalize now. |
| 253 |
|
| 254 |
Since SHBasisMap contains result of SH function * solid angle of pixel |
| 255 |
(on cube map, pixels have different solid angles), |
| 256 |
so below we divide by 4*Pi (sphere area, sum of solid angles for every |
| 257 |
pixel). } |
| 258 |
|
| 259 |
SHVector[LM] := SHVector[LM] / (4 * Pi); |
| 260 |
end; |
| 261 |
end; |
| 262 |
|
| 263 |
end. |