Debian Codegraph @

src/transform/castleinternalcubemaps.pas

raw
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 { Utilities for cube (environment) maps. }
17 unit CastleInternalCubeMaps;
18
19 {$I castleconf.inc}
20
21 interface
22
23 uses Math,
24 CastleVectors, CastleInternalCompositeImage, CastleUtils;
25
26 type
27 TCubeMapSide = CastleInternalCompositeImage.TCubeMapSide;
28
29 TCubeMapInfo = record
30 Dir, Up, Side: TVector3;
31 ScreenX, ScreenY: Integer;
32 end;
33
34 const
35 CubeMapSize = 16;
36
37 { Information about cube map faces.
38
39 Names and orientation of faces match precisely OpenGL naming and
40 orientation (see http://www.opengl.org/registry/specs/ARB/texture_cube_map.txt),
41 so it's straighforward to use this for OpenGL cube maps. }
42 CubeMapInfo: array [TCubeMapSide] of TCubeMapInfo =
43 ( (Dir: (X: 1; Y: 0; Z: 0); Up: (X:0; Y: -1; Z: 0); Side: (X: 0; Y: 0; Z:-1); ScreenX: 3; ScreenY: 0),
44 (Dir: (X:-1; Y: 0; Z: 0); Up: (X:0; Y: -1; Z: 0); Side: (X: 0; Y: 0; Z: 1); ScreenX: 1; ScreenY: 0),
45
46 (Dir: (X: 0; Y: 1; Z: 0); Up: (X:0; Y: 0; Z: 1); Side: (X: 1; Y: 0; Z: 0); ScreenX: 2; ScreenY: -1),
47 (Dir: (X: 0; Y: -1; Z: 0); Up: (X:0; Y: 0; Z: -1); Side: (X: 1; Y: 0; Z: 0); ScreenX: 2; ScreenY: +1),
48
49 (Dir: (X: 0; Y: 0; Z: 1); Up: (X:0; Y: -1; Z: 0); Side: (X: 1; Y: 0; Z: 0); ScreenX: 2; ScreenY: 0),
50 (Dir: (X: 0; Y: 0; Z: -1); Up: (X:0; Y: -1; Z: 0); Side: (X:-1; Y: 0; Z: 0); ScreenX: 0; ScreenY: 0)
51 );
52
53 { Direction corresponding to given cube map side and pixel number.
54 That is, assuming that the middle of cube is placed at (0, 0, 0),
55 each pixel of the cube map corresponds to some direction.
56
57 Pixel number must be between 0 and CubeMapSize*2-1.
58 It's assumed that pixels
59 are in the same order as they are in OpenGL images, that is row-by-row,
60 from the lowest row to highest, in each row from left to right.
61
62 Returned vector is @italic(not normalized). }
63 function CubeMapDirection(const Side: TCubeMapSide;
64 const Pixel: Cardinal): TVector3;
65
66 { Return cube map side and pixel that is the closest to
67 given direction Dir. This is the reverse of CubeMapDirection function.
68
69 Given here Dir need not be normalized, although must not be zero. }
70 procedure DirectionToCubeMap(const Dir: TVector3;
71 out Side: TCubeMapSide; out Pixel: Cardinal);
72
73 type
74 TCubeMapSide4 = array [0..3] of TCubeMapSide;
75
76 { Return 4 cube map indexes (side and pixel, along with ratio)
77 that are closest to given direction Dir.
78 All ratios will sum to 1.
79
80 This is like DirectionToCubeMap, except this returns 4 values.
81 It allows you to do bilinear interpolation between cube map items. }
82 procedure Direction4ToCubeMap(const Dir: TVector3;
83 out Side: TCubeMapSide4;
84 out Pixel: TVector4Cardinal;
85 out Ratio: TVector4);
86
87 type
88 { Cube map, with each item being a Float. }
89 TCubeMapFloat = array [TCubeMapSide, 0..Sqr(CubeMapSize) - 1] of Float;
90 PCubeMapFloat = ^TCubeMapFloat;
91
92 { Cube map, with each item being in 0..1 range, encoded as a Byte.
93 This assumes that every item is actually a float in 0..1 range,
94 encoded as Byte. }
95 TCubeMapByte = array [TCubeMapSide, 0..Sqr(CubeMapSize) - 1] of Byte;
96 PCubeMapByte = ^TCubeMapByte;
97
98 { Calculate solid angle of given pixel on the cube map. }
99 function CubeMapSolidAngle(const Side: TCubeMapSide;
100 const Pixel: Cardinal): Single;
101
102 implementation
103
104 uses SysUtils;
105
106 { Note: CubeMapSolidAngle assumes that implementation of this actually
107 returns the position of the middle of the pixel. That is, it assumes
108 you don't normalize the returned direction. }
109 function CubeMapDirection(const Side: TCubeMapSide;
110 const Pixel: Cardinal): TVector3;
111 var
112 PixelX, PixelY: Cardinal;
113 begin
114 PixelX := Pixel mod CubeMapSize;
115 PixelY := Pixel div CubeMapSize;
116 { Result = exactly CubeMapInfo[Side].Dir when
117 PixelX/Y = CubeMapSize/2 (pixel is on the middle of the image). }
118 Result := CubeMapInfo[Side].Dir;
119 Result := Result + CubeMapInfo[Side].Side * (-1 + 2 * PixelX/CubeMapSize
120
121 { We want the generated direction to be exactly in the middle of
122 cube map pixel.
123
124 Reasons? For perfection, and to be synchronized with captured
125 images (from OpenGL, and such), and to be synchronized with
126 DirectionToCubeMap, and to cover the cube map most fairly.
127
128 So we want to have results for min PixelX (0) and max PixelX
129 (CubeMapSize - 1) have the same distance from middle (absolute value).
130 For "-1 + 2 * PixelX/CubeMapSize", we have min = -1,
131 max = -1 + 2 * (1 - 1/CubeMapSize) = -1 + 2 - 2/CubeMapSize
132 = 1 - 2/CubeMapSize.
133
134 So if we just add 1/CubeMapSize, we'll have
135 min = -1 + 1/CubeMapSize,
136 max = 1 - 1/CubeMapSize,
137 so all will be perfect. }
138 + 1/CubeMapSize
139 );
140 Result := Result + CubeMapInfo[Side].Up * (-1 + 2 * PixelY/CubeMapSize
141 + 1/CubeMapSize);
142 end;
143
144 procedure DirectionToCubeMap(const Dir: TVector3;
145 out Side: TCubeMapSide; out Pixel: Cardinal);
146 var
147 SidePlane: TVector4;
148 SidePlaneDir: TVector3 absolute SidePlane;
149 SideCoord: Integer;
150 SideIntersect: TVector3;
151 PixelX, PixelY: Integer;
152 begin
153 SideCoord := MaxAbsVectorCoord(Dir);
154 case SideCoord of
155 0: if Dir[0] >= 0 then Side := csPositiveX else Side := csNegativeX;
156 1: if Dir[1] >= 0 then Side := csPositiveY else Side := csNegativeY;
157 2: if Dir[2] >= 0 then Side := csPositiveZ else Side := csNegativeZ;
158 end;
159
160 SidePlaneDir := CubeMapInfo[Side].Dir;
161 SidePlane.W := -1;
162
163 if not TryPlaneRayIntersection(SideIntersect,
164 SidePlane, TVector3.Zero, Dir) then
165 raise Exception.CreateFmt('DirectionToCubeMap: direction (%s) doesn''t hit it''s cube map side (%d)',
166 [Dir.ToRawString, Integer(Side)]);
167
168 { We're not interested in this coord, this is either 1 or -1.
169 Having this non-zero would break TVector3.DotProduct (projecting to Side/Up)
170 in following code. }
171 SideIntersect.Data[SideCoord] := 0;
172
173 PixelX := Round(MapRange(
174 TVector3.DotProduct(SideIntersect, CubeMapInfo[Side].Side),
175 { 1/CubeMapSize here, to take into account that the perfect ray
176 goes exactly through the middle pixel of cube map pixel.
177 See CubeMapDirection reasoning. }
178 -1 + 1/CubeMapSize,
179 1 - 1/CubeMapSize,
180 0, CubeMapSize - 1));
181
182 PixelY := Round(MapRange(
183 TVector3.DotProduct(SideIntersect, CubeMapInfo[Side].Up),
184 -1 + 1/CubeMapSize,
185 1 - 1/CubeMapSize,
186 0, CubeMapSize - 1));
187
188 { clamp, just to be safe }
189 ClampVar(PixelX, 0, CubeMapSize - 1);
190 ClampVar(PixelY, 0, CubeMapSize - 1);
191
192 Pixel := PixelY * CubeMapSize + PixelX;
193 end;
194
195 procedure Direction4ToCubeMap(const Dir: TVector3;
196 out Side: TCubeMapSide4;
197 out Pixel: TVector4Cardinal;
198 out Ratio: TVector4);
199 var
200 SidePlane: TVector4;
201 SidePlaneDir: TVector3 absolute SidePlane;
202 SideCoord: Integer;
203 SideIntersect: TVector3;
204 PixelFX, PixelFY, PixelXFrac, PixelYFrac: Single;
205 PixelX, PixelY: array [0..3] of Cardinal;
206 I: Cardinal;
207 PixelXTrunc, PixelYTrunc: Integer;
208 begin
209 SideCoord := MaxAbsVectorCoord(Dir);
210 case SideCoord of
211 0: if Dir[0] >= 0 then Side[0] := csPositiveX else Side[0] := csNegativeX;
212 1: if Dir[1] >= 0 then Side[0] := csPositiveY else Side[0] := csNegativeY;
213 2: if Dir[2] >= 0 then Side[0] := csPositiveZ else Side[0] := csNegativeZ;
214 end;
215
216 { TODO: for now, all four sides are always equal.
217 This means that bilinear interpolation using this will look
218 wrong (have aliasing) on the edges of cube map. }
219 Side[1] := Side[0];
220 Side[2] := Side[0];
221 Side[3] := Side[0];
222
223 SidePlaneDir := CubeMapInfo[Side[0]].Dir;
224 SidePlane.W := -1;
225
226 if not TryPlaneRayIntersection(SideIntersect,
227 SidePlane, TVector3.Zero, Dir) then
228 raise Exception.CreateFmt('DirectionToCubeMap: direction (%s) doesn''t hit it''s cube map side (%d)',
229 [Dir.ToRawString, Integer(Side[0])]);
230
231 { We're not interested in this coord, this is either 1 or -1.
232 Having this non-zero would break TVector3.DotProduct (projecting to Side/Up)
233 in following code. }
234 SideIntersect.Data[SideCoord] := 0;
235
236 PixelFX := MapRange(
237 TVector3.DotProduct(SideIntersect, CubeMapInfo[Side[0]].Side),
238 { 1/CubeMapSize here, to take into account that the perfect ray
239 goes exactly through the middle pixel of cube map pixel.
240 See CubeMapDirection reasoning. }
241 -1 + 1/CubeMapSize,
242 1 - 1/CubeMapSize,
243 0, CubeMapSize - 1);
244
245 PixelFY := MapRange(
246 TVector3.DotProduct(SideIntersect, CubeMapInfo[Side[0]].Up),
247 -1 + 1/CubeMapSize,
248 1 - 1/CubeMapSize,
249 0, CubeMapSize - 1);
250
251 PixelXTrunc := Trunc(PixelFX);
252 PixelYTrunc := Trunc(PixelFY);
253 PixelXFrac := Frac(PixelFX);
254 PixelYFrac := Frac(PixelFY);
255
256 PixelX[0] := PixelXTrunc;
257 PixelY[0] := PixelYTrunc;
258 Ratio.X := (1-PixelXFrac) * (1-PixelYFrac);
259
260 PixelX[1] := PixelXTrunc+1;
261 PixelY[1] := PixelYTrunc;
262 Ratio.Y := PixelXFrac * (1-PixelYFrac);
263
264 PixelX[2] := PixelXTrunc;
265 PixelY[2] := PixelYTrunc+1;
266 Ratio.Z := (1-PixelXFrac) * PixelYFrac;
267
268 PixelX[3] := PixelXTrunc+1;
269 PixelY[3] := PixelYTrunc+1;
270 Ratio.W := PixelXFrac * PixelYFrac;
271
272 { test: Writeln((Ratio[0] + Ratio[1] + Ratio[2] + Ratio[3]):1:10); }
273
274 { clamp, just to be safe }
275 for I := 0 to 3 do
276 begin
277 ClampVar(PixelX[I], 0, CubeMapSize - 1);
278 ClampVar(PixelY[I], 0, CubeMapSize - 1);
279 Pixel.Data[I] := PixelY[I] * CubeMapSize + PixelX[I];
280 end;
281 end;
282
283 function CubeMapSolidAngle(const Side: TCubeMapSide;
284 const Pixel: Cardinal): Single;
285
286 { An approximation of solid angle valid for small angles is:
287
288 cos(angle between vector from zero through middle of the polygon
289 and normal vector of polygon)
290 * polygon area
291 / Sqr(distance from zero to middle of the polygon)
292
293 "middle of the polygon" = just Dir (we depend here on CubeMapDirection
294 implementation --- this is Ok, we're in the same unit).
295
296 The cos(...) = vector dot product between normalized(dir) and normal of
297 this side.
298
299 The area is always Sqr(2/CubeMapSize) (since cube map = cube of size 2,
300 each side has CubeMapSize * CubeMapSize pixels. }
301
302 { A note on accuracy: I tried to return here Float, and use Float values
303 in the middle, but testing with
304 castle_game_engine/tests/testcubemap.pas
305 shows that it doesn't matter. Single is enough, Double or Extended
306 doesn't improve it. }
307
308 var
309 Dir: TVector3;
310 DirLength: Single;
311 begin
312 Dir := CubeMapDirection(Side, Pixel);
313 DirLength := Dir.Length;
314
315 { normalize Dir. Since we already have DirLength,
316 we can just call VectorScale. }
317 Dir := Dir * (1 / DirLength);
318
319 Result := TVector3.DotProduct(Dir, CubeMapInfo[Side].Dir) *
320 ( 4 / Sqr(CubeMapSize) ) /
321 Sqr(DirLength);
322 end;
323
324 end.