| 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 |
{ OpenGL utilities for cube (environment) maps. } |
| 17 |
unit CastleInternalGLCubeMaps; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses {$ifdef OpenGLES} CastleGLES, {$else} CastleGL, {$endif} |
| 24 |
CastleVectors, CastleInternalCubeMaps, CastleImages, CastleInternalCompositeImage, |
| 25 |
CastleGLImages, CastleTransform, CastleGLUtils; |
| 26 |
|
| 27 |
type |
| 28 |
TCubeMapRenderEvent = procedure (const RenderParams: TRenderParams) of object; {$ifdef FPC}experimental;{$endif} |
| 29 |
|
| 30 |
{ Calculate spherical harmonics basis describing environment rendered |
| 31 |
by OpenGL. Environment is rendered by |
| 32 |
Render(true) callback, from the |
| 33 |
CapturePoint. It's rendered to color buffer, and captured as grayscale. |
| 34 |
Captured pixel value is just assumed to be the value of spherical function |
| 35 |
at this direction. It's also scaled by ScaleColor (since rendering |
| 36 |
to OpenGL catches values in 0..1 range, but SH vector can express |
| 37 |
values from any range). |
| 38 |
|
| 39 |
This changes RenderContext.Viewport, |
| 40 |
so be sure to reset RenderContext.Viewport to something normal after calling this. |
| 41 |
|
| 42 |
The maps will be drawn in the color buffer (from positions MapScreenX, Y), |
| 43 |
so will actually be visible (call this before RenderContext.Clear or such if you |
| 44 |
want to hide them). } |
| 45 |
procedure SHVectorGLCapture( |
| 46 |
var SHVector: array of Single; |
| 47 |
const CapturePoint: TVector3; |
| 48 |
const Render: TCubeMapRenderEvent; |
| 49 |
const MapScreenX, MapScreenY: Integer; |
| 50 |
const ScaleColor: Single); experimental; |
| 51 |
|
| 52 |
{ Capture cube map by rendering environment from CapturePoint. |
| 53 |
|
| 54 |
Environment is rendered by the Render callback. |
| 55 |
It will be called with custom TRenderingCamera instance, |
| 56 |
with TRenderingCamera.Target set to rtCubeMapEnvironment, |
| 57 |
and matrix set to appropriate view from the CapturePoint. |
| 58 |
|
| 59 |
Cube map is recorded in six images you provide in the Images parameter. |
| 60 |
These must be already created TCastleImage instances, with the exact same size. |
| 61 |
(They do not have to be square, or have power-of-two |
| 62 |
size, or honor GL_MAX_CUBE_MAP_TEXTURE_SIZE_ARB limit, |
| 63 |
as we do not initialize actual OpenGL cube map here. |
| 64 |
You can use generated images for any purpose.) |
| 65 |
The classes of these images will also matter --- e.g. use TGrayscaleImage |
| 66 |
to capture scene as grayscale, use TRGBImage for RGB colors. |
| 67 |
|
| 68 |
This changes RenderContext.Viewport, |
| 69 |
so be sure to reset RenderContext.Viewport to something normal after calling this. |
| 70 |
|
| 71 |
ProjectionNear, ProjectionFar parameters will be used to set GL |
| 72 |
projection matrix. ProjectionFar may be equal to ZFarInfinity, |
| 73 |
as always. } |
| 74 |
procedure GLCaptureCubeMapImages( |
| 75 |
const Images: TCubeMapImages; |
| 76 |
const CapturePoint: TVector3; |
| 77 |
const Render: TRenderFromViewFunction; |
| 78 |
const ProjectionNear, ProjectionFar: Single); |
| 79 |
|
| 80 |
{ Capture cube map to composite (DDS, KTX...) image by rendering environment from CapturePoint. |
| 81 |
|
| 82 |
See GLCaptureCubeMapImages for documentation, this works the same, |
| 83 |
but it creates TCompositeImage instance containing all six images. } |
| 84 |
function GLCaptureCubeMapComposite( |
| 85 |
const Size: Cardinal; |
| 86 |
const CapturePoint: TVector3; |
| 87 |
const Render: TRenderFromViewFunction; |
| 88 |
const ProjectionNear, ProjectionFar: Single): TCompositeImage; |
| 89 |
|
| 90 |
{ Capture cube map to OpenGL cube map texture by rendering environment |
| 91 |
from CapturePoint. |
| 92 |
|
| 93 |
See GLCaptureCubeMapImages for documentation, this works the same, |
| 94 |
but it captures images to given OpenGL texture name Tex. |
| 95 |
Tex must already be created cube map texture (with OpenGL |
| 96 |
size and internal formats set), with square images of Size. |
| 97 |
This also means that Size must be a valid OpenGL cube map texture size, |
| 98 |
you can check it by IsCubeMapTextureSized. |
| 99 |
|
| 100 |
This captures the cube map images to "zero" texture level. |
| 101 |
If you use mipmaps, it's your problem how to generate other texture levels |
| 102 |
--- in the simplest case, call GenerateMipmap(GL_TEXTURE_CUBE_MAP). |
| 103 |
|
| 104 |
It uses RenderToTexture to render to the texture, so it will use |
| 105 |
framebuffer if available, and it's fast. } |
| 106 |
procedure GLCaptureCubeMapTexture( |
| 107 |
const Tex: TGLuint; |
| 108 |
const Size: Cardinal; |
| 109 |
const CapturePoint: TVector3; |
| 110 |
const Render: TRenderFromViewFunction; |
| 111 |
const ProjectionNear, ProjectionFar: Single; |
| 112 |
RenderToTexture: TGLRenderToTexture); |
| 113 |
|
| 114 |
implementation |
| 115 |
|
| 116 |
uses SysUtils, CastleInternalSphericalHarmonics, CastleRectangles, CastleRenderContext, |
| 117 |
CastleProjection, CastleScene, CastleInternalShapesRenderer; |
| 118 |
|
| 119 |
procedure SHVectorGLCapture( |
| 120 |
var SHVector: array of Single; |
| 121 |
const CapturePoint: TVector3; |
| 122 |
const Render: TCubeMapRenderEvent; |
| 123 |
const MapScreenX, MapScreenY: Integer; |
| 124 |
const ScaleColor: Single); |
| 125 |
var |
| 126 |
ShapesCollector: TShapesCollector; |
| 127 |
ShapesRenderer: TShapesRenderer; |
| 128 |
|
| 129 |
procedure DrawMap(Side: TCubeMapSide); |
| 130 |
var |
| 131 |
Map: TGrayscaleImage; |
| 132 |
I, SHBasis, ScreenX, ScreenY: Integer; |
| 133 |
RenderParams: TRenderParams; |
| 134 |
begin |
| 135 |
ScreenX := CubeMapInfo[Side].ScreenX * CubeMapSize + MapScreenX; |
| 136 |
ScreenY := CubeMapInfo[Side].ScreenY * CubeMapSize + MapScreenY; |
| 137 |
|
| 138 |
RenderContext.Viewport := Rectangle(ScreenX, ScreenY, CubeMapSize, CubeMapSize); |
| 139 |
|
| 140 |
RenderParams := TBasicRenderParams.Create; |
| 141 |
try |
| 142 |
{ TBasicRenderParams will automatically have good defaults (inclusive) for InShadow and ShadowVolumesReceivers. } |
| 143 |
RenderParams.RenderingCamera := TRenderingCamera.Create; |
| 144 |
try |
| 145 |
RenderParams.RenderingCamera.FromViewVectors( |
| 146 |
CapturePoint, CubeMapInfo[Side].Dir, CubeMapInfo[Side].Up, |
| 147 |
RenderContext.ProjectionMatrix |
| 148 |
); |
| 149 |
RenderParams.RenderingCamera.Target := rtCubeMapEnvironment; |
| 150 |
RenderParams.Frustum := @RenderParams.RenderingCamera.Frustum; |
| 151 |
RenderParams.RendererToPrepareShapes := ShapesRenderer.Renderer; |
| 152 |
RenderParams.Collector := ShapesCollector; |
| 153 |
RenderParams.Transparent := false; Render(RenderParams); |
| 154 |
RenderParams.Transparent := true ; Render(RenderParams); |
| 155 |
ShapesRenderer.Render(ShapesCollector, RenderParams); |
| 156 |
finally FreeAndNil(RenderParams.RenderingCamera) end; |
| 157 |
finally FreeAndNil(RenderParams) end; |
| 158 |
|
| 159 |
Map := SaveScreen_noflush(TGrayscaleImage, |
| 160 |
Rectangle(ScreenX, ScreenY, CubeMapSize, CubeMapSize), cbBack) as TGrayscaleImage; |
| 161 |
try |
| 162 |
{ Use the Map to calculate SHVector[SHBasis] (this is the actual |
| 163 |
purpose of drawing the Render). SHVector[SHBasis] is just a dot product |
| 164 |
for all directions (for all pixels, in this case) of |
| 165 |
light intensity values * SH basis values. } |
| 166 |
|
| 167 |
for I := 0 to Sqr(CubeMapSize) - 1 do |
| 168 |
for SHBasis := 0 to High(SHVector) do |
| 169 |
SHVector[SHBasis] := SHVector[SHBasis] + ((Map.Pixels[I]/255) * |
| 170 |
ScaleColor * |
| 171 |
SHBasisMap[SHBasis, Side, I]); |
| 172 |
finally FreeAndNil(Map) end; |
| 173 |
end; |
| 174 |
|
| 175 |
var |
| 176 |
SHBasis: Integer; |
| 177 |
Side: TCubeMapSide; |
| 178 |
SavedProjectionMatrix: TMatrix4; |
| 179 |
begin |
| 180 |
InitializeSHBasisMap; |
| 181 |
|
| 182 |
ShapesCollector := TShapesCollector.Create; |
| 183 |
ShapesRenderer := TShapesRenderer.Create; |
| 184 |
|
| 185 |
{ Call all DrawMap. This wil draw maps, get them, |
| 186 |
and calculate SHVector describing them. } |
| 187 |
|
| 188 |
for SHBasis := 0 to High(SHVector) do |
| 189 |
SHVector[SHBasis] := 0; |
| 190 |
|
| 191 |
SavedProjectionMatrix := RenderContext.ProjectionMatrix; |
| 192 |
PerspectiveProjection(90, 1, 0.01, 100); |
| 193 |
|
| 194 |
for Side := Low(TCubeMapSide) to High(TCubeMapSide) do |
| 195 |
DrawMap(Side); |
| 196 |
|
| 197 |
RenderContext.ProjectionMatrix := SavedProjectionMatrix; |
| 198 |
|
| 199 |
for SHBasis := 0 to High(SHVector) do |
| 200 |
begin |
| 201 |
{ Each SHVector[SHBasis] is now calculated for all sphere points. |
| 202 |
We want this to be integral over a sphere, so normalize now. |
| 203 |
|
| 204 |
Since SHBasisMap contains result of SH function * solid angle of pixel |
| 205 |
(on cube map, pixels have different solid angles), |
| 206 |
so below we divide by 4*Pi (sphere area, sum of solid angles for every |
| 207 |
pixel). } |
| 208 |
SHVector[SHBasis] := SHVector[SHBasis] / (4 * Pi); |
| 209 |
end; |
| 210 |
|
| 211 |
FreeAndNil(ShapesCollector); |
| 212 |
FreeAndNil(ShapesRenderer); |
| 213 |
end; |
| 214 |
|
| 215 |
procedure SetRenderingCamera(const RenderingCamera: TRenderingCamera; |
| 216 |
const CapturePoint: TVector3; |
| 217 |
const Side: TCubeMapSide); |
| 218 |
begin |
| 219 |
RenderingCamera.FromViewVectors( |
| 220 |
CapturePoint, CubeMapInfo[Side].Dir, CubeMapInfo[Side].Up, |
| 221 |
RenderContext.ProjectionMatrix); |
| 222 |
end; |
| 223 |
|
| 224 |
procedure GLCaptureCubeMapImages( |
| 225 |
const Images: TCubeMapImages; |
| 226 |
const CapturePoint: TVector3; |
| 227 |
const Render: TRenderFromViewFunction; |
| 228 |
const ProjectionNear, ProjectionFar: Single); |
| 229 |
var |
| 230 |
Width, Height: Cardinal; |
| 231 |
RenderToTexture: TGLRenderToTexture; |
| 232 |
|
| 233 |
procedure DrawMap(Side: TCubeMapSide); |
| 234 |
var |
| 235 |
RenderingCamera: TRenderingCamera; |
| 236 |
begin |
| 237 |
RenderToTexture.RenderBegin; |
| 238 |
|
| 239 |
RenderContext.Viewport := Rectangle(0, 0, Width, Height); |
| 240 |
|
| 241 |
RenderingCamera := TRenderingCamera.Create; |
| 242 |
try |
| 243 |
RenderingCamera.Target := rtCubeMapEnvironment; |
| 244 |
SetRenderingCamera(RenderingCamera, CapturePoint, Side); |
| 245 |
Render(RenderingCamera); |
| 246 |
finally FreeAndNil(RenderingCamera) end; |
| 247 |
|
| 248 |
SaveScreen_NoFlush(Images[Side], 0, 0, RenderToTexture.ColorBuffer); |
| 249 |
|
| 250 |
RenderToTexture.RenderEnd(Side < High(Side)); |
| 251 |
end; |
| 252 |
|
| 253 |
var |
| 254 |
Side: TCubeMapSide; |
| 255 |
SavedProjectionMatrix: TMatrix4; |
| 256 |
begin |
| 257 |
Width := Images[csPositiveX].Width ; |
| 258 |
Height := Images[csPositiveX].Height; |
| 259 |
|
| 260 |
RenderToTexture := TGLRenderToTexture.Create(Width, Height); |
| 261 |
try |
| 262 |
RenderToTexture.Buffer := tbNone; |
| 263 |
RenderToTexture.GLContextOpen; |
| 264 |
|
| 265 |
SavedProjectionMatrix := RenderContext.ProjectionMatrix; |
| 266 |
PerspectiveProjection(90, 1, ProjectionNear, ProjectionFar); |
| 267 |
|
| 268 |
for Side := Low(TCubeMapSide) to High(TCubeMapSide) do |
| 269 |
DrawMap(Side); |
| 270 |
|
| 271 |
RenderContext.ProjectionMatrix := SavedProjectionMatrix; |
| 272 |
finally FreeAndNil(RenderToTexture) end; |
| 273 |
end; |
| 274 |
|
| 275 |
function GLCaptureCubeMapComposite( |
| 276 |
const Size: Cardinal; |
| 277 |
const CapturePoint: TVector3; |
| 278 |
const Render: TRenderFromViewFunction; |
| 279 |
const ProjectionNear, ProjectionFar: Single): TCompositeImage; |
| 280 |
var |
| 281 |
Images: TCubeMapImages; |
| 282 |
Side: TCubeMapSide; |
| 283 |
begin |
| 284 |
for Side := Low(Side) to High(Side) do |
| 285 |
Images[Side] := TRGBImage.Create(Size, Size); |
| 286 |
|
| 287 |
GLCaptureCubeMapImages(Images, CapturePoint, Render, |
| 288 |
ProjectionNear, ProjectionFar); |
| 289 |
|
| 290 |
Result := TCompositeImage.Create; |
| 291 |
Result.Width := Size; |
| 292 |
Result.Height := Size; |
| 293 |
Result.CompositeType := ctCubeMap; |
| 294 |
Result.CubeMapSides := AllCubeMapSides; |
| 295 |
Result.Mipmaps := false; |
| 296 |
Result.MipmapsCount := 1; |
| 297 |
Result.AddCubeMapImages(Images); |
| 298 |
end; |
| 299 |
|
| 300 |
procedure GLCaptureCubeMapTexture( |
| 301 |
const Tex: TGLuint; |
| 302 |
const Size: Cardinal; |
| 303 |
const CapturePoint: TVector3; |
| 304 |
const Render: TRenderFromViewFunction; |
| 305 |
const ProjectionNear, ProjectionFar: Single; |
| 306 |
RenderToTexture: TGLRenderToTexture); |
| 307 |
|
| 308 |
procedure DrawMap(Side: TCubeMapSide); |
| 309 |
var |
| 310 |
RenderingCamera: TRenderingCamera; |
| 311 |
begin |
| 312 |
RenderToTexture.RenderBegin; |
| 313 |
RenderToTexture.SetTexture(Tex, GL_TEXTURE_CUBE_MAP_POSITIVE_X + Ord(Side)); |
| 314 |
|
| 315 |
RenderingCamera := TRenderingCamera.Create; |
| 316 |
try |
| 317 |
RenderingCamera.Target := rtCubeMapEnvironment; |
| 318 |
SetRenderingCamera(RenderingCamera, CapturePoint, Side); |
| 319 |
Render(RenderingCamera); |
| 320 |
finally FreeAndNil(RenderingCamera) end; |
| 321 |
|
| 322 |
RenderToTexture.RenderEnd(Side < High(Side)); |
| 323 |
end; |
| 324 |
|
| 325 |
var |
| 326 |
Side: TCubeMapSide; |
| 327 |
SavedProjectionMatrix: TMatrix4; |
| 328 |
SavedViewport: TRectangle; |
| 329 |
begin |
| 330 |
RenderToTexture.CompleteTextureTarget := GL_TEXTURE_CUBE_MAP; |
| 331 |
|
| 332 |
SavedProjectionMatrix := RenderContext.ProjectionMatrix; |
| 333 |
PerspectiveProjection(90, 1, ProjectionNear, ProjectionFar); |
| 334 |
|
| 335 |
SavedViewport := RenderContext.Viewport; |
| 336 |
RenderContext.Viewport := Rectangle(0, 0, Size, Size); |
| 337 |
|
| 338 |
for Side := Low(TCubeMapSide) to High(TCubeMapSide) do |
| 339 |
DrawMap(Side); |
| 340 |
|
| 341 |
RenderContext.ProjectionMatrix := SavedProjectionMatrix; |
| 342 |
RenderContext.Viewport := SavedViewport; |
| 343 |
end; |
| 344 |
|
| 345 |
end. |