| 1 |
{ |
| 2 |
Copyright 2007-2023 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 |
{ Rendering shadow volumes in OpenGL (TGLShadowVolumeRenderer). } |
| 17 |
unit CastleInternalGLShadowVolumes; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses |
| 24 |
{$ifdef OpenGLES} CastleGLES, {$else} CastleGL, {$endif} |
| 25 |
CastleTransform, CastleVectors, CastleBoxes, CastleGLUtils, CastleFrustum, |
| 26 |
CastleRenderPrimitives; |
| 27 |
|
| 28 |
type |
| 29 |
TGLShadowVolumeRenderer = class; |
| 30 |
|
| 31 |
TSVRenderProc = procedure (const Params: TRenderParams) of object; |
| 32 |
|
| 33 |
{ Shadow volume rendering in OpenGL. |
| 34 |
This class provides various utilities related to shadow volume rendering. |
| 35 |
It provides everything, except it doesn't |
| 36 |
actually render the 3D models or their shadow volumes (actual rendering |
| 37 |
is provided by T3D descendants, like TCastleScene.RenderShadowVolume). |
| 38 |
|
| 39 |
For general usage tutorial of this class, |
| 40 |
see [https://castle-engine.io/vrml_engine_doc/output/xsl/html/chapter.shadows.html] } |
| 41 |
TGLShadowVolumeRenderer = class(TBaseShadowVolumeRenderer) |
| 42 |
private |
| 43 |
FrustumAndLightPlanes: array [0..5] of TVector4; |
| 44 |
FrustumAndLightPlanesCount: Cardinal; |
| 45 |
FFrustum: TFrustum; |
| 46 |
FrustumNearPoints: TFrustumPoints; |
| 47 |
|
| 48 |
FCasterShadowPossiblyVisible: boolean; |
| 49 |
FZFail: boolean; |
| 50 |
FZFailAndLightCap: boolean; |
| 51 |
|
| 52 |
FLightPosition: TVector4; |
| 53 |
|
| 54 |
StencilConfigurationKnown: boolean; |
| 55 |
StencilConfigurationKnownZFail: boolean; |
| 56 |
|
| 57 |
FCount: boolean; |
| 58 |
FCountCasters: Cardinal; |
| 59 |
FCountShadowsNotVisible: Cardinal; |
| 60 |
FCountZPass: Cardinal; |
| 61 |
FCountZFailNoLightCap: Cardinal; |
| 62 |
FCountZFailAndLightCap: Cardinal; |
| 63 |
|
| 64 |
FMesh: TCastleRenderUnlitMesh; |
| 65 |
FDebugRender: Boolean; |
| 66 |
|
| 67 |
procedure UpdateCount; |
| 68 |
procedure SetDebugRender(const Value: Boolean); |
| 69 |
public |
| 70 |
constructor Create; |
| 71 |
destructor Destroy; override; |
| 72 |
|
| 73 |
{ Create resources that require rendering (OpenGL) context, like @link(Mesh). } |
| 74 |
procedure PrepareRenderingResources; |
| 75 |
|
| 76 |
{ Call this when camera frustum is known and light position (of the shadow |
| 77 |
casting light) is known, typically at the beginning of your drawing routine. |
| 78 |
|
| 79 |
You have to call this before any InitCaster / GetCasterShadowPossiblyVisible |
| 80 |
call. All subsequent InitCaster / GetCasterShadowPossiblyVisible calls |
| 81 |
assume that Frustum and LightPosition remain like this. |
| 82 |
|
| 83 |
It also resets CountCasters and other counters for debug purposes. } |
| 84 |
procedure InitFrustumAndLight( |
| 85 |
const Frustum: TFrustum; |
| 86 |
const ALightPosition: TVector4); |
| 87 |
|
| 88 |
{ Light casting shadows position, initialized by InitFrustumAndLight. } |
| 89 |
property LightPosition: TVector4 read FLightPosition; |
| 90 |
|
| 91 |
{ Call this when the bounding box of shadow caster is known. |
| 92 |
|
| 93 |
This calculates various things related to shadow volumes rendering |
| 94 |
of this scene. 1. checks whether you need to render shadow of the |
| 95 |
object inside CasterBox, setting CasterShadowPossiblyVisible. |
| 96 |
2. checks whether ZFail method is needed, setting ZFail. |
| 97 |
|
| 98 |
This assumes that Frustum and LightPosition values given |
| 99 |
in InitFrustumAndLight are OK. |
| 100 |
|
| 101 |
Also note that after InitFrustumAndLight, all InitCaster will assume that |
| 102 |
they have complete control over glStencilOp states for the time |
| 103 |
of rendering shadow volumes. In other words: InitCaster will setup |
| 104 |
some stencil configuration, depending on ZFail state. |
| 105 |
For the sake of speed, we try to avoid setting the same state |
| 106 |
twice, so we optimize it: first InitCaster after InitFrustumAndLight |
| 107 |
does always setup stencil confguration. Following InitCaster will |
| 108 |
only change stencil configuration if ZFail value will actually change. |
| 109 |
This means that if e.g. all objects will be detected to be in z-pass |
| 110 |
method, then stencil configuration will be done only once. |
| 111 |
|
| 112 |
The above optimization works OK if you do not change StencilOp configuration |
| 113 |
yourself during SV rendering. } |
| 114 |
procedure InitCaster(const CasterBox: TBox3D); |
| 115 |
|
| 116 |
{ Check is shadow caster's shadow is possibly visible. |
| 117 |
Unlike InitCaster, this doesn't change any state, |
| 118 |
it doesn't set any OpenGL stuff, |
| 119 |
it doesn't update @link(CasterShadowPossiblyVisible) property. } |
| 120 |
function GetCasterShadowPossiblyVisible(const CasterBox: TBox3D): Boolean; |
| 121 |
|
| 122 |
{ Does the shadow need to be rendered, calculated by last InitCaster call. } |
| 123 |
property CasterShadowPossiblyVisible: boolean read FCasterShadowPossiblyVisible; |
| 124 |
|
| 125 |
{ Is the ZFail method needed, set by InitCaster. } |
| 126 |
property ZFail: boolean read FZFail; |
| 127 |
|
| 128 |
{ Is the ZFail with light caps method needed, set by InitCaster. } |
| 129 |
property ZFailAndLightCap: boolean read FZFailAndLightCap; |
| 130 |
|
| 131 |
{ Statistics of shadow volumes. They are enabled by default, |
| 132 |
as calculating them takes practically no time. |
| 133 |
@groupBegin } |
| 134 |
property Count: boolean read FCount write FCount default true; |
| 135 |
property CountCasters: Cardinal read FCountCasters; |
| 136 |
property CountShadowsNotVisible: Cardinal read FCountShadowsNotVisible; |
| 137 |
property CountZPass: Cardinal read FCountZPass; |
| 138 |
property CountZFailNoLightCap: Cardinal read FCountZFailNoLightCap; |
| 139 |
property CountZFailAndLightCap: Cardinal read FCountZFailAndLightCap; |
| 140 |
{ @groupEnd } |
| 141 |
|
| 142 |
{ Do actual rendering with shadow volumes. |
| 143 |
|
| 144 |
You have to provide the appropriate callbacks that render given |
| 145 |
scene parts. |
| 146 |
|
| 147 |
Params.Transparent and Params.ShadowVolumesReceivers and Params.InShadow |
| 148 |
are changed here (their previous values are ignored). |
| 149 |
They cannot be modified by our callbacks. |
| 150 |
|
| 151 |
RenderOnePass renders part of the scene. |
| 152 |
|
| 153 |
@unorderedList( |
| 154 |
@item( |
| 155 |
When Params.ShadowVolumesReceivers includes @true, renders things that |
| 156 |
may be in the shadow. |
| 157 |
You should use Params.InShadow to either display the version |
| 158 |
of the scene in the shadows (so probably darker, probably with some |
| 159 |
lights off) or the version that is currently lighted |
| 160 |
(probably brighter, with normal scene lights on).) |
| 161 |
|
| 162 |
@item( |
| 163 |
When Params.ShadowVolumesReceivers includes @true, renders things that |
| 164 |
must never be considered in shadow (are not shadow receivers). |
| 165 |
Params.InShadow is always @false when Params.ShadowVolumesReceivers is only [@false] |
| 166 |
(to render only stuff that is never in shadow).) |
| 167 |
) |
| 168 |
|
| 169 |
RenderOnePass must also honour Params.Transparent, |
| 170 |
rendering only opaque or only transparent parts. |
| 171 |
For Transparent = @true, always Params.InShadow = @false. |
| 172 |
Shadow volumes simply don't allow transparent object |
| 173 |
to function properly as shadow receivers. |
| 174 |
Reading [http://developer.nvidia.com/object/fast_shadow_volumes.html] |
| 175 |
notes: they also just do separate rendering pass to render the |
| 176 |
partially-transparent parts, IOW they also note that transparent parts |
| 177 |
simply don't work at all with shadow volumes. |
| 178 |
|
| 179 |
RenderShadowVolumes renders shadow volumes from shadow casters. } |
| 180 |
procedure Render( |
| 181 |
const Params: TRenderParams; |
| 182 |
const RenderOnePass: TSVRenderProc; |
| 183 |
const RenderShadowVolumes: TSVRenderProc); |
| 184 |
|
| 185 |
{ Use this to render shadow quads. } |
| 186 |
property Mesh: TCastleRenderUnlitMesh read FMesh; |
| 187 |
|
| 188 |
{ Shadow volumes will be also actually drawn to color buffer |
| 189 |
(as yellow blended polygons), useful for debugging. } |
| 190 |
property DebugRender: Boolean read FDebugRender write SetDebugRender default false; |
| 191 |
end; |
| 192 |
|
| 193 |
implementation |
| 194 |
|
| 195 |
uses SysUtils, |
| 196 |
CastleUtils, CastleStringUtils, CastleLog, CastleGLVersion, |
| 197 |
CastleTriangles, CastleRenderOptions, CastleRenderContext; |
| 198 |
|
| 199 |
constructor TGLShadowVolumeRenderer.Create; |
| 200 |
begin |
| 201 |
inherited; |
| 202 |
FCount := true; |
| 203 |
end; |
| 204 |
|
| 205 |
procedure TGLShadowVolumeRenderer.PrepareRenderingResources; |
| 206 |
begin |
| 207 |
Assert(FMesh = nil, 'Call TGLShadowVolumeRenderer.PrepareRenderingResources only once'); |
| 208 |
FMesh := TCastleRenderUnlitMesh.Create(DebugRender); |
| 209 |
end; |
| 210 |
|
| 211 |
destructor TGLShadowVolumeRenderer.Destroy; |
| 212 |
begin |
| 213 |
FreeAndNil(FMesh); |
| 214 |
inherited; |
| 215 |
end; |
| 216 |
|
| 217 |
procedure TGLShadowVolumeRenderer.InitFrustumAndLight( |
| 218 |
const Frustum: TFrustum; |
| 219 |
const ALightPosition: TVector4); |
| 220 |
|
| 221 |
procedure CalculateFrustumAndLightPlanes; |
| 222 |
var |
| 223 |
FP, LastPlane: TFrustumPlane; |
| 224 |
LightPos: TVector4; |
| 225 |
begin |
| 226 |
FrustumAndLightPlanesCount := 0; |
| 227 |
|
| 228 |
LastPlane := High(FP); |
| 229 |
Assert(LastPlane = fpFar); |
| 230 |
|
| 231 |
{ if infinite far plane, then ignore it } |
| 232 |
if Frustum.FarInfinity then |
| 233 |
LastPlane := Pred(LastPlane); |
| 234 |
|
| 235 |
LightPos := LightPosition; |
| 236 |
{ For directional lights, as LightPos consider the light source |
| 237 |
being at infinity at the position *from which* the light emanates. |
| 238 |
We want to add to FrustumAndLightPlanes the planes the are further |
| 239 |
from this light. } |
| 240 |
if LightPos.W = 0 then |
| 241 |
LightPos := -LightPos; |
| 242 |
|
| 243 |
for FP := Low(FP) to LastPlane do |
| 244 |
begin |
| 245 |
{ This checks that LightPos is inside Frustum.Planes[FP] plane. |
| 246 |
Remember that Frustum.Planes[FP] plane direction (XYZ) points inside |
| 247 |
the frustum. |
| 248 |
|
| 249 |
For positional lights (point, spot): |
| 250 |
LightPosition.W <> 0 (usually LightPosition.W = 1.0), and then |
| 251 |
this is normal test on which side of plane (Frustum.Planes[FP]) |
| 252 |
lies a point in homogeneous coordinates (LightPos). |
| 253 |
|
| 254 |
For directional lights: |
| 255 |
LightPosition.W = 0, and then this is a dot product between |
| 256 |
plane direction (Frustum.Planes[FP]) and inverted light direction (LightPos, |
| 257 |
equal -LightPosition in this case). So we check if light source position |
| 258 |
is on the inside of the plane. } |
| 259 |
if TVector4.DotProduct(Frustum.Planes[FP], LightPos) >= 0 then |
| 260 |
begin |
| 261 |
FrustumAndLightPlanes[FrustumAndLightPlanesCount] := Frustum.Planes[FP]; |
| 262 |
Inc(FrustumAndLightPlanesCount); |
| 263 |
end; |
| 264 |
end; |
| 265 |
|
| 266 |
{ TODO: a better convex hull between light position and frustum could |
| 267 |
be useful. We should add additional planes to FrustumAndLightPlanes |
| 268 |
for this. Some pointers on [http://www.terathon.com/gdc06_lengyel.ppt]. } |
| 269 |
end; |
| 270 |
|
| 271 |
begin |
| 272 |
FFrustum := Frustum; |
| 273 |
FLightPosition := ALightPosition; |
| 274 |
|
| 275 |
Frustum.CalculatePoints(FrustumNearPoints); |
| 276 |
|
| 277 |
CalculateFrustumAndLightPlanes; |
| 278 |
|
| 279 |
StencilConfigurationKnown := false; |
| 280 |
|
| 281 |
FCountCasters := 0; |
| 282 |
FCountShadowsNotVisible := 0; |
| 283 |
FCountZPass := 0; |
| 284 |
FCountZFailNoLightCap := 0; |
| 285 |
FCountZFailAndLightCap := 0; |
| 286 |
end; |
| 287 |
|
| 288 |
function TGLShadowVolumeRenderer.GetCasterShadowPossiblyVisible(const CasterBox: TBox3D): Boolean; |
| 289 |
var |
| 290 |
I: Integer; |
| 291 |
|
| 292 |
function CasterCornerOutsideFrustum(const X, Y, Z: Integer): boolean; |
| 293 |
begin |
| 294 |
Result := |
| 295 |
CasterBox.Data[X][0] * FrustumAndLightPlanes[I][0] + |
| 296 |
CasterBox.Data[Y][1] * FrustumAndLightPlanes[I][1] + |
| 297 |
CasterBox.Data[Z][2] * FrustumAndLightPlanes[I][2] + |
| 298 |
FrustumAndLightPlanes[I][3] < 0; |
| 299 |
end; |
| 300 |
|
| 301 |
begin |
| 302 |
if CasterBox.IsEmpty then |
| 303 |
Exit(false); |
| 304 |
for I := 0 to Integer(FrustumAndLightPlanesCount) - 1 do |
| 305 |
begin |
| 306 |
if CasterCornerOutsideFrustum(0, 0, 0) and |
| 307 |
CasterCornerOutsideFrustum(0, 0, 1) and |
| 308 |
CasterCornerOutsideFrustum(0, 1, 0) and |
| 309 |
CasterCornerOutsideFrustum(0, 1, 1) and |
| 310 |
CasterCornerOutsideFrustum(1, 0, 0) and |
| 311 |
CasterCornerOutsideFrustum(1, 0, 1) and |
| 312 |
CasterCornerOutsideFrustum(1, 1, 0) and |
| 313 |
CasterCornerOutsideFrustum(1, 1, 1) then |
| 314 |
Exit(false); |
| 315 |
end; |
| 316 |
Result := true; |
| 317 |
end; |
| 318 |
|
| 319 |
procedure TGLShadowVolumeRenderer.InitCaster(const CasterBox: TBox3D); |
| 320 |
|
| 321 |
procedure DontSetupStencil(const CasterBox: TBox3D); |
| 322 |
|
| 323 |
function CalculateZFail: boolean; |
| 324 |
|
| 325 |
{ Returns if CasterBox is (at least partially) |
| 326 |
inside the Plane (i.e. where the plane equation is <= 0). |
| 327 |
Also returns @true if Plane is invalid, since in this case result |
| 328 |
of CalculateZFail should depend on other planes. } |
| 329 |
function InsidePlane(const Plane: TVector4): boolean; |
| 330 |
|
| 331 |
function CalculatePoint(const X, Y, Z: Integer): Single; |
| 332 |
begin |
| 333 |
Result := Plane[0] * CasterBox.Data[X][0] + |
| 334 |
Plane[1] * CasterBox.Data[Y][1] + |
| 335 |
Plane[2] * CasterBox.Data[Z][2] + |
| 336 |
Plane[3]; |
| 337 |
end; |
| 338 |
|
| 339 |
begin |
| 340 |
Result := |
| 341 |
( (Plane[0] = 0) and (Plane[1] = 0) and (Plane[2] = 0) ) or |
| 342 |
(CalculatePoint(0, 0, 0) <= 0) or |
| 343 |
(CalculatePoint(0, 0, 1) <= 0) or |
| 344 |
(CalculatePoint(0, 1, 0) <= 0) or |
| 345 |
(CalculatePoint(0, 1, 1) <= 0) or |
| 346 |
(CalculatePoint(1, 0, 0) <= 0) or |
| 347 |
(CalculatePoint(1, 0, 1) <= 0) or |
| 348 |
(CalculatePoint(1, 1, 0) <= 0) or |
| 349 |
(CalculatePoint(1, 1, 1) <= 0); |
| 350 |
end; |
| 351 |
|
| 352 |
var |
| 353 |
NearPlane: TVector4; |
| 354 |
begin |
| 355 |
if LightPosition.W <> 0 then |
| 356 |
begin |
| 357 |
{ Idea: calculate a pyramid between light position and near plane rectangle |
| 358 |
of the frustum. Assuming light point is positional and it does not |
| 359 |
lie on the near plane, this is simple: such pyramid has 4 side planes |
| 360 |
(created by two succeding near plane rectangle points and light pos), |
| 361 |
and 1 additional plane for near plane. |
| 362 |
|
| 363 |
Now, if for any such plane, CasterBox is outside, then ZFail is for sure |
| 364 |
not needed. |
| 365 |
|
| 366 |
Actually, even when the light lies exactly on near rectangle plane, |
| 367 |
usually this is still OK. The trouble will occur only if the light lies |
| 368 |
exactly on one of near rectangle points, since then an invalid |
| 369 |
plane (with 1st three items = 0) will be calculated. In such case, |
| 370 |
at most two planes out of 5 will be invalid (we assume that all 4 near |
| 371 |
rectangle points are for sure different, so the light pos may collide |
| 372 |
with only 1 of them, so only two plane calculations will lead to |
| 373 |
invalid plane). In such case, it's OK to simply ignore invalid planes. |
| 374 |
|
| 375 |
That's why InsidePlane simply checks for and ignores invalid planes. |
| 376 |
} |
| 377 |
|
| 378 |
{ FrustumNearPoints meaning: |
| 379 |
0: left , top |
| 380 |
1: right, top |
| 381 |
2: right, bottom |
| 382 |
3: left , bottom } |
| 383 |
|
| 384 |
NearPlane := TrianglePlane( |
| 385 |
FrustumNearPoints[2].XYZ, FrustumNearPoints[1].XYZ, FrustumNearPoints[0].XYZ); |
| 386 |
|
| 387 |
{ Now NearPlane points CCW outside of the frustum, but this is not |
| 388 |
necessarily what we want. We want NearPlane to point CCW outside |
| 389 |
from light+near plane pyramid. In other words, LightPosition should be |
| 390 |
on CW side of this plane. If LightPosition is on CCW side, |
| 391 |
flip NearPlane. Also, calculations of other side planes should |
| 392 |
generate flipped planes. } |
| 393 |
|
| 394 |
if TVector4.DotProduct(NearPlane, LightPosition) > 0 then |
| 395 |
begin |
| 396 |
NearPlane := -NearPlane; |
| 397 |
Result := |
| 398 |
InsidePlane(NearPlane) and |
| 399 |
InsidePlane(TrianglePlane(FrustumNearPoints[1].XYZ, FrustumNearPoints[0].XYZ, LightPosition.XYZ)) and |
| 400 |
InsidePlane(TrianglePlane(FrustumNearPoints[2].XYZ, FrustumNearPoints[1].XYZ, LightPosition.XYZ)) and |
| 401 |
InsidePlane(TrianglePlane(FrustumNearPoints[3].XYZ, FrustumNearPoints[2].XYZ, LightPosition.XYZ)) and |
| 402 |
InsidePlane(TrianglePlane(FrustumNearPoints[0].XYZ, FrustumNearPoints[3].XYZ, LightPosition.XYZ)); |
| 403 |
end else |
| 404 |
begin |
| 405 |
Result := |
| 406 |
InsidePlane(NearPlane) and |
| 407 |
InsidePlane(TrianglePlane(FrustumNearPoints[0].XYZ, FrustumNearPoints[1].XYZ, LightPosition.XYZ)) and |
| 408 |
InsidePlane(TrianglePlane(FrustumNearPoints[1].XYZ, FrustumNearPoints[2].XYZ, LightPosition.XYZ)) and |
| 409 |
InsidePlane(TrianglePlane(FrustumNearPoints[2].XYZ, FrustumNearPoints[3].XYZ, LightPosition.XYZ)) and |
| 410 |
InsidePlane(TrianglePlane(FrustumNearPoints[3].XYZ, FrustumNearPoints[0].XYZ, LightPosition.XYZ)); |
| 411 |
end; |
| 412 |
end else |
| 413 |
begin |
| 414 |
{ For directional light, this is somewhat similar to positional lights, |
| 415 |
except that you have 4 planes (each one from a segment of near rectangle, |
| 416 |
extruded to infinity in both directions). |
| 417 |
|
| 418 |
The corner cases may occur when light direction is the same as direction |
| 419 |
of one of the segments. This will make at most 2 such planes invalid |
| 420 |
(as 2 pairs of segments have different direction than other 2 pairs |
| 421 |
of near rectangle segments). So we simply ignore such invald planes, |
| 422 |
so again we can use InsidePlane function. } |
| 423 |
|
| 424 |
{ Although NearPlane for directional lights is not a plane that delimits |
| 425 |
the pyramid, we still calculate NearPlane to decide in which direction |
| 426 |
our 4 planes should be calculated, so that they point CCW outside. } |
| 427 |
NearPlane := TrianglePlane( |
| 428 |
FrustumNearPoints[2].XYZ, FrustumNearPoints[1].XYZ, FrustumNearPoints[0].XYZ); |
| 429 |
|
| 430 |
if TVector3.DotProduct(NearPlane.XYZ, LightPosition.XYZ) > 0 then |
| 431 |
begin |
| 432 |
Result := |
| 433 |
InsidePlane(TrianglePlane(FrustumNearPoints[0].XYZ, FrustumNearPoints[1].XYZ, (FrustumNearPoints[0].XYZ + LightPosition.XYZ))) and |
| 434 |
InsidePlane(TrianglePlane(FrustumNearPoints[1].XYZ, FrustumNearPoints[2].XYZ, (FrustumNearPoints[1].XYZ + LightPosition.XYZ))) and |
| 435 |
InsidePlane(TrianglePlane(FrustumNearPoints[2].XYZ, FrustumNearPoints[3].XYZ, (FrustumNearPoints[2].XYZ + LightPosition.XYZ))) and |
| 436 |
InsidePlane(TrianglePlane(FrustumNearPoints[3].XYZ, FrustumNearPoints[0].XYZ, (FrustumNearPoints[3].XYZ + LightPosition.XYZ))); |
| 437 |
end else |
| 438 |
begin |
| 439 |
Result := |
| 440 |
InsidePlane(TrianglePlane(FrustumNearPoints[1].XYZ, FrustumNearPoints[0].XYZ, (FrustumNearPoints[1].XYZ + LightPosition.XYZ))) and |
| 441 |
InsidePlane(TrianglePlane(FrustumNearPoints[2].XYZ, FrustumNearPoints[1].XYZ, (FrustumNearPoints[2].XYZ + LightPosition.XYZ))) and |
| 442 |
InsidePlane(TrianglePlane(FrustumNearPoints[3].XYZ, FrustumNearPoints[2].XYZ, (FrustumNearPoints[3].XYZ + LightPosition.XYZ))) and |
| 443 |
InsidePlane(TrianglePlane(FrustumNearPoints[0].XYZ, FrustumNearPoints[3].XYZ, (FrustumNearPoints[0].XYZ + LightPosition.XYZ))); |
| 444 |
end; |
| 445 |
end; |
| 446 |
end; |
| 447 |
|
| 448 |
begin |
| 449 |
{ Frustum culling for shadow volumes. |
| 450 |
If the CasterBox is outside of convex hull light pos + frustum, |
| 451 |
the shadow of this scene is not visible for sure. } |
| 452 |
FCasterShadowPossiblyVisible := GetCasterShadowPossiblyVisible(CasterBox); |
| 453 |
|
| 454 |
FZFail := FCasterShadowPossiblyVisible and CalculateZFail; |
| 455 |
|
| 456 |
FZFailAndLightCap := ZFail and |
| 457 |
FFrustum.Box3DCollisionPossibleSimple(CasterBox); |
| 458 |
|
| 459 |
UpdateCount; |
| 460 |
end; |
| 461 |
|
| 462 |
procedure OnlySetupStencil; |
| 463 |
|
| 464 |
{ Set glStencilOpSeparate (suitable for both front and back faces) as appropriate. |
| 465 |
Use this before rendering shadow volumes. |
| 466 |
It uses ZFail to decide what setup is necessary. } |
| 467 |
procedure ActuallySetStencilConfiguration; |
| 468 |
begin |
| 469 |
if ZFail then |
| 470 |
begin |
| 471 |
glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_DECR_WRAP, GL_KEEP); |
| 472 |
glStencilOpSeparate(GL_BACK , GL_KEEP, GL_INCR_WRAP, GL_KEEP); |
| 473 |
end else |
| 474 |
begin |
| 475 |
glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_KEEP, GL_INCR_WRAP); |
| 476 |
glStencilOpSeparate(GL_BACK , GL_KEEP, GL_KEEP, GL_DECR_WRAP); |
| 477 |
end; |
| 478 |
end; |
| 479 |
|
| 480 |
begin |
| 481 |
if FCasterShadowPossiblyVisible then |
| 482 |
begin |
| 483 |
{ setup stencil configuration. To avoid state too often changes, |
| 484 |
use StencilConfigurationKnown. } |
| 485 |
if (not StencilConfigurationKnown) or |
| 486 |
(StencilConfigurationKnownZFail <> ZFail) then |
| 487 |
begin |
| 488 |
ActuallySetStencilConfiguration; |
| 489 |
StencilConfigurationKnown := true; |
| 490 |
StencilConfigurationKnownZFail := ZFail; |
| 491 |
end; |
| 492 |
{ else, if configuration known and equals current, nothing to do } |
| 493 |
end; |
| 494 |
end; |
| 495 |
|
| 496 |
begin |
| 497 |
DontSetupStencil(CasterBox); |
| 498 |
OnlySetupStencil; |
| 499 |
end; |
| 500 |
|
| 501 |
procedure TGLShadowVolumeRenderer.UpdateCount; |
| 502 |
begin |
| 503 |
{ update counters } |
| 504 |
if Count then |
| 505 |
begin |
| 506 |
Inc(FCountCasters); |
| 507 |
|
| 508 |
if FCasterShadowPossiblyVisible then |
| 509 |
begin |
| 510 |
if ZFail then |
| 511 |
begin |
| 512 |
if ZFailAndLightCap then |
| 513 |
Inc(FCountZFailAndLightCap) else |
| 514 |
Inc(FCountZFailNoLightCap); |
| 515 |
end else |
| 516 |
Inc(FCountZPass); |
| 517 |
end else |
| 518 |
Inc(FCountShadowsNotVisible); |
| 519 |
end; |
| 520 |
end; |
| 521 |
|
| 522 |
procedure TGLShadowVolumeRenderer.Render( |
| 523 |
const Params: TRenderParams; |
| 524 |
const RenderOnePass: TSVRenderProc; |
| 525 |
const RenderShadowVolumes: TSVRenderProc); |
| 526 |
|
| 527 |
const |
| 528 |
{ Which stencil bits should be tested when determining which things |
| 529 |
are in the scene ? |
| 530 |
|
| 531 |
Not only *while rendering* shadow quads but also *after this rendering* |
| 532 |
value in stencil buffer may be > 1 (so you need more than 1 bit |
| 533 |
to hold it in stencil buffer). |
| 534 |
|
| 535 |
Why ? It's obvious that *while rendering* (e.g. right after rendering |
| 536 |
all front quads) this value may be > 1. But when the point |
| 537 |
is in the shadow because it's inside more than one shadow |
| 538 |
(cast by 2 different shadow quads) then even *after rendering* |
| 539 |
this point will have value > 1. |
| 540 |
|
| 541 |
So it's important that this constant spans a couple of bits. |
| 542 |
More precisely, it should be the maximum number of possibly overlapping |
| 543 |
front shadow quads from any possible camera view. Practically speaking, |
| 544 |
it will always be too little (for complicated shadow casters), |
| 545 |
but stencil_wrap will hopefully in this case minimize artifacts. } |
| 546 |
StencilShadowBits = $FF; |
| 547 |
var |
| 548 |
SavedCount: boolean; |
| 549 |
SavedDepthBufferUpdate: Boolean; |
| 550 |
SavedColorChannels: TColorChannels; |
| 551 |
SavedDepthFunc: TDepthFunction; |
| 552 |
SavedCullFace, SavedDepthTest: Boolean; |
| 553 |
begin |
| 554 |
Assert(GLFeatures.ShadowVolumesPossible); |
| 555 |
|
| 556 |
Params.InShadow := false; |
| 557 |
Params.Transparent := false; |
| 558 |
Params.ShadowVolumesReceivers := [false]; |
| 559 |
RenderOnePass(Params); |
| 560 |
|
| 561 |
Params.InShadow := true; |
| 562 |
Params.Transparent := false; |
| 563 |
Params.ShadowVolumesReceivers := [true]; |
| 564 |
RenderOnePass(Params); |
| 565 |
|
| 566 |
glEnable(GL_STENCIL_TEST); |
| 567 |
{ Note that stencil buffer is set to all 0 now. } |
| 568 |
{ Calculate shadows to the stencil buffer. } |
| 569 |
|
| 570 |
SavedCullFace := RenderContext.CullFace; |
| 571 |
SavedDepthTest := RenderContext.DepthTest; |
| 572 |
SavedDepthBufferUpdate := RenderContext.DepthBufferUpdate; |
| 573 |
SavedColorChannels := RenderContext.ColorChannels; |
| 574 |
|
| 575 |
RenderContext.DepthTest := true; |
| 576 |
{ Don't write anything to depth or color buffers. } |
| 577 |
RenderContext.DepthBufferUpdate := false; |
| 578 |
RenderContext.ColorChannels := []; |
| 579 |
|
| 580 |
glStencilFunc(GL_ALWAYS, 0, 0); |
| 581 |
RenderShadowVolumes(Params); |
| 582 |
|
| 583 |
RenderContext.DepthBufferUpdate := SavedDepthBufferUpdate; |
| 584 |
RenderContext.ColorChannels := SavedColorChannels; |
| 585 |
RenderContext.CullFace := SavedCullFace; |
| 586 |
RenderContext.DepthTest := SavedDepthTest; |
| 587 |
glDisable(GL_STENCIL_TEST); |
| 588 |
|
| 589 |
{ Now render everything once again, with lights turned on. |
| 590 |
But render only things not in shadow. |
| 591 |
|
| 592 |
---------------------------------------- |
| 593 |
Long (but maybe educational) explanation why glDepthFunc(GL_LEQUAL) |
| 594 |
below is crucial: |
| 595 |
|
| 596 |
What should I do with depth buffer ? Now it contains opaque |
| 597 |
never-shadowed things, and all shadowed things. |
| 598 |
At some time (see revision 2048 in Kambi SVN repository on kocury) I called |
| 599 |
glClear(GL_DEPTH_BUFFER_BIT) before rendering shadowed things for the |
| 600 |
second time. While this seems to work, the 2nd pass leaves the depth-buffer |
| 601 |
untouched on shadowed places. So it creates a whole |
| 602 |
lot of problems for rendering transparent things at the end: |
| 603 |
1. I have to render them before glClear(GL_DEPTH_BUFFER_BIT), |
| 604 |
since they can be occluded by any level parts (shadowed on not). |
| 605 |
2. I have to render them once again (but only into depth buffer, |
| 606 |
i.e. with temporary |
| 607 |
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE)) |
| 608 |
after glClear(GL_DEPTH_BUFFER_BIT), since they can occlude |
| 609 |
non-shadowed level parts. |
| 610 |
|
| 611 |
This is easy doable for opaque parts. But what about transparent |
| 612 |
things? In other words, where should we call |
| 613 |
RenderOnePass(Transparent=true, ShadowVolumesReceivers=false) |
| 614 |
and RenderOnePass(Transparent=true, InShadow=false, ShadowVolumesReceivers=true)? |
| 615 |
They should be rendered but they don't affect depth buffer. |
| 616 |
Well, clearly, they have to be rendered |
| 617 |
before glClear(GL_DEPTH_BUFFER_BIT) (for the same reason that |
| 618 |
opaque parts must: because they can be occluded by any level |
| 619 |
part, shadowed or not). But rendering non-shadowed parts may then |
| 620 |
cover them (since they cannot be rendered to depth buffer). |
| 621 |
So I should render them once again, but *only at the places |
| 622 |
where Render_Shadowed_Lights below passed the stencil test (=0) and the depth test*. |
| 623 |
But the second condition is not so easy (I would have to change stencil |
| 624 |
buffer once again, based on Level.Render hits). |
| 625 |
|
| 626 |
The simpler version, to just render them second time everywhere where |
| 627 |
were not in shadow, i.e. stencil test (=0) passes would be no good: |
| 628 |
we would render transparent things twice in the places on the level |
| 629 |
when they are not in shadow. |
| 630 |
|
| 631 |
So basically, it's all doable, but not trivial, and (more important) |
| 632 |
I'm losing rendering time on handling this. And without taking proper care |
| 633 |
about transparent parts, transparent things |
| 634 |
may be e.g. visible through the walls, |
| 635 |
in the places where shadow falls on the wall. |
| 636 |
|
| 637 |
This was all talking assuming that we do glClear(GL_DEPTH_BUFFER_BIT) |
| 638 |
before the 2nd pass. But do we really have to ? No! |
| 639 |
It's enough to just set depth test GL_LEQUAL (instead of default |
| 640 |
GL_LESS) and then the 2nd pass will naturally cover the level |
| 641 |
rendered in the 1st pass. That's all, it's easy to implement, |
| 642 |
works perfectly, and is fast. |
| 643 |
|
| 644 |
End of long explanation about glDepthFunc(GL_LEQUAL). |
| 645 |
---------------------------------------- |
| 646 |
} |
| 647 |
|
| 648 |
{ for now, InternalPass is only 0 or 1, and 1 is used only here } |
| 649 |
Assert(Params.InternalPass = 0); |
| 650 |
Inc(Params.InternalPass); |
| 651 |
|
| 652 |
SavedDepthFunc := RenderContext.DepthFunc; |
| 653 |
RenderContext.DepthFunc := dfLessEqual; |
| 654 |
|
| 655 |
{ setup stencil : don't modify stencil, stencil test passes only for =0 } |
| 656 |
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); |
| 657 |
glStencilFunc(GL_EQUAL, 0, StencilShadowBits); |
| 658 |
glEnable(GL_STENCIL_TEST); |
| 659 |
Inc(Params.StencilTest); |
| 660 |
Params.InShadow := false; |
| 661 |
Params.Transparent := false; |
| 662 |
Params.ShadowVolumesReceivers := [true]; |
| 663 |
RenderOnePass(Params); |
| 664 |
Dec(Params.StencilTest); |
| 665 |
glDisable(GL_STENCIL_TEST); |
| 666 |
|
| 667 |
RenderContext.DepthFunc := SavedDepthFunc; |
| 668 |
|
| 669 |
if DebugRender then |
| 670 |
begin |
| 671 |
SavedCount := Count; |
| 672 |
Count := false; |
| 673 |
|
| 674 |
SavedDepthBufferUpdate := RenderContext.DepthBufferUpdate; |
| 675 |
SavedDepthTest := RenderContext.DepthTest; |
| 676 |
|
| 677 |
RenderContext.DepthBufferUpdate := false; |
| 678 |
RenderContext.DepthTest := true; |
| 679 |
RenderContext.BlendingEnable; |
| 680 |
|
| 681 |
RenderShadowVolumes(Params); |
| 682 |
|
| 683 |
RenderContext.BlendingDisable; |
| 684 |
RenderContext.DepthBufferUpdate := SavedDepthBufferUpdate; |
| 685 |
RenderContext.DepthTest := SavedDepthTest; |
| 686 |
|
| 687 |
Count := SavedCount; |
| 688 |
end; |
| 689 |
|
| 690 |
Params.InShadow := false; |
| 691 |
Params.Transparent := true; |
| 692 |
Params.ShadowVolumesReceivers := [true]; |
| 693 |
RenderOnePass(Params); |
| 694 |
|
| 695 |
Params.InShadow := false; |
| 696 |
Params.Transparent := true; |
| 697 |
Params.ShadowVolumesReceivers := [false]; |
| 698 |
RenderOnePass(Params); |
| 699 |
end; |
| 700 |
|
| 701 |
procedure TGLShadowVolumeRenderer.SetDebugRender(const Value: Boolean); |
| 702 |
begin |
| 703 |
if FDebugRender <> Value then |
| 704 |
begin |
| 705 |
FDebugRender := Value; |
| 706 |
if (Mesh <> nil) and (Mesh.UseColor <> Value) then |
| 707 |
begin |
| 708 |
FreeAndNil(FMesh); |
| 709 |
FMesh := TCastleRenderUnlitMesh.Create(DebugRender); |
| 710 |
FMesh.Color := Vector4(1, 1, 0, 0.3); |
| 711 |
end; |
| 712 |
end; |
| 713 |
end; |
| 714 |
|
| 715 |
end. |