| 1 |
{ |
| 2 |
Copyright 2003-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 |
{ X3D lights rendering. Internal for CastleInternalRenderer. @exclude } |
| 17 |
unit CastleRendererInternalLights; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses CastleVectors, CastleGLUtils, X3DNodes, CastleRendererInternalShader, |
| 24 |
CastleTransform, CastleShapes; |
| 25 |
|
| 26 |
type |
| 27 |
{ Callback type for @link(TLightsRenderer.LightRenderEvent). |
| 28 |
Modify whether the light is "on" right before it's rendered. |
| 29 |
By default, LightOn is the value of Light.LightNode.FdOn field. |
| 30 |
This is useful to filter lights. } |
| 31 |
TLightRenderEvent = procedure (const Shape: TShape; |
| 32 |
const Light: TLightInstance; |
| 33 |
const IsGlobalLight: Boolean; var LightOn: boolean) of object; |
| 34 |
|
| 35 |
{ Render lights. } |
| 36 |
TLightsRenderer = class |
| 37 |
private |
| 38 |
FLightRenderEvent: TLightRenderEvent; |
| 39 |
LightsDone: array of PLightInstance; |
| 40 |
FMaxLightsPerShape: Cardinal; |
| 41 |
{ This avoids configuring the same light many times in case of fixed-function. |
| 42 |
|
| 43 |
Remembers what X3D light is set on which OpenGL light, |
| 44 |
and this way avoids needless reconfiguring of OpenGL lights. |
| 45 |
This may speed up rendering, avoiding changing OpenGL state when not |
| 46 |
necessary. |
| 47 |
Assumes that nothing changes OpenGL light properties during our |
| 48 |
lifetime. } |
| 49 |
function NeedRenderLight(Index: Integer; Light: PLightInstance): boolean; |
| 50 |
|
| 51 |
procedure SetMaxLightsPerShape(const Value: Cardinal); |
| 52 |
public |
| 53 |
{ Statistics of how many OpenGL light setups were done |
| 54 |
(Statistics[true]) vs how many were avoided (Statistics[false]). |
| 55 |
The second number should usually be much higher, prooving |
| 56 |
that using a cache of LightsDone inside this class was useful. } |
| 57 |
Statistics: array [boolean] of Cardinal; |
| 58 |
|
| 59 |
RenderingCamera: TRenderingCamera; |
| 60 |
|
| 61 |
constructor Create; |
| 62 |
|
| 63 |
{ Maximum lights per shape. |
| 64 |
|
| 65 |
Only when ancient fixed-function rendering is used: value passed here is |
| 66 |
clamped by OpenGL implementation to GL_MAX_LIGHTS. } |
| 67 |
property MaxLightsPerShape: Cardinal read FMaxLightsPerShape |
| 68 |
write SetMaxLightsPerShape; |
| 69 |
|
| 70 |
{ Set lights properties. |
| 71 |
|
| 72 |
Handles: |
| 73 |
|
| 74 |
- OpenGL fixed-function pipeline, if GLFeatures.EnableFixedFunction. |
| 75 |
Enables/disables and sets parameters for fixed-function lights. |
| 76 |
|
| 77 |
- Shader rendering using TShader. |
| 78 |
Lights are passed to TShader.EnableLight. |
| 79 |
|
| 80 |
Both GlobalLights and SceneLights may be @nil, |
| 81 |
which is equivalent to an empty list. |
| 82 |
|
| 83 |
GlobalLights, SceneLights lists are simply concatenated, |
| 84 |
this renderer considers lights on both lists the same. |
| 85 |
But TLightRenderEvent can apply different filtering on them. |
| 86 |
} |
| 87 |
procedure Render(const Shape: TShape; |
| 88 |
const GlobalLights, SceneLights: TLightInstancesList; |
| 89 |
const Shader: TShader); |
| 90 |
|
| 91 |
{ Modify whether light is "on" right before rendering the light. |
| 92 |
|
| 93 |
This event, if assigned, must be deterministic, |
| 94 |
based purely on light properties (and other TLightRenderEvent params). |
| 95 |
For example, it's Ok to |
| 96 |
make LightRenderEvent that turns off lights that have shadowVolumes = TRUE. |
| 97 |
It is @italic(not Ok) to make LightRenderEvent that sets LightOn to |
| 98 |
a random boolean value. That because caching here assumes that |
| 99 |
for the same Light values, LightRenderEvent will set LightOn the same. } |
| 100 |
property LightRenderEvent: TLightRenderEvent |
| 101 |
read FLightRenderEvent write FLightRenderEvent; |
| 102 |
end; |
| 103 |
|
| 104 |
implementation |
| 105 |
|
| 106 |
uses SysUtils, Math, |
| 107 |
{$ifdef OpenGLES} CastleGLES, {$else} CastleGL, {$endif} |
| 108 |
CastleUtils, CastleBoxes, CastleInternalGLUtils; |
| 109 |
|
| 110 |
{ Set and enable OpenGL light properties based on X3D light. |
| 111 |
|
| 112 |
This does something only when ancient OpenGL fixed-function pipeline |
| 113 |
has to be used. |
| 114 |
For shaders, we pass light information to shaders in uniform variables, |
| 115 |
this is handled by CastleRendererInternalShader. |
| 116 |
|
| 117 |
Requires that current OpenGL matrix is modelview. |
| 118 |
Always preserves the matrix value (by using up to one modelview |
| 119 |
matrix stack slot). |
| 120 |
|
| 121 |
We do not examine Light.LightNode.FdOn.Value here. |
| 122 |
|
| 123 |
We make no assumptions about the previous state of this OpenGL light. |
| 124 |
We simply always set all the parameters to fully define the required |
| 125 |
light behavior. Some light parameters may not be set, but only because |
| 126 |
they are not used --- for example, if a light is not a spot light, |
| 127 |
then we set GL_SPOT_CUTOFF to 180 (indicates that light has no spot), |
| 128 |
but don't necessarily set GL_SPOT_DIRECTION or GL_SPOT_EXPONENT |
| 129 |
(as OpenGL will not use them anyway). } |
| 130 |
procedure glLightFromX3DLight( |
| 131 |
glLightNum: Integer; |
| 132 |
const Light: TLightInstance; |
| 133 |
const RenderingCamera: TRenderingCamera); |
| 134 |
|
| 135 |
{$ifndef OpenGLES} |
| 136 |
|
| 137 |
procedure SetupDirectionalLight(LightNode: TAbstractDirectionalLightNode); |
| 138 |
begin |
| 139 |
glLighti(glLightNum, GL_SPOT_CUTOFF, 180); |
| 140 |
end; |
| 141 |
|
| 142 |
procedure SetupPointLight(LightNode: TAbstractPointLightNode); |
| 143 |
begin |
| 144 |
glLighti(glLightNum, GL_SPOT_CUTOFF, 180); |
| 145 |
end; |
| 146 |
|
| 147 |
procedure SetupSpotLight_1(LightNode: TSpotLightNode_1); |
| 148 |
begin |
| 149 |
glLightv(glLightNum, GL_SPOT_DIRECTION, LightNode.FdDirection.Value); |
| 150 |
glLightf(glLightNum, GL_SPOT_EXPONENT, LightNode.SpotExponent); |
| 151 |
glLightf(glLightNum, GL_SPOT_CUTOFF, LightNode.SpotCutoffDeg); |
| 152 |
end; |
| 153 |
|
| 154 |
procedure SetupSpotLight(LightNode: TSpotLightNode); |
| 155 |
begin |
| 156 |
glLightv(glLightNum, GL_SPOT_DIRECTION, LightNode.FdDirection.Value); |
| 157 |
glLightf(glLightNum, GL_SPOT_EXPONENT, LightNode.SpotExponentApproximate); |
| 158 |
glLightf(glLightNum, GL_SPOT_CUTOFF, LightNode.SpotCutoffDeg); |
| 159 |
end; |
| 160 |
|
| 161 |
var |
| 162 |
LightNode: TAbstractPunctualLightNode; |
| 163 |
SetNoAttenuation: boolean; |
| 164 |
Attenuat: TVector3; |
| 165 |
Color3, AmbientColor3: TVector3; |
| 166 |
Color4, AmbientColor4: TVector4; |
| 167 |
begin |
| 168 |
if not GLFeatures.EnableFixedFunction then |
| 169 |
Exit; |
| 170 |
|
| 171 |
if not (Light.Node is TAbstractPunctualLightNode) then |
| 172 |
Exit; |
| 173 |
LightNode := TAbstractPunctualLightNode(Light.Node); |
| 174 |
|
| 175 |
glLightNum := glLightNum + GL_LIGHT0; |
| 176 |
|
| 177 |
glPushMatrix; |
| 178 |
try |
| 179 |
if Light.WorldCoordinates then |
| 180 |
glLoadMatrix(RenderingCamera.Matrix); |
| 181 |
|
| 182 |
glMultMatrix(Light.Transform); |
| 183 |
|
| 184 |
glLightv(glLightNum, GL_POSITION, LightNode.PositionAndDirection); |
| 185 |
|
| 186 |
if LightNode is TAbstractDirectionalLightNode then |
| 187 |
SetupDirectionalLight(TAbstractDirectionalLightNode(LightNode)) else |
| 188 |
if LightNode is TAbstractPointLightNode then |
| 189 |
SetupPointLight(TAbstractPointLightNode(LightNode)) else |
| 190 |
if LightNode is TSpotLightNode_1 then |
| 191 |
SetupSpotLight_1(TSpotLightNode_1(LightNode)) else |
| 192 |
if LightNode is TSpotLightNode then |
| 193 |
SetupSpotLight(TSpotLightNode(LightNode)) else |
| 194 |
raise EInternalError.Create('Unknown light node class'); |
| 195 |
|
| 196 |
{ setup attenuation for OpenGL light } |
| 197 |
SetNoAttenuation := true; |
| 198 |
|
| 199 |
if (LightNode is TAbstractPositionalLightNode) then |
| 200 |
begin |
| 201 |
Attenuat := TAbstractPositionalLightNode(LightNode).FdAttenuation.Value; |
| 202 |
if not Attenuat.IsZero then |
| 203 |
begin |
| 204 |
SetNoAttenuation := false; |
| 205 |
glLightf(glLightNum, GL_CONSTANT_ATTENUATION, Attenuat[0]); |
| 206 |
glLightf(glLightNum, GL_LINEAR_ATTENUATION, Attenuat[1]); |
| 207 |
glLightf(glLightNum, GL_QUADRATIC_ATTENUATION, Attenuat[2]); |
| 208 |
end; |
| 209 |
end; |
| 210 |
|
| 211 |
if SetNoAttenuation then |
| 212 |
begin |
| 213 |
{ lights with no Attenuation field or with Attenuation = (0, 0, 0) |
| 214 |
get default Attenuation = (1, 0, 0) } |
| 215 |
glLightf(glLightNum, GL_CONSTANT_ATTENUATION, 1); |
| 216 |
glLightf(glLightNum, GL_LINEAR_ATTENUATION, 0); |
| 217 |
glLightf(glLightNum, GL_QUADRATIC_ATTENUATION, 0); |
| 218 |
end; |
| 219 |
finally glPopMatrix end; |
| 220 |
|
| 221 |
{ calculate Color4 = light color * light intensity } |
| 222 |
Color3 := LightNode.FdColor.Value * LightNode.FdIntensity.Value; |
| 223 |
Color4 := Vector4(Color3, 1); |
| 224 |
|
| 225 |
{ calculate AmbientColor4 = light color * light ambient intensity } |
| 226 |
AmbientColor3 := LightNode.FdColor.Value * LightNode.FdAmbientIntensity.Value; |
| 227 |
AmbientColor4 := Vector4(AmbientColor3, 1); |
| 228 |
|
| 229 |
glLightv(glLightNum, GL_AMBIENT, AmbientColor4); |
| 230 |
glLightv(glLightNum, GL_DIFFUSE, Color4); |
| 231 |
glLightv(glLightNum, GL_SPECULAR, Color4); |
| 232 |
|
| 233 |
glEnable(glLightNum); |
| 234 |
{$else} |
| 235 |
begin |
| 236 |
{ We pass light information to shaders in our own uniform variables, |
| 237 |
this is handled by CastleRendererInternalShader. } |
| 238 |
{$endif} |
| 239 |
end; |
| 240 |
|
| 241 |
{ TLightsRenderer ----------------------------------------------- } |
| 242 |
|
| 243 |
constructor TLightsRenderer.Create; |
| 244 |
begin |
| 245 |
inherited Create; |
| 246 |
end; |
| 247 |
|
| 248 |
procedure TLightsRenderer.SetMaxLightsPerShape(const Value: Cardinal); |
| 249 |
begin |
| 250 |
if FMaxLightsPerShape <> Value then |
| 251 |
begin |
| 252 |
FMaxLightsPerShape := Value; |
| 253 |
if GLFeatures.EnableFixedFunction then |
| 254 |
MinVar(FMaxLightsPerShape, GLFeatures.MaxLights); |
| 255 |
{ Length(LightsDone) is always = FMaxLightsPerShape. |
| 256 |
Note that enlarging / initializing this array initializes |
| 257 |
new items with @nil, which is what we want, so that NeedsRenderLight |
| 258 |
treats it OK. } |
| 259 |
SetLength(LightsDone, FMaxLightsPerShape); |
| 260 |
end; |
| 261 |
end; |
| 262 |
|
| 263 |
function TLightsRenderer.NeedRenderLight(Index: Integer; Light: PLightInstance): boolean; |
| 264 |
begin |
| 265 |
Result := not ( |
| 266 |
( { Light Index is currently disabled, and we want it disabled: Ok. } |
| 267 |
( (LightsDone[Index] = nil) and |
| 268 |
(Light = nil) ) |
| 269 |
or |
| 270 |
{ Light Index is currently enabled, and we want it enabled, |
| 271 |
with the same LightNode and Transform: Ok. |
| 272 |
(Other TLightInstance record properties are calculated from |
| 273 |
LightNode and Transform, so no need to compare them). } |
| 274 |
( (LightsDone[Index] <> nil) and |
| 275 |
(Light <> nil) and |
| 276 |
(LightsDone[Index]^.Node = Light^.Node) and |
| 277 |
(TMatrix4.PerfectlyEquals( |
| 278 |
LightsDone[Index]^.Transform, Light^.Transform)) ) |
| 279 |
)); |
| 280 |
if Result then |
| 281 |
{ Update LightsDone[Index], if change required. } |
| 282 |
LightsDone[Index] := Light; |
| 283 |
Inc(Statistics[Result]); |
| 284 |
end; |
| 285 |
|
| 286 |
procedure TLightsRenderer.Render(const Shape: TShape; |
| 287 |
const GlobalLights, SceneLights: TLightInstancesList; |
| 288 |
const Shader: TShader); |
| 289 |
var |
| 290 |
LightsEnabled: Cardinal; |
| 291 |
|
| 292 |
function InsideLightRadius(const Light: TLightInstance): Boolean; |
| 293 |
var |
| 294 |
LightPosNode: TAbstractPositionalLightNode; |
| 295 |
ShapeBox: TBox3D; |
| 296 |
RadiusInWorld: Single; |
| 297 |
LocationInWorld: TVector3; |
| 298 |
begin |
| 299 |
Result := true; |
| 300 |
if Light.Node is TAbstractPositionalLightNode then |
| 301 |
begin |
| 302 |
LightPosNode := TAbstractPositionalLightNode(Light.Node); |
| 303 |
if LightPosNode.HasRadius then |
| 304 |
begin |
| 305 |
ShapeBox := Shader.ShapeBoundingBoxInWorld; |
| 306 |
if ShapeBox.IsEmpty then |
| 307 |
Exit(false); |
| 308 |
|
| 309 |
{ calculate RadiusInWorld, LocationInWorld. |
| 310 |
Note: we optimize WorldCoordinates = true case, as most often --- |
| 311 |
once we'll design lights in editor, then lights are usually in different |
| 312 |
scene than original. } |
| 313 |
if Light.WorldCoordinates then |
| 314 |
begin |
| 315 |
RadiusInWorld := Light.Radius; |
| 316 |
LocationInWorld := Light.Location; |
| 317 |
end else |
| 318 |
begin |
| 319 |
RadiusInWorld := Approximate3DScale(Shader.SceneTransform) * Light.Radius; |
| 320 |
LocationInWorld := Shader.SceneTransform.MultPoint(Light.Location); |
| 321 |
end; |
| 322 |
|
| 323 |
{ Light will never reach the shape. } |
| 324 |
if ShapeBox.PointDistanceSqr(LocationInWorld) > Sqr(RadiusInWorld) then |
| 325 |
Exit(false); |
| 326 |
end; |
| 327 |
end; |
| 328 |
end; |
| 329 |
|
| 330 |
procedure AddList(const Lights: TLightInstancesList; const IsGlobalLight: Boolean); |
| 331 |
var |
| 332 |
I: Integer; |
| 333 |
LightOn: boolean; |
| 334 |
Light: PLightInstance; |
| 335 |
begin |
| 336 |
for I := 0 to Lights.Count - 1 do |
| 337 |
begin |
| 338 |
Light := PLightInstance(Lights.Ptr(I)); |
| 339 |
|
| 340 |
LightOn := Light^.Node.FdOn.Value; |
| 341 |
if Assigned(LightRenderEvent) then |
| 342 |
LightRenderEvent(Shape, Light^, IsGlobalLight, LightOn); |
| 343 |
|
| 344 |
if LightOn and InsideLightRadius(Light^) then |
| 345 |
begin |
| 346 |
if NeedRenderLight(LightsEnabled, Light) then |
| 347 |
glLightFromX3DLight(LightsEnabled, Light^, RenderingCamera); |
| 348 |
Shader.EnableLight(LightsEnabled, Light); |
| 349 |
Inc(LightsEnabled); |
| 350 |
if LightsEnabled >= FMaxLightsPerShape then Exit; |
| 351 |
end; |
| 352 |
end; |
| 353 |
end; |
| 354 |
|
| 355 |
{$ifndef OpenGLES} |
| 356 |
var |
| 357 |
I: Integer; |
| 358 |
{$endif} |
| 359 |
begin |
| 360 |
Assert(RenderingCamera <> nil); |
| 361 |
|
| 362 |
LightsEnabled := 0; |
| 363 |
if LightsEnabled >= FMaxLightsPerShape then Exit; |
| 364 |
|
| 365 |
if GlobalLights <> nil then |
| 366 |
begin |
| 367 |
AddList(GlobalLights, true); |
| 368 |
if LightsEnabled >= FMaxLightsPerShape then Exit; |
| 369 |
end; |
| 370 |
|
| 371 |
if SceneLights <> nil then |
| 372 |
begin |
| 373 |
AddList(SceneLights, false); |
| 374 |
if LightsEnabled >= FMaxLightsPerShape then Exit; |
| 375 |
end; |
| 376 |
|
| 377 |
if GLFeatures.EnableFixedFunction then |
| 378 |
begin |
| 379 |
{$ifndef OpenGLES} |
| 380 |
{ Disable remaining light for fixed-function pipeline } |
| 381 |
for I := LightsEnabled to FMaxLightsPerShape - 1 do |
| 382 |
if NeedRenderLight(I, nil) then |
| 383 |
glDisable(GL_LIGHT0 + I); |
| 384 |
{$endif} |
| 385 |
end; |
| 386 |
end; |
| 387 |
|
| 388 |
end. |