/usr/src/castle-game-engine-5.0.0/x3d/x3d_lighting.inc is in castle-game-engine-src 5.0.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 | {
Copyright 2002-2014 Michalis Kamburelis.
This file is part of "Castle Game Engine".
"Castle Game Engine" is free software; see the file COPYING.txt,
included in this distribution, for details about the copyright.
"Castle Game Engine" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
----------------------------------------------------------------------------
}
{$ifdef read_interface}
TGeneratedShadowMapNode = class;
TLightScope = (
{ Light shines everywhere. VRML/X3D >= 2 calls these lights global. }
lsGlobal,
{ Light shines only within it's VRML/X3D grouping node.
VRML/X3D >= 2 calls these lights not global. }
lsLocal,
{ Light shines only on the following shapes within
it's VRML/X3D grouping node.
This is used by VRML 1.0 (and Inventor) light sources. }
lsLocalVRML1);
{ Base class for all VRML / X3D light nodes.
Note that even the old VRML 1.0 light nodes inherit from this.
Although they interpret some bits differently
("ambientIntensity" < 0 has special meaning).
But most of the fields behave identically, they only have different
default values. }
TAbstractLightNode = class(TAbstractChildNode)
private
FTransform: TMatrix4Single;
SavedDefaultShadowMap: boolean;
SavedDefaultShadowMapUpdate: TTextureUpdate;
SavedDefaultShadowMapSize: Integer;
SavedDefaultShadowMapScale: Single;
SavedDefaultShadowMapBias: Single;
SavedDefaultShadowMapCompareMode: string;
protected
procedure BeforeTraverse(StateStack: TX3DGraphTraverseStateStack); override;
procedure MiddleTraverse(StateStack: TX3DGraphTraverseStateStack); override;
public
procedure CreateNode; override;
private FFdOn: TSFBool;
public property FdOn: TSFBool read FFdOn;
private FFdIntensity: TSFFloat;
public property FdIntensity: TSFFloat read FFdIntensity;
private FFdColor: TSFColor;
public property FdColor: TSFColor read FFdColor;
private FFdAmbientIntensity: TSFFloat;
public property FdAmbientIntensity: TSFFloat read FFdAmbientIntensity;
private FFdGlobal: TSFBool;
public property FdGlobal: TSFBool read FFdGlobal;
private FFdShadowVolumes: TSFBool;
public property FdShadowVolumes: TSFBool read FFdShadowVolumes;
private FFdShadowVolumesMain: TSFBool;
public property FdShadowVolumesMain: TSFBool read FFdShadowVolumesMain;
private FFdShowProxyGeometry: TSFBool;
{ showProxyGeometry field is an Avalon extension, see
[http://instant-reality.com/documentation/nodetype/Light/]. }
public property FdShowProxyGeometry: TSFBool read FFdShowProxyGeometry;
private FFdProjectionNear: TSFFloat;
{ projectionNear / projectionFar / up are Kambi extensions, see
[http://castle-engine.sourceforge.net/x3d_extensions.php#section_ext_light_projective] }
public property FdProjectionNear: TSFFloat read FFdProjectionNear;
private FFdProjectionFar: TSFFloat;
public property FdProjectionFar: TSFFloat read FFdProjectionFar;
private FFdUp: TSFVec3f;
public property FdUp: TSFVec3f read FFdUp;
private FFdDefaultShadowMap: TSFNode;
public property FdDefaultShadowMap: TSFNode read FFdDefaultShadowMap;
private FFdShadows: TSFBool;
public property FdShadows: TSFBool read FFdShadows;
private FFdEffects: TMFNode;
public property FdEffects: TMFNode read FFdEffects;
{ Transformation of this light node.
Normal lights can be instanced many times within the scene, with
various transformation, so @italic(this transformation property
cannot be used).
However, in special cases, you know that light node occurs only once
within the scene (see
[http://castle-engine.sourceforge.net/x3d_extensions.php#section_ext_shadow_maps]).
Then it's useful.
It is gathered during traversing. Last BeforeTraverse call for this
node sets Transform properties. By default, it represents identity
transformation.
@groupBegin }
property Transform: TMatrix4Single read FTransform;
{ @groupEnd }
{ Matrices for rendering shadow map from this light.
Identity in this class, override for subclasses able to do shadow mapping.
@groupBegin }
function ProjectionMatrix: TMatrix4Single; virtual;
function ModelviewMatrix: TMatrix4Single; virtual;
function ModelviewRotationMatrix: TMatrix4Single; virtual;
function GetProjectorMatrix: TMatrix4Single;
{ @groupEnd }
{ Light location, direction and up vectors.
Useful for example when you think of lights as cameras (for shadow maps).
DirectionLocal must be exactly zero for PointLight (that doesn't
have a direction).
@groupBegin }
function LocationLocal: TVector3Single; virtual;
function Location: TVector3Single;
function DirectionLocal: TVector3Single; virtual;
function Direction: TVector3Single;
procedure GetView(out Pos, Dir, Up: TVector3Single);
{ @groupEnd }
{ Calculate distances between the given Box and this light source.
This is intended to capture the depth distances where the box
resides, useful for calculating e.g. depth ranges to capture in
the shadow maps.
Depending on light source type, various distance measures may be used,
appropriate to light sources projection.
Always MinDistance <= MaxDistance. They may be negative when
we measure along the light's direction.
@raises EBox3DEmpty When used with an empty box. }
procedure Box3DDistances(const Box: TBox3D;
out MinDistance, MaxDistance: Single); virtual; abstract;
{ Create TLightInstance record describing this light node under given
State. }
function CreateLightInstance(State: TX3DGraphTraverseState): TLightInstance;
{ Update TLightInstance record when lighting State changes.
Assumes that LightInstance.Node = Self.
This will set LightInstance.Transform properties, and recalculate
all LightInstance properties based on Transform. }
procedure UpdateLightInstanceState(
var LightInstance: TLightInstance;
State: TX3DGraphTraverseState);
{ Update TLightInstance record when lighting location/direction (and other
properties precalculated on TLightInstance) change.
Assumes that LightInstance.Node = Self. }
procedure UpdateLightInstance(
var LightInstance: TLightInstance); virtual;
function TransformationChange: TNodeTransformationChange; override;
{ Internal, to workaround small X3DShadowMaps problem. @exclude }
procedure DefaultShadowMapSave(Node: TGeneratedShadowMapNode);
{ Internal, to workaround small X3DShadowMaps problem. @exclude }
function DefaultShadowMapLoad(Node: TGeneratedShadowMapNode): boolean;
{ Light scope. Default implementation returns lsGlobal or lsLocal,
depending on "global" field value (this follows VRML/X3D >= 2.0 rules). }
function Scope: TLightScope; virtual;
{ Position expressed in homogeneous coordinates.
For positional lights, the last component is always 1.
For directional lights, the last component is always 0.
Note that this expressed is in the local light node coordinate system. }
function Position: TVector4Single; virtual; abstract;
end;
TAbstractDirectionalLightNode = class(TAbstractLightNode)
public
procedure CreateNode; override;
class function ClassNodeTypeName: string; override;
private FFdDirection: TSFVec3f;
public property FdDirection: TSFVec3f read FFdDirection;
private FFdProjectionRectangle: TSFVec4f;
public property FdProjectionRectangle: TSFVec4f read FFdProjectionRectangle;
private FFdProjectionLocation: TSFVec3f;
public property FdProjectionLocation: TSFVec3f read FFdProjectionLocation;
procedure UpdateLightInstance(var LightInstance: TLightInstance); override;
function ProjectionMatrix: TMatrix4Single; override;
function ModelviewMatrix: TMatrix4Single; override;
function ModelviewRotationMatrix: TMatrix4Single; override;
function LocationLocal: TVector3Single; override;
function DirectionLocal: TVector3Single; override;
procedure Box3DDistances(const Box: TBox3D;
out MinDistance, MaxDistance: Single); override;
function Position: TVector4Single; override;
end;
TAbstractPositionalLightNode = class(TAbstractLightNode)
public
procedure CreateNode; override;
private FFdLocation: TSFVec3f;
public property FdLocation: TSFVec3f read FFdLocation;
private FFdAttenuation: TSFVec3f;
public property FdAttenuation: TSFVec3f read FFdAttenuation;
{ Calculate light intensity drop because of the distance to the light.
This follows the equation @code(1/max( attenuation[0] + ... ))
from the VRML/X3D specification, it is the same as OpenGL attenuation.
Since calculating the DistanceToLight for the @link(Attenuation)
method may be time-consuming in some situations,
you can check DistanceNeededForAttenuation first.
When the DistanceNeededForAttenuation returns @false,
then the value of DistanceToLight parameter is ignored (you can
pass anything).
The DistanceToLight should be a distance in the light source
local coordinate system. TODO: although our renderers currently
ignore this: ray-tracer uses global coord system,
OpenGL (fixed-function and shader) renderer uses eye coord system
(should be equal to global coord system for normal cameras).
@groupBegin }
function DistanceNeededForAttenuation: boolean;
function Attenuation(const DistanceToLight: Single): Single; overload;
function Attenuation(const DistanceToLight: Double): Double; overload;
{ @groupEnd }
{ Is attenuation relevant. When @false, you know that @link(Attenuation)
function always returns 1, so there's no point in using it at all. }
function HasAttenuation: boolean;
private FFdRadius: TSFFloat;
public property FdRadius: TSFFloat read FFdRadius;
{ Should the "radius" field be taken into account. }
function HasRadius: boolean; virtual;
procedure UpdateLightInstance(var LightInstance: TLightInstance); override;
function Position: TVector4Single; override;
end;
TAbstractPointLightNode = class(TAbstractPositionalLightNode)
public
procedure CreateNode; override;
class function ClassNodeTypeName: string; override;
function LocationLocal: TVector3Single; override;
procedure Box3DDistances(const Box: TBox3D;
out MinDistance, MaxDistance: Single); override;
end;
TDirectionalLightNode = class(TAbstractDirectionalLightNode)
public
procedure CreateNode; override;
class function URNMatching(const URN: string): boolean; override;
class function ForVRMLVersion(const Version: TX3DVersion): boolean;
override;
end;
TDirectionalLightNode_2 = TDirectionalLightNode;
TPointLightNode = class(TAbstractPointLightNode)
public
procedure CreateNode; override;
class function URNMatching(const URN: string): boolean; override;
class function ForVRMLVersion(const Version: TX3DVersion): boolean;
override;
end;
TPointLightNode_2 = TPointLightNode;
TSpotLightNode = class(TAbstractPositionalLightNode)
public
procedure CreateNode; override;
class function ClassNodeTypeName: string; override;
class function URNMatching(const URN: string): boolean; override;
private FFdBeamWidth: TSFFloat;
public property FdBeamWidth: TSFFloat read FFdBeamWidth;
private FFdCutOffAngle: TSFFloat;
public property FdCutOffAngle: TSFFloat read FFdCutOffAngle;
private FFdDirection: TSFVec3f;
public property FdDirection: TSFVec3f read FFdDirection;
private FFdProjectionAngle: TSFFloat;
public property FdProjectionAngle: TSFFloat read FFdProjectionAngle;
class function ForVRMLVersion(const Version: TX3DVersion): boolean;
override;
procedure UpdateLightInstance(var LightInstance: TLightInstance); override;
function ProjectionMatrix: TMatrix4Single; override;
function ModelviewMatrix: TMatrix4Single; override;
function ModelviewRotationMatrix: TMatrix4Single; override;
function LocationLocal: TVector3Single; override;
function DirectionLocal: TVector3Single; override;
procedure Box3DDistances(const Box: TBox3D;
out MinDistance, MaxDistance: Single); override;
{ Spot cutoff angle (based on cutOffAngle).
Expressed in degrees, clamped to correct range
(since user can input any value in VRML, and also conversion
radians -> degrees could accidentally raise value slightly > 90,
so cutOffAngle = 1.5708 is in degrees 90.0002104591,
which would cause OpenGL fixed-function error). }
function SpotCutoffDeg: Single;
function SpotCosCutoff: Single;
{ Approximate spot exponent that could be used to render this spot light.
Do not use this, unless you really don't have to.
X3D spot light should have a linear fallback, from beamWidth to cutOffAngle,
and there is no sensible way to approximate it by an exponent.
Use this only if you have to render spot light with exponent,
e.g. for OpenGL fixed-function pipeline. }
function SpotExponentApproximate: Single;
end;
TSpotLightNode_2 = TSpotLightNode;
{$endif read_interface}
{$ifdef read_implementation}
const
FallbackProjectionNear = 1;
FallbackProjectionFar = 100;
FallbackProjectionRectangle: TVector4Single = (-10, -10, 10, 10);
procedure TAbstractLightNode.CreateNode;
begin
inherited;
FFdGlobal := TSFBool.Create(Self, 'global', false);
FdGlobal.ChangesAlways := [chEverything];
Fields.Add(FFdGlobal);
FFdOn := TSFBool.Create(Self, 'on', true);
FdOn.ChangesAlways := [chVisibleNonGeometry];
Fields.Add(FFdOn);
FFdIntensity := TSFFloat.Create(Self, 'intensity', 1);
FdIntensity.ChangesAlways := [chVisibleNonGeometry];
Fields.Add(FFdIntensity);
FFdColor := TSFColor.Create(Self, 'color', Vector3Single(1, 1, 1));
FdColor.ChangesAlways := [chVisibleNonGeometry];
Fields.Add(FFdColor);
FFdAmbientIntensity := TSFFloat.Create(Self, 'ambientIntensity', 0);
FdAmbientIntensity.ChangesAlways := [chVisibleNonGeometry];
Fields.Add(FFdAmbientIntensity);
FFdShadowVolumes := TSFBool.Create(Self, 'shadowVolumes', false);
FdShadowVolumes.AddAlternativeName('kambiShadows', 0);
FdShadowVolumes.ChangesAlways := [chLightForShadowVolumes];
Fields.Add(FFdShadowVolumes);
FFdShadowVolumesMain := TSFBool.Create(Self, 'shadowVolumesMain', false);
FdShadowVolumesMain.AddAlternativeName('kambiShadowsMain', 0);
FdShadowVolumesMain.ChangesAlways := [chLightForShadowVolumes];
Fields.Add(FFdShadowVolumesMain);
FFdShowProxyGeometry := TSFBool.Create(Self, 'showProxyGeometry', false);
Fields.Add(FFdShowProxyGeometry);
FFdProjectionNear := TSFFloat.Create(Self, 'projectionNear', 0);
{ We want to set UpdateNeeded := true for all GeneratedShadowMap using
this light. For now, simply send chVisibleGeometry that will
force updating all GeneratedShadowMaps. }
FdProjectionNear.ChangesAlways := [chVisibleGeometry];
Fields.Add(FFdProjectionNear);
FFdProjectionFar := TSFFloat.Create(Self, 'projectionFar', 0);
{ We want to set UpdateNeeded := true for all GeneratedShadowMap using
this light. For now, simply send chVisibleGeometry that will
force updating all GeneratedShadowMaps. }
FdProjectionFar.ChangesAlways := [chVisibleGeometry];
Fields.Add(FFdProjectionFar);
FFdUp := TSFVec3f.Create(Self, 'up', ZeroVector3Single);
{ We want to set UpdateNeeded := true for all GeneratedShadowMap using
this light. For now, simply send chVisibleGeometry that will
force updating all GeneratedShadowMaps. }
FdUp.ChangesAlways := [chVisibleGeometry];
Fields.Add(FFdUp);
FFdDefaultShadowMap := TSFNode.Create(Self, 'defaultShadowMap', [TGeneratedShadowMapNode]);
FdDefaultShadowMap.Exposed := false;
FdDefaultShadowMap.ChangesAlways := [chEverything];
Fields.Add(FFdDefaultShadowMap);
FFdShadows := TSFBool.Create(Self, 'shadows', false);
FdShadows.Exposed := false;
FdShadows.ChangesAlways := [chShadowMaps];
Fields.Add(FFdShadows);
FFdEffects := TMFNode.Create(Self, 'effects', [TEffectNode]);
FdEffects.Exposed := false;
FdEffects.ChangesAlways := [chEverything];
Fields.Add(FFdEffects);
DefaultContainerField := 'children';
FTransform := IdentityMatrix4Single;
end;
procedure TAbstractLightNode.BeforeTraverse(
StateStack: TX3DGraphTraverseStateStack);
begin
inherited;
FTransform := StateStack.Top.Transform;
end;
function TAbstractLightNode.ProjectionMatrix: TMatrix4Single;
begin
Result := IdentityMatrix4Single;
end;
function TAbstractLightNode.ModelviewMatrix: TMatrix4Single;
begin
Result := IdentityMatrix4Single;
end;
function TAbstractLightNode.ModelviewRotationMatrix: TMatrix4Single;
begin
Result := IdentityMatrix4Single;
end;
function TAbstractLightNode.GetProjectorMatrix: TMatrix4Single;
begin
Result := ProjectionMatrix * ModelviewMatrix;
end;
function TAbstractLightNode.LocationLocal: TVector3Single;
begin
Result := ZeroVector3Single;
end;
function TAbstractLightNode.Location: TVector3Single;
begin
Result := MatrixMultPoint(Transform, LocationLocal);
end;
function TAbstractLightNode.Direction: TVector3Single;
begin
Result := MatrixMultDirection(Transform, DirectionLocal);
end;
function TAbstractLightNode.DirectionLocal: TVector3Single;
begin
Result := ZeroVector3Single;
end;
procedure TAbstractLightNode.GetView(out Pos, Dir, Up: TVector3Single);
begin
Pos := MatrixMultPoint (Transform, LocationLocal);
Dir := MatrixMultDirection(Transform, DirectionLocal);
Up := FdUp.Value;
{ Up = zero means "calculate anything suitable".
We could let AnyOrthogonalVector do this job, but when +Y is sensible
(when it results in something non-parallel to Dir), let's use it.
This makes calculated "up" more deterministic. }
if PerfectlyZeroVector(Up) then
Up := Vector3Single(0, 1, 0);
Up := MatrixMultDirection(Transform, Up);
{ When the "up" vector is parallel to the dir then fix it.
Note: when "up" is not parallel, then ModelviewMatrix will
take care of adjusting it to be orthogonal. }
if VectorsParallel(Up, Dir) then
Up := AnyOrthogonalVector(Dir);
end;
function TAbstractLightNode.CreateLightInstance(
State: TX3DGraphTraverseState): TLightInstance;
begin
Result.Node := Self;
Result.WorldCoordinates := false;
UpdateLightInstanceState(Result, State);
end;
procedure TAbstractLightNode.UpdateLightInstanceState(
var LightInstance: TLightInstance;
State: TX3DGraphTraverseState);
begin
LightInstance.Transform := State.Transform;
LightInstance.TransformScale := State.TransformScale;
UpdateLightInstance(LightInstance);
end;
procedure TAbstractLightNode.UpdateLightInstance(
var LightInstance: TLightInstance);
begin
{ Nothing to do in this class. }
Assert(LightInstance.Node = Self);
end;
procedure TAbstractLightNode.MiddleTraverse(StateStack: TX3DGraphTraverseStateStack);
begin
inherited;
if Scope = lsLocalVRML1 then
StateStack.Top.AddLight(CreateLightInstance(StateStack.Top));
end;
function TAbstractLightNode.TransformationChange: TNodeTransformationChange;
begin
Result := ntcLight;
end;
procedure TAbstractLightNode.DefaultShadowMapSave(Node: TGeneratedShadowMapNode);
begin
SavedDefaultShadowMap := true;
SavedDefaultShadowMapUpdate := Node.FdUpdate .Value;
SavedDefaultShadowMapSize := Node.FdSize .Value;
SavedDefaultShadowMapScale := Node.FdScale .Value;
SavedDefaultShadowMapBias := Node.FdBias .Value;
SavedDefaultShadowMapCompareMode := Node.FdCompareMode .Value;
end;
function TAbstractLightNode.DefaultShadowMapLoad(Node: TGeneratedShadowMapNode): boolean;
begin
Result := SavedDefaultShadowMap;
if Result then
begin
Node.FdUpdate .Value := SavedDefaultShadowMapUpdate ;
Node.FdSize .Value := SavedDefaultShadowMapSize ;
Node.FdScale .Value := SavedDefaultShadowMapScale ;
Node.FdBias .Value := SavedDefaultShadowMapBias ;
Node.FdCompareMode .Value := SavedDefaultShadowMapCompareMode;
end;
end;
function TAbstractLightNode.Scope: TLightScope;
begin
if FdGlobal.Value then
Result := lsGlobal else
Result := lsLocal;
end;
procedure TAbstractDirectionalLightNode.CreateNode;
begin
inherited;
FFdDirection := TSFVec3f.Create(Self, 'direction', Vector3Single(0, 0, -1));
FdDirection.ChangesAlways := [chLightInstanceProperty, chLightLocationDirection];
Fields.Add(FFdDirection);
FFdProjectionRectangle := TSFVec4f.Create(Self, 'projectionRectangle', ZeroVector4Single);
{ We want to set UpdateNeeded := true for all GeneratedShadowMap using
this light. For now, simply send chVisibleGeometry that will
force updating all GeneratedShadowMaps. }
FdProjectionRectangle.ChangesAlways := [chVisibleGeometry];
Fields.Add(FFdProjectionRectangle);
FFdProjectionLocation := TSFVec3f.Create(Self, 'projectionLocation', ZeroVector3Single);
{ We want to set UpdateNeeded := true for all GeneratedShadowMap using
this light. For now, simply send chVisibleGeometry that will
force updating all GeneratedShadowMaps. }
FdProjectionLocation.ChangesAlways := [chVisibleGeometry];
Fields.Add(FFdProjectionLocation);
end;
class function TAbstractDirectionalLightNode.ClassNodeTypeName: string;
begin
Result := 'DirectionalLight';
end;
procedure TAbstractDirectionalLightNode.UpdateLightInstance(
var LightInstance: TLightInstance);
begin
inherited;
LightInstance.Direction := Normalized(MatrixMultDirection(
LightInstance.Transform, FdDirection.Value));
end;
function TAbstractDirectionalLightNode.ProjectionMatrix: TMatrix4Single;
var
N, F: Single;
R: TVector4Single;
begin
{ If author didn't provide and X3DShadowMaps unit didn't calculate
values for some fields, then use FallbackProjection* defaults here. }
N := FdProjectionNear.Value;
if N = 0 then N := FallbackProjectionNear;
F := FdProjectionFar.Value;
if F = 0 then F := FallbackProjectionFar;
R := FdProjectionRectangle.Value;
if PerfectlyZeroVector(R) then R := FallbackProjectionRectangle;
{ Beware: order of projectionRectangle
is different than typical OpenGL and our OrthoProjMatrix params. }
Result := OrthoProjMatrix(R[0], R[2], R[1], R[3], N, F);
end;
function TAbstractDirectionalLightNode.ModelviewMatrix: TMatrix4Single;
var
Pos, Dir, Up: TVector3Single;
begin
GetView(Pos, Dir, Up);
Result := LookDirMatrix(Pos, Dir, Up);
end;
function TAbstractDirectionalLightNode.ModelviewRotationMatrix: TMatrix4Single;
var
Pos, Dir, Up: TVector3Single;
begin
GetView(Pos, Dir, Up);
Result := LookDirMatrix(ZeroVector3Single, Dir, Up);
end;
function TAbstractDirectionalLightNode.LocationLocal: TVector3Single;
begin
Result := FdProjectionLocation.Value;
end;
function TAbstractDirectionalLightNode.DirectionLocal: TVector3Single;
begin
Result := FdDirection.Value;
end;
procedure TAbstractDirectionalLightNode.Box3DDistances(const Box: TBox3D;
out MinDistance, MaxDistance: Single);
begin
Box.DirectionDistances(Location, Direction, MinDistance, MaxDistance);
end;
function TAbstractDirectionalLightNode.Position: TVector4Single;
begin
Result := Vector4Single(-FdDirection.Value, 0);
end;
procedure TAbstractPositionalLightNode.CreateNode;
begin
inherited;
FFdLocation := TSFVec3f.Create(Self, 'location', Vector3Single(0, 0, 0));
FdLocation.ChangesAlways := [chLightInstanceProperty, chLightLocationDirection];
Fields.Add(FFdLocation);
{ X3D specification comment: (-Inf,Inf) }
FFdAttenuation := TSFVec3f.Create(Self, 'attenuation', Vector3Single(1, 0, 0));
FdAttenuation.ChangesAlways := [chVisibleNonGeometry];
Fields.Add(FFdAttenuation);
{ X3D specification comment: [0,Inf) }
FFdRadius := TSFFloat.Create(Self, 'radius', 100);
FdRadius.ChangesAlways := [chLightInstanceProperty];
Fields.Add(FFdRadius);
{ X3D specification comment: [0,Inf) }
end;
function TAbstractPositionalLightNode.DistanceNeededForAttenuation: boolean;
begin
Result := (FdAttenuation.Value[1] <> 0) or
(FdAttenuation.Value[2] <> 0);
end;
function TAbstractPositionalLightNode.HasAttenuation: boolean;
begin
Result := ((FdAttenuation.Value[0] <> 1) and
(FdAttenuation.Value[0] <> 0)) or
(FdAttenuation.Value[1] <> 0) or
(FdAttenuation.Value[2] <> 0);
end;
function TAbstractPositionalLightNode.HasRadius: boolean;
begin
Result := true;
end;
procedure TAbstractPositionalLightNode.UpdateLightInstance(
var LightInstance: TLightInstance);
begin
inherited;
LightInstance.Location := MatrixMultPoint(LightInstance.Transform,
FdLocation.Value);
{ TODO: For non-uniform scale, this will simply use average scale.
This is not fully correct, VRML spec doesn't clarify this
but I guess that the intention was that the non-uniform scale will
make radius non-uniform, i.e. light volume will not be a regular sphere
but some 3d ellipsoid. Unfortunately this would require quite more
work, AddGlobalLights (in TCastleSceneCore) would then have to check for collision
between
sphere transformed by matrix Transform
and
bounding box
which I don't know how to do *easily*... }
LightInstance.Radius := FdRadius.Value * LightInstance.TransformScale;
end;
{$define ATTENUATION_IMPLEMENTATION:=
begin
(* moglibysmy tu nie badac czy DistanceNeededForAttenuation i zawsze
robic wersje pelna (bo przeciez
FdAttenuation.Value[1] * DistanceToLight +
FdAttenuation.Value[2] * Sqr(DistanceToLight)
i tak bedzie = 0 gdy FdAttenuation.Value[1] = FdAttenuation.Value[2] = 0.
Ale wydaje mi sie ze tak jest szybciej - testowanie kosztuje nas
troszke czasu ale mozemy sobie w ten sposob ocalic 2 x mnozenie i dodawanie. *)
(* we check whether attenuation = (0, 0, 0). VRML 97 spec says that specifying
(0, 0, 0) should be equal to specifying (1, 0, 0). (well, we avoid
division by zero possibility this way so it's quite sensible, even
if it wastes some time) *)
if (FdAttenuation.Value[0] = 0) and
(FdAttenuation.Value[1] = 0) and
(FdAttenuation.Value[2] = 0) then result := 1;
if DistanceNeededForAttenuation then
result := 1/ CastleUtils.max(FdAttenuation.Value[0] +
FdAttenuation.Value[1] * DistanceToLight +
FdAttenuation.Value[2] * Sqr(DistanceToLight), Single(1.0)) else
result := 1/ CastleUtils.max(FdAttenuation.Value[0], Single(1.0));
end;}
function TAbstractPositionalLightNode.Attenuation(const DistanceToLight: Single): Single;
ATTENUATION_IMPLEMENTATION
function TAbstractPositionalLightNode.Attenuation(const DistanceToLight: Double): Double;
ATTENUATION_IMPLEMENTATION
function TAbstractPositionalLightNode.Position: TVector4Single;
begin
Result := Vector4Single(FdLocation.Value, 1);
end;
procedure TAbstractPointLightNode.CreateNode;
begin
inherited;
{ no new fields - this is just TAbstractPositionalLightNode }
end;
class function TAbstractPointLightNode.ClassNodeTypeName: string;
begin
Result := 'PointLight';
end;
function TAbstractPointLightNode.LocationLocal: TVector3Single;
begin
Result := FdLocation.Value;
end;
procedure TAbstractPointLightNode.Box3DDistances(const Box: TBox3D;
out MinDistance, MaxDistance: Single);
begin
Box.PointDistances(Location, MinDistance, MaxDistance);
end;
procedure TDirectionalLightNode.CreateNode;
begin
inherited;
FdGlobal.Value := false;
DefaultContainerField := 'children';
end;
class function TDirectionalLightNode.URNMatching(const URN: string): boolean;
begin
Result := (inherited URNMatching(URN)) or
(URN = URNVRML97Nodes + ClassNodeTypeName) or
(URN = URNX3DNodes + ClassNodeTypeName);
end;
class function TDirectionalLightNode.ForVRMLVersion(const Version: TX3DVersion): boolean;
begin
Result := Version.Major >= 2;
end;
procedure TPointLightNode.CreateNode;
begin
inherited;
FdGlobal.Value := true;
DefaultContainerField := 'children';
end;
class function TPointLightNode.URNMatching(const URN: string): boolean;
begin
Result := (inherited URNMatching(URN)) or
(URN = URNVRML97Nodes + ClassNodeTypeName) or
(URN = URNX3DNodes + ClassNodeTypeName);
end;
class function TPointLightNode.ForVRMLVersion(const Version: TX3DVersion): boolean;
begin
Result := Version.Major >= 2;
end;
procedure TSpotLightNode.CreateNode;
begin
inherited;
FFdBeamWidth := TSFFloat.Create(Self, 'beamWidth', Pi / 2);
FdBeamWidth.ChangesAlways := [chVisibleNonGeometry];
Fields.Add(FFdBeamWidth);
{ X3D specification comment: (0,Pi/2] }
FFdCutOffAngle := TSFFloat.Create(Self, 'cutOffAngle', Pi / 4);
FdCutOffAngle.ChangesAlways := [chVisibleNonGeometry];
Fields.Add(FFdCutOffAngle);
{ X3D specification comment: (0,Pi/2] }
FFdDirection := TSFVec3f.Create(Self, 'direction', Vector3Single(0, 0, -1));
FdDirection.ChangesAlways := [chLightInstanceProperty, chLightLocationDirection];
Fields.Add(FFdDirection);
{ X3D specification comment: (-Inf,Inf) }
FdGlobal.Value := true;
FFdProjectionAngle := TSFFloat.Create(Self, 'projectionAngle', 0);
{ We want to set UpdateNeeded := true for all GeneratedShadowMap using
this light. For now, simply send chVisibleGeometry that will
force updating all GeneratedShadowMaps. }
FdProjectionAngle.ChangesAlways := [chVisibleGeometry];
Fields.Add(FFdProjectionAngle);
DefaultContainerField := 'children';
end;
class function TSpotLightNode.ClassNodeTypeName: string;
begin
Result := 'SpotLight';
end;
class function TSpotLightNode.URNMatching(const URN: string): boolean;
begin
Result := (inherited URNMatching(URN)) or
(URN = URNVRML97Nodes + ClassNodeTypeName) or
(URN = URNX3DNodes + ClassNodeTypeName);
end;
class function TSpotLightNode.ForVRMLVersion(const Version: TX3DVersion): boolean;
begin
Result := Version.Major >= 2;
end;
procedure TSpotLightNode.UpdateLightInstance(
var LightInstance: TLightInstance);
begin
inherited;
LightInstance.Direction := Normalized(MatrixMultDirection(
LightInstance.Transform, FdDirection.Value));
end;
function TSpotLightNode.ProjectionMatrix: TMatrix4Single;
var
Angle, N, F: Single;
begin
{ If author didn't provide and X3DShadowMaps unit didn't calculate
values for some fields, then use FallbackProjection* defaults here. }
if FdprojectionAngle.Value <= 0 then
Angle := 2 * FdCutOffAngle.Value else
Angle := FdProjectionAngle.Value;
N := FdProjectionNear.Value;
if N = 0 then N := FallbackProjectionNear;
F := FdProjectionFar.Value;
if F = 0 then F := FallbackProjectionFar;
Result := PerspectiveProjMatrixRad(Angle, 1, N, F);
end;
function TSpotLightNode.ModelviewMatrix: TMatrix4Single;
var
Pos, Dir, Up: TVector3Single;
begin
GetView(Pos, Dir, Up);
Result := LookDirMatrix(Pos, Dir, Up);
end;
function TSpotLightNode.ModelviewRotationMatrix: TMatrix4Single;
var
Pos, Dir, Up: TVector3Single;
begin
GetView(Pos, Dir, Up);
Result := LookDirMatrix(ZeroVector3Single, Dir, Up);
end;
function TSpotLightNode.LocationLocal: TVector3Single;
begin
Result := FdLocation.Value;
end;
function TSpotLightNode.DirectionLocal: TVector3Single;
begin
Result := FdDirection.Value;
end;
procedure TSpotLightNode.Box3DDistances(const Box: TBox3D;
out MinDistance, MaxDistance: Single);
begin
{ TODO: MaxDistance should be a little larger, as spot light rays
are not parallel. }
Box.DirectionDistances(Location, Direction, MinDistance, MaxDistance);
end;
function TSpotLightNode.SpotCutoffDeg: Single;
begin
Result := Min(90, RadToDeg(FdCutOffAngle.Value));
end;
function TSpotLightNode.SpotCosCutoff: Single;
begin
Result := Cos(FdCutOffAngle.Value);
end;
function TSpotLightNode.SpotExponentApproximate: Single;
begin
{ There is no way to exactly translate beamWidth to OpenGL GL_SPOT_EXPONENT.
GL_SPOT_EXPONENT is an exponent for cosinus.
beamWidth says to use constant intensity within beamWidth angle,
and linear drop off to cutOffAngle.
See [http://castle-engine.sourceforge.net/vrml_engine_doc/output/xsl/html/chapter.opengl_rendering.html#section.vrml_lights]
for more discussion. }
if FdBeamWidth.Value >= FdCutOffAngle.Value then
Result := 0 else
Result := Clamped(0.5 / Max(FdBeamWidth.Value, 0.0001), 0.0, 128.0);
end;
procedure RegisterLightingNodes;
begin
NodesManager.RegisterNodeClasses([
TDirectionalLightNode,
TPointLightNode,
TSpotLightNode
]);
end;
{$endif read_implementation}
|